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