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