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