The great audio processing overhaul.
[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
19 #include <fstream>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <cmath>
23 #include <cerrno>
24 #include <string>
25 #include <climits>
26 #include <fcntl.h>
27 #include <cstdlib>
28 #include <ctime>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31
32 #include "pbd/error.h"
33 #include "pbd/basename.h"
34 #include <glibmm/thread.h>
35 #include "pbd/xml++.h"
36 #include "pbd/memento_command.h"
37 #include "pbd/enumwriter.h"
38
39 #include "ardour/ardour.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/configuration.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/io.h"
44 #include "ardour/midi_diskstream.h"
45 #include "ardour/midi_playlist.h"
46 #include "ardour/midi_port.h"
47 #include "ardour/midi_region.h"
48 #include "ardour/playlist_factory.h"
49 #include "ardour/region_factory.h"
50 #include "ardour/send.h"
51 #include "ardour/session.h"
52 #include "ardour/smf_source.h"
53 #include "ardour/utils.h"
54
55 #include "i18n.h"
56 #include <locale.h>
57
58 using namespace std;
59 using namespace ARDOUR;
60 using namespace PBD;
61
62 nframes_t MidiDiskstream::midi_readahead = 4096;
63
64 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65         : Diskstream(sess, name, flag)
66         , _playback_buf(0)
67         , _capture_buf(0)
68         , _source_port(0)
69         , _last_flush_frame(0)
70         , _note_mode(Sustained)
71         , _frames_written_to_ringbuffer(0)
72         , _frames_read_from_ringbuffer(0)
73 {
74         /* prevent any write sources from being created */
75
76         in_set_state = true;
77
78         init(flag);
79         use_new_playlist ();
80
81         in_set_state = false;
82
83         assert(!destructive());
84 }
85         
86 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
87         : Diskstream(sess, node)
88         , _playback_buf(0)
89         , _capture_buf(0)
90         , _source_port(0)
91         , _last_flush_frame(0)
92         , _note_mode(Sustained)
93         , _frames_written_to_ringbuffer(0)
94         , _frames_read_from_ringbuffer(0)
95 {
96         in_set_state = true;
97         init (Recordable);
98
99         if (set_state (node)) {
100                 in_set_state = false;
101                 throw failed_constructor();
102         }
103
104         in_set_state = false;
105
106         if (destructive()) {
107                 use_destructive_playlist ();
108         }
109 }
110
111 void
112 MidiDiskstream::init (Diskstream::Flag f)
113 {
114         Diskstream::init(f);
115
116         /* there are no channels at this point, so these
117            two calls just get speed_buffer_size and wrap_buffer
118            size setup without duplicating their code.
119         */
120
121         set_block_size (_session.get_block_size());
122         allocate_temporary_buffers ();
123
124         const size_t size = _session.midi_diskstream_buffer_size();
125         _playback_buf = new MidiRingBuffer<nframes_t>(size);
126         _capture_buf = new MidiRingBuffer<nframes_t>(size);
127         
128         _n_channels = ChanCount(DataType::MIDI, 1);
129
130         assert(recordable());
131 }
132
133 MidiDiskstream::~MidiDiskstream ()
134 {
135         Glib::Mutex::Lock lm (state_lock);
136 }
137
138         
139 void
140 MidiDiskstream::non_realtime_locate (nframes_t position)
141 {
142         if (_write_source) {
143                 _write_source->set_timeline_position (position);
144         }
145         seek(position, false);
146 }
147
148
149 void
150 MidiDiskstream::non_realtime_input_change ()
151 {
152         { 
153                 Glib::Mutex::Lock lm (state_lock);
154
155                 if (input_change_pending == NoChange) {
156                         return;
157                 }
158
159                 if (input_change_pending & ConfigurationChanged) {
160                         if (_io->n_inputs().n_midi() != _n_channels.n_midi()) {
161                                 error << "Can not feed IO " << _io->n_inputs()
162                                         << " with diskstream " << _n_channels << endl;
163                         }
164                 } 
165
166                 get_input_sources ();
167                 set_capture_offset ();
168
169                 if (first_input_change) {
170                         set_align_style (_persistent_alignment_style);
171                         first_input_change = false;
172                 } else {
173                         set_align_style_from_io ();
174                 }
175
176                 input_change_pending = NoChange;
177                 
178                 /* implicit unlock */
179         }
180
181         /* reset capture files */
182
183         reset_write_sources (false);
184
185         /* now refill channel buffers */
186
187         if (speed() != 1.0f || speed() != -1.0f) {
188                 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
189         }
190         else {
191                 seek (_session.transport_frame());
192         }
193
194         _last_flush_frame = _session.transport_frame();
195 }
196
197 void
198 MidiDiskstream::get_input_sources ()
199 {
200         uint32_t ni = _io->n_inputs().n_midi();
201
202         if (ni == 0) {
203                 return;
204         }
205
206         // This is all we do for now at least
207         assert(ni == 1);
208
209         _source_port = _io->midi_input(0);
210
211         // do... stuff?
212 }               
213
214 int
215 MidiDiskstream::find_and_use_playlist (const string& name)
216 {
217         boost::shared_ptr<MidiPlaylist> playlist;
218                 
219         if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlist_by_name (name))) == 0) {
220                 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
221         }
222
223         if (!playlist) {
224                 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
225                 return -1;
226         }
227
228         return use_playlist (playlist);
229 }
230
231 int
232 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
233 {       
234         assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
235
236         Diskstream::use_playlist(playlist);
237
238         return 0;
239 }
240
241 int
242 MidiDiskstream::use_new_playlist ()
243 {       
244         string newname;
245         boost::shared_ptr<MidiPlaylist> playlist;
246
247         if (!in_set_state && destructive()) {
248                 return 0;
249         }
250
251         if (_playlist) {
252                 newname = Playlist::bump_name (_playlist->name(), _session);
253         } else {
254                 newname = Playlist::bump_name (_name, _session);
255         }
256
257         if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
258                         DataType::MIDI, _session, newname, hidden()))) != 0) {
259                 
260                 playlist->set_orig_diskstream_id (id());
261                 return use_playlist (playlist);
262
263         } else { 
264                 return -1;
265         }
266 }
267
268 int
269 MidiDiskstream::use_copy_playlist ()
270 {
271         assert(midi_playlist());
272
273         if (destructive()) {
274                 return 0;
275         }
276
277         if (_playlist == 0) {
278                 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
279                 return -1;
280         }
281
282         string newname;
283         boost::shared_ptr<MidiPlaylist> playlist;
284
285         newname = Playlist::bump_name (_playlist->name(), _session);
286         
287         if ((playlist  = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
288                 playlist->set_orig_diskstream_id (id());
289                 return use_playlist (playlist);
290         } else { 
291                 return -1;
292         }
293 }
294
295 /** Overloaded from parent to die horribly
296  */
297 int
298 MidiDiskstream::set_destructive (bool yn)
299 {
300         assert( ! destructive());
301         assert( ! yn);
302         return -1;
303 }
304         
305 void
306 MidiDiskstream::set_note_mode (NoteMode m)
307 {
308         _note_mode = m;
309         midi_playlist()->set_note_mode(m);
310         if (_write_source && _write_source->model())
311                 _write_source->model()->set_note_mode(m);
312 }
313
314 void
315 MidiDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
316 {
317         // FIXME: waaay too much code to duplicate (AudioDiskstream)
318         
319         int possibly_recording;
320         int rolling;
321         int change;
322         const int transport_rolling = 0x4;
323         const int track_rec_enabled = 0x2;
324         const int global_rec_enabled = 0x1;
325
326         /* merge together the 3 factors that affect record status, and compute
327            what has changed.
328         */
329
330         rolling = _session.transport_speed() != 0.0f;
331         possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
332         change = possibly_recording ^ last_possibly_recording;
333
334         if (possibly_recording == last_possibly_recording) {
335                 return;
336         }
337
338         /* change state */
339
340         /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
341
342         if (((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record)) || 
343             ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
344                 
345                 /* starting to record: compute first+last frames */
346
347                 first_recordable_frame = transport_frame + _capture_offset;
348                 last_recordable_frame = max_frames;
349                 capture_start_frame = transport_frame;
350
351                 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
352
353                         /* was stopped, now rolling (and recording) */
354
355                         if (_alignment_style == ExistingMaterial) {
356                                 first_recordable_frame += _session.worst_output_latency();
357                         } else {
358                                 first_recordable_frame += _roll_delay;
359                         }
360                 
361                 } else {
362
363                         /* was rolling, but record state changed */
364
365                         if (_alignment_style == ExistingMaterial) {
366
367
368                                 if (!Config->get_punch_in()) {
369
370                                         /* manual punch in happens at the correct transport frame
371                                            because the user hit a button. but to get alignment correct 
372                                            we have to back up the position of the new region to the 
373                                            appropriate spot given the roll delay.
374                                         */
375
376                                         capture_start_frame -= _roll_delay;
377
378                                         /* XXX paul notes (august 2005): i don't know why
379                                            this is needed.
380                                         */
381
382                                         first_recordable_frame += _capture_offset;
383
384                                 } else {
385
386                                         /* autopunch toggles recording at the precise
387                                            transport frame, and then the DS waits
388                                            to start recording for a time that depends
389                                            on the output latency.
390                                         */
391
392                                         first_recordable_frame += _session.worst_output_latency();
393                                 }
394
395                         } else {
396
397                                 if (Config->get_punch_in()) {
398                                         first_recordable_frame += _roll_delay;
399                                 } else {
400                                         capture_start_frame -= _roll_delay;
401                                 }
402                         }
403                         
404                 }
405
406         } else if (!record_enabled() || !can_record) {
407                 
408                 /* stop recording */
409
410                 last_recordable_frame = transport_frame + _capture_offset;
411                 
412                 if (_alignment_style == ExistingMaterial) {
413                         last_recordable_frame += _session.worst_output_latency();
414                 } else {
415                         last_recordable_frame += _roll_delay;
416                 }
417         }
418
419         last_possibly_recording = possibly_recording;
420 }
421
422 int
423 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input)
424 {
425         // FIXME: waay too much code to duplicate (AudioDiskstream::process)
426         int       ret = -1;
427         nframes_t rec_offset = 0;
428         nframes_t rec_nframes = 0;
429         bool      nominally_recording;
430         bool      re = record_enabled ();
431         bool      collect_playback = true;
432
433         /* if we've already processed the frames corresponding to this call,
434            just return. this allows multiple routes that are taking input
435            from this diskstream to call our ::process() method, but have
436            this stuff only happen once. more commonly, it allows both
437            the AudioTrack that is using this AudioDiskstream *and* the Session
438            to call process() without problems.
439            */
440
441         if (_processed) {
442                 return 0;
443         }
444         
445         commit_should_unlock = false;
446
447         check_record_status (transport_frame, nframes, can_record);
448
449         nominally_recording = (can_record && re);
450
451         if (nframes == 0) {
452                 _processed = true;
453                 return 0;
454         }
455
456         /* This lock is held until the end of ::commit, so these two functions
457            must always be called as a pair. The only exception is if this function
458            returns a non-zero value, in which case, ::commit should not be called.
459            */
460
461         // If we can't take the state lock return.
462         if (!state_lock.trylock()) {
463                 return 1;
464         }
465         commit_should_unlock = true;
466         adjust_capture_position = 0;
467
468         if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
469                 OverlapType ot;
470
471                 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
472
473                 switch (ot) {
474                         case OverlapNone:
475                                 rec_nframes = 0;
476                                 break;
477
478                         case OverlapInternal:
479                                 /*     ----------    recrange
480                                            |---|       transrange
481                                            */
482                                 rec_nframes = nframes;
483                                 rec_offset = 0;
484                                 break;
485
486                         case OverlapStart:
487                                 /*    |--------|    recrange
488                                           -----|          transrange
489                                           */
490                                 rec_nframes = transport_frame + nframes - first_recordable_frame;
491                                 if (rec_nframes) {
492                                         rec_offset = first_recordable_frame - transport_frame;
493                                 }
494                                 break;
495
496                         case OverlapEnd:
497                                 /*    |--------|    recrange
498                                           |--------  transrange
499                                           */
500                                 rec_nframes = last_recordable_frame - transport_frame;
501                                 rec_offset = 0;
502                                 break;
503
504                         case OverlapExternal:
505                                 /*    |--------|    recrange
506                                           --------------  transrange
507                                           */
508                                 rec_nframes = last_recordable_frame - last_recordable_frame;
509                                 rec_offset = first_recordable_frame - transport_frame;
510                                 break;
511                 }
512
513                 if (rec_nframes && !was_recording) {
514                         capture_captured = 0;
515                         was_recording = true;
516                 }
517         }
518
519
520         if (can_record && !_last_capture_regions.empty()) {
521                 _last_capture_regions.clear ();
522         }
523
524         if (nominally_recording || rec_nframes) {
525
526                 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
527                 MidiBuffer& buf = _source_port->get_midi_buffer(nframes);
528                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
529                         const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
530                         assert(ev.buffer());
531                         _capture_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
532                 }
533         
534         } else {
535
536                 if (was_recording) {
537                         finish_capture (rec_monitors_input);
538                 }
539
540         }
541
542         if (rec_nframes) {
543
544                 /* data will be written to disk */
545
546                 if (rec_nframes == nframes && rec_offset == 0) {
547                         playback_distance = nframes;
548                 }
549
550                 adjust_capture_position = rec_nframes;
551
552         } else if (nominally_recording) {
553
554                 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
555
556                 playback_distance = nframes;
557                 collect_playback = false;
558
559         }
560
561         if (collect_playback) {
562
563                 /* we're doing playback */
564
565                 nframes_t necessary_samples;
566
567                 /* no varispeed playback if we're recording, because the output .... TBD */
568
569                 if (rec_nframes == 0 && _actual_speed != 1.0f) {
570                         necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
571                 } else {
572                         necessary_samples = nframes;
573                 }
574
575                 // Pump entire port buffer into playback buffer (FIXME: split cycles?)
576                 MidiBuffer& buf = _source_port->get_midi_buffer(nframes);
577                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
578                         const Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
579                         assert(ev.buffer());
580                         _playback_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
581                 }
582         }
583
584         ret = 0;
585
586         _processed = true;
587
588         if (ret) {
589
590                 /* we're exiting with failure, so ::commit will not
591                    be called. unlock the state lock.
592                    */
593
594                 commit_should_unlock = false;
595                 state_lock.unlock();
596         } 
597
598         return ret;
599 }
600
601 bool
602 MidiDiskstream::commit (nframes_t nframes)
603 {
604         bool need_butler = false;
605
606         if (_actual_speed < 0.0) {
607                 playback_sample -= playback_distance;
608         } else {
609                 playback_sample += playback_distance;
610         }
611
612         if (adjust_capture_position != 0) {
613                 capture_captured += adjust_capture_position;
614                 adjust_capture_position = 0;
615         }
616
617         uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
618         uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
619         if ((frames_written - frames_read) + nframes < midi_readahead) {
620                 need_butler = true;
621         }
622
623         /*cerr << "MDS written: " << frames_written << " - read: " << frames_read <<
624                 " = " << frames_written - frames_read
625                 << " + " << nframes << " < " << midi_readahead << " = " << need_butler << ")" << endl;*/
626         
627         if (commit_should_unlock) {
628                 state_lock.unlock();
629         }
630
631         _processed = false;
632
633         return need_butler;
634 }
635
636 void
637 MidiDiskstream::set_pending_overwrite (bool yn)
638 {
639         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
640         
641         pending_overwrite = yn;
642         
643         overwrite_frame = playback_sample;
644 }
645
646 int
647 MidiDiskstream::overwrite_existing_buffers ()
648 {
649         //read(overwrite_frame, disk_io_chunk_frames, false);
650         overwrite_queued = false;
651         pending_overwrite = false;
652
653         return 0;
654 }
655
656 int
657 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
658 {
659         Glib::Mutex::Lock lm (state_lock);
660         int ret = -1;
661         
662         _playback_buf->reset();
663         _capture_buf->reset();
664         g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
665         g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
666
667         playback_sample = frame;
668         file_frame = frame;
669
670         if (complete_refill) {
671                 while ((ret = do_refill_with_alloc ()) > 0) ;
672         } else {
673                 ret = do_refill_with_alloc ();
674         }
675
676         return ret;
677 }
678
679 int
680 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
681 {
682         uint32_t frames_read    = g_atomic_int_get(&_frames_read_from_ringbuffer);
683         uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
684         return ((frames_written - frames_read) < distance);
685 }
686
687 int
688 MidiDiskstream::internal_playback_seek (nframes_t distance)
689 {
690         cerr << "MDS: internal_playback_seek " << distance << endl;
691
692         first_recordable_frame += distance;
693         playback_sample += distance;
694
695         return 0;
696 }
697
698 /** @a start is set to the new frame position (TIME) read up to */
699 int
700 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
701 {       
702         nframes_t this_read = 0;
703         bool reloop = false;
704         nframes_t loop_end = 0;
705         nframes_t loop_start = 0;
706         nframes_t loop_length = 0;
707         Location *loc = 0;
708
709         if (!reversed) {
710                 /* Make the use of a Location atomic for this read operation.
711                    
712                    Note: Locations don't get deleted, so all we care about
713                    when I say "atomic" is that we are always pointing to
714                    the same one and using a start/length values obtained
715                    just once.
716                 */
717                 
718                 if ((loc = loop_location) != 0) {
719                         loop_start = loc->start();
720                         loop_end = loc->end();
721                         loop_length = loop_end - loop_start;
722                 }
723                 
724                 /* if we are looping, ensure that the first frame we read is at the correct
725                    position within the loop.
726                 */
727                 
728                 if (loc && (start >= loop_end)) {
729                         //cerr << "start adjusted from " << start;
730                         start = loop_start + ((start - loop_start) % loop_length);
731                         //cerr << "to " << start << endl;
732                 }
733                 //cerr << "start is " << start << "  loopstart: " << loop_start << "  loopend: " << loop_end << endl;
734         }
735
736         while (dur) {
737
738                 /* take any loop into account. we can't read past the end of the loop. */
739
740                 if (loc && (loop_end - start < dur)) {
741                         this_read = loop_end - start;
742                         //cerr << "reloop true: thisread: " << this_read << "  dur: " << dur << endl;
743                         reloop = true;
744                 } else {
745                         reloop = false;
746                         this_read = dur;
747                 }
748
749                 if (this_read == 0) {
750                         break;
751                 }
752
753                 this_read = min(dur,this_read);
754
755                 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
756                         error << string_compose(
757                                         _("MidiDiskstream %1: cannot read %2 from playlist at frame %3"),
758                                         _id, this_read, start) << endmsg;
759                         return -1;
760                 }
761                 
762                 //cout << "MDS this read " << this_read << " start = " << start << endl;
763                 g_atomic_int_add(&_frames_written_to_ringbuffer, this_read);
764
765                 _read_data_count = _playlist->read_data_count();
766                 
767                 if (reversed) {
768
769                         // Swap note ons with note offs here.  etc?
770                         // Fully reversing MIDI requires look-ahead (well, behind) to find previous
771                         // CC values etc.  hard.
772
773                 } else {
774                         
775                         /* if we read to the end of the loop, go back to the beginning */
776                         
777                         if (reloop) {
778                                 // Synthesize LoopEvent here, because the next events
779                                 // written will have non-monotonic timestamps.
780                                 _playback_buf->write(loop_end - 1, LoopEventType, 0, 0);
781                                 //cout << "Pushing LoopEvent ts=" << loop_end-1 
782                                 //     << " start+this_read " << start+this_read << endl;
783
784                                 start = loop_start;
785                         } else {
786                                 start += this_read;
787                         }
788                 } 
789
790                 dur -= this_read;
791                 //offset += this_read;
792         }
793
794         return 0;
795 }
796
797 int
798 MidiDiskstream::do_refill_with_alloc ()
799 {
800         return do_refill();
801 }
802
803 int
804 MidiDiskstream::do_refill ()
805 {
806         int     ret         = 0;
807         size_t  write_space = _playback_buf->write_space();
808         bool    reversed    = (_visible_speed * _session.transport_speed()) < 0.0f;
809
810         if (write_space == 0) {
811                 return 0;
812         }
813         
814         if (reversed) {
815                 return 0;
816         }
817
818         /* at end: nothing to do */
819         if (file_frame == max_frames) {
820                 return 0;
821         }
822
823         // At this point we...
824         assert(_playback_buf->write_space() > 0); // ... have something to write to, and
825         assert(file_frame <= max_frames); // ... something to write
826
827         // now calculate how much time is in the ringbuffer.
828         // and lets write as much as we need to get this to be midi_readahead;
829         uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
830         uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
831         if ((frames_written - frames_read) >= midi_readahead) {
832                 //cout << "MDS Nothing to do. all fine" << endl;
833                 return 0;
834         }
835
836         nframes_t to_read = midi_readahead - (frames_written - frames_read);
837
838         //cout << "MDS read for midi_readahead " << to_read << "  rb_contains: "
839         //      << frames_written - frames_read << endl;
840
841         to_read = min(to_read, (max_frames - file_frame));
842         
843         if (read (file_frame, to_read, reversed)) {
844                 ret = -1;
845         }
846                 
847         return ret;
848 }
849
850 /** Flush pending data to disk.
851  *
852  * Important note: this function will write *AT MOST* disk_io_chunk_frames
853  * of data to disk. it will never write more than that.  If it writes that
854  * much and there is more than that waiting to be written, it will return 1,
855  * otherwise 0 on success or -1 on failure.
856  * 
857  * If there is less than disk_io_chunk_frames to be written, no data will be
858  * written at all unless @a force_flush is true.
859  */
860 int
861 MidiDiskstream::do_flush (RunContext context, bool force_flush)
862 {
863         uint32_t to_write;
864         int32_t ret = 0;
865         nframes_t total;
866
867         _write_data_count = 0;
868
869         total = _session.transport_frame() - _last_flush_frame;
870         
871         if (_last_flush_frame > _session.transport_frame()
872                         || _last_flush_frame < capture_start_frame) {
873                 _last_flush_frame = _session.transport_frame();
874         }
875
876         if (total == 0 || _capture_buf->read_space() == 0
877                         || (!force_flush && (total < disk_io_chunk_frames && was_recording))) {
878                 goto out;
879         }
880
881         /* if there are 2+ chunks of disk i/o possible for
882            this track, let the caller know so that it can arrange
883            for us to be called again, ASAP.
884
885            if we are forcing a flush, then if there is* any* extra
886            work, let the caller know.
887
888            if we are no longer recording and there is any extra work,
889            let the caller know too.
890            */
891
892         if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
893                 ret = 1;
894         } 
895
896         to_write = disk_io_chunk_frames;
897
898         assert(!destructive());
899
900         if (record_enabled()
901                         && (   (_session.transport_frame() - _last_flush_frame > disk_io_chunk_frames)
902                                 || force_flush)) {
903                 if ((!_write_source) || _write_source->midi_write (*_capture_buf, capture_start_frame, to_write) != to_write) {
904                         error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
905                         return -1;
906                 } else {
907                         _last_flush_frame = _session.transport_frame();
908                 }
909         }
910
911 out:
912         return ret;
913 }
914
915 void
916 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
917 {
918         uint32_t buffer_position;
919         bool more_work = true;
920         int err = 0;
921         boost::shared_ptr<MidiRegion> region;
922         nframes_t total_capture;
923         MidiRegion::SourceList srcs;
924         MidiRegion::SourceList::iterator src;
925         vector<CaptureInfo*>::iterator ci;
926         bool mark_write_completed = false;
927
928         finish_capture (true);
929
930         /* butler is already stopped, but there may be work to do 
931            to flush remaining data to disk.
932            */
933
934         while (more_work && !err) {
935                 switch (do_flush (TransportContext, true)) {
936                         case 0:
937                                 more_work = false;
938                                 break;
939                         case 1:
940                                 break;
941                         case -1:
942                                 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
943                                 err++;
944                 }
945         }
946
947         /* XXX is there anything we can do if err != 0 ? */
948         Glib::Mutex::Lock lm (capture_info_lock);
949
950         if (capture_info.empty()) {
951                 return;
952         }
953
954         if (abort_capture) {
955
956                 if (_write_source) {
957
958                         _write_source->mark_for_remove ();
959                         _write_source->drop_references ();
960                         _write_source.reset();
961                 }
962
963                 /* new source set up in "out" below */
964
965         } else {
966
967                 assert(_write_source);
968
969                 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
970                         total_capture += (*ci)->frames;
971                 }
972
973                 /* figure out the name for this take */
974         
975                 srcs.push_back (_write_source);
976                 _write_source->set_timeline_position (capture_info.front()->start);
977                 _write_source->set_captured_for (_name);
978
979                 string whole_file_region_name;
980                 whole_file_region_name = region_name_from_path (_write_source->name(), true);
981
982                 /* Register a new region with the Session that
983                    describes the entire source. Do this first
984                    so that any sub-regions will obviously be
985                    children of this one (later!)
986                    */
987
988                 try {
989                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, 0,
990                                         total_capture, whole_file_region_name, 0,
991                                         Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile)));
992
993                         region = boost::dynamic_pointer_cast<MidiRegion> (rx);
994                         region->special_set_position (capture_info.front()->start);
995                 }
996
997
998                 catch (failed_constructor& err) {
999                         error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1000                         /* XXX what now? */
1001                 }
1002
1003                 _last_capture_regions.push_back (region);
1004
1005                 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1006
1007                 XMLNode &before = _playlist->get_state();
1008                 _playlist->freeze ();
1009
1010                 for (buffer_position = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1011
1012                         string region_name;
1013
1014                         _session.region_name (region_name, _write_source->name(), false);
1015
1016                         // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1017
1018                         try {
1019                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1020                                 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1021                         }
1022
1023                         catch (failed_constructor& err) {
1024                                 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1025                                 continue; /* XXX is this OK? */
1026                         }
1027                         
1028                         region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1029
1030                         _last_capture_regions.push_back (region);
1031
1032                         // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1033
1034                         i_am_the_modifier++;
1035                         _playlist->add_region (region, (*ci)->start);
1036                         i_am_the_modifier--;
1037
1038                         buffer_position += (*ci)->frames;
1039                 }
1040
1041                 _playlist->thaw ();
1042                 XMLNode &after = _playlist->get_state();
1043                 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1044
1045         }
1046
1047         mark_write_completed = true;
1048
1049         reset_write_sources (mark_write_completed);
1050
1051         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1052                 delete *ci;
1053         }
1054
1055         capture_info.clear ();
1056         capture_start_frame = 0;
1057 }
1058
1059 void
1060 MidiDiskstream::transport_looped (nframes_t transport_frame)
1061 {
1062         if (was_recording) {
1063
1064                 // adjust the capture length knowing that the data will be recorded to disk
1065                 // only necessary after the first loop where we're recording
1066                 if (capture_info.size() == 0) {
1067                         capture_captured += _capture_offset;
1068
1069                         if (_alignment_style == ExistingMaterial) {
1070                                 capture_captured += _session.worst_output_latency();
1071                         } else {
1072                                 capture_captured += _roll_delay;
1073                         }
1074                 }
1075
1076                 finish_capture (true);
1077
1078                 // the next region will start recording via the normal mechanism
1079                 // we'll set the start position to the current transport pos
1080                 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1081                 capture_start_frame = transport_frame;
1082                 first_recordable_frame = transport_frame; // mild lie
1083                 last_recordable_frame = max_frames;
1084                 was_recording = true;
1085         }
1086 }
1087
1088 void
1089 MidiDiskstream::finish_capture (bool rec_monitors_input)
1090 {
1091         was_recording = false;
1092         
1093         if (capture_captured == 0) {
1094                 return;
1095         }
1096
1097         // Why must we destroy?
1098         assert(!destructive());
1099
1100         CaptureInfo* ci = new CaptureInfo;
1101         
1102         ci->start  = capture_start_frame;
1103         ci->frames = capture_captured;
1104         
1105         /* XXX theoretical race condition here. Need atomic exchange ? 
1106            However, the circumstances when this is called right 
1107            now (either on record-disable or transport_stopped)
1108            mean that no actual race exists. I think ...
1109            We now have a capture_info_lock, but it is only to be used
1110            to synchronize in the transport_stop and the capture info
1111            accessors, so that invalidation will not occur (both non-realtime).
1112         */
1113
1114         // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1115
1116         capture_info.push_back (ci);
1117         capture_captured = 0;
1118 }
1119
1120 void
1121 MidiDiskstream::set_record_enabled (bool yn)
1122 {
1123         if (!recordable() || !_session.record_enabling_legal()) {
1124                 return;
1125         }
1126
1127         assert(!destructive());
1128         
1129         if (yn && _source_port == 0) {
1130
1131                 /* pick up connections not initiated *from* the IO object
1132                    we're associated with.
1133                 */
1134
1135                 get_input_sources ();
1136         }
1137
1138         /* yes, i know that this not proof against race conditions, but its
1139            good enough. i think.
1140         */
1141
1142         if (record_enabled() != yn) {
1143                 if (yn) {
1144                         engage_record_enable ();
1145                 } else {
1146                         disengage_record_enable ();
1147                 }
1148         }
1149 }
1150
1151 void
1152 MidiDiskstream::engage_record_enable ()
1153 {
1154     bool rolling = _session.transport_speed() != 0.0f;
1155
1156         g_atomic_int_set (&_record_enabled, 1);
1157         
1158         if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1159                 _source_port->request_monitor_input (!(Config->get_auto_input() && rolling));
1160         }
1161
1162         // FIXME: Why is this necessary?  Isn't needed for AudioDiskstream...
1163         if (!_write_source)
1164                 use_new_write_source();
1165
1166         _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1167
1168         RecordEnableChanged (); /* EMIT SIGNAL */
1169 }
1170
1171 void
1172 MidiDiskstream::disengage_record_enable ()
1173 {
1174         g_atomic_int_set (&_record_enabled, 0);
1175         if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1176                 if (_source_port) {
1177                         _source_port->request_monitor_input (false);
1178                 }
1179         }
1180
1181         RecordEnableChanged (); /* EMIT SIGNAL */
1182 }
1183
1184 XMLNode&
1185 MidiDiskstream::get_state ()
1186 {
1187         XMLNode* node = new XMLNode ("MidiDiskstream");
1188         char buf[64];
1189         LocaleGuard lg (X_("POSIX"));
1190
1191         snprintf (buf, sizeof(buf), "0x%x", _flags);
1192         node->add_property ("flags", buf);
1193
1194         node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1195         
1196         snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1197         node->add_property("channel-mask", buf);
1198         
1199         node->add_property ("playlist", _playlist->name());
1200         
1201         snprintf (buf, sizeof(buf), "%f", _visible_speed);
1202         node->add_property ("speed", buf);
1203
1204         node->add_property("name", _name);
1205         id().print(buf, sizeof(buf));
1206         node->add_property("id", buf);
1207
1208         if (_write_source && _session.get_record_enabled()) {
1209
1210                 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1211                 XMLNode* cs_grandchild;
1212
1213                 cs_grandchild = new XMLNode (X_("file"));
1214                 cs_grandchild->add_property (X_("path"), _write_source->path());
1215                 cs_child->add_child_nocopy (*cs_grandchild);
1216
1217                 /* store the location where capture will start */
1218
1219                 Location* pi;
1220
1221                 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1222                         snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1223                 } else {
1224                         snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1225                 }
1226
1227                 cs_child->add_property (X_("at"), buf);
1228                 node->add_child_nocopy (*cs_child);
1229         }
1230
1231         if (_extra_xml) {
1232                 node->add_child_copy (*_extra_xml);
1233         }
1234
1235         return* node;
1236 }
1237
1238 int
1239 MidiDiskstream::set_state (const XMLNode& node)
1240 {
1241         const XMLProperty* prop;
1242         XMLNodeList nlist = node.children();
1243         XMLNodeIterator niter;
1244         uint32_t nchans = 1;
1245         XMLNode* capture_pending_node = 0;
1246         LocaleGuard lg (X_("POSIX"));
1247
1248         in_set_state = true;
1249
1250         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1251                 /*if ((*niter)->name() == IO::state_node_name) {
1252                         deprecated_io_node = new XMLNode (**niter);
1253                 }*/
1254                 assert ((*niter)->name() != IO::state_node_name);
1255
1256                 if ((*niter)->name() == X_("CapturingSources")) {
1257                         capture_pending_node = *niter;
1258                 }
1259         }
1260
1261         /* prevent write sources from being created */
1262         
1263         in_set_state = true;
1264         
1265         if ((prop = node.property ("name")) != 0) {
1266                 _name = prop->value();
1267         } 
1268
1269         if ((prop = node.property ("id")) != 0) {
1270                 _id = prop->value ();
1271         }
1272
1273         if ((prop = node.property ("flags")) != 0) {
1274                 _flags = Flag (string_2_enum (prop->value(), _flags));
1275         }
1276
1277         ChannelMode channel_mode = AllChannels;
1278         if ((prop = node.property ("channel-mode")) != 0) {
1279                 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1280         }
1281         
1282         unsigned int channel_mask = 0xFFFF;
1283         if ((prop = node.property ("channel-mask")) != 0) {
1284                 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1285                 if (channel_mask & (~0xFFFF)) {
1286                         warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1287                 }
1288         }
1289
1290         set_channel_mode(channel_mode, channel_mask);
1291         
1292         if ((prop = node.property ("channels")) != 0) {
1293                 nchans = atoi (prop->value().c_str());
1294         }
1295         
1296         if ((prop = node.property ("playlist")) == 0) {
1297                 return -1;
1298         }
1299
1300         {
1301                 bool had_playlist = (_playlist != 0);
1302         
1303                 if (find_and_use_playlist (prop->value())) {
1304                         return -1;
1305                 }
1306
1307                 if (!had_playlist) {
1308                         _playlist->set_orig_diskstream_id (_id);
1309                 }
1310                 
1311                 if (capture_pending_node) {
1312                         use_pending_capture_data (*capture_pending_node);
1313                 }
1314
1315         }
1316
1317         if ((prop = node.property ("speed")) != 0) {
1318                 double sp = atof (prop->value().c_str());
1319
1320                 if (realtime_set_speed (sp, false)) {
1321                         non_realtime_set_speed ();
1322                 }
1323         }
1324
1325         in_set_state = false;
1326
1327         /* make sure this is clear before we do anything else */
1328
1329         // FIXME?
1330         //_capturing_source = 0;
1331
1332         /* write sources are handled when we handle the input set 
1333            up of the IO that owns this DS (::non_realtime_input_change())
1334         */
1335                 
1336         in_set_state = false;
1337
1338         return 0;
1339 }
1340
1341 int
1342 MidiDiskstream::use_new_write_source (uint32_t n)
1343 {
1344         if (!recordable()) {
1345                 return 1;
1346         }
1347
1348         assert(n == 0);
1349
1350         if (_write_source) {
1351
1352                 if (_write_source->is_empty ()) {
1353                         _write_source->mark_for_remove ();
1354                         _write_source.reset();
1355                 } else {
1356                         _write_source.reset();
1357                 }
1358         }
1359
1360         try {
1361                 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1362                 if (!_write_source) {
1363                         throw failed_constructor();
1364                 }
1365         } 
1366
1367         catch (failed_constructor &err) {
1368                 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1369                 _write_source.reset();
1370                 return -1;
1371         }
1372
1373         _write_source->set_allow_remove_if_empty (true);
1374
1375         return 0;
1376 }
1377
1378 void
1379 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1380 {
1381         if (!recordable()) {
1382                 return;
1383         }
1384
1385         if (_write_source && mark_write_complete) {
1386                 _write_source->mark_streaming_write_completed ();
1387         }
1388
1389         use_new_write_source (0);
1390                         
1391         if (record_enabled()) {
1392                 //_capturing_sources.push_back (_write_source);
1393         }
1394 }
1395
1396 int
1397 MidiDiskstream::rename_write_sources ()
1398 {
1399         if (_write_source != 0) {
1400                 _write_source->set_source_name (_name, destructive());
1401                 /* XXX what to do if this fails ? */
1402         }
1403         return 0;
1404 }
1405
1406 void
1407 MidiDiskstream::set_block_size (nframes_t nframes)
1408 {
1409 }
1410
1411 void
1412 MidiDiskstream::allocate_temporary_buffers ()
1413 {
1414 }
1415
1416 void
1417 MidiDiskstream::monitor_input (bool yn)
1418 {
1419         if (_source_port)
1420                 _source_port->ensure_monitor_input (yn);
1421 }
1422
1423 void
1424 MidiDiskstream::set_align_style_from_io ()
1425 {
1426         bool have_physical = false;
1427
1428         if (_io == 0) {
1429                 return;
1430         }
1431
1432         get_input_sources ();
1433         
1434         if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1435                 have_physical = true;
1436         }
1437
1438         if (have_physical) {
1439                 set_align_style (ExistingMaterial);
1440         } else {
1441                 set_align_style (CaptureTime);
1442         }
1443 }
1444
1445
1446 float
1447 MidiDiskstream::playback_buffer_load () const
1448 {
1449         return (float) ((double) _playback_buf->read_space()/
1450                         (double) _playback_buf->capacity());
1451 }
1452
1453 float
1454 MidiDiskstream::capture_buffer_load () const
1455 {
1456         return (float) ((double) _capture_buf->write_space()/
1457                         (double) _capture_buf->capacity());
1458 }
1459
1460 int
1461 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1462 {
1463         return 0;
1464 }
1465
1466 /** Writes playback events in the given range to \a dst, translating time stamps
1467  * so that an event at \a start has time = 0
1468  */
1469 void
1470 MidiDiskstream::get_playback (MidiBuffer& dst, nframes_t start, nframes_t end)
1471 {
1472         dst.clear();
1473         assert(dst.size() == 0);
1474         
1475         // Reverse.  ... We just don't do reverse, ok?  Back off.
1476         if (end <= start) {
1477                 return;
1478         }
1479
1480         // Check only events added this offset cycle
1481         MidiBuffer::iterator this_cycle_start = dst.end();
1482         
1483         // Translates stamps to be relative to start
1484         #if 1
1485                 _playback_buf->read(dst, start, end);
1486         #else   
1487                 const size_t events_read = _playback_buf->read(dst, start, end);
1488                 cout << "MDS events read = " << events_read
1489                 << " start = " << start << " end = " << end
1490                 << " readspace " << _playback_buf->read_space()
1491                 << " writespace " << _playback_buf->write_space() << endl;
1492         #endif
1493         
1494         gint32 frames_read = end - start;
1495         g_atomic_int_add(&_frames_read_from_ringbuffer, frames_read);
1496         
1497         // Feed the data through the MidiStateTracker
1498         // If it detects a LoopEvent it will add necessary note offs
1499         if (_midi_state_tracker.track(this_cycle_start, dst.end())) {
1500                 _midi_state_tracker.resolve_notes(dst, end-start - 1);
1501         }
1502 }
1503