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