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>
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 "pbd/memento_command.h"
53 #include "ardour_ui.h"
57 #include "audio_time_axis.h"
58 #include "midi_time_axis.h"
59 #include "session_import_dialog.h"
61 #include "gui_thread.h"
62 #include "interthread_progress_window.h"
67 using namespace ARDOUR
;
70 using namespace Gtkmm2ext
;
71 using namespace Editing
;
74 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
77 Editor::add_external_audio_action (ImportMode mode_hint
)
80 MessageDialog
msg (_("You can't import or embed an audiofile until you have a session loaded."));
86 sfbrowser
= new SoundFileOmega (*this, _("Add Existing Media"), _session
, 0, true, mode_hint
);
88 sfbrowser
->set_mode (mode_hint
);
91 external_audio_dialog ();
95 Editor::external_audio_dialog ()
97 vector
<Glib::ustring
> paths
;
101 MessageDialog
msg (_("You can't import or embed an audiofile until you have a session loaded."));
108 for (TrackSelection::iterator x
= selection
->tracks
.begin(); x
!= selection
->tracks
.end(); ++x
) {
109 AudioTimeAxisView
* atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
113 } else if (atv
->is_audio_track()) {
118 if (sfbrowser
== 0) {
119 sfbrowser
= new SoundFileOmega (*this, _("Add Existing Media"), _session
, track_cnt
, true);
121 sfbrowser
->reset (track_cnt
);
124 sfbrowser
->show_all ();
132 int response
= sfbrowser
->run ();
136 // leave the dialog open
144 // cancel from the browser - we are done
151 paths
= sfbrowser
->get_paths ();
153 ImportPosition pos
= sfbrowser
->get_position ();
154 ImportMode mode
= sfbrowser
->get_mode ();
155 ImportDisposition chns
= sfbrowser
->get_channel_disposition ();
159 case ImportAtEditPoint
:
160 where
= get_preferred_edit_position ();
162 case ImportAtTimestamp
:
165 case ImportAtPlayhead
:
166 where
= playhead_cursor
->current_frame
;
169 where
= _session
->current_start_frame();
173 SrcQuality quality
= sfbrowser
->get_src_quality();
176 if (sfbrowser
->copy_files_btn
.get_active()) {
177 do_import (paths
, chns
, mode
, quality
, where
);
179 do_embed (paths
, chns
, mode
, where
);
182 if (response
== RESPONSE_APPLY
) {
183 sfbrowser
->clear_selection ();
187 } while (keepRunning
);
191 Editor::session_import_dialog ()
193 SessionImportDialog
dialog (_session
);
194 ensure_float (dialog
);
198 typedef std::map
<PBD::ID
,boost::shared_ptr
<ARDOUR::Source
> > SourceMap
;
201 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
204 * true = show "Update", "Import" and "Skip"
205 * false = show "Import", and "Cancel"
208 * 0 To update an existing source of the same name
209 * 1 To import/embed the file normally (make sure the new name will be unique)
210 * 2 If the user wants to skip this file
213 Editor::check_whether_and_how_to_import(string path
, bool all_or_nothing
)
215 string
wave_name (Glib::path_get_basename(path
));
217 SourceMap all_sources
= _session
->get_sources();
218 bool wave_name_exists
= false;
220 for (SourceMap::iterator i
= all_sources
.begin(); i
!= all_sources
.end(); ++i
) {
221 string
tmp (Glib::path_get_basename (i
->second
->path()));
222 if (tmp
== wave_name
) {
223 wave_name_exists
= true;
230 if (wave_name_exists
) {
232 if (all_or_nothing
) {
233 // updating is still disabled
234 //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);
235 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
);
237 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
);
240 MessageDialog
dialog(message
, false, Gtk::MESSAGE_QUESTION
, Gtk::BUTTONS_NONE
, true);
242 if (all_or_nothing
) {
244 //dialog.add_button("Update", 0);
245 dialog
.add_button("Import", 1);
246 dialog
.add_button("Skip", 2);
248 dialog
.add_button("Import", 1);
249 dialog
.add_button("Cancel", 2);
252 //dialog.add_button("Skip all", 4); // All or rest?
256 function
= dialog
.run ();
264 boost::shared_ptr
<AudioTrack
>
265 Editor::get_nth_selected_audio_track (int nth
) const
267 AudioTimeAxisView
* atv
;
268 TrackSelection::iterator x
;
270 for (x
= selection
->tracks
.begin(); nth
> 0 && x
!= selection
->tracks
.end(); ++x
) {
272 atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
276 } else if (atv
->is_audio_track()) {
281 if (x
== selection
->tracks
.end()) {
282 atv
= dynamic_cast<AudioTimeAxisView
*>(selection
->tracks
.back());
284 atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
287 if (!atv
|| !atv
->is_audio_track()) {
288 return boost::shared_ptr
<AudioTrack
>();
291 return atv
->audio_track();
294 boost::shared_ptr
<MidiTrack
>
295 Editor::get_nth_selected_midi_track (int nth
) const
297 MidiTimeAxisView
* mtv
;
298 TrackSelection::iterator x
;
300 for (x
= selection
->tracks
.begin(); nth
> 0 && x
!= selection
->tracks
.end(); ++x
) {
302 mtv
= dynamic_cast<MidiTimeAxisView
*>(*x
);
306 } else if (mtv
->is_midi_track()) {
311 if (x
== selection
->tracks
.end()) {
312 mtv
= dynamic_cast<MidiTimeAxisView
*>(selection
->tracks
.back());
314 mtv
= dynamic_cast<MidiTimeAxisView
*>(*x
);
317 if (!mtv
|| !mtv
->is_midi_track()) {
318 return boost::shared_ptr
<MidiTrack
>();
321 return mtv
->midi_track();
325 Editor::do_import (vector
<ustring
> paths
, ImportDisposition chns
, ImportMode mode
, SrcQuality quality
, nframes64_t
& pos
)
327 boost::shared_ptr
<Track
> track
;
328 vector
<ustring
> to_import
;
331 current_interthread_info
= &import_status
;
332 import_status
.current
= 1;
333 import_status
.total
= paths
.size ();
335 ImportProgressWindow
ipw (&import_status
, _("Import"), _("Cancel Import"));
337 if (chns
== Editing::ImportMergeFiles
) {
339 /* create 1 region from all paths, add to 1 track,
344 for (vector
<ustring
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
345 int check
= check_whether_and_how_to_import(*a
, false);
353 import_sndfiles (paths
, mode
, quality
, pos
, 1, 1, track
, false);
358 bool replace
= false;
361 for (vector
<ustring
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
363 const int check
= check_whether_and_how_to_import (*a
, true);
370 fatal
<< "Updating existing sources should be disabled!" << endmsg
;
377 fatal
<< "Illegal return " << check
<< " from check_whether_and_how_to_import()!" << endmsg
;
382 case Editing::ImportDistinctFiles
:
385 to_import
.push_back (*a
);
387 if (mode
== Editing::ImportToTrack
) {
388 track
= get_nth_selected_audio_track (nth
++);
391 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, 1, -1, track
, replace
) == 0);
394 case Editing::ImportDistinctChannels
:
397 to_import
.push_back (*a
);
399 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, -1, -1, track
, replace
) == 0);
402 case Editing::ImportSerializeFiles
:
405 to_import
.push_back (*a
);
407 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, 1, 1, track
, replace
) == 0);
410 case Editing::ImportMergeFiles
:
411 // Not entered, handled in earlier if() branch
419 Editor::do_embed (vector
<ustring
> paths
, ImportDisposition chns
, ImportMode mode
, nframes64_t
& pos
)
421 boost::shared_ptr
<Track
> track
;
422 bool check_sample_rate
= true;
424 vector
<ustring
> to_embed
;
425 bool multi
= paths
.size() > 1;
429 case Editing::ImportDistinctFiles
:
430 for (vector
<ustring
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
433 to_embed
.push_back (*a
);
435 if (mode
== Editing::ImportToTrack
) {
436 track
= get_nth_selected_audio_track (nth
++);
439 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, 1, -1, track
) < -1) {
445 case Editing::ImportDistinctChannels
:
446 for (vector
<ustring
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
449 to_embed
.push_back (*a
);
451 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, -1, -1, track
) < -1) {
457 case Editing::ImportMergeFiles
:
458 if (embed_sndfiles (paths
, multi
, check_sample_rate
, mode
, pos
, 1, 1, track
) < -1) {
463 case Editing::ImportSerializeFiles
:
464 for (vector
<ustring
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
467 to_embed
.push_back (*a
);
469 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, 1, 1, track
) < -1) {
480 _session
->save_state ("");
485 Editor::import_sndfiles (vector
<ustring
> paths
, ImportMode mode
, SrcQuality quality
, nframes64_t
& pos
,
486 int target_regions
, int target_tracks
, boost::shared_ptr
<Track
>& track
, bool replace
)
488 import_status
.paths
= paths
;
489 import_status
.done
= false;
490 import_status
.cancel
= false;
491 import_status
.freeze
= false;
492 import_status
.done
= 0.0;
493 import_status
.quality
= quality
;
494 import_status
.replace_existing_source
= replace
;
496 import_status
.mode
= mode
;
497 import_status
.pos
= pos
;
498 import_status
.target_tracks
= target_tracks
;
499 import_status
.target_regions
= target_regions
;
500 import_status
.track
= track
;
501 import_status
.replace
= replace
;
503 track_canvas
->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH
));
506 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
507 which, if successful, will add the files as regions to the region list. its up to us
508 (the GUI) to direct additional steps after that.
511 pthread_create_and_store ("import", &import_status
.thread
, _import_thread
, this);
512 pthread_detach (import_status
.thread
);
514 while (!import_status
.done
&& !import_status
.cancel
) {
515 gtk_main_iteration ();
518 import_status
.done
= true;
520 if (!import_status
.cancel
&& !import_status
.sources
.empty()) {
521 if (add_sources (import_status
.paths
,
522 import_status
.sources
,
525 import_status
.target_regions
,
526 import_status
.target_tracks
,
527 track
, false) == 0) {
528 _session
->save_state ("");
531 /* update position from results */
533 pos
= import_status
.pos
;
537 track_canvas
->get_window()->set_cursor (*current_canvas_cursor
);
542 Editor::embed_sndfiles (vector
<Glib::ustring
> paths
, bool multifile
,
543 bool& check_sample_rate
, ImportMode mode
, nframes64_t
& pos
, int target_regions
, int target_tracks
,
544 boost::shared_ptr
<Track
>& track
)
546 boost::shared_ptr
<AudioFileSource
> source
;
551 Glib::ustring path_to_use
;
553 track_canvas
->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH
));
556 for (vector
<Glib::ustring
>::iterator p
= paths
.begin(); p
!= paths
.end(); ++p
) {
560 if (Config
->get_try_link_for_embed()) {
562 /* lets see if we can link it into the session */
564 sys::path tmp
= _session
->session_directory().sound_path() / Glib::path_get_basename(path
);
565 linked_path
= tmp
.to_string();
567 path_to_use
= linked_path
;
569 if (link (path
.c_str(), linked_path
.c_str()) == 0) {
571 /* there are many reasons why link(2) might have failed.
572 but if it succeeds, we now have a link in the
573 session sound dir that will protect against
574 unlinking of the original path. nice.
578 path_to_use
= Glib::path_get_basename (path
);
582 /* one possible reason is that its already linked */
584 if (errno
== EEXIST
) {
587 if (stat (linked_path
.c_str(), &sb
) == 0) {
588 if (sb
.st_nlink
> 1) { // its a hard link, assume its the one we want
590 path_to_use
= Glib::path_get_basename (path
);
597 /* note that we temporarily truncated _id at the colon */
601 if (!AudioFileSource::get_soundfile_info (path
, finfo
, error_msg
)) {
602 error
<< string_compose(_("Editor: cannot open file \"%1\", (%2)"), path
, error_msg
) << endmsg
;
606 if (check_sample_rate
&& (finfo
.samplerate
!= (int) _session
->frame_rate())) {
607 vector
<string
> choices
;
610 choices
.push_back (_("Cancel entire import"));
611 choices
.push_back (_("Don't embed it"));
612 choices
.push_back (_("Embed all without questions"));
614 Gtkmm2ext::Choice
rate_choice (
616 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
617 short_path (path
, 40)),
621 int resx
= rate_choice
.run ();
624 case 0: /* stop a multi-file import */
627 case 1: /* don't embed this one */
630 case 2: /* do it, and the rest without asking */
631 check_sample_rate
= false;
640 choices
.push_back (_("Cancel"));
641 choices
.push_back (_("Embed it anyway"));
643 Gtkmm2ext::Choice
rate_choice (
645 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path
),
649 int resx
= rate_choice
.run ();
652 case 0: /* don't import */
664 track_canvas
->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH
));
666 for (int n
= 0; n
< finfo
.channels
; ++n
) {
669 /* check if we have this thing embedded already */
671 boost::shared_ptr
<Source
> s
;
673 if ((s
= _session
->source_by_path_and_channel (path
, n
)) == 0) {
675 source
= boost::dynamic_pointer_cast
<AudioFileSource
> (
676 SourceFactory::createReadable (DataType::AUDIO
, *_session
,
678 (mode
== ImportAsTapeTrack
679 ? Source::Destructive
683 source
= boost::dynamic_pointer_cast
<AudioFileSource
> (s
);
686 sources
.push_back(source
);
689 catch (failed_constructor
& err
) {
690 error
<< string_compose(_("could not open %1"), path
) << endmsg
;
694 ARDOUR_UI::instance()->flush_pending ();
698 if (sources
.empty()) {
702 ret
= add_sources (paths
, sources
, pos
, mode
, target_regions
, target_tracks
, track
, true);
705 track_canvas
->get_window()->set_cursor (*current_canvas_cursor
);
710 Editor::add_sources (vector
<Glib::ustring
> paths
, SourceList
& sources
, nframes64_t
& pos
, ImportMode mode
,
711 int target_regions
, int target_tracks
, boost::shared_ptr
<Track
>& track
, bool /*add_channel_suffix*/)
713 vector
<boost::shared_ptr
<Region
> > regions
;
715 uint32_t input_chan
= 0;
716 uint32_t output_chan
= 0;
719 use_timestamp
= (pos
== -1);
722 if (sources
[0]->natural_position() != 0) {
723 pos
= sources
[0]->natural_position();
725 pos
= _session
->current_start_frame();
729 // kludge (for MIDI we're abusing "channel" for "track" here)
730 if (SMFSource::safe_midi_file_extension (paths
.front())) {
734 if (target_regions
== 1) {
736 /* take all the sources we have and package them up as a region */
738 region_name
= region_name_from_path (paths
.front(), (sources
.size() > 1), false);
742 plist
.add (ARDOUR::Properties::start
, 0);
743 plist
.add (ARDOUR::Properties::length
, sources
[0]->length (pos
));
744 plist
.add (ARDOUR::Properties::name
, region_name
);
745 plist
.add (ARDOUR::Properties::layer
, 0);
746 plist
.add (ARDOUR::Properties::whole_file
, true);
747 plist
.add (ARDOUR::Properties::external
, true);
749 boost::shared_ptr
<Region
> r
= RegionFactory::create (sources
, plist
);
751 if (use_timestamp
&& boost::dynamic_pointer_cast
<AudioRegion
>(r
)) {
752 boost::dynamic_pointer_cast
<AudioRegion
>(r
)->special_set_position(sources
[0]->natural_position());
755 regions
.push_back (r
);
758 } else if (target_regions
== -1 || target_regions
> 1) {
760 /* take each source and create a region for each one */
763 SourceList::iterator x
;
766 for (n
= 0, x
= sources
.begin(); x
!= sources
.end(); ++x
, ++n
) {
769 just_one
.push_back (*x
);
771 region_name
= region_name_from_path ((*x
)->path(), false, false, sources
.size(), n
);
775 plist
.add (ARDOUR::Properties::start
, 0);
776 plist
.add (ARDOUR::Properties::length
, (*x
)->length (pos
));
777 plist
.add (ARDOUR::Properties::name
, region_name
);
778 plist
.add (ARDOUR::Properties::layer
, 0);
779 plist
.add (ARDOUR::Properties::whole_file
, true);
780 plist
.add (ARDOUR::Properties::external
, true);
782 boost::shared_ptr
<Region
> r
= RegionFactory::create (just_one
, plist
);
784 if (use_timestamp
&& boost::dynamic_pointer_cast
<AudioRegion
>(r
)) {
785 boost::dynamic_pointer_cast
<AudioRegion
>(r
)->special_set_position((*x
)->natural_position());
788 regions
.push_back (r
);
792 if (target_regions
== 1) {
793 input_chan
= regions
.front()->n_channels();
795 if (target_tracks
== 1) {
796 input_chan
= regions
.size();
802 if (Config
->get_output_auto_connect() & AutoConnectMaster
) {
803 output_chan
= (_session
->master_out() ? _session
->master_out()->n_inputs().n_audio() : input_chan
);
805 output_chan
= input_chan
;
810 for (vector
<boost::shared_ptr
<Region
> >::iterator r
= regions
.begin(); r
!= regions
.end(); ++r
, ++n
) {
812 finish_bringing_in_material (*r
, input_chan
, output_chan
, pos
, mode
, track
);
814 if (target_tracks
!= 1) {
817 pos
+= (*r
)->length();
821 /* setup peak file building in another thread */
823 for (SourceList::iterator x
= sources
.begin(); x
!= sources
.end(); ++x
) {
824 SourceFactory::setup_peakfile (*x
, true);
831 Editor::finish_bringing_in_material (boost::shared_ptr
<Region
> region
, uint32_t in_chans
, uint32_t out_chans
, nframes64_t
& pos
,
832 ImportMode mode
, boost::shared_ptr
<Track
>& existing_track
)
834 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
>(region
);
835 boost::shared_ptr
<MidiRegion
> mr
= boost::dynamic_pointer_cast
<MidiRegion
>(region
);
839 /* relax, its been done */
844 if (!existing_track
) {
847 existing_track
= get_nth_selected_audio_track (0);
849 existing_track
= get_nth_selected_midi_track (0);
852 if (!existing_track
) {
857 boost::shared_ptr
<Playlist
> playlist
= existing_track
->playlist();
858 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
, region
->properties()));
859 begin_reversible_command (_("insert file"));
860 playlist
->clear_history ();
861 playlist
->add_region (copy
, pos
);
862 _session
->add_command (new StatefulDiffCommand (playlist
));
863 commit_reversible_command ();
869 if (!existing_track
) {
871 list
<boost::shared_ptr
<AudioTrack
> > at (_session
->new_audio_track (in_chans
, out_chans
, Normal
, 0, 1));
877 existing_track
= at
.front();
879 list
<boost::shared_ptr
<MidiTrack
> > mt (_session
->new_midi_track (Normal
, 0, 1));
885 existing_track
= mt
.front();
888 existing_track
->set_name (region
->name());
891 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
));
892 existing_track
->playlist()->add_region (copy
, pos
);
897 case ImportAsTapeTrack
:
902 list
<boost::shared_ptr
<AudioTrack
> > at (_session
->new_audio_track (in_chans
, out_chans
, Destructive
));
904 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
));
905 at
.front()->set_name (basename_nosuffix (copy
->name()));
906 at
.front()->playlist()->add_region (copy
, pos
);
916 Editor::_import_thread (void *arg
)
918 SessionEvent::create_per_thread_pool ("import events", 64);
920 Editor
*ed
= (Editor
*) arg
;
921 return ed
->import_thread ();
925 Editor::import_thread ()
927 _session
->import_audiofiles (import_status
);
928 pthread_exit_pbd (0);