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