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