main fix: when transport stops, clear per-region per-playlist note trackers even...
[ardour.git] / libs / ardour / audio_playlist_importer.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/audio_playlist_importer.h"
22
23 #include <sstream>
24
25 #include "pbd/failed_constructor.h"
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28
29 #include "ardour/audio_region_importer.h"
30 #include "ardour/session.h"
31 #include "ardour/playlist.h"
32 #include "ardour/playlist_factory.h"
33 #include "ardour/session_playlists.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace PBD;
39 using namespace ARDOUR;
40
41 /**** Handler ***/
42 AudioPlaylistImportHandler::AudioPlaylistImportHandler (XMLTree const & source, Session & session, AudioRegionImportHandler & region_handler, const char * nodename) :
43   ElementImportHandler (source, session),
44   region_handler (region_handler)
45 {
46         XMLNode const * root = source.root();
47         XMLNode const * playlists;
48
49         if (!(playlists = root->child (nodename))) {
50                 throw failed_constructor();
51         }
52
53         XMLNodeList const & pl_children = playlists->children();
54         for (XMLNodeList::const_iterator it = pl_children.begin(); it != pl_children.end(); ++it) {
55                 const XMLProperty* type = (*it)->property("type");
56                 if ( !type || type->value() == "audio" ) {
57                         try {
58                                 elements.push_back (ElementPtr ( new AudioPlaylistImporter (source, session, *this, **it)));
59                         } catch (failed_constructor err) {
60                                 set_dirty();
61                         }
62                 }
63         }
64 }
65
66 string
67 AudioPlaylistImportHandler::get_info () const
68 {
69         return _("Audio Playlists");
70 }
71
72 void
73 AudioPlaylistImportHandler::get_regions (XMLNode const & node, ElementList & list) const
74 {
75         region_handler.create_regions_from_children (node, list);
76 }
77
78 void
79 AudioPlaylistImportHandler::update_region_id (XMLProperty* id_prop)
80 {
81         PBD::ID old_id (id_prop->value());
82         PBD::ID new_id (region_handler.get_new_id (old_id));
83         id_prop->set_value (new_id.to_s());
84 }
85
86 void
87 AudioPlaylistImportHandler::playlists_by_diskstream (PBD::ID const & id, PlaylistList & list) const
88 {
89         for (ElementList::const_iterator it = elements.begin(); it != elements.end(); ++it) {
90                 boost::shared_ptr<AudioPlaylistImporter> pl = boost::dynamic_pointer_cast<AudioPlaylistImporter> (*it);
91                 if (pl && pl->orig_diskstream() == id) {
92                         list.push_back (PlaylistPtr (new AudioPlaylistImporter (*pl)));
93                 }
94         }
95 }
96
97 /*** AudioPlaylistImporter ***/
98 AudioPlaylistImporter::AudioPlaylistImporter (XMLTree const & source, Session & session, AudioPlaylistImportHandler & handler, XMLNode const & node) :
99   ElementImporter (source, session),
100   handler (handler),
101   orig_node (node),
102   xml_playlist (node),
103   diskstream_id ("0")
104 {
105         bool ds_ok = false;
106
107         populate_region_list ();
108
109         // Parse XML
110         XMLPropertyList const & props = xml_playlist.properties();
111         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
112                 string prop = (*it)->name();
113                 if (!prop.compare("type") || !prop.compare("frozen")) {
114                         // All ok
115                 } else if (!prop.compare("name")) {
116                         name = (*it)->value();
117                 } else if (!prop.compare("orig-diskstream-id")) {
118                         orig_diskstream_id = (*it)->value();
119                         ds_ok = true;
120                 } else {
121                         std::cerr << string_compose (X_("AudioPlaylistImporter did not recognise XML-property \"%1\""), prop) << endmsg;
122                 }
123         }
124
125         if (!ds_ok) {
126                 error << string_compose (X_("AudioPlaylistImporter (%1): did not find XML-property \"orig_diskstream_id\" which is mandatory"), name) << endmsg;
127                 throw failed_constructor();
128         }
129 }
130
131 AudioPlaylistImporter::AudioPlaylistImporter (AudioPlaylistImporter const & other) :
132   ElementImporter (other.source, other.session),
133   handler (other.handler),
134   orig_node (other.orig_node),
135   xml_playlist (other.xml_playlist),
136   orig_diskstream_id (other.orig_diskstream_id)
137 {
138         populate_region_list ();
139 }
140
141 AudioPlaylistImporter::~AudioPlaylistImporter ()
142 {
143
144 }
145
146 string
147 AudioPlaylistImporter::get_info () const
148 {
149         XMLNodeList children = xml_playlist.children();
150         unsigned int regions = 0;
151         std::ostringstream oss;
152
153         for (XMLNodeIterator it = children.begin(); it != children.end(); it++) {
154                 if ((*it)->name() == "Region") {
155                         ++regions;
156                 }
157         }
158
159         oss << regions << " ";
160
161         if (regions == 1) {
162                 oss << _("region");
163         } else {
164                 oss << _("regions");
165         }
166
167         return oss.str();
168 }
169
170 bool
171 AudioPlaylistImporter::_prepare_move ()
172 {
173         // Rename
174         while (session.playlists->by_name (name) || !handler.check_name (name)) {
175                 std::pair<bool, string> rename_pair = *Rename (_("A playlist with this name already exists, please rename it."), name);
176                 if (!rename_pair.first) {
177                         return false;
178                 }
179                 name = rename_pair.second;
180         }
181         xml_playlist.property ("name")->set_value (name);
182         handler.add_name (name);
183
184         return true;
185 }
186
187 void
188 AudioPlaylistImporter::_cancel_move ()
189 {
190         handler.remove_name (name);
191 }
192
193 void
194 AudioPlaylistImporter::_move ()
195 {
196         boost::shared_ptr<Playlist> playlist;
197
198         // Update diskstream id
199         xml_playlist.property ("orig-diskstream-id")->set_value (diskstream_id.to_s());
200
201         // Update region XML in playlist and prepare sources
202         xml_playlist.remove_nodes("Region");
203         for (RegionList::iterator it = regions.begin(); it != regions.end(); ++it) {
204                 xml_playlist.add_child_copy ((*it)->get_xml());
205                 (*it)->add_sources_to_session();
206                 if ((*it)->broken()) {
207                         handler.set_dirty();
208                         set_broken();
209                         return; // TODO clean up?
210                 }
211         }
212
213         // Update region ids in crossfades
214         XMLNodeList crossfades = xml_playlist.children("Crossfade");
215         for (XMLNodeIterator it = crossfades.begin(); it != crossfades.end(); ++it) {
216                 XMLProperty* in = (*it)->property("in");
217                 XMLProperty* out = (*it)->property("out");
218                 if (!in || !out) {
219                         error << string_compose (X_("AudioPlaylistImporter (%1): did not find the \"in\" or \"out\" property from a crossfade"), name) << endmsg;
220                 }
221
222                 handler.update_region_id (in);
223                 handler.update_region_id (out);
224
225                 // rate convert length and position
226                 XMLProperty* length = (*it)->property("length");
227                 if (length) {
228                         length->set_value (rate_convert_samples (length->value()));
229                 }
230
231                 XMLProperty* position = (*it)->property("position");
232                 if (position) {
233                         position->set_value (rate_convert_samples (position->value()));
234                 }
235         }
236
237         // Create playlist
238         playlist = PlaylistFactory::create (session, xml_playlist, false, true);
239 }
240
241 void
242 AudioPlaylistImporter::set_diskstream (PBD::ID const & id)
243 {
244         diskstream_id = id;
245 }
246
247 void
248 AudioPlaylistImporter::populate_region_list ()
249 {
250         ElementImportHandler::ElementList elements;
251         handler.get_regions (orig_node, elements);
252         for (ElementImportHandler::ElementList::iterator it = elements.begin(); it != elements.end(); ++it) {
253                 regions.push_back (boost::dynamic_pointer_cast<AudioRegionImporter> (*it));
254         }
255 }
256
257 string
258 UnusedAudioPlaylistImportHandler::get_info () const
259 {
260         return _("Audio Playlists (unused)");
261 }