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