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