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