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 "ardour/operations.h"
52 #include "pbd/memento_command.h"
54 #include "ardour_ui.h"
58 #include "audio_time_axis.h"
59 #include "midi_time_axis.h"
60 #include "session_import_dialog.h"
62 #include "gui_thread.h"
63 #include "interthread_progress_window.h"
64 #include "mouse_cursors.h"
65 #include "editor_cursors.h"
70 using namespace ARDOUR
;
73 using namespace Gtkmm2ext
;
74 using namespace Editing
;
77 /* Functions supporting the incorporation of external (non-captured) audio material into ardour */
80 Editor::add_external_audio_action (ImportMode mode_hint
)
83 MessageDialog
msg (_("You can't import or embed an audiofile until you have a session loaded."));
89 sfbrowser
= new SoundFileOmega (*this, _("Add Existing Media"), _session
, 0, true, mode_hint
);
91 sfbrowser
->set_mode (mode_hint
);
94 external_audio_dialog ();
98 Editor::external_audio_dialog ()
100 vector
<string
> paths
;
101 uint32_t audio_track_cnt
;
102 uint32_t midi_track_cnt
;
105 MessageDialog
msg (_("You can't import or embed an audiofile until you have a session loaded."));
113 for (TrackSelection::iterator x
= selection
->tracks
.begin(); x
!= selection
->tracks
.end(); ++x
) {
114 AudioTimeAxisView
* atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
117 if (atv
->is_audio_track()) {
122 MidiTimeAxisView
* mtv
= dynamic_cast<MidiTimeAxisView
*>(*x
);
125 if (mtv
->is_midi_track()) {
132 if (sfbrowser
== 0) {
133 sfbrowser
= new SoundFileOmega (*this, _("Add Existing Media"), _session
, audio_track_cnt
, midi_track_cnt
, true);
135 sfbrowser
->reset (audio_track_cnt
, midi_track_cnt
);
138 sfbrowser
->show_all ();
146 int response
= sfbrowser
->run ();
150 // leave the dialog open
158 // cancel from the browser - we are done
165 vector
<string
> upaths
= sfbrowser
->get_paths ();
166 for (vector
<string
>::iterator x
= upaths
.begin(); x
!= upaths
.end(); ++x
) {
167 paths
.push_back (*x
);
170 ImportPosition pos
= sfbrowser
->get_position ();
171 ImportMode mode
= sfbrowser
->get_mode ();
172 ImportDisposition chns
= sfbrowser
->get_channel_disposition ();
176 case ImportAtEditPoint
:
177 where
= get_preferred_edit_position ();
179 case ImportAtTimestamp
:
182 case ImportAtPlayhead
:
183 where
= playhead_cursor
->current_frame
;
186 where
= _session
->current_start_frame();
190 SrcQuality quality
= sfbrowser
->get_src_quality();
193 if (sfbrowser
->copy_files_btn
.get_active()) {
194 do_import (paths
, chns
, mode
, quality
, where
);
196 do_embed (paths
, chns
, mode
, where
);
199 if (response
== RESPONSE_APPLY
) {
200 sfbrowser
->clear_selection ();
204 } while (keepRunning
);
208 Editor::session_import_dialog ()
210 SessionImportDialog
dialog (_session
);
211 ensure_float (dialog
);
215 typedef std::map
<PBD::ID
,boost::shared_ptr
<ARDOUR::Source
> > SourceMap
;
218 * Updating is still disabled, see note in libs/ardour/import.cc Session::import_audiofiles()
221 * true = show "Update", "Import" and "Skip"
222 * false = show "Import", and "Cancel"
225 * 0 To update an existing source of the same name
226 * 1 To import/embed the file normally (make sure the new name will be unique)
227 * 2 If the user wants to skip this file
230 Editor::check_whether_and_how_to_import(string path
, bool all_or_nothing
)
232 string
wave_name (Glib::path_get_basename(path
));
234 SourceMap all_sources
= _session
->get_sources();
235 bool already_exists
= false;
238 if ((existing
= _session
->count_sources_by_origin (path
)) > 0) {
239 already_exists
= true;
244 if (already_exists
) {
246 if (all_or_nothing
) {
247 // updating is still disabled
248 //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);
249 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
);
251 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
);
254 MessageDialog
dialog(message
, false, Gtk::MESSAGE_QUESTION
, Gtk::BUTTONS_NONE
, true);
256 if (all_or_nothing
) {
258 //dialog.add_button("Update", 0);
259 dialog
.add_button("Import", 1);
260 dialog
.add_button("Skip", 2);
262 dialog
.add_button("Import", 1);
263 dialog
.add_button("Cancel", 2);
266 //dialog.add_button("Skip all", 4); // All or rest?
270 function
= dialog
.run ();
278 boost::shared_ptr
<AudioTrack
>
279 Editor::get_nth_selected_audio_track (int nth
) const
281 AudioTimeAxisView
* atv
;
282 TrackSelection::iterator x
;
284 for (x
= selection
->tracks
.begin(); nth
> 0 && x
!= selection
->tracks
.end(); ++x
) {
286 atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
290 } else if (atv
->is_audio_track()) {
295 if (x
== selection
->tracks
.end()) {
296 atv
= dynamic_cast<AudioTimeAxisView
*>(selection
->tracks
.back());
298 atv
= dynamic_cast<AudioTimeAxisView
*>(*x
);
301 if (!atv
|| !atv
->is_audio_track()) {
302 return boost::shared_ptr
<AudioTrack
>();
305 return atv
->audio_track();
308 boost::shared_ptr
<MidiTrack
>
309 Editor::get_nth_selected_midi_track (int nth
) const
311 MidiTimeAxisView
* mtv
;
312 TrackSelection::iterator x
;
314 for (x
= selection
->tracks
.begin(); nth
> 0 && x
!= selection
->tracks
.end(); ++x
) {
316 mtv
= dynamic_cast<MidiTimeAxisView
*>(*x
);
320 } else if (mtv
->is_midi_track()) {
325 if (x
== selection
->tracks
.end()) {
326 mtv
= dynamic_cast<MidiTimeAxisView
*>(selection
->tracks
.back());
328 mtv
= dynamic_cast<MidiTimeAxisView
*>(*x
);
331 if (!mtv
|| !mtv
->is_midi_track()) {
332 return boost::shared_ptr
<MidiTrack
>();
335 return mtv
->midi_track();
339 Editor::do_import (vector
<string
> paths
, ImportDisposition chns
, ImportMode mode
, SrcQuality quality
, framepos_t
& pos
)
341 boost::shared_ptr
<Track
> track
;
342 vector
<string
> to_import
;
344 bool use_timestamp
= (pos
== -1);
346 current_interthread_info
= &import_status
;
347 import_status
.current
= 1;
348 import_status
.total
= paths
.size ();
349 import_status
.all_done
= false;
351 ImportProgressWindow
ipw (&import_status
, _("Import"), _("Cancel Import"));
355 if (chns
== Editing::ImportMergeFiles
) {
357 /* create 1 region from all paths, add to 1 track,
362 for (vector
<string
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
363 int check
= check_whether_and_how_to_import(*a
, false);
374 ok
= (import_sndfiles (paths
, mode
, quality
, pos
, 1, 1, track
, false) == 0);
379 bool replace
= false;
381 for (vector
<string
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
383 const int check
= check_whether_and_how_to_import (*a
, true);
390 fatal
<< "Updating existing sources should be disabled!" << endmsg
;
397 fatal
<< "Illegal return " << check
<< " from check_whether_and_how_to_import()!" << endmsg
;
401 /* have to reset this for every file we handle */
410 case Editing::ImportDistinctFiles
:
413 to_import
.push_back (*a
);
415 if (mode
== Editing::ImportToTrack
) {
416 track
= get_nth_selected_audio_track (nth
++);
419 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, 1, -1, track
, replace
) == 0);
422 case Editing::ImportDistinctChannels
:
425 to_import
.push_back (*a
);
427 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, -1, -1, track
, replace
) == 0);
430 case Editing::ImportSerializeFiles
:
433 to_import
.push_back (*a
);
435 ok
= (import_sndfiles (to_import
, mode
, quality
, pos
, 1, 1, track
, replace
) == 0);
438 case Editing::ImportMergeFiles
:
439 // Not entered, handled in earlier if() branch
446 _session
->save_state ("");
449 import_status
.all_done
= true;
453 Editor::do_embed (vector
<string
> paths
, ImportDisposition chns
, ImportMode mode
, framepos_t
& pos
)
455 boost::shared_ptr
<Track
> track
;
456 bool check_sample_rate
= true;
458 vector
<string
> to_embed
;
459 bool multi
= paths
.size() > 1;
461 bool use_timestamp
= (pos
== -1);
464 case Editing::ImportDistinctFiles
:
465 for (vector
<string
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
467 /* have to reset this for every file we handle */
473 to_embed
.push_back (*a
);
475 if (mode
== Editing::ImportToTrack
) {
476 track
= get_nth_selected_audio_track (nth
++);
479 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, 1, -1, track
) < -1) {
485 case Editing::ImportDistinctChannels
:
486 for (vector
<string
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
488 /* have to reset this for every file we handle */
494 to_embed
.push_back (*a
);
496 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, -1, -1, track
) < -1) {
502 case Editing::ImportMergeFiles
:
503 if (embed_sndfiles (paths
, multi
, check_sample_rate
, mode
, pos
, 1, 1, track
) < -1) {
508 case Editing::ImportSerializeFiles
:
509 for (vector
<string
>::iterator a
= paths
.begin(); a
!= paths
.end(); ++a
) {
511 /* have to reset this for every file we handle */
517 to_embed
.push_back (*a
);
519 if (embed_sndfiles (to_embed
, multi
, check_sample_rate
, mode
, pos
, 1, 1, track
) < -1) {
530 _session
->save_state ("");
535 Editor::import_sndfiles (vector
<string
> paths
, ImportMode mode
, SrcQuality quality
, framepos_t
& pos
,
536 int target_regions
, int target_tracks
, boost::shared_ptr
<Track
>& track
, bool replace
)
538 import_status
.paths
= paths
;
539 import_status
.done
= false;
540 import_status
.cancel
= false;
541 import_status
.freeze
= false;
542 import_status
.quality
= quality
;
543 import_status
.replace_existing_source
= replace
;
545 import_status
.mode
= mode
;
546 import_status
.pos
= pos
;
547 import_status
.target_tracks
= target_tracks
;
548 import_status
.target_regions
= target_regions
;
549 import_status
.track
= track
;
550 import_status
.replace
= replace
;
552 set_canvas_cursor (_cursors
->wait
);
555 /* start import thread for this spec. this will ultimately call Session::import_audiofiles()
556 which, if successful, will add the files as regions to the region list. its up to us
557 (the GUI) to direct additional steps after that.
560 pthread_create_and_store ("import", &import_status
.thread
, _import_thread
, this);
561 pthread_detach (import_status
.thread
);
563 while (!import_status
.done
&& !import_status
.cancel
) {
564 gtk_main_iteration ();
567 import_status
.done
= true;
571 if (!import_status
.cancel
&& !import_status
.sources
.empty()) {
572 result
= add_sources (
574 import_status
.sources
,
577 import_status
.target_regions
,
578 import_status
.target_tracks
,
582 /* update position from results */
584 pos
= import_status
.pos
;
587 set_canvas_cursor (current_canvas_cursor
);
592 Editor::embed_sndfiles (vector
<string
> paths
, bool multifile
,
593 bool& check_sample_rate
, ImportMode mode
, framepos_t
& pos
, int target_regions
, int target_tracks
,
594 boost::shared_ptr
<Track
>& track
)
596 boost::shared_ptr
<AudioFileSource
> source
;
602 set_canvas_cursor (_cursors
->wait
);
605 for (vector
<string
>::iterator p
= paths
.begin(); p
!= paths
.end(); ++p
) {
610 /* note that we temporarily truncated _id at the colon */
612 if (!AudioFileSource::get_soundfile_info (path
, finfo
, error_msg
)) {
613 error
<< string_compose(_("Editor: cannot open file \"%1\", (%2)"), path
, error_msg
) << endmsg
;
617 if (check_sample_rate
&& (finfo
.samplerate
!= (int) _session
->frame_rate())) {
618 vector
<string
> choices
;
621 choices
.push_back (_("Cancel entire import"));
622 choices
.push_back (_("Don't embed it"));
623 choices
.push_back (_("Embed all without questions"));
625 Gtkmm2ext::Choice
rate_choice (
627 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"),
628 short_path (path
, 40)),
632 int resx
= rate_choice
.run ();
635 case 0: /* stop a multi-file import */
638 case 1: /* don't embed this one */
641 case 2: /* do it, and the rest without asking */
642 check_sample_rate
= false;
651 choices
.push_back (_("Cancel"));
652 choices
.push_back (_("Embed it anyway"));
654 Gtkmm2ext::Choice
rate_choice (
656 string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path
),
660 int resx
= rate_choice
.run ();
663 case 0: /* don't import */
675 set_canvas_cursor (_cursors
->wait
);
677 for (int n
= 0; n
< finfo
.channels
; ++n
) {
680 /* check if we have this thing embedded already */
682 boost::shared_ptr
<Source
> s
;
684 if ((s
= _session
->source_by_path_and_channel (path
, n
)) == 0) {
686 source
= boost::dynamic_pointer_cast
<AudioFileSource
> (
687 SourceFactory::createReadable (DataType::AUDIO
, *_session
,
689 (mode
== ImportAsTapeTrack
690 ? Source::Destructive
694 source
= boost::dynamic_pointer_cast
<AudioFileSource
> (s
);
697 sources
.push_back(source
);
700 catch (failed_constructor
& err
) {
701 error
<< string_compose(_("could not open %1"), path
) << endmsg
;
705 ARDOUR_UI::instance()->flush_pending ();
709 if (sources
.empty()) {
713 ret
= add_sources (paths
, sources
, pos
, mode
, target_regions
, target_tracks
, track
, true);
716 set_canvas_cursor (current_canvas_cursor
);
721 Editor::add_sources (vector
<string
> paths
, SourceList
& sources
, framepos_t
& pos
, ImportMode mode
,
722 int target_regions
, int target_tracks
, boost::shared_ptr
<Track
>& track
, bool /*add_channel_suffix*/)
724 vector
<boost::shared_ptr
<Region
> > regions
;
726 uint32_t input_chan
= 0;
727 uint32_t output_chan
= 0;
730 use_timestamp
= (pos
== -1);
732 // kludge (for MIDI we're abusing "channel" for "track" here)
733 if (SMFSource::safe_midi_file_extension (paths
.front())) {
737 if (target_regions
== 1) {
739 /* take all the sources we have and package them up as a region */
741 region_name
= region_name_from_path (paths
.front(), (sources
.size() > 1), false);
743 /* we checked in import_sndfiles() that there were not too many */
745 while (RegionFactory::region_by_name (region_name
)) {
746 region_name
= bump_name_once (region_name
, '.');
751 plist
.add (ARDOUR::Properties::start
, 0);
752 plist
.add (ARDOUR::Properties::length
, sources
[0]->length (pos
));
753 plist
.add (ARDOUR::Properties::name
, region_name
);
754 plist
.add (ARDOUR::Properties::layer
, 0);
755 plist
.add (ARDOUR::Properties::whole_file
, true);
756 plist
.add (ARDOUR::Properties::external
, true);
758 boost::shared_ptr
<Region
> r
= RegionFactory::create (sources
, plist
);
760 if (use_timestamp
&& boost::dynamic_pointer_cast
<AudioRegion
>(r
)) {
761 boost::dynamic_pointer_cast
<AudioRegion
>(r
)->special_set_position(sources
[0]->natural_position());
764 regions
.push_back (r
);
767 } else if (target_regions
== -1 || target_regions
> 1) {
769 /* take each source and create a region for each one */
772 SourceList::iterator x
;
775 for (n
= 0, x
= sources
.begin(); x
!= sources
.end(); ++x
, ++n
) {
778 just_one
.push_back (*x
);
780 boost::shared_ptr
<FileSource
> fs
= boost::dynamic_pointer_cast
<FileSource
> (*x
);
783 region_name
= region_name_from_path (fs
->path(), false, false, sources
.size(), n
);
785 region_name
= (*x
)->name();
790 /* Fudge region length to ensure it is non-zero; make it 1 beat at 120bpm
791 for want of a better idea. It can't be too small, otherwise if this
792 is a MIDI region the conversion from frames -> beats -> frames will
793 round it back down to 0 again.
795 framecnt_t len
= (*x
)->length (pos
);
797 len
= (60 / 120) * _session
->frame_rate ();
800 plist
.add (ARDOUR::Properties::start
, 0);
801 plist
.add (ARDOUR::Properties::length
, len
);
802 plist
.add (ARDOUR::Properties::name
, region_name
);
803 plist
.add (ARDOUR::Properties::layer
, 0);
804 plist
.add (ARDOUR::Properties::whole_file
, true);
805 plist
.add (ARDOUR::Properties::external
, true);
807 boost::shared_ptr
<Region
> r
= RegionFactory::create (just_one
, plist
);
809 if (use_timestamp
&& boost::dynamic_pointer_cast
<AudioRegion
>(r
)) {
810 boost::dynamic_pointer_cast
<AudioRegion
>(r
)->special_set_position((*x
)->natural_position());
813 regions
.push_back (r
);
817 if (target_regions
== 1) {
818 input_chan
= regions
.front()->n_channels();
820 if (target_tracks
== 1) {
821 input_chan
= regions
.size();
827 if (Config
->get_output_auto_connect() & AutoConnectMaster
) {
828 output_chan
= (_session
->master_out() ? _session
->master_out()->n_inputs().n_audio() : input_chan
);
830 output_chan
= input_chan
;
836 for (vector
<boost::shared_ptr
<Region
> >::iterator r
= regions
.begin(); r
!= regions
.end(); ++r
, ++n
) {
837 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
> (*r
);
842 /* get timestamp for this region */
844 const boost::shared_ptr
<Source
> s (ar
->sources().front());
845 const boost::shared_ptr
<AudioSource
> as
= boost::dynamic_pointer_cast
<AudioSource
> (s
);
849 if (as
->natural_position() != 0) {
850 pos
= as
->natural_position();
851 } else if (target_tracks
== 1) {
852 /* hmm, no timestamp available, put it after the previous region
855 pos
= get_preferred_edit_position ();
860 pos
= get_preferred_edit_position ();
863 /* should really get first position in MIDI file, but for now, use edit position*/
864 pos
= get_preferred_edit_position ();
869 finish_bringing_in_material (*r
, input_chan
, output_chan
, pos
, mode
, track
);
871 rlen
= (*r
)->length();
873 if (target_tracks
!= 1) {
876 if (!use_timestamp
|| !ar
) {
877 /* line each one up right after the other */
878 pos
+= (*r
)->length();
883 /* setup peak file building in another thread */
885 for (SourceList::iterator x
= sources
.begin(); x
!= sources
.end(); ++x
) {
886 SourceFactory::setup_peakfile (*x
, true);
893 Editor::finish_bringing_in_material (boost::shared_ptr
<Region
> region
, uint32_t in_chans
, uint32_t out_chans
, framepos_t
& pos
,
894 ImportMode mode
, boost::shared_ptr
<Track
>& existing_track
)
896 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
>(region
);
897 boost::shared_ptr
<MidiRegion
> mr
= boost::dynamic_pointer_cast
<MidiRegion
>(region
);
901 /* relax, its been done */
906 if (!existing_track
) {
909 existing_track
= get_nth_selected_audio_track (0);
911 existing_track
= get_nth_selected_midi_track (0);
914 if (!existing_track
) {
919 boost::shared_ptr
<Playlist
> playlist
= existing_track
->playlist();
920 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
, region
->properties()));
921 begin_reversible_command (Operations::insert_file
);
922 playlist
->clear_changes ();
923 playlist
->add_region (copy
, pos
);
924 _session
->add_command (new StatefulDiffCommand (playlist
));
925 commit_reversible_command ();
931 if (!existing_track
) {
933 list
<boost::shared_ptr
<AudioTrack
> > at (_session
->new_audio_track (in_chans
, out_chans
, Normal
, 0, 1));
939 existing_track
= at
.front();
941 list
<boost::shared_ptr
<MidiTrack
> > mt (_session
->new_midi_track (Normal
, 0, 1));
947 existing_track
= mt
.front();
950 existing_track
->set_name (region
->name());
953 boost::shared_ptr
<Playlist
> playlist
= existing_track
->playlist();
954 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
));
955 begin_reversible_command (Operations::insert_file
);
956 playlist
->clear_changes ();
957 playlist
->add_region (copy
, pos
);
958 _session
->add_command (new StatefulDiffCommand (playlist
));
959 commit_reversible_command ();
963 case ImportAsTapeTrack
:
969 list
<boost::shared_ptr
<AudioTrack
> > at (_session
->new_audio_track (in_chans
, out_chans
, Destructive
));
971 boost::shared_ptr
<Playlist
> playlist
= at
.front()->playlist();
972 boost::shared_ptr
<Region
> copy (RegionFactory::create (region
));
973 begin_reversible_command (Operations::insert_file
);
974 playlist
->clear_changes ();
975 playlist
->add_region (copy
, pos
);
976 _session
->add_command (new StatefulDiffCommand (playlist
));
977 commit_reversible_command ();
987 Editor::_import_thread (void *arg
)
989 SessionEvent::create_per_thread_pool ("import events", 64);
991 Editor
*ed
= (Editor
*) arg
;
992 return ed
->import_thread ();
996 Editor::import_thread ()
998 _session
->import_audiofiles (import_status
);
999 pthread_exit_pbd (0);