Copy local state in AudioRegionView copy constructor. Fixes #4047.
[ardour2.git] / gtk2_ardour / editor_audio_import.cc
blob4a7c8c56543fea754525319aa1d12fb88f8dab91
1 /*
2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <algorithm>
27 #include <sndfile.h>
29 #include "pbd/pthread_utils.h"
30 #include "pbd/basename.h"
31 #include "pbd/shortpath.h"
32 #include "pbd/stateful_diff_command.h"
34 #include <gtkmm2ext/choice.h>
36 #include "ardour/session.h"
37 #include "ardour/session_directory.h"
38 #include "ardour/audioplaylist.h"
39 #include "ardour/audioregion.h"
40 #include "ardour/audio_diskstream.h"
41 #include "ardour/midi_track.h"
42 #include "ardour/midi_region.h"
43 #include "ardour/utils.h"
44 #include "ardour/audio_track.h"
45 #include "ardour/audioplaylist.h"
46 #include "ardour/audiofilesource.h"
47 #include "ardour/region_factory.h"
48 #include "ardour/source_factory.h"
49 #include "ardour/session.h"
50 #include "ardour/smf_source.h"
51 #include "ardour/operations.h"
52 #include "pbd/memento_command.h"
54 #include "ardour_ui.h"
55 #include "editor.h"
56 #include "sfdb_ui.h"
57 #include "editing.h"
58 #include "audio_time_axis.h"
59 #include "midi_time_axis.h"
60 #include "session_import_dialog.h"
61 #include "utils.h"
62 #include "gui_thread.h"
63 #include "interthread_progress_window.h"
64 #include "mouse_cursors.h"
65 #include "editor_cursors.h"
67 #include "i18n.h"
69 using namespace std;
70 using namespace ARDOUR;
71 using namespace PBD;
72 using namespace Gtk;
73 using namespace Gtkmm2ext;
74 using namespace Editing;
75 using std::string;
77 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
79 void
80 Editor::add_external_audio_action (ImportMode mode_hint)
82 if (_session == 0) {
83 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
84 msg.run ();
85 return;
88 if (sfbrowser == 0) {
89 sfbrowser = new SoundFileOmega (*this, _("Add Existing Media"), _session, 0, true, mode_hint);
90 } else {
91 sfbrowser->set_mode (mode_hint);
94 external_audio_dialog ();
97 void
98 Editor::external_audio_dialog ()
100 vector<string> paths;
101 uint32_t track_cnt;
103 if (_session == 0) {
104 MessageDialog msg (_("You can't import or embed an audiofile until you have a session loaded."));
105 msg.run ();
106 return;
109 track_cnt = 0;
111 for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
112 AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*>(*x);
114 if (!atv) {
115 continue;
116 } else if (atv->is_audio_track()) {
117 track_cnt++;
121 if (sfbrowser == 0) {
122 sfbrowser = new SoundFileOmega (*this, _("Add Existing Media"), _session, track_cnt, true);
123 } else {
124 sfbrowser->reset (track_cnt);
127 sfbrowser->show_all ();
130 bool keepRunning;
132 do {
133 keepRunning = false;
135 int response = sfbrowser->run ();
137 switch (response) {
138 case RESPONSE_APPLY:
139 // leave the dialog open
140 break;
142 case RESPONSE_OK:
143 sfbrowser->hide ();
144 break;
146 default:
147 // cancel from the browser - we are done
148 sfbrowser->hide ();
149 return;
152 /* lets do it */
154 vector<string> upaths = sfbrowser->get_paths ();
155 for (vector<string>::iterator x = upaths.begin(); x != upaths.end(); ++x) {
156 paths.push_back (*x);
159 ImportPosition pos = sfbrowser->get_position ();
160 ImportMode mode = sfbrowser->get_mode ();
161 ImportDisposition chns = sfbrowser->get_channel_disposition ();
162 framepos_t where;
164 switch (pos) {
165 case ImportAtEditPoint:
166 where = get_preferred_edit_position ();
167 break;
168 case ImportAtTimestamp:
169 where = -1;
170 break;
171 case ImportAtPlayhead:
172 where = playhead_cursor->current_frame;
173 break;
174 case ImportAtStart:
175 where = _session->current_start_frame();
176 break;
179 SrcQuality quality = sfbrowser->get_src_quality();
182 if (sfbrowser->copy_files_btn.get_active()) {
183 do_import (paths, chns, mode, quality, where);
184 } else {
185 do_embed (paths, chns, mode, where);
188 if (response == RESPONSE_APPLY) {
189 sfbrowser->clear_selection ();
190 keepRunning = true;
193 } while (keepRunning);
196 void
197 Editor::session_import_dialog ()
199 SessionImportDialog dialog (_session);
200 ensure_float (dialog);
201 dialog.run ();
204 typedef std::map<PBD::ID,boost::shared_ptr<ARDOUR::Source> > SourceMap;
207 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
209 * all_or_nothing:
210 * true = show "Update", "Import" and "Skip"
211 * false = show "Import", and "Cancel"
213 * Returns:
214 * 0 To update an existing source of the same name
215 * 1 To import/embed the file normally (make sure the new name will be unique)
216 * 2 If the user wants to skip this file
219 Editor::check_whether_and_how_to_import(string path, bool all_or_nothing)
221 string wave_name (Glib::path_get_basename(path));
223 SourceMap all_sources = _session->get_sources();
224 bool already_exists = false;
225 uint32_t existing;
227 if ((existing = _session->count_sources_by_origin (path)) > 0) {
228 already_exists = true;
231 int function = 1;
233 if (already_exists) {
234 string message;
235 if (all_or_nothing) {
236 // updating is still disabled
237 //message = string_compose(_("The session already contains a source file named %1. Do you want to update that file (and thus all regions using the file) or import this file as a new file?"),wave_name);
238 message = string_compose (_("The session already contains a source file named %1. Do you want to import %1 as a new file, or skip it?"), wave_name);
239 } else {
240 message = string_compose (_("The session already contains a source file named %1. Do you want to import %2 as a new source, or skip it?"), wave_name, wave_name);
243 MessageDialog dialog(message, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE, true);
245 if (all_or_nothing) {
246 // disabled
247 //dialog.add_button("Update", 0);
248 dialog.add_button("Import", 1);
249 dialog.add_button("Skip", 2);
250 } else {
251 dialog.add_button("Import", 1);
252 dialog.add_button("Cancel", 2);
255 //dialog.add_button("Skip all", 4); // All or rest?
257 dialog.show();
259 function = dialog.run ();
261 dialog.hide();
264 return function;
267 boost::shared_ptr<AudioTrack>
268 Editor::get_nth_selected_audio_track (int nth) const
270 AudioTimeAxisView* atv;
271 TrackSelection::iterator x;
273 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
275 atv = dynamic_cast<AudioTimeAxisView*>(*x);
277 if (!atv) {
278 continue;
279 } else if (atv->is_audio_track()) {
280 --nth;
284 if (x == selection->tracks.end()) {
285 atv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.back());
286 } else {
287 atv = dynamic_cast<AudioTimeAxisView*>(*x);
290 if (!atv || !atv->is_audio_track()) {
291 return boost::shared_ptr<AudioTrack>();
294 return atv->audio_track();
297 boost::shared_ptr<MidiTrack>
298 Editor::get_nth_selected_midi_track (int nth) const
300 MidiTimeAxisView* mtv;
301 TrackSelection::iterator x;
303 for (x = selection->tracks.begin(); nth > 0 && x != selection->tracks.end(); ++x) {
305 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
307 if (!mtv) {
308 continue;
309 } else if (mtv->is_midi_track()) {
310 --nth;
314 if (x == selection->tracks.end()) {
315 mtv = dynamic_cast<MidiTimeAxisView*>(selection->tracks.back());
316 } else {
317 mtv = dynamic_cast<MidiTimeAxisView*>(*x);
320 if (!mtv || !mtv->is_midi_track()) {
321 return boost::shared_ptr<MidiTrack>();
324 return mtv->midi_track();
327 void
328 Editor::do_import (vector<string> paths, ImportDisposition chns, ImportMode mode, SrcQuality quality, framepos_t& pos)
330 boost::shared_ptr<Track> track;
331 vector<string> to_import;
332 int nth = 0;
333 bool use_timestamp = (pos == -1);
335 current_interthread_info = &import_status;
336 import_status.current = 1;
337 import_status.total = paths.size ();
338 import_status.all_done = false;
340 ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
342 bool ok = true;
344 if (chns == Editing::ImportMergeFiles) {
346 /* create 1 region from all paths, add to 1 track,
347 ignore "track"
350 bool cancel = false;
351 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
352 int check = check_whether_and_how_to_import(*a, false);
353 if (check == 2) {
354 cancel = true;
355 break;
359 if (cancel) {
360 ok = false;
361 } else {
362 ipw.show ();
363 ok = (import_sndfiles (paths, mode, quality, pos, 1, 1, track, false) == 0);
366 } else {
368 bool replace = false;
370 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
372 const int check = check_whether_and_how_to_import (*a, true);
374 switch (check) {
375 case 2:
376 // user said skip
377 continue;
378 case 0:
379 fatal << "Updating existing sources should be disabled!" << endmsg;
380 /* NOTREACHED*/
381 break;
382 case 1:
383 replace = false;
384 break;
385 default:
386 fatal << "Illegal return " << check << " from check_whether_and_how_to_import()!" << endmsg;
387 /* NOTREACHED*/
390 /* have to reset this for every file we handle */
392 if (use_timestamp) {
393 pos = -1;
396 ipw.show ();
398 switch (chns) {
399 case Editing::ImportDistinctFiles:
401 to_import.clear ();
402 to_import.push_back (*a);
404 if (mode == Editing::ImportToTrack) {
405 track = get_nth_selected_audio_track (nth++);
408 ok = (import_sndfiles (to_import, mode, quality, pos, 1, -1, track, replace) == 0);
409 break;
411 case Editing::ImportDistinctChannels:
413 to_import.clear ();
414 to_import.push_back (*a);
416 ok = (import_sndfiles (to_import, mode, quality, pos, -1, -1, track, replace) == 0);
417 break;
419 case Editing::ImportSerializeFiles:
421 to_import.clear ();
422 to_import.push_back (*a);
424 ok = (import_sndfiles (to_import, mode, quality, pos, 1, 1, track, replace) == 0);
425 break;
427 case Editing::ImportMergeFiles:
428 // Not entered, handled in earlier if() branch
429 break;
434 if (ok) {
435 _session->save_state ("");
438 import_status.all_done = true;
441 void
442 Editor::do_embed (vector<string> paths, ImportDisposition chns, ImportMode mode, framepos_t& pos)
444 boost::shared_ptr<Track> track;
445 bool check_sample_rate = true;
446 bool ok = false;
447 vector<string> to_embed;
448 bool multi = paths.size() > 1;
449 int nth = 0;
451 switch (chns) {
452 case Editing::ImportDistinctFiles:
453 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
455 to_embed.clear ();
456 to_embed.push_back (*a);
458 if (mode == Editing::ImportToTrack) {
459 track = get_nth_selected_audio_track (nth++);
462 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, -1, track) < -1) {
463 goto out;
466 break;
468 case Editing::ImportDistinctChannels:
469 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
471 to_embed.clear ();
472 to_embed.push_back (*a);
474 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, -1, -1, track) < -1) {
475 goto out;
478 break;
480 case Editing::ImportMergeFiles:
481 if (embed_sndfiles (paths, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
482 goto out;
484 break;
486 case Editing::ImportSerializeFiles:
487 for (vector<string>::iterator a = paths.begin(); a != paths.end(); ++a) {
489 to_embed.clear ();
490 to_embed.push_back (*a);
492 if (embed_sndfiles (to_embed, multi, check_sample_rate, mode, pos, 1, 1, track) < -1) {
493 goto out;
496 break;
499 ok = true;
501 out:
502 if (ok) {
503 _session->save_state ("");
508 Editor::import_sndfiles (vector<string> paths, ImportMode mode, SrcQuality quality, framepos_t& pos,
509 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool replace)
511 import_status.paths = paths;
512 import_status.done = false;
513 import_status.cancel = false;
514 import_status.freeze = false;
515 import_status.quality = quality;
516 import_status.replace_existing_source = replace;
518 import_status.mode = mode;
519 import_status.pos = pos;
520 import_status.target_tracks = target_tracks;
521 import_status.target_regions = target_regions;
522 import_status.track = track;
523 import_status.replace = replace;
525 set_canvas_cursor (_cursors->wait);
526 gdk_flush ();
528 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
529 which, if successful, will add the files as regions to the region list. its up to us
530 (the GUI) to direct additional steps after that.
533 pthread_create_and_store ("import", &import_status.thread, _import_thread, this);
534 pthread_detach (import_status.thread);
536 while (!import_status.done && !import_status.cancel) {
537 gtk_main_iteration ();
540 import_status.done = true;
542 int result = -1;
544 if (!import_status.cancel && !import_status.sources.empty()) {
545 result = add_sources (
546 import_status.paths,
547 import_status.sources,
548 import_status.pos,
549 import_status.mode,
550 import_status.target_regions,
551 import_status.target_tracks,
552 track, false
555 /* update position from results */
557 pos = import_status.pos;
560 set_canvas_cursor (current_canvas_cursor);
561 return result;
565 Editor::embed_sndfiles (vector<string> paths, bool multifile,
566 bool& check_sample_rate, ImportMode mode, framepos_t& pos, int target_regions, int target_tracks,
567 boost::shared_ptr<Track>& track)
569 boost::shared_ptr<AudioFileSource> source;
570 SourceList sources;
571 string linked_path;
572 SoundFileInfo finfo;
573 int ret = 0;
575 set_canvas_cursor (_cursors->wait);
576 gdk_flush ();
578 for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
580 string path = *p;
581 string error_msg;
583 /* note that we temporarily truncated _id at the colon */
585 if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
586 error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), path, error_msg ) << endmsg;
587 goto out;
590 if (check_sample_rate && (finfo.samplerate != (int) _session->frame_rate())) {
591 vector<string> choices;
593 if (multifile) {
594 choices.push_back (_("Cancel entire import"));
595 choices.push_back (_("Don't embed it"));
596 choices.push_back (_("Embed all without questions"));
598 Gtkmm2ext::Choice rate_choice (
599 _("Sample rate"),
600 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
601 short_path (path, 40)),
602 choices, false
605 int resx = rate_choice.run ();
607 switch (resx) {
608 case 0: /* stop a multi-file import */
609 ret = -2;
610 goto out;
611 case 1: /* don't embed this one */
612 ret = -1;
613 goto out;
614 case 2: /* do it, and the rest without asking */
615 check_sample_rate = false;
616 break;
617 case 3: /* do it */
618 break;
619 default:
620 ret = -2;
621 goto out;
623 } else {
624 choices.push_back (_("Cancel"));
625 choices.push_back (_("Embed it anyway"));
627 Gtkmm2ext::Choice rate_choice (
628 _("Sample rate"),
629 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
630 choices, false
633 int resx = rate_choice.run ();
635 switch (resx) {
636 case 0: /* don't import */
637 ret = -1;
638 goto out;
639 case 1: /* do it */
640 break;
641 default:
642 ret = -2;
643 goto out;
648 set_canvas_cursor (_cursors->wait);
650 for (int n = 0; n < finfo.channels; ++n) {
651 try {
653 /* check if we have this thing embedded already */
655 boost::shared_ptr<Source> s;
657 if ((s = _session->source_by_path_and_channel (path, n)) == 0) {
659 source = boost::dynamic_pointer_cast<AudioFileSource> (
660 SourceFactory::createReadable (DataType::AUDIO, *_session,
661 path, n,
662 (mode == ImportAsTapeTrack
663 ? Source::Destructive
664 : Source::Flag (0)),
665 true, true));
666 } else {
667 source = boost::dynamic_pointer_cast<AudioFileSource> (s);
670 sources.push_back(source);
673 catch (failed_constructor& err) {
674 error << string_compose(_("could not open %1"), path) << endmsg;
675 goto out;
678 ARDOUR_UI::instance()->flush_pending ();
682 if (sources.empty()) {
683 goto out;
686 ret = add_sources (paths, sources, pos, mode, target_regions, target_tracks, track, true);
688 out:
689 set_canvas_cursor (current_canvas_cursor);
690 return ret;
694 Editor::add_sources (vector<string> paths, SourceList& sources, framepos_t& pos, ImportMode mode,
695 int target_regions, int target_tracks, boost::shared_ptr<Track>& track, bool /*add_channel_suffix*/)
697 vector<boost::shared_ptr<Region> > regions;
698 string region_name;
699 uint32_t input_chan = 0;
700 uint32_t output_chan = 0;
701 bool use_timestamp;
703 use_timestamp = (pos == -1);
705 // kludge (for MIDI we're abusing "channel" for "track" here)
706 if (SMFSource::safe_midi_file_extension (paths.front())) {
707 target_regions = -1;
710 if (target_regions == 1) {
712 /* take all the sources we have and package them up as a region */
714 region_name = region_name_from_path (paths.front(), (sources.size() > 1), false);
716 /* we checked in import_sndfiles() that there were not too many */
718 while (RegionFactory::region_by_name (region_name)) {
719 region_name = bump_name_once (region_name, '.');
722 PropertyList plist;
724 plist.add (ARDOUR::Properties::start, 0);
725 plist.add (ARDOUR::Properties::length, sources[0]->length (pos));
726 plist.add (ARDOUR::Properties::name, region_name);
727 plist.add (ARDOUR::Properties::layer, 0);
728 plist.add (ARDOUR::Properties::whole_file, true);
729 plist.add (ARDOUR::Properties::external, true);
731 boost::shared_ptr<Region> r = RegionFactory::create (sources, plist);
733 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
734 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position(sources[0]->natural_position());
737 regions.push_back (r);
740 } else if (target_regions == -1 || target_regions > 1) {
742 /* take each source and create a region for each one */
744 SourceList just_one;
745 SourceList::iterator x;
746 uint32_t n;
748 for (n = 0, x = sources.begin(); x != sources.end(); ++x, ++n) {
750 just_one.clear ();
751 just_one.push_back (*x);
753 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (*x);
755 if (fs) {
756 region_name = region_name_from_path (fs->path(), false, false, sources.size(), n);
757 } else{
758 region_name = (*x)->name();
761 PropertyList plist;
763 /* Fudge region length to ensure it is non-zero */
764 framecnt_t len = (*x)->length (pos);
765 if (len == 0) {
766 len = 1;
769 plist.add (ARDOUR::Properties::start, 0);
770 plist.add (ARDOUR::Properties::length, len);
771 plist.add (ARDOUR::Properties::name, region_name);
772 plist.add (ARDOUR::Properties::layer, 0);
773 plist.add (ARDOUR::Properties::whole_file, true);
774 plist.add (ARDOUR::Properties::external, true);
776 boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
778 if (use_timestamp && boost::dynamic_pointer_cast<AudioRegion>(r)) {
779 boost::dynamic_pointer_cast<AudioRegion>(r)->special_set_position((*x)->natural_position());
782 regions.push_back (r);
786 if (target_regions == 1) {
787 input_chan = regions.front()->n_channels();
788 } else {
789 if (target_tracks == 1) {
790 input_chan = regions.size();
791 } else {
792 input_chan = 1;
796 if (Config->get_output_auto_connect() & AutoConnectMaster) {
797 output_chan = (_session->master_out() ? _session->master_out()->n_inputs().n_audio() : input_chan);
798 } else {
799 output_chan = input_chan;
802 int n = 0;
803 framepos_t rlen = 0;
805 for (vector<boost::shared_ptr<Region> >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) {
806 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (*r);
808 if (use_timestamp) {
809 if (ar) {
811 /* get timestamp for this region */
813 const boost::shared_ptr<Source> s (ar->sources().front());
814 const boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource> (s);
816 assert (as);
818 if (as->natural_position() != 0) {
819 pos = as->natural_position();
820 } else if (target_tracks == 1) {
821 /* hmm, no timestamp available, put it after the previous region
823 if (n == 0) {
824 pos = get_preferred_edit_position ();
825 } else {
826 pos += rlen;
828 } else {
829 pos = get_preferred_edit_position ();
831 } else {
832 /* should really get first position in MIDI file, but for now, use edit position*/
833 pos = get_preferred_edit_position ();
838 finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track);
840 rlen = (*r)->length();
842 if (target_tracks != 1) {
843 track.reset ();
844 } else {
845 if (!use_timestamp || !ar) {
846 /* line each one up right after the other */
847 pos += (*r)->length();
852 /* setup peak file building in another thread */
854 for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) {
855 SourceFactory::setup_peakfile (*x, true);
858 return 0;
862 Editor::finish_bringing_in_material (boost::shared_ptr<Region> region, uint32_t in_chans, uint32_t out_chans, framepos_t& pos,
863 ImportMode mode, boost::shared_ptr<Track>& existing_track)
865 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(region);
866 boost::shared_ptr<MidiRegion> mr = boost::dynamic_pointer_cast<MidiRegion>(region);
868 switch (mode) {
869 case ImportAsRegion:
870 /* relax, its been done */
871 break;
873 case ImportToTrack:
875 if (!existing_track) {
877 if (ar) {
878 existing_track = get_nth_selected_audio_track (0);
879 } else if (mr) {
880 existing_track = get_nth_selected_midi_track (0);
883 if (!existing_track) {
884 return -1;
888 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
889 boost::shared_ptr<Region> copy (RegionFactory::create (region, region->properties()));
890 begin_reversible_command (Operations::insert_file);
891 playlist->clear_changes ();
892 playlist->add_region (copy, pos);
893 _session->add_command (new StatefulDiffCommand (playlist));
894 commit_reversible_command ();
895 break;
898 case ImportAsTrack:
900 if (!existing_track) {
901 if (ar) {
902 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Normal, 0, 1));
904 if (at.empty()) {
905 return -1;
908 existing_track = at.front();
909 } else if (mr) {
910 list<boost::shared_ptr<MidiTrack> > mt (_session->new_midi_track (Normal, 0, 1));
912 if (mt.empty()) {
913 return -1;
916 existing_track = mt.front();
919 existing_track->set_name (region->name());
922 boost::shared_ptr<Playlist> playlist = existing_track->playlist();
923 boost::shared_ptr<Region> copy (RegionFactory::create (region));
924 begin_reversible_command (Operations::insert_file);
925 playlist->clear_changes ();
926 playlist->add_region (copy, pos);
927 _session->add_command (new StatefulDiffCommand (playlist));
928 commit_reversible_command ();
929 break;
932 case ImportAsTapeTrack:
934 if (!ar) {
935 return -1;
938 list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (in_chans, out_chans, Destructive));
939 if (!at.empty()) {
940 boost::shared_ptr<Playlist> playlist = at.front()->playlist();
941 boost::shared_ptr<Region> copy (RegionFactory::create (region));
942 begin_reversible_command (Operations::insert_file);
943 playlist->clear_changes ();
944 playlist->add_region (copy, pos);
945 _session->add_command (new StatefulDiffCommand (playlist));
946 commit_reversible_command ();
948 break;
952 return 0;
955 void *
956 Editor::_import_thread (void *arg)
958 SessionEvent::create_per_thread_pool ("import events", 64);
960 Editor *ed = (Editor *) arg;
961 return ed->import_thread ();
964 void *
965 Editor::import_thread ()
967 _session->import_audiofiles (import_status);
968 pthread_exit_pbd (0);
969 /*NOTREACHED*/
970 return 0;