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