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