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