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