fdf90923084508454ea2afb35b2df1f138e85b03
[ardour.git] / libs / ardour / midi_diskstream.cc
1 /*
2     Copyright (C) 2000-2003 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id: diskstream.cc 567 2006-06-07 14:54:12Z trutkin $
19 */
20
21 #include <fstream>
22 #include <cstdio>
23 #include <unistd.h>
24 #include <cmath>
25 #include <cerrno>
26 #include <string>
27 #include <climits>
28 #include <fcntl.h>
29 #include <cstdlib>
30 #include <ctime>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33
34 #include <pbd/error.h>
35 #include <pbd/basename.h>
36 #include <glibmm/thread.h>
37 #include <pbd/xml++.h>
38
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/midi_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/smf_source.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/midi_playlist.h>
48 #include <ardour/cycle_timer.h>
49 #include <ardour/midi_region.h>
50
51 #include "i18n.h"
52 #include <locale.h>
53
54 using namespace std;
55 using namespace ARDOUR;
56 using namespace PBD;
57
58 sigc::signal<void,list<SMFSource*>*> MidiDiskstream::DeleteSources;
59
60 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
61         : Diskstream(sess, name, flag)
62         , _playlist(NULL)
63 {
64         /* prevent any write sources from being created */
65
66         in_set_state = true;
67
68         init (flag);
69         use_new_playlist ();
70
71         in_set_state = false;
72
73         DiskstreamCreated (this); /* EMIT SIGNAL */
74 }
75         
76 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
77         : Diskstream(sess, node)
78         , _playlist(NULL)
79 {
80         in_set_state = true;
81         init (Recordable);
82
83         if (set_state (node)) {
84                 in_set_state = false;
85                 throw failed_constructor();
86         }
87
88         in_set_state = false;
89
90         if (destructive()) {
91                 use_destructive_playlist ();
92         }
93
94         DiskstreamCreated (this); /* EMIT SIGNAL */
95 }
96
97 void
98 MidiDiskstream::init (Diskstream::Flag f)
99 {
100         Diskstream::init(f);
101
102         /* there are no channels at this point, so these
103            two calls just get speed_buffer_size and wrap_buffer
104            size setup without duplicating their code.
105         */
106
107         set_block_size (_session.get_block_size());
108         allocate_temporary_buffers ();
109
110         /* FIXME: this is now done before the above.  OK? */
111         /*pending_overwrite = false;
112         overwrite_frame = 0;
113         overwrite_queued = false;
114         input_change_pending = NoChange;*/
115
116         _n_channels = 1;
117 }
118
119 MidiDiskstream::~MidiDiskstream ()
120 {
121         Glib::Mutex::Lock lm (state_lock);
122
123         if (_playlist)
124                 _playlist->unref ();
125 }
126 /*
127 void
128 MidiDiskstream::handle_input_change (IOChange change, void *src)
129 {
130         Glib::Mutex::Lock lm (state_lock);
131
132         if (!(input_change_pending & change)) {
133                 input_change_pending = IOChange (input_change_pending|change);
134                 _session.request_input_change_handling ();
135         }
136 }
137 */
138 void
139 MidiDiskstream::non_realtime_input_change ()
140 {
141 }
142
143 void
144 MidiDiskstream::get_input_sources ()
145 {
146 }               
147
148 int
149 MidiDiskstream::find_and_use_playlist (const string& name)
150 {
151         Playlist* pl;
152         MidiPlaylist* playlist;
153                 
154         if ((pl = _session.playlist_by_name (name)) == 0) {
155                 playlist = new MidiPlaylist(_session, name);
156                 pl = playlist;
157         }
158
159         if ((playlist = dynamic_cast<MidiPlaylist*> (pl)) == 0) {
160                 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't a midi playlist"), name) << endmsg;
161                 return -1;
162         }
163
164         return use_playlist (playlist);
165 }
166
167 int
168 MidiDiskstream::use_playlist (Playlist* playlist)
169 {
170         assert(dynamic_cast<MidiPlaylist*>(playlist));
171
172         {
173                 Glib::Mutex::Lock lm (state_lock);
174
175                 if (playlist == _playlist) {
176                         return 0;
177                 }
178
179                 plstate_connection.disconnect();
180                 plmod_connection.disconnect ();
181                 plgone_connection.disconnect ();
182
183                 if (_playlist) {
184                         _playlist->unref();
185                 }
186                         
187                 _playlist = dynamic_cast<MidiPlaylist*>(playlist);
188                 _playlist->ref();
189
190                 if (!in_set_state && recordable()) {
191                         reset_write_sources (false);
192                 }
193                 
194                 plstate_connection = _playlist->StateChanged.connect (mem_fun (*this, &MidiDiskstream::playlist_changed));
195                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &MidiDiskstream::playlist_modified));
196                 plgone_connection = _playlist->GoingAway.connect (mem_fun (*this, &MidiDiskstream::playlist_deleted));
197         }
198
199         if (!overwrite_queued) {
200                 _session.request_overwrite_buffer (this);
201                 overwrite_queued = true;
202         }
203         
204         PlaylistChanged (); /* EMIT SIGNAL */
205         _session.set_dirty ();
206
207         return 0;
208 }
209
210 int
211 MidiDiskstream::use_new_playlist ()
212 {
213         string newname;
214         MidiPlaylist* playlist;
215
216         if (!in_set_state && destructive()) {
217                 return 0;
218         }
219
220         if (_playlist) {
221                 newname = Playlist::bump_name (_playlist->name(), _session);
222         } else {
223                 newname = Playlist::bump_name (_name, _session);
224         }
225
226         if ((playlist = new MidiPlaylist (_session, newname, hidden())) != 0) {
227                 playlist->set_orig_diskstream_id (id());
228                 return use_playlist (playlist);
229         } else { 
230                 return -1;
231         }
232 }
233
234 int
235 MidiDiskstream::use_copy_playlist ()
236 {
237         if (destructive()) {
238                 return 0;
239         }
240
241         if (_playlist == 0) {
242                 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
243                 return -1;
244         }
245
246         string newname;
247         MidiPlaylist* playlist;
248
249         newname = Playlist::bump_name (_playlist->name(), _session);
250         
251         if ((playlist  = new MidiPlaylist (*_playlist, newname)) != 0) {
252                 playlist->set_orig_diskstream_id (id());
253                 return use_playlist (playlist);
254         } else { 
255                 return -1;
256         }
257 }
258
259
260 void
261 MidiDiskstream::playlist_deleted (Playlist* pl)
262 {
263         /* this catches an ordering issue with session destruction. playlists 
264            are destroyed before diskstreams. we have to invalidate any handles
265            we have to the playlist.
266         */
267
268         _playlist = 0;
269 }
270
271
272 void
273 MidiDiskstream::setup_destructive_playlist ()
274 {
275         /* a single full-sized region */
276
277         //MidiRegion* region = new MidiRegion (srcs, 0, max_frames, _name);
278         //_playlist->add_region (*region, 0);           
279 }
280
281 void
282 MidiDiskstream::use_destructive_playlist ()
283 {
284         /* use the sources associated with the single full-extent region */
285         
286         Playlist::RegionList* rl = _playlist->regions_at (0);
287
288         if (rl->empty()) {
289                 reset_write_sources (false, true);
290                 return;
291         }
292
293         MidiRegion* region = dynamic_cast<MidiRegion*> (rl->front());
294
295         if (region == 0) {
296                 throw failed_constructor();
297         }
298
299         delete rl;
300
301         /* the source list will never be reset for a destructive track */
302 }
303
304 void
305 MidiDiskstream::set_io (IO& io)
306 {
307         _io = &io;
308         set_align_style_from_io ();
309 }
310
311 void
312 MidiDiskstream::non_realtime_set_speed ()
313 {
314         if (_buffer_reallocation_required)
315         {
316                 Glib::Mutex::Lock lm (state_lock);
317                 allocate_temporary_buffers ();
318
319                 _buffer_reallocation_required = false;
320         }
321
322         if (_seek_required) {
323                 if (speed() != 1.0f || speed() != -1.0f) {
324                         seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
325                 }
326                 else {
327                         seek (_session.transport_frame(), true);
328                 }
329
330                 _seek_required = false;
331         }
332 }
333
334 void
335 MidiDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
336 {
337 }
338
339 int
340 MidiDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
341 {
342         return 0;
343 }
344
345 bool
346 MidiDiskstream::commit (jack_nframes_t nframes)
347 {
348         return 0;
349 }
350
351 void
352 MidiDiskstream::set_pending_overwrite (bool yn)
353 {
354         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
355         
356         pending_overwrite = yn;
357
358         overwrite_frame = playback_sample;
359         //overwrite_offset = channels.front().playback_buf->get_read_ptr();
360 }
361
362 int
363 MidiDiskstream::overwrite_existing_buffers ()
364 {
365         return 0;
366 }
367
368 int
369 MidiDiskstream::seek (jack_nframes_t frame, bool complete_refill)
370 {
371         return 0;
372 }
373
374 int
375 MidiDiskstream::can_internal_playback_seek (jack_nframes_t distance)
376 {
377         return 0;
378 }
379
380 int
381 MidiDiskstream::internal_playback_seek (jack_nframes_t distance)
382 {
383         return 0;
384 }
385
386 int
387 MidiDiskstream::read (RawMidi* buf, RawMidi* mixdown_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt, bool reversed)
388 {
389         return 0;
390 }
391
392 int
393 MidiDiskstream::do_refill (RawMidi* mixdown_buffer, float* gain_buffer, char * workbuf)
394 {
395         return 0;
396 }       
397
398 int
399 MidiDiskstream::do_flush (char * workbuf, bool force_flush)
400 {
401         return 0;
402 }
403
404 void
405 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
406 {
407 }
408
409 void
410 MidiDiskstream::finish_capture (bool rec_monitors_input)
411 {
412 }
413
414 void
415 MidiDiskstream::set_record_enabled (bool yn, void* src)
416 {
417 }
418
419 XMLNode&
420 MidiDiskstream::get_state ()
421 {
422         XMLNode* node = new XMLNode ("MidiDiskstream");
423         char buf[64];
424         LocaleGuard lg (X_("POSIX"));
425
426         snprintf (buf, sizeof(buf), "0x%x", _flags);
427         node->add_property ("flags", buf);
428
429         node->add_property ("playlist", _playlist->name());
430         
431         snprintf (buf, sizeof(buf), "%f", _visible_speed);
432         node->add_property ("speed", buf);
433
434         node->add_property("name", _name);
435         snprintf (buf, sizeof(buf), "%" PRIu64, id());
436         node->add_property("id", buf);
437
438         if (!_capturing_sources.empty() && _session.get_record_enabled()) {
439
440                 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
441                 XMLNode* cs_grandchild;
442
443                 for (vector<SMFSource*>::iterator i = _capturing_sources.begin(); i != _capturing_sources.end(); ++i) {
444                         cs_grandchild = new XMLNode (X_("file"));
445                         cs_grandchild->add_property (X_("path"), (*i)->path());
446                         cs_child->add_child_nocopy (*cs_grandchild);
447                 }
448
449                 /* store the location where capture will start */
450
451                 Location* pi;
452
453                 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
454                         snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
455                 } else {
456                         snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
457                 }
458
459                 cs_child->add_property (X_("at"), buf);
460                 node->add_child_nocopy (*cs_child);
461         }
462
463         if (_extra_xml) {
464                 node->add_child_copy (*_extra_xml);
465         }
466
467         return* node;
468 }
469
470 int
471 MidiDiskstream::set_state (const XMLNode& node)
472 {
473         const XMLProperty* prop;
474         XMLNodeList nlist = node.children();
475         XMLNodeIterator niter;
476         uint32_t nchans = 1;
477         XMLNode* capture_pending_node = 0;
478         LocaleGuard lg (X_("POSIX"));
479
480         in_set_state = true;
481
482         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
483                 if ((*niter)->name() == IO::state_node_name) {
484                         deprecated_io_node = new XMLNode (**niter);
485                 }
486
487                 if ((*niter)->name() == X_("CapturingSources")) {
488                         capture_pending_node = *niter;
489                 }
490         }
491
492         /* prevent write sources from being created */
493         
494         in_set_state = true;
495         
496         if ((prop = node.property ("name")) != 0) {
497                 _name = prop->value();
498         } 
499
500         if (deprecated_io_node) {
501                 if ((prop = deprecated_io_node->property ("id")) != 0) {
502                         sscanf (prop->value().c_str(), "%" PRIu64, &_id);
503                 }
504         } else {
505                 if ((prop = node.property ("id")) != 0) {
506                         sscanf (prop->value().c_str(), "%" PRIu64, &_id);
507                 }
508         }
509
510         if ((prop = node.property ("flags")) != 0) {
511                 _flags = strtol (prop->value().c_str(), 0, 0);
512         }
513
514         if ((prop = node.property ("channels")) != 0) {
515                 nchans = atoi (prop->value().c_str());
516         }
517         
518         if ((prop = node.property ("playlist")) == 0) {
519                 return -1;
520         }
521
522         {
523                 bool had_playlist = (_playlist != 0);
524         
525                 if (find_and_use_playlist (prop->value())) {
526                         return -1;
527                 }
528
529                 if (!had_playlist) {
530                         _playlist->set_orig_diskstream_id (_id);
531                 }
532                 
533                 if (!destructive() && capture_pending_node) {
534                         /* destructive streams have one and only one source per channel,
535                            and so they never end up in pending capture in any useful
536                            sense.
537                         */
538                         use_pending_capture_data (*capture_pending_node);
539                 }
540
541         }
542
543         if ((prop = node.property ("speed")) != 0) {
544                 double sp = atof (prop->value().c_str());
545
546                 if (realtime_set_speed (sp, false)) {
547                         non_realtime_set_speed ();
548                 }
549         }
550
551         in_set_state = false;
552
553         /* make sure this is clear before we do anything else */
554
555         _capturing_sources.clear ();
556
557         /* write sources are handled when we handle the input set 
558            up of the IO that owns this DS (::non_realtime_input_change())
559         */
560                 
561         in_set_state = false;
562
563         return 0;
564 }
565
566 int
567 MidiDiskstream::use_new_write_source (uint32_t n)
568 {
569         return 0;
570 }
571
572 void
573 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
574 {
575 }
576
577 int
578 MidiDiskstream::rename_write_sources ()
579 {
580         return 0;
581 }
582
583 void
584 MidiDiskstream::set_block_size (jack_nframes_t nframes)
585 {
586 }
587
588 void
589 MidiDiskstream::allocate_temporary_buffers ()
590 {
591 }
592
593 void
594 MidiDiskstream::monitor_input (bool yn)
595 {
596 }
597
598 void
599 MidiDiskstream::set_align_style_from_io ()
600 {
601 }
602
603
604 float
605 MidiDiskstream::playback_buffer_load () const
606 {
607         return 0;
608 }
609
610 float
611 MidiDiskstream::capture_buffer_load () const
612 {
613         return 0;
614 }
615
616
617 int
618 MidiDiskstream::use_pending_capture_data (XMLNode& node)
619 {
620         return 0;
621 }