Move EventRingBuffer to libardour.
[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
31 #include "pbd/error.h"
32 #include "pbd/ffs.h"
33 #include "pbd/basename.h"
34 #include <glibmm/threads.h>
35 #include "pbd/xml++.h"
36 #include "pbd/memento_command.h"
37 #include "pbd/enumwriter.h"
38 #include "pbd/stateful_diff_command.h"
39 #include "pbd/stacktrace.h"
40
41 #include "ardour/audioengine.h"
42 #include "ardour/butler.h"
43 #include "ardour/debug.h"
44 #include "ardour/io.h"
45 #include "ardour/midi_diskstream.h"
46 #include "ardour/midi_model.h"
47 #include "ardour/midi_playlist.h"
48 #include "ardour/midi_port.h"
49 #include "ardour/midi_region.h"
50 #include "ardour/midi_ring_buffer.h"
51 #include "ardour/playlist_factory.h"
52 #include "ardour/region_factory.h"
53 #include "ardour/session.h"
54 #include "ardour/session_playlists.h"
55 #include "ardour/smf_source.h"
56 #include "ardour/types.h"
57 #include "ardour/utils.h"
58
59 #include "midi++/types.h"
60
61 #include "i18n.h"
62 #include <locale.h>
63
64 using namespace std;
65 using namespace ARDOUR;
66 using namespace PBD;
67
68 framecnt_t MidiDiskstream::midi_readahead = 4096;
69
70 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
71         : Diskstream(sess, name, flag)
72         , _playback_buf(0)
73         , _capture_buf(0)
74         , _note_mode(Sustained)
75         , _frames_written_to_ringbuffer(0)
76         , _frames_read_from_ringbuffer(0)
77         , _frames_pending_write(0)
78         , _num_captured_loops(0)
79         , _accumulated_capture_offset(0)
80         , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
81 {
82         in_set_state = true;
83
84         init ();
85         use_new_playlist ();
86         use_new_write_source (0);
87
88         in_set_state = false;
89
90         if (destructive()) {
91                 throw failed_constructor();
92         }
93 }
94
95 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
96         : Diskstream(sess, node)
97         , _playback_buf(0)
98         , _capture_buf(0)
99         , _note_mode(Sustained)
100         , _frames_written_to_ringbuffer(0)
101         , _frames_read_from_ringbuffer(0)
102         , _frames_pending_write(0)
103         , _num_captured_loops(0)
104         , _accumulated_capture_offset(0)
105         , _gui_feed_buffer(AudioEngine::instance()->raw_buffer_size (DataType::MIDI))
106 {
107         in_set_state = true;
108
109         init ();
110
111         if (set_state (node, Stateful::loading_state_version)) {
112                 in_set_state = false;
113                 throw failed_constructor();
114         }
115
116         use_new_write_source (0);
117
118         in_set_state = false;
119 }
120
121 void
122 MidiDiskstream::init ()
123 {
124         /* there are no channels at this point, so these
125            two calls just get speed_buffer_size and wrap_buffer
126            size setup without duplicating their code.
127         */
128
129         set_block_size (_session.get_block_size());
130         allocate_temporary_buffers ();
131
132         const size_t size = _session.butler()->midi_diskstream_buffer_size();
133         _playback_buf = new MidiRingBuffer<framepos_t>(size);
134         _capture_buf = new MidiRingBuffer<framepos_t>(size);
135
136         _n_channels = ChanCount(DataType::MIDI, 1);
137 }
138
139 MidiDiskstream::~MidiDiskstream ()
140 {
141         Glib::Threads::Mutex::Lock lm (state_lock);
142         delete _playback_buf;
143         delete _capture_buf;
144 }
145
146
147 void
148 MidiDiskstream::non_realtime_locate (framepos_t position)
149 {
150         if (_write_source) {
151                 _write_source->set_timeline_position (position);
152         }
153         seek (position, false);
154 }
155
156
157 void
158 MidiDiskstream::non_realtime_input_change ()
159 {
160         {
161                 Glib::Threads::Mutex::Lock lm (state_lock);
162
163                 if (input_change_pending.type == IOChange::NoChange) {
164                         return;
165                 }
166
167                 if (input_change_pending.type & IOChange::ConfigurationChanged) {
168                         uint32_t ni = _io->n_ports().n_midi();
169
170                         if (ni != _n_channels.n_midi()) {
171                                 error << string_compose (_("%1: I/O configuration change %4 requested to use %2, but channel setup is %3"),
172                                                          name(),
173                                                          _io->n_ports(),
174                                                          _n_channels, input_change_pending.type)
175                                       << endmsg;
176                         }
177
178                         if (ni == 0) {
179                                 _source_port.reset ();
180                         } else {
181                                 _source_port = _io->midi(0);
182                         }
183                 }
184
185                 if (input_change_pending.type & IOChange::ConnectionsChanged) {
186                         set_capture_offset ();
187                         set_align_style_from_io ();
188                 }
189
190                 input_change_pending.type = IOChange::NoChange;
191
192                 /* implicit unlock */
193         }
194
195         /* unlike with audio, there is never any need to reset write sources
196            based on input configuration changes because ... a MIDI track
197            has just 1 MIDI port as input, always.
198         */
199
200         /* now refill channel buffers */
201
202         if (speed() != 1.0f || speed() != -1.0f) {
203                 seek ((framepos_t) (_session.transport_frame() * (double) speed()));
204         }
205         else {
206                 seek (_session.transport_frame());
207         }
208
209         g_atomic_int_set(const_cast<gint*> (&_frames_pending_write), 0);
210         g_atomic_int_set(const_cast<gint*> (&_num_captured_loops), 0);
211 }
212
213 int
214 MidiDiskstream::find_and_use_playlist (const string& name)
215 {
216         boost::shared_ptr<MidiPlaylist> playlist;
217
218         if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlists->by_name (name))) == 0) {
219                 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
220         }
221
222         if (!playlist) {
223                 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't a midi playlist"), name) << endmsg;
224                 return -1;
225         }
226
227         return use_playlist (playlist);
228 }
229
230 int
231 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
232 {
233         if (boost::dynamic_pointer_cast<MidiPlaylist>(playlist)) {
234                 Diskstream::use_playlist(playlist);
235         }
236
237         return 0;
238 }
239
240 int
241 MidiDiskstream::use_new_playlist ()
242 {
243         string newname;
244         boost::shared_ptr<MidiPlaylist> playlist;
245
246         if (!in_set_state && destructive()) {
247                 return 0;
248         }
249
250         if (_playlist) {
251                 newname = Playlist::bump_name (_playlist->name(), _session);
252         } else {
253                 newname = Playlist::bump_name (_name, _session);
254         }
255
256         if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
257                         DataType::MIDI, _session, newname, hidden()))) != 0) {
258
259                 return use_playlist (playlist);
260
261         } else {
262                 return -1;
263         }
264 }
265
266 int
267 MidiDiskstream::use_copy_playlist ()
268 {
269         if (destructive()) {
270                 return 0;
271         }
272
273         if (_playlist == 0) {
274                 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
275                 return -1;
276         }
277
278         string newname;
279         boost::shared_ptr<MidiPlaylist> playlist;
280
281         newname = Playlist::bump_name (_playlist->name(), _session);
282
283         if ((playlist  = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
284                 return use_playlist (playlist);
285         } else {
286                 return -1;
287         }
288 }
289
290 /** Overloaded from parent to die horribly
291  */
292 int
293 MidiDiskstream::set_destructive (bool yn)
294 {
295         return yn ? -1 : 0;
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 /** Get the start, end, and length of a location "atomically".
308  *
309  * Note: Locations don't get deleted, so all we care about when I say "atomic"
310  * is that we are always pointing to the same one and using start/length values
311  * obtained just once.  Use this function to achieve this since location being
312  * a parameter achieves this.
313  */
314 static void
315 get_location_times(const Location* location,
316                    framepos_t*     start,
317                    framepos_t*     end,
318                    framepos_t*     length)
319 {
320         if (location) {
321                 *start  = location->start();
322                 *end    = location->end();
323                 *length = *end - *start;
324         }
325 }
326
327 int
328 MidiDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance, bool need_disk_signal)
329 {
330         framecnt_t rec_offset = 0;
331         framecnt_t rec_nframes = 0;
332         bool      nominally_recording;
333         bool      re = record_enabled ();
334         bool      can_record = _session.actively_recording ();
335
336         playback_distance = 0;
337
338         check_record_status (transport_frame, can_record);
339
340         nominally_recording = (can_record && re);
341
342         if (nframes == 0) {
343                 return 0;
344         }
345
346         boost::shared_ptr<MidiPort> sp = _source_port.lock ();
347
348         if (sp == 0) {
349                 return 1;
350         }
351
352         Glib::Threads::Mutex::Lock sm (state_lock, Glib::Threads::TRY_LOCK);
353
354         if (!sm.locked()) {
355                 return 1;
356         }
357
358         const Location* const loop_loc    = loop_location;
359         framepos_t            loop_start  = 0;
360         framepos_t            loop_end    = 0;
361         framepos_t            loop_length = 0;
362         get_location_times(loop_loc, &loop_start, &loop_end, &loop_length);
363
364         adjust_capture_position = 0;
365
366         if (nominally_recording || (re && was_recording && _session.get_record_enabled() && _session.config.get_punch_in())) {
367                 Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
368
369                 calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset);
370                 /* For audio: not writing frames to the capture ringbuffer offsets
371                  * the recording. For midi: we need to keep track of the record range
372                  * and subtract the accumulated difference from the event time.
373                  */
374                 if (rec_nframes) {
375                         _accumulated_capture_offset += rec_offset;
376                 } else {
377                         _accumulated_capture_offset += nframes;
378                 }
379
380                 if (rec_nframes && !was_recording) {
381                         if (loop_loc) {
382                                 /* Loop recording, so pretend the capture started at the loop
383                                    start rgardless of what time it is now, so the source starts
384                                    at the loop start and can handle time wrapping around.
385                                    Otherwise, start the source right now as usual.
386                                 */
387                                 capture_captured    = transport_frame - loop_start;
388                                 capture_start_frame = loop_start;
389                         }
390                         _write_source->mark_write_starting_now(
391                                 capture_start_frame, capture_captured, loop_length);
392                         g_atomic_int_set(const_cast<gint*> (&_frames_pending_write), 0);
393                         g_atomic_int_set(const_cast<gint*> (&_num_captured_loops), 0);
394                         was_recording = true;
395                 }
396         }
397
398         if (can_record && !_last_capture_sources.empty()) {
399                 _last_capture_sources.clear ();
400         }
401
402         if (nominally_recording || rec_nframes) {
403
404                 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
405                 MidiBuffer& buf = sp->get_midi_buffer(nframes);
406                 ChannelMode mode = AllChannels; // _track->get_capture_channel_mode ();
407                 uint32_t mask = 0xffff; // _track->get_capture_channel_mask ();
408
409                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
410                         Evoral::MIDIEvent<MidiBuffer::TimeType> ev(*i, false);
411                         if (ev.time() + rec_offset > rec_nframes) {
412                                 break;
413                         }
414 #ifndef NDEBUG
415                         if (DEBUG::MidiIO & PBD::debug_bits) {
416                                 const uint8_t* __data = ev.buffer();
417                                 DEBUG_STR_DECL(a);
418                                 DEBUG_STR_APPEND(a, string_compose ("mididiskstream %1 capture event @ %2 + %3 sz %4 ", this, ev.time(), transport_frame, ev.size()));
419                                 for (size_t i=0; i < ev.size(); ++i) {
420                                         DEBUG_STR_APPEND(a,hex);
421                                         DEBUG_STR_APPEND(a,"0x");
422                                         DEBUG_STR_APPEND(a,(int)__data[i]);
423                                         DEBUG_STR_APPEND(a,' ');
424                                 }
425                                 DEBUG_STR_APPEND(a,'\n');
426                                 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
427                         }
428 #endif
429                         /* Write events to the capture buffer in frames from session start,
430                            but ignoring looping so event time progresses monotonically.
431                            The source knows the loop length so it knows exactly where the
432                            event occurs in the series of recorded loops and can implement
433                            any desirable behaviour.  We don't want to send event with
434                            transport time here since that way the source can not
435                            reconstruct their actual time; future clever MIDI looping should
436                            probably be implemented in the source instead of here.
437                         */
438                         const framecnt_t loop_offset = _num_captured_loops * loop_length;
439                         const framepos_t event_time = transport_frame + loop_offset - _accumulated_capture_offset + ev.time();
440                         if (event_time < 0 || event_time < first_recordable_frame) {
441                                 continue;
442                         }
443                         switch (mode) {
444                         case AllChannels:
445                                 _capture_buf->write(event_time,
446                                                     ev.type(), ev.size(), ev.buffer());
447                                 break;
448                         case FilterChannels:
449                                 if (ev.is_channel_event()) {
450                                         if ((1<<ev.channel()) & mask) {
451                                                 _capture_buf->write(event_time,
452                                                                     ev.type(), ev.size(), ev.buffer());
453                                         }
454                                 } else {
455                                         _capture_buf->write(event_time,
456                                                             ev.type(), ev.size(), ev.buffer());
457                                 }
458                                 break;
459                         case ForceChannel:
460                                 if (ev.is_channel_event()) {
461                                         ev.set_channel (PBD::ffs(mask) - 1);
462                                 }
463                                 _capture_buf->write(event_time,
464                                                     ev.type(), ev.size(), ev.buffer());
465                                 break;
466                         }
467                 }
468                 g_atomic_int_add(const_cast<gint*>(&_frames_pending_write), nframes);
469
470                 if (buf.size() != 0) {
471                         Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex, Glib::Threads::TRY_LOCK);
472
473                         if (lm.locked ()) {
474                                 /* Copy this data into our GUI feed buffer and tell the GUI
475                                    that it can read it if it likes.
476                                 */
477                                 _gui_feed_buffer.clear ();
478                                 
479                                 for (MidiBuffer::iterator i = buf.begin(); i != buf.end(); ++i) {
480                                         /* This may fail if buf is larger than _gui_feed_buffer, but it's not really
481                                            the end of the world if it does.
482                                         */
483                                         _gui_feed_buffer.push_back ((*i).time() + transport_frame, (*i).size(), (*i).buffer());
484                                 }
485                         }
486
487                         DataRecorded (_write_source); /* EMIT SIGNAL */
488                 }
489
490         } else {
491
492                 if (was_recording) {
493                         finish_capture ();
494                 }
495                 _accumulated_capture_offset = 0;
496
497         }
498
499         if (rec_nframes) {
500
501                 /* data will be written to disk */
502
503                 if (rec_nframes == nframes && rec_offset == 0) {
504                         playback_distance = nframes;
505                 }
506
507                 adjust_capture_position = rec_nframes;
508
509         } else if (nominally_recording) {
510
511                 /* XXXX do this for MIDI !!!
512                    can't do actual capture yet - waiting for latency effects to finish before we start
513                    */
514
515                 playback_distance = nframes;
516
517         } else {
518
519                 /* XXX: should be doing varispeed stuff here, similar to the code in AudioDiskstream::process */
520
521                 playback_distance = nframes;
522
523         }
524
525         if (need_disk_signal) {
526                 /* copy the diskstream data to all output buffers */
527                 
528                 MidiBuffer& mbuf (bufs.get_midi (0));
529                 get_playback (mbuf, nframes);
530                 
531                 /* leave the audio count alone */
532                 ChanCount cnt (DataType::MIDI, 1);
533                 cnt.set (DataType::AUDIO, bufs.count().n_audio());
534                 bufs.set_count (cnt);
535         }
536
537         return 0;
538 }
539
540 frameoffset_t
541 MidiDiskstream::calculate_playback_distance (pframes_t nframes)
542 {
543         frameoffset_t playback_distance = nframes;
544
545         /* XXX: should be doing varispeed stuff once it's implemented in ::process() above */
546
547         if (_actual_speed < 0.0) {
548                 return -playback_distance;
549         } else {
550                 return playback_distance;
551         }
552 }
553
554 bool
555 MidiDiskstream::commit (framecnt_t playback_distance)
556 {
557         bool need_butler = false;
558
559         if (_actual_speed < 0.0) {
560                 playback_sample -= playback_distance;
561         } else {
562                 playback_sample += playback_distance;
563         }
564
565         if (adjust_capture_position != 0) {
566                 capture_captured += adjust_capture_position;
567                 adjust_capture_position = 0;
568         }
569
570         uint32_t frames_read = g_atomic_int_get(const_cast<gint*>(&_frames_read_from_ringbuffer));
571         uint32_t frames_written = g_atomic_int_get(const_cast<gint*>(&_frames_written_to_ringbuffer));
572
573         /*
574           cerr << name() << " MDS written: " << frames_written << " - read: " << frames_read <<
575           " = " << frames_written - frames_read
576           << " + " << playback_distance << " < " << midi_readahead << " = " << need_butler << ")" << endl;
577         */
578
579         /* frames_read will generally be less than frames_written, but
580          * immediately after an overwrite, we can end up having read some data
581          * before we've written any. we don't need to trip an assert() on this,
582          * but we do need to check so that the decision on whether or not we
583          * need the butler is done correctly.
584          */
585         
586         if (frames_read <= frames_written) {
587                 if ((frames_written - frames_read) + playback_distance < midi_readahead) {
588                         need_butler = true;
589                 }
590         }
591
592
593         return need_butler;
594 }
595
596 void
597 MidiDiskstream::set_pending_overwrite (bool yn)
598 {
599         /* called from audio thread, so we can use the read ptr and playback sample as we wish */
600
601         _pending_overwrite = yn;
602         overwrite_frame = playback_sample;
603 }
604
605 int
606 MidiDiskstream::overwrite_existing_buffers ()
607 {
608         /* This is safe as long as the butler thread is suspended, which it should be */
609         _playback_buf->reset ();
610
611         g_atomic_int_set (&_frames_read_from_ringbuffer, 0);
612         g_atomic_int_set (&_frames_written_to_ringbuffer, 0);
613
614         read (overwrite_frame, disk_io_chunk_frames, false);
615         file_frame = overwrite_frame; // it was adjusted by ::read()
616         overwrite_queued = false;
617         _pending_overwrite = false;
618
619         return 0;
620 }
621
622 int
623 MidiDiskstream::seek (framepos_t frame, bool complete_refill)
624 {
625         Glib::Threads::Mutex::Lock lm (state_lock);
626         int ret = -1;
627
628         if (g_atomic_int_get (&_frames_read_from_ringbuffer) == 0) {
629                 /* we haven't read anything since the last seek,
630                    so flush all note trackers to prevent
631                    wierdness
632                 */
633                 reset_tracker ();
634         }
635
636         _playback_buf->reset();
637         _capture_buf->reset();
638         g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
639         g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
640
641         playback_sample = frame;
642         file_frame = frame;
643
644         if (complete_refill) {
645                 while ((ret = do_refill_with_alloc ()) > 0) ;
646         } else {
647                 ret = do_refill_with_alloc ();
648         }
649
650         return ret;
651 }
652
653 int
654 MidiDiskstream::can_internal_playback_seek (framecnt_t distance)
655 {
656         uint32_t frames_read    = g_atomic_int_get(&_frames_read_from_ringbuffer);
657         uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
658         return ((frames_written - frames_read) < distance);
659 }
660
661 int
662 MidiDiskstream::internal_playback_seek (framecnt_t distance)
663 {
664         first_recordable_frame += distance;
665         playback_sample += distance;
666
667         return 0;
668 }
669
670 /** @a start is set to the new frame position (TIME) read up to */
671 int
672 MidiDiskstream::read (framepos_t& start, framecnt_t dur, bool reversed)
673 {
674         framecnt_t this_read   = 0;
675         bool       reloop      = false;
676         framepos_t loop_end    = 0;
677         framepos_t loop_start  = 0;
678         framecnt_t loop_length = 0;
679         Location*  loc         = 0;
680
681         if (!reversed) {
682
683                 loc = loop_location;
684                 get_location_times(loc, &loop_start, &loop_end, &loop_length);
685
686                 /* if we are looping, ensure that the first frame we read is at the correct
687                    position within the loop.
688                 */
689
690                 if (loc && (start >= loop_end)) {
691                         //cerr << "start adjusted from " << start;
692                         start = loop_start + ((start - loop_start) % loop_length);
693                         //cerr << "to " << start << endl;
694                 }
695                 // cerr << "start is " << start << " end " << start+dur << "  loopstart: " << loop_start << "  loopend: " << loop_end << endl;
696         }
697
698         while (dur) {
699
700                 /* take any loop into account. we can't read past the end of the loop. */
701
702                 if (loc && (loop_end - start <= dur)) {
703                         this_read = loop_end - start;
704                         // cerr << "reloop true: thisread: " << this_read << "  dur: " << dur << endl;
705                         reloop = true;
706                 } else {
707                         reloop = false;
708                         this_read = dur;
709                 }
710
711                 if (this_read == 0) {
712                         break;
713                 }
714
715                 this_read = min(dur,this_read);
716
717                 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
718                         error << string_compose(
719                                         _("MidiDiskstream %1: cannot read %2 from playlist at frame %3"),
720                                         id(), this_read, start) << endmsg;
721                         return -1;
722                 }
723                 
724                 g_atomic_int_add (&_frames_written_to_ringbuffer, this_read);
725
726                 if (reversed) {
727
728                         // Swap note ons with note offs here.  etc?
729                         // Fully reversing MIDI requires look-ahead (well, behind) to find previous
730                         // CC values etc.  hard.
731
732                 } else {
733
734                         /* if we read to the end of the loop, go back to the beginning */
735                         if (reloop) {
736                                 // Synthesize LoopEvent here, because the next events
737                                 // written will have non-monotonic timestamps.
738                                 start = loop_start;
739                         } else {
740                                 start += this_read;
741                         }
742                 }
743
744                 dur -= this_read;
745                 //offset += this_read;
746         }
747
748         return 0;
749 }
750
751 int
752 MidiDiskstream::do_refill_with_alloc ()
753 {
754         return do_refill();
755 }
756
757 int
758 MidiDiskstream::do_refill ()
759 {
760         int     ret         = 0;
761         size_t  write_space = _playback_buf->write_space();
762         bool    reversed    = (_visible_speed * _session.transport_speed()) < 0.0f;
763
764         if (write_space == 0) {
765                 return 0;
766         }
767
768         if (reversed) {
769                 return 0;
770         }
771
772         /* at end: nothing to do */
773         if (file_frame == max_framepos) {
774                 return 0;
775         }
776
777         /* no space to write */
778         if (_playback_buf->write_space() == 0) {
779                 return 0;
780         }
781
782         uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
783         uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
784         if ((frames_written - frames_read) >= midi_readahead) {
785                 return 0;
786         }
787
788         framecnt_t to_read = midi_readahead - (frames_written - frames_read);
789
790         //cout << "MDS read for midi_readahead " << to_read << "  rb_contains: "
791         //      << frames_written - frames_read << endl;
792
793         to_read = (framecnt_t) min ((framecnt_t) to_read, (framecnt_t) (max_framepos - file_frame));
794
795         if (read (file_frame, to_read, reversed)) {
796                 ret = -1;
797         }
798
799         return ret;
800 }
801
802 /** Flush pending data to disk.
803  *
804  * Important note: this function will write *AT MOST* disk_io_chunk_frames
805  * of data to disk. it will never write more than that.  If it writes that
806  * much and there is more than that waiting to be written, it will return 1,
807  * otherwise 0 on success or -1 on failure.
808  *
809  * If there is less than disk_io_chunk_frames to be written, no data will be
810  * written at all unless @a force_flush is true.
811  */
812 int
813 MidiDiskstream::do_flush (RunContext /*context*/, bool force_flush)
814 {
815         framecnt_t to_write;
816         int32_t ret = 0;
817
818         if (!_write_source) {
819                 return 0;
820         }
821
822         const framecnt_t total = g_atomic_int_get(const_cast<gint*> (&_frames_pending_write));
823
824         if (total == 0 || 
825             _capture_buf->read_space() == 0 || 
826             (!force_flush && (total < disk_io_chunk_frames) && was_recording)) {
827                 goto out;
828         }
829
830         /* if there are 2+ chunks of disk i/o possible for
831            this track), let the caller know so that it can arrange
832            for us to be called again, ASAP.
833
834            if we are forcing a flush, then if there is* any* extra
835            work, let the caller know.
836
837            if we are no longer recording and there is any extra work,
838            let the caller know too.
839            */
840
841         if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
842                 ret = 1;
843         }
844
845         if (force_flush) {
846                 /* push out everything we have, right now */
847                 to_write = max_framecnt;
848         } else {
849                 to_write = disk_io_chunk_frames;
850         }
851
852         if (record_enabled() && ((total > disk_io_chunk_frames) || force_flush)) {
853                 if (_write_source->midi_write (*_capture_buf, get_capture_start_frame (0), to_write) != to_write) {
854                         error << string_compose(_("MidiDiskstream %1: cannot write to disk"), id()) << endmsg;
855                         return -1;
856                 } 
857                 g_atomic_int_add(const_cast<gint*> (&_frames_pending_write), -to_write);
858         }
859
860 out:
861         return ret;
862 }
863
864 void
865 MidiDiskstream::transport_stopped_wallclock (struct tm& /*when*/, time_t /*twhen*/, bool abort_capture)
866 {
867         bool more_work = true;
868         int err = 0;
869         boost::shared_ptr<MidiRegion> region;
870         MidiRegion::SourceList srcs;
871         MidiRegion::SourceList::iterator src;
872         vector<CaptureInfo*>::iterator ci;
873
874         finish_capture ();
875
876         /* butler is already stopped, but there may be work to do
877            to flush remaining data to disk.
878            */
879
880         while (more_work && !err) {
881                 switch (do_flush (TransportContext, true)) {
882                 case 0:
883                         more_work = false;
884                         break;
885                 case 1:
886                         break;
887                 case -1:
888                         error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
889                         err++;
890                 }
891         }
892
893         /* XXX is there anything we can do if err != 0 ? */
894         Glib::Threads::Mutex::Lock lm (capture_info_lock);
895
896         if (capture_info.empty()) {
897                 goto no_capture_stuff_to_do;
898         }
899
900         if (abort_capture) {
901
902                 if (_write_source) {
903                         _write_source->mark_for_remove ();
904                         _write_source->drop_references ();
905                         _write_source.reset();
906                 }
907
908                 /* new source set up in "out" below */
909
910         } else {
911
912                 framecnt_t total_capture = 0;
913                 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
914                         total_capture += (*ci)->frames;
915                 }
916
917                 if (_write_source->length (capture_info.front()->start) != 0) {
918
919                         /* phew, we have data */
920
921                         /* figure out the name for this take */
922
923                         srcs.push_back (_write_source);
924
925                         _write_source->set_timeline_position (capture_info.front()->start);
926                         _write_source->set_captured_for (_name);
927
928                         /* set length in beats to entire capture length */
929
930                         BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start);
931                         const Evoral::MusicalTime total_capture_beats = converter.from (total_capture);
932                         _write_source->set_length_beats (total_capture_beats);
933
934                         /* flush to disk: this step differs from the audio path,
935                            where all the data is already on disk.
936                         */
937
938                         _write_source->mark_midi_streaming_write_completed (Evoral::Sequence<Evoral::MusicalTime>::ResolveStuckNotes, total_capture_beats);
939
940                         /* we will want to be able to keep (over)writing the source
941                            but we don't want it to be removable. this also differs
942                            from the audio situation, where the source at this point
943                            must be considered immutable. luckily, we can rely on
944                            MidiSource::mark_streaming_write_completed() to have
945                            already done the necessary work for that.
946                         */
947
948                         string whole_file_region_name;
949                         whole_file_region_name = region_name_from_path (_write_source->name(), true);
950
951                         /* Register a new region with the Session that
952                            describes the entire source. Do this first
953                            so that any sub-regions will obviously be
954                            children of this one (later!)
955                         */
956
957                         try {
958                                 PropertyList plist;
959
960                                 plist.add (Properties::name, whole_file_region_name);
961                                 plist.add (Properties::whole_file, true);
962                                 plist.add (Properties::automatic, true);
963                                 plist.add (Properties::start, 0);
964                                 plist.add (Properties::length, total_capture);
965                                 plist.add (Properties::layer, 0);
966
967                                 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
968
969                                 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
970                                 region->special_set_position (capture_info.front()->start);
971                         }
972
973
974                         catch (failed_constructor& err) {
975                                 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
976                                 /* XXX what now? */
977                         }
978
979                         _last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
980
981                         _playlist->clear_changes ();
982                         _playlist->freeze ();
983
984                         /* Session frame time of the initial capture in this pass, which is where the source starts */
985                         framepos_t initial_capture = 0;
986                         if (!capture_info.empty()) {
987                                 initial_capture = capture_info.front()->start;
988                         }
989
990                         for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
991
992                                 string region_name;
993
994                                 RegionFactory::region_name (region_name, _write_source->name(), false);
995
996                                 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
997                                                         _name, (*ci)->start, (*ci)->frames, region_name));
998
999
1000                                 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1001
1002                                 try {
1003                                         PropertyList plist;
1004
1005                                         /* start of this region is the offset between the start of its capture and the start of the whole pass */
1006                                         plist.add (Properties::start, (*ci)->start - initial_capture);
1007                                         plist.add (Properties::length, (*ci)->frames);
1008                                         plist.add (Properties::length_beats, converter.from((*ci)->frames));
1009                                         plist.add (Properties::name, region_name);
1010
1011                                         boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1012                                         region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1013                                 }
1014
1015                                 catch (failed_constructor& err) {
1016                                         error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1017                                         continue; /* XXX is this OK? */
1018                                 }
1019
1020                                 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1021
1022                                 i_am_the_modifier++;
1023                                 _playlist->add_region (region, (*ci)->start);
1024                                 i_am_the_modifier--;
1025                         }
1026
1027                         _playlist->thaw ();
1028                         _session.add_command (new StatefulDiffCommand(_playlist));
1029
1030                 } else {
1031
1032                         /* No data was recorded, so this capture will
1033                            effectively be aborted; do the same as we
1034                            do for an explicit abort.
1035                         */
1036
1037                         if (_write_source) {
1038                                 _write_source->mark_for_remove ();
1039                                 _write_source->drop_references ();
1040                                 _write_source.reset();
1041                         }
1042                 }
1043
1044         }
1045
1046         use_new_write_source (0);
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   no_capture_stuff_to_do:
1056
1057         reset_tracker ();
1058 }
1059
1060 void
1061 MidiDiskstream::transport_looped (framepos_t)
1062 {
1063         /* Here we only keep track of the number of captured loops so monotonic
1064            event times can be delivered to the write source in process().  Trying
1065            to be clever here is a world of trouble, it is better to simply record
1066            the input in a straightforward non-destructive way.  In the future when
1067            we want to implement more clever MIDI looping modes it should be done in
1068            the Source and/or entirely after the capture is finished.
1069         */
1070         if (was_recording) {
1071                 g_atomic_int_add(const_cast<gint*> (&_num_captured_loops), 1);
1072         }
1073 }
1074
1075 void
1076 MidiDiskstream::finish_capture ()
1077 {
1078         was_recording = false;
1079
1080         if (capture_captured == 0) {
1081                 return;
1082         }
1083
1084         CaptureInfo* ci = new CaptureInfo;
1085
1086         ci->start  = capture_start_frame;
1087         ci->frames = capture_captured;
1088
1089         /* XXX theoretical race condition here. Need atomic exchange ?
1090            However, the circumstances when this is called right
1091            now (either on record-disable or transport_stopped)
1092            mean that no actual race exists. I think ...
1093            We now have a capture_info_lock, but it is only to be used
1094            to synchronize in the transport_stop and the capture info
1095            accessors, so that invalidation will not occur (both non-realtime).
1096         */
1097
1098         // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1099
1100         capture_info.push_back (ci);
1101         capture_captured = 0;
1102 }
1103
1104 void
1105 MidiDiskstream::set_record_enabled (bool yn)
1106 {
1107         if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_midi() == 0) {
1108                 return;
1109         }
1110
1111         /* yes, i know that this not proof against race conditions, but its
1112            good enough. i think.
1113         */
1114
1115         if (record_enabled() != yn) {
1116                 if (yn) {
1117                         engage_record_enable ();
1118                 } else {
1119                         disengage_record_enable ();
1120                 }
1121                 
1122                 RecordEnableChanged (); /* EMIT SIGNAL */
1123         }
1124 }
1125
1126 bool
1127 MidiDiskstream::prep_record_enable ()
1128 {
1129         if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_midi() == 0) {
1130                 return false;
1131         }
1132
1133         bool const rolling = _session.transport_speed() != 0.0f;
1134
1135         boost::shared_ptr<MidiPort> sp = _source_port.lock ();
1136         
1137         if (sp && Config->get_monitoring_model() == HardwareMonitoring) {
1138                 sp->request_input_monitoring (!(_session.config.get_auto_input() && rolling));
1139         }
1140
1141         return true;
1142 }
1143
1144 bool
1145 MidiDiskstream::prep_record_disable ()
1146 {
1147
1148         return true;
1149 }
1150
1151 XMLNode&
1152 MidiDiskstream::get_state ()
1153 {
1154         XMLNode& node (Diskstream::get_state());
1155         char buf[64];
1156         LocaleGuard lg (X_("POSIX"));
1157
1158         if (_write_source && _session.get_record_enabled()) {
1159
1160                 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1161                 XMLNode* cs_grandchild;
1162
1163                 cs_grandchild = new XMLNode (X_("file"));
1164                 cs_grandchild->add_property (X_("path"), _write_source->path());
1165                 cs_child->add_child_nocopy (*cs_grandchild);
1166
1167                 /* store the location where capture will start */
1168
1169                 Location* pi;
1170
1171                 if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1172                         snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
1173                 } else {
1174                         snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
1175                 }
1176
1177                 cs_child->add_property (X_("at"), buf);
1178                 node.add_child_nocopy (*cs_child);
1179         }
1180
1181         return node;
1182 }
1183
1184 int
1185 MidiDiskstream::set_state (const XMLNode& node, int version)
1186 {
1187         XMLNodeList nlist = node.children();
1188         XMLNodeIterator niter;
1189         XMLNode* capture_pending_node = 0;
1190         LocaleGuard lg (X_("POSIX"));
1191
1192         /* prevent write sources from being created */
1193
1194         in_set_state = true;
1195
1196         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1197                 if ((*niter)->name() == X_("CapturingSources")) {
1198                         capture_pending_node = *niter;
1199                 }
1200         }
1201
1202         if (Diskstream::set_state (node, version)) {
1203                 return -1;
1204         }
1205
1206         if (capture_pending_node) {
1207                 use_pending_capture_data (*capture_pending_node);
1208         }
1209
1210         in_set_state = false;
1211
1212         return 0;
1213 }
1214
1215 int
1216 MidiDiskstream::use_new_write_source (uint32_t n)
1217 {
1218         if (!_session.writable() || !recordable()) {
1219                 return 1;
1220         }
1221
1222         _accumulated_capture_offset = 0;
1223         _write_source.reset();
1224
1225         try {
1226                 _write_source = boost::dynamic_pointer_cast<SMFSource>(
1227                         _session.create_midi_source_for_session (write_source_name ()));
1228
1229                 if (!_write_source) {
1230                         throw failed_constructor();
1231                 }
1232         }
1233
1234         catch (failed_constructor &err) {
1235                 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1236                 _write_source.reset();
1237                 return -1;
1238         }
1239
1240         return 0;
1241 }
1242 /**
1243  * We want to use the name of the existing write source (the one that will be
1244  * used by the next capture) for another purpose. So change the name of the
1245  * current source, and return its current name.
1246  *
1247  * Return an empty string if the change cannot be accomplished.
1248  */
1249 std::string
1250 MidiDiskstream::steal_write_source_name ()
1251 {
1252         string our_old_name = _write_source->name();
1253
1254         /* this will bump the name of the current write source to the next one
1255          * (e.g. "MIDI 1-1" gets renamed to "MIDI 1-2"), thus leaving the
1256          * current write source name (e.g. "MIDI 1-1" available). See the
1257          * comments in Session::create_midi_source_by_stealing_name() about why
1258          * we do this.
1259          */
1260
1261         try {
1262                 string new_path = _session.new_midi_source_path (name());
1263                 
1264                 if (_write_source->rename (new_path)) {
1265                         return string();
1266                 }
1267         } catch (...) {
1268                 return string ();
1269         }
1270         
1271         return our_old_name;
1272 }
1273
1274 void
1275 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool /*force*/)
1276 {
1277         if (!_session.writable() || !recordable()) {
1278                 return;
1279         }
1280
1281         if (_write_source && mark_write_complete) {
1282                 _write_source->mark_streaming_write_completed ();
1283         }
1284         use_new_write_source (0);
1285 }
1286
1287 void
1288 MidiDiskstream::set_block_size (pframes_t /*nframes*/)
1289 {
1290 }
1291
1292 void
1293 MidiDiskstream::allocate_temporary_buffers ()
1294 {
1295 }
1296
1297 void
1298 MidiDiskstream::ensure_input_monitoring (bool yn)
1299 {
1300         boost::shared_ptr<MidiPort> sp = _source_port.lock ();
1301         
1302         if (sp) {
1303                 sp->ensure_input_monitoring (yn);
1304         }
1305 }
1306
1307 void
1308 MidiDiskstream::set_align_style_from_io ()
1309 {
1310         if (_alignment_choice != Automatic) {
1311                 return;
1312         }
1313
1314         /* XXX Not sure what, if anything we can do with MIDI
1315            as far as capture alignment etc.
1316         */
1317
1318         set_align_style (ExistingMaterial);
1319 }
1320
1321
1322 float
1323 MidiDiskstream::playback_buffer_load () const
1324 {
1325         /* For MIDI it's not trivial to differentiate the following two cases:
1326            
1327            1.  The playback buffer is empty because the system has run out of time to fill it.
1328            2.  The playback buffer is empty because there is no more data on the playlist.
1329
1330            If we use a simple buffer load computation, we will report that the MIDI diskstream
1331            cannot keep up when #2 happens, when in fact it can.  Since MIDI data rates
1332            are so low compared to audio, just give a pretend answer here.
1333         */
1334         
1335         return 1;
1336 }
1337
1338 float
1339 MidiDiskstream::capture_buffer_load () const
1340 {
1341         /* We don't report playback buffer load, so don't report capture load either */
1342         
1343         return 1;
1344 }
1345
1346 int
1347 MidiDiskstream::use_pending_capture_data (XMLNode& /*node*/)
1348 {
1349         return 0;
1350 }
1351
1352 void
1353 MidiDiskstream::flush_playback (framepos_t start, framepos_t end)
1354 {
1355         _playback_buf->flush (start, end);
1356         g_atomic_int_add (&_frames_read_from_ringbuffer, end - start);
1357 }
1358
1359 /** Writes playback events from playback_sample for nframes to dst, translating time stamps
1360  *  so that an event at playback_sample has time = 0
1361  */
1362 void
1363 MidiDiskstream::get_playback (MidiBuffer& dst, framecnt_t nframes)
1364 {
1365         dst.clear();
1366
1367         Location* loc = loop_location;
1368
1369         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1370                              "%1 MDS pre-read read %8 @ %4..%5 from %2 write to %3, LOOPED ? %6-%7\n", _name,
1371                              _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr(), playback_sample, playback_sample + nframes, 
1372                              (loc ? loc->start() : -1), (loc ? loc->end() : -1), nframes));
1373
1374         // cerr << "================\n";
1375         // _playback_buf->dump (cerr);
1376         // cerr << "----------------\n";
1377
1378         size_t events_read = 0; 
1379
1380         if (loc) {
1381                 framepos_t effective_start;
1382
1383                 if (playback_sample >= loc->end()) {
1384                         effective_start = loc->start() + ((playback_sample - loc->end()) % loc->length());
1385                 } else {
1386                         effective_start = playback_sample;
1387                 }
1388                 
1389                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("looped, effective start adjusted to %1\n", effective_start));
1390
1391                 if (effective_start == loc->start()) {
1392                         /* We need to turn off notes that may extend
1393                            beyond the loop end.
1394                         */
1395
1396                         _playback_buf->loop_resolve (dst, 0);
1397                 }
1398
1399                 if (loc->end() >= effective_start && loc->end() < effective_start + nframes) {
1400                         /* end of loop is within the range we are reading, so
1401                            split the read in two, and lie about the location
1402                            for the 2nd read
1403                         */
1404                         framecnt_t first, second;
1405
1406                         first = loc->end() - effective_start;
1407                         second = nframes - first;
1408
1409                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read for eff %1 end %2: %3 and %4\n",
1410                                                                               effective_start, loc->end(), first, second));
1411
1412                         if (first) {
1413                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #1, from %1 for %2\n",
1414                                                                                       effective_start, first));
1415                                 events_read = _playback_buf->read (dst, effective_start, first);
1416                         } 
1417
1418                         if (second) {
1419                                 DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #2, from %1 for %2\n",
1420                                                                                       loc->start(), second));
1421                                 events_read += _playback_buf->read (dst, loc->start(), second);
1422                         }
1423                                                                     
1424                 } else {
1425                         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose ("loop read #3, adjusted start as %1 for %2\n",
1426                                                                               effective_start, nframes));
1427                         events_read = _playback_buf->read (dst, effective_start, effective_start + nframes);
1428                 }
1429         } else {
1430                 events_read = _playback_buf->read (dst, playback_sample, playback_sample + nframes);
1431         }
1432
1433         DEBUG_TRACE (DEBUG::MidiDiskstreamIO, string_compose (
1434                              "%1 MDS events read %2 range %3 .. %4 rspace %5 wspace %6 r@%7 w@%8\n",
1435                              _name, events_read, playback_sample, playback_sample + nframes,
1436                              _playback_buf->read_space(), _playback_buf->write_space(),
1437                              _playback_buf->get_read_ptr(), _playback_buf->get_write_ptr()));
1438
1439         g_atomic_int_add (&_frames_read_from_ringbuffer, nframes);
1440 }
1441
1442 bool
1443 MidiDiskstream::set_name (string const & name)
1444 {
1445         if (_name == name) {
1446                 return true;
1447         }
1448         Diskstream::set_name (name);
1449
1450         /* get a new write source so that its name reflects the new diskstream name */
1451         use_new_write_source (0);
1452
1453         return true;
1454 }
1455
1456 bool
1457 MidiDiskstream::set_write_source_name (const std::string& str) {
1458         if (_write_source_name == str) {
1459                 return true;
1460         }
1461         Diskstream::set_write_source_name (str);
1462         if (_write_source_name == name()) {
1463                 return true;
1464         }
1465         use_new_write_source (0);
1466         return true;
1467 }
1468
1469 boost::shared_ptr<MidiBuffer>
1470 MidiDiskstream::get_gui_feed_buffer () const
1471 {
1472         boost::shared_ptr<MidiBuffer> b (new MidiBuffer (AudioEngine::instance()->raw_buffer_size (DataType::MIDI)));
1473         
1474         Glib::Threads::Mutex::Lock lm (_gui_feed_buffer_mutex);
1475         b->copy (_gui_feed_buffer);
1476         return b;
1477 }
1478
1479 void
1480 MidiDiskstream::reset_tracker ()
1481 {
1482         _playback_buf->reset_tracker ();
1483
1484         boost::shared_ptr<MidiPlaylist> mp (midi_playlist());
1485
1486         if (mp) {
1487                 mp->clear_note_trackers ();
1488         }
1489 }