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