2 Copyright (C) 2000-2003 Paul Davis
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.
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.
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.
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>
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>
59 using namespace ARDOUR;
62 nframes_t MidiDiskstream::midi_readahead = 4096;
64 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65 : Diskstream(sess, name, flag)
69 , _last_flush_frame(0)
70 , _note_mode(Sustained)
71 , _frames_written_to_ringbuffer(0)
72 , _frames_read_from_ringbuffer(0)
74 /* prevent any write sources from being created */
83 assert(!destructive());
86 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
87 : Diskstream(sess, node)
91 , _last_flush_frame(0)
92 , _note_mode(Sustained)
93 , _frames_written_to_ringbuffer(0)
94 , _frames_read_from_ringbuffer(0)
99 if (set_state (node)) {
100 in_set_state = false;
101 throw failed_constructor();
104 in_set_state = false;
107 use_destructive_playlist ();
112 MidiDiskstream::init (Diskstream::Flag f)
116 /* there are no channels at this point, so these
117 two calls just get speed_buffer_size and wrap_buffer
118 size setup without duplicating their code.
121 set_block_size (_session.get_block_size());
122 allocate_temporary_buffers ();
124 _playback_buf = new MidiRingBuffer (_session.midi_diskstream_buffer_size());
125 _capture_buf = new MidiRingBuffer (_session.midi_diskstream_buffer_size());
127 _n_channels = ChanCount(DataType::MIDI, 1);
129 assert(recordable());
132 MidiDiskstream::~MidiDiskstream ()
134 Glib::Mutex::Lock lm (state_lock);
139 MidiDiskstream::non_realtime_locate (nframes_t position)
141 assert(_write_source);
142 _write_source->set_timeline_position (position);
143 seek(position, false);
148 MidiDiskstream::non_realtime_input_change ()
151 Glib::Mutex::Lock lm (state_lock);
153 if (input_change_pending == NoChange) {
157 if (input_change_pending & ConfigurationChanged) {
158 assert(_io->n_inputs() == _n_channels);
161 get_input_sources ();
162 set_capture_offset ();
164 if (first_input_change) {
165 set_align_style (_persistent_alignment_style);
166 first_input_change = false;
168 set_align_style_from_io ();
171 input_change_pending = NoChange;
173 /* implicit unlock */
176 /* reset capture files */
178 reset_write_sources (false);
180 /* now refill channel buffers */
182 if (speed() != 1.0f || speed() != -1.0f) {
183 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
186 seek (_session.transport_frame());
189 _last_flush_frame = _session.transport_frame();
193 MidiDiskstream::get_input_sources ()
195 uint32_t ni = _io->n_inputs().n_midi();
201 // This is all we do for now at least
204 _source_port = _io->midi_input(0);
210 MidiDiskstream::find_and_use_playlist (const string& name)
212 boost::shared_ptr<MidiPlaylist> playlist;
214 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlist_by_name (name))) == 0) {
215 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
219 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
223 return use_playlist (playlist);
227 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
229 assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
231 Diskstream::use_playlist(playlist);
237 MidiDiskstream::use_new_playlist ()
240 boost::shared_ptr<MidiPlaylist> playlist;
242 if (!in_set_state && destructive()) {
247 newname = Playlist::bump_name (_playlist->name(), _session);
249 newname = Playlist::bump_name (_name, _session);
252 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
253 DataType::MIDI, _session, newname, hidden()))) != 0) {
255 playlist->set_orig_diskstream_id (id());
256 return use_playlist (playlist);
264 MidiDiskstream::use_copy_playlist ()
266 assert(midi_playlist());
272 if (_playlist == 0) {
273 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
278 boost::shared_ptr<MidiPlaylist> playlist;
280 newname = Playlist::bump_name (_playlist->name(), _session);
282 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
283 playlist->set_orig_diskstream_id (id());
284 return use_playlist (playlist);
290 /** Overloaded from parent to die horribly
293 MidiDiskstream::set_destructive (bool yn)
295 assert( ! destructive());
301 MidiDiskstream::set_note_mode (NoteMode m)
304 midi_playlist()->set_note_mode(m);
305 if (_write_source && _write_source->model())
306 _write_source->model()->set_note_mode(m);
310 MidiDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
312 // FIXME: waaay too much code to duplicate (AudioDiskstream)
314 int possibly_recording;
317 const int transport_rolling = 0x4;
318 const int track_rec_enabled = 0x2;
319 const int global_rec_enabled = 0x1;
321 /* merge together the 3 factors that affect record status, and compute
325 rolling = _session.transport_speed() != 0.0f;
326 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
327 change = possibly_recording ^ last_possibly_recording;
329 if (possibly_recording == last_possibly_recording) {
335 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
337 if (((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record)) ||
338 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
340 /* starting to record: compute first+last frames */
342 first_recordable_frame = transport_frame + _capture_offset;
343 last_recordable_frame = max_frames;
344 capture_start_frame = transport_frame;
346 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
348 /* was stopped, now rolling (and recording) */
350 if (_alignment_style == ExistingMaterial) {
351 first_recordable_frame += _session.worst_output_latency();
353 first_recordable_frame += _roll_delay;
358 /* was rolling, but record state changed */
360 if (_alignment_style == ExistingMaterial) {
363 if (!Config->get_punch_in()) {
365 /* manual punch in happens at the correct transport frame
366 because the user hit a button. but to get alignment correct
367 we have to back up the position of the new region to the
368 appropriate spot given the roll delay.
371 capture_start_frame -= _roll_delay;
373 /* XXX paul notes (august 2005): i don't know why
377 first_recordable_frame += _capture_offset;
381 /* autopunch toggles recording at the precise
382 transport frame, and then the DS waits
383 to start recording for a time that depends
384 on the output latency.
387 first_recordable_frame += _session.worst_output_latency();
392 if (Config->get_punch_in()) {
393 first_recordable_frame += _roll_delay;
395 capture_start_frame -= _roll_delay;
401 } else if (!record_enabled() || !can_record) {
405 last_recordable_frame = transport_frame + _capture_offset;
407 if (_alignment_style == ExistingMaterial) {
408 last_recordable_frame += _session.worst_output_latency();
410 last_recordable_frame += _roll_delay;
414 last_possibly_recording = possibly_recording;
418 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
420 // FIXME: waay too much code to duplicate (AudioDiskstream::process)
422 nframes_t rec_offset = 0;
423 nframes_t rec_nframes = 0;
424 bool nominally_recording;
425 bool re = record_enabled ();
426 bool collect_playback = false;
428 /* if we've already processed the frames corresponding to this call,
429 just return. this allows multiple routes that are taking input
430 from this diskstream to call our ::process() method, but have
431 this stuff only happen once. more commonly, it allows both
432 the AudioTrack that is using this AudioDiskstream *and* the Session
433 to call process() without problems.
440 commit_should_unlock = false;
442 check_record_status (transport_frame, nframes, can_record);
444 nominally_recording = (can_record && re);
451 /* This lock is held until the end of ::commit, so these two functions
452 must always be called as a pair. The only exception is if this function
453 returns a non-zero value, in which case, ::commit should not be called.
456 // If we can't take the state lock return.
457 if (!state_lock.trylock()) {
460 commit_should_unlock = true;
461 adjust_capture_position = 0;
463 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
466 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
473 case OverlapInternal:
474 /* ---------- recrange
477 rec_nframes = nframes;
482 /* |--------| recrange
485 rec_nframes = transport_frame + nframes - first_recordable_frame;
487 rec_offset = first_recordable_frame - transport_frame;
492 /* |--------| recrange
495 rec_nframes = last_recordable_frame - transport_frame;
499 case OverlapExternal:
500 /* |--------| recrange
501 -------------- transrange
503 rec_nframes = last_recordable_frame - last_recordable_frame;
504 rec_offset = first_recordable_frame - transport_frame;
508 if (rec_nframes && !was_recording) {
509 capture_captured = 0;
510 was_recording = true;
515 if (can_record && !_last_capture_regions.empty()) {
516 _last_capture_regions.clear ();
519 if (nominally_recording || rec_nframes) {
521 assert(_source_port);
523 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
524 //_capture_buf->write(_source_port->get_midi_buffer(), transport_frame);
525 size_t num_events = _source_port->get_midi_buffer( nframes, offset ).size();
526 size_t to_write = std::min(_capture_buf->write_space(), num_events);
528 MidiBuffer::iterator port_iter = _source_port->get_midi_buffer( nframes, offset ).begin();
530 for (size_t i=0; i < to_write; ++i) {
531 const Evoral::MIDIEvent& ev = *port_iter;
533 _capture_buf->write(ev.time() + transport_frame, ev.type(), ev.size(), ev.buffer());
540 finish_capture (rec_monitors_input);
547 /* XXX XXX XXX XXX XXX XXX XXX XXX */
549 /* data will be written to disk */
551 if (rec_nframes == nframes && rec_offset == 0) {
553 playback_distance = nframes;
556 collect_playback = true;
559 adjust_capture_position = rec_nframes;
561 } else if (nominally_recording) {
563 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
565 playback_distance = nframes;
569 collect_playback = true;
572 if (collect_playback) {
574 /* we're doing playback */
576 nframes_t necessary_samples;
578 /* no varispeed playback if we're recording, because the output .... TBD */
580 if (rec_nframes == 0 && _actual_speed != 1.0f) {
581 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
583 necessary_samples = nframes;
586 // XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
587 // Write into playback buffer here, and whatnot?
588 //cerr << "MDS FIXME: collect playback" << endl;
598 /* we're exiting with failure, so ::commit will not
599 be called. unlock the state lock.
602 commit_should_unlock = false;
610 MidiDiskstream::commit (nframes_t nframes)
612 bool need_butler = false;
614 if (_actual_speed < 0.0) {
615 playback_sample -= playback_distance;
617 playback_sample += playback_distance;
620 if (adjust_capture_position != 0) {
621 capture_captured += adjust_capture_position;
622 adjust_capture_position = 0;
626 * can't do this with midi: write space is in bytes, chunk_frames is in frames
628 need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
630 need_butler = _playback_buf->write_space() >= disk_io_chunk_frames
631 || _capture_buf->read_space() >= disk_io_chunk_frames;
634 // Use The Counters To calculate how much time the Ringbuffer holds.
635 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
636 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
637 if ((frames_written - frames_read) <= midi_readahead)
640 if (commit_should_unlock) {
650 MidiDiskstream::set_pending_overwrite (bool yn)
652 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
654 pending_overwrite = yn;
656 overwrite_frame = playback_sample;
660 MidiDiskstream::overwrite_existing_buffers ()
662 //read(overwrite_frame, disk_io_chunk_frames, false);
663 overwrite_queued = false;
664 pending_overwrite = false;
670 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
672 Glib::Mutex::Lock lm (state_lock);
675 _playback_buf->reset();
676 _capture_buf->reset();
677 g_atomic_int_set(&_frames_read_from_ringbuffer, 0);
678 g_atomic_int_set(&_frames_written_to_ringbuffer, 0);
680 playback_sample = frame;
683 if (complete_refill) {
684 while ((ret = do_refill_with_alloc ()) > 0) ;
686 ret = do_refill_with_alloc ();
693 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
695 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
696 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
697 if ((frames_written-frames_read) < distance) {
705 MidiDiskstream::internal_playback_seek (nframes_t distance)
707 cerr << "MDS: internal_playback_seek " << distance << endl;
709 first_recordable_frame += distance;
710 playback_sample += distance;
715 /** @a start is set to the new frame position (TIME) read up to */
717 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
719 nframes_t this_read = 0;
721 nframes_t loop_end = 0;
722 nframes_t loop_start = 0;
723 nframes_t loop_length = 0;
727 /* Make the use of a Location atomic for this read operation.
729 Note: Locations don't get deleted, so all we care about
730 when I say "atomic" is that we are always pointing to
731 the same one and using a start/length values obtained
735 if ((loc = loop_location) != 0) {
736 loop_start = loc->start();
737 loop_end = loc->end();
738 loop_length = loop_end - loop_start;
741 /* if we are looping, ensure that the first frame we read is at the correct
742 position within the loop.
745 if (loc && (start >= loop_end)) {
746 //cerr << "start adjusted from " << start;
747 start = loop_start + ((start - loop_start) % loop_length);
748 //cerr << "to " << start << endl;
750 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
755 /* take any loop into account. we can't read past the end of the loop. */
757 if (loc && (loop_end - start < dur)) {
758 this_read = loop_end - start;
759 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
766 if (this_read == 0) {
770 this_read = min(dur,this_read);
772 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
773 error << string_compose(_("MidiDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
777 //cout << "this write " << this_read << "start= " << start << endl;
778 g_atomic_int_add(&_frames_written_to_ringbuffer, this_read);
780 _read_data_count = _playlist->read_data_count();
784 // Swap note ons with note offs here. etc?
785 // Fully reversing MIDI required look-ahead (well, behind) to find previous
786 // CC values etc. hard.
790 /* if we read to the end of the loop, go back to the beginning */
793 // Synthesize LoopEvent here, because the next events
794 // written will have non-monotonic timestamps.
795 _playback_buf->write(loop_end - 1, LoopEventType, 0, 0);
796 //cout << "Pushing LoopEvent ts=" << loop_end-1
797 // << " start+this_read " << start+this_read << endl;
806 //offset += this_read;
813 MidiDiskstream::do_refill_with_alloc ()
819 MidiDiskstream::do_refill ()
822 size_t write_space = _playback_buf->write_space();
823 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
825 if (write_space == 0) {
833 /* at end: nothing to do */
834 if (file_frame == max_frames) {
838 // At this point we...
839 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
840 assert(file_frame <= max_frames); // ... something to write
842 // now calculate how much time is in the ringbuffer.
843 // and lets write as much as we need to get this to be midi_readahead;
844 uint32_t frames_read = g_atomic_int_get(&_frames_read_from_ringbuffer);
845 uint32_t frames_written = g_atomic_int_get(&_frames_written_to_ringbuffer);
846 if ((frames_written-frames_read) >= midi_readahead) {
847 //cout << "Nothing to do. all fine" << endl;
851 nframes_t to_read = midi_readahead - (frames_written - frames_read);
853 //cout << "read for midi_readahead " << to_read << " rb_contains: " << frames_written-frames_read << endl;
855 to_read = min(to_read, (max_frames - file_frame));
857 if (read (file_frame, to_read, reversed)) {
864 /** Flush pending data to disk.
866 * Important note: this function will write *AT MOST* disk_io_chunk_frames
867 * of data to disk. it will never write more than that. If it writes that
868 * much and there is more than that waiting to be written, it will return 1,
869 * otherwise 0 on success or -1 on failure.
871 * If there is less than disk_io_chunk_frames to be written, no data will be
872 * written at all unless @a force_flush is true.
875 MidiDiskstream::do_flush (RunContext context, bool force_flush)
881 _write_data_count = 0;
883 if (_last_flush_frame > _session.transport_frame()
884 || _last_flush_frame < capture_start_frame) {
885 _last_flush_frame = _session.transport_frame();
888 total = _session.transport_frame() - _last_flush_frame;
890 if (total == 0 || (_capture_buf->read_space() == 0 && _session.transport_speed() == 0) || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
894 /* if there are 2+ chunks of disk i/o possible for
895 this track, let the caller know so that it can arrange
896 for us to be called again, ASAP.
898 if we are forcing a flush, then if there is* any* extra
899 work, let the caller know.
901 if we are no longer recording and there is any extra work,
902 let the caller know too.
905 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
909 to_write = disk_io_chunk_frames;
911 assert(!destructive());
913 if (record_enabled() && _session.transport_frame() - _last_flush_frame > disk_io_chunk_frames) {
914 if ((!_write_source) || _write_source->midi_write (*_capture_buf, to_write) != to_write) {
915 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
918 _last_flush_frame = _session.transport_frame();
927 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
929 uint32_t buffer_position;
930 bool more_work = true;
932 boost::shared_ptr<MidiRegion> region;
933 nframes_t total_capture;
934 MidiRegion::SourceList srcs;
935 MidiRegion::SourceList::iterator src;
936 vector<CaptureInfo*>::iterator ci;
937 bool mark_write_completed = false;
939 finish_capture (true);
941 /* butler is already stopped, but there may be work to do
942 to flush remaining data to disk.
945 while (more_work && !err) {
946 switch (do_flush (TransportContext, true)) {
953 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
958 /* XXX is there anything we can do if err != 0 ? */
959 Glib::Mutex::Lock lm (capture_info_lock);
961 if (capture_info.empty()) {
969 _write_source->mark_for_remove ();
970 _write_source->drop_references ();
971 _write_source.reset();
974 /* new source set up in "out" below */
978 assert(_write_source);
980 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
981 total_capture += (*ci)->frames;
984 /* figure out the name for this take */
986 srcs.push_back (_write_source);
987 _write_source->set_timeline_position (capture_info.front()->start);
988 _write_source->set_captured_for (_name);
990 string whole_file_region_name;
991 whole_file_region_name = region_name_from_path (_write_source->name(), true);
993 /* Register a new region with the Session that
994 describes the entire source. Do this first
995 so that any sub-regions will obviously be
996 children of this one (later!)
1000 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, _write_source->last_capture_start_frame(), total_capture,
1001 whole_file_region_name,
1002 0, Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile)));
1004 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1005 region->special_set_position (capture_info.front()->start);
1009 catch (failed_constructor& err) {
1010 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1014 _last_capture_regions.push_back (region);
1016 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1018 XMLNode &before = _playlist->get_state();
1019 _playlist->freeze ();
1021 for (buffer_position = _write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1025 _session.region_name (region_name, _write_source->name(), false);
1027 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1030 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1031 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1034 catch (failed_constructor& err) {
1035 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1036 continue; /* XXX is this OK? */
1039 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1041 _last_capture_regions.push_back (region);
1043 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1045 i_am_the_modifier++;
1046 _playlist->add_region (region, (*ci)->start);
1047 i_am_the_modifier--;
1049 buffer_position += (*ci)->frames;
1053 XMLNode &after = _playlist->get_state();
1054 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1058 mark_write_completed = true;
1060 reset_write_sources (mark_write_completed);
1062 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1066 capture_info.clear ();
1067 capture_start_frame = 0;
1071 MidiDiskstream::transport_looped (nframes_t transport_frame)
1073 if (was_recording) {
1075 // adjust the capture length knowing that the data will be recorded to disk
1076 // only necessary after the first loop where we're recording
1077 if (capture_info.size() == 0) {
1078 capture_captured += _capture_offset;
1080 if (_alignment_style == ExistingMaterial) {
1081 capture_captured += _session.worst_output_latency();
1083 capture_captured += _roll_delay;
1087 finish_capture (true);
1089 // the next region will start recording via the normal mechanism
1090 // we'll set the start position to the current transport pos
1091 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1092 capture_start_frame = transport_frame;
1093 first_recordable_frame = transport_frame; // mild lie
1094 last_recordable_frame = max_frames;
1095 was_recording = true;
1100 MidiDiskstream::finish_capture (bool rec_monitors_input)
1102 was_recording = false;
1104 if (capture_captured == 0) {
1108 // Why must we destroy?
1109 assert(!destructive());
1111 CaptureInfo* ci = new CaptureInfo;
1113 ci->start = capture_start_frame;
1114 ci->frames = capture_captured;
1116 /* XXX theoretical race condition here. Need atomic exchange ?
1117 However, the circumstances when this is called right
1118 now (either on record-disable or transport_stopped)
1119 mean that no actual race exists. I think ...
1120 We now have a capture_info_lock, but it is only to be used
1121 to synchronize in the transport_stop and the capture info
1122 accessors, so that invalidation will not occur (both non-realtime).
1125 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1127 capture_info.push_back (ci);
1128 capture_captured = 0;
1132 MidiDiskstream::set_record_enabled (bool yn)
1134 if (!recordable() || !_session.record_enabling_legal()) {
1138 assert(!destructive());
1140 if (yn && _source_port == 0) {
1142 /* pick up connections not initiated *from* the IO object
1143 we're associated with.
1146 get_input_sources ();
1149 /* yes, i know that this not proof against race conditions, but its
1150 good enough. i think.
1153 if (record_enabled() != yn) {
1155 engage_record_enable ();
1157 disengage_record_enable ();
1163 MidiDiskstream::engage_record_enable ()
1165 bool rolling = _session.transport_speed() != 0.0f;
1167 g_atomic_int_set (&_record_enabled, 1);
1169 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1170 _source_port->request_monitor_input (!(Config->get_auto_input() && rolling));
1173 // FIXME: Why is this necessary? Isn't needed for AudioDiskstream...
1175 use_new_write_source();
1177 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1179 RecordEnableChanged (); /* EMIT SIGNAL */
1183 MidiDiskstream::disengage_record_enable ()
1185 g_atomic_int_set (&_record_enabled, 0);
1186 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1188 _source_port->request_monitor_input (false);
1192 RecordEnableChanged (); /* EMIT SIGNAL */
1196 MidiDiskstream::get_state ()
1198 XMLNode* node = new XMLNode ("MidiDiskstream");
1200 LocaleGuard lg (X_("POSIX"));
1202 snprintf (buf, sizeof(buf), "0x%x", _flags);
1203 node->add_property ("flags", buf);
1205 node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1207 snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1208 node->add_property("channel-mask", buf);
1210 node->add_property ("playlist", _playlist->name());
1212 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1213 node->add_property ("speed", buf);
1215 node->add_property("name", _name);
1216 id().print(buf, sizeof(buf));
1217 node->add_property("id", buf);
1219 if (_write_source && _session.get_record_enabled()) {
1221 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1222 XMLNode* cs_grandchild;
1224 cs_grandchild = new XMLNode (X_("file"));
1225 cs_grandchild->add_property (X_("path"), _write_source->path());
1226 cs_child->add_child_nocopy (*cs_grandchild);
1228 /* store the location where capture will start */
1232 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1233 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1235 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1238 cs_child->add_property (X_("at"), buf);
1239 node->add_child_nocopy (*cs_child);
1243 node->add_child_copy (*_extra_xml);
1250 MidiDiskstream::set_state (const XMLNode& node)
1252 const XMLProperty* prop;
1253 XMLNodeList nlist = node.children();
1254 XMLNodeIterator niter;
1255 uint32_t nchans = 1;
1256 XMLNode* capture_pending_node = 0;
1257 LocaleGuard lg (X_("POSIX"));
1259 in_set_state = true;
1261 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1262 /*if ((*niter)->name() == IO::state_node_name) {
1263 deprecated_io_node = new XMLNode (**niter);
1265 assert ((*niter)->name() != IO::state_node_name);
1267 if ((*niter)->name() == X_("CapturingSources")) {
1268 capture_pending_node = *niter;
1272 /* prevent write sources from being created */
1274 in_set_state = true;
1276 if ((prop = node.property ("name")) != 0) {
1277 _name = prop->value();
1280 if ((prop = node.property ("id")) != 0) {
1281 _id = prop->value ();
1284 if ((prop = node.property ("flags")) != 0) {
1285 _flags = Flag (string_2_enum (prop->value(), _flags));
1288 ChannelMode channel_mode = AllChannels;
1289 if ((prop = node.property ("channel-mode")) != 0) {
1290 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1293 unsigned int channel_mask = 0xFFFF;
1294 if ((prop = node.property ("channel-mask")) != 0) {
1295 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1296 if (channel_mask & (~0xFFFF)) {
1297 warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1301 set_channel_mode(channel_mode, channel_mask);
1303 if ((prop = node.property ("channels")) != 0) {
1304 nchans = atoi (prop->value().c_str());
1307 if ((prop = node.property ("playlist")) == 0) {
1312 bool had_playlist = (_playlist != 0);
1314 if (find_and_use_playlist (prop->value())) {
1318 if (!had_playlist) {
1319 _playlist->set_orig_diskstream_id (_id);
1322 if (capture_pending_node) {
1323 use_pending_capture_data (*capture_pending_node);
1328 if ((prop = node.property ("speed")) != 0) {
1329 double sp = atof (prop->value().c_str());
1331 if (realtime_set_speed (sp, false)) {
1332 non_realtime_set_speed ();
1336 in_set_state = false;
1338 /* make sure this is clear before we do anything else */
1341 //_capturing_source = 0;
1343 /* write sources are handled when we handle the input set
1344 up of the IO that owns this DS (::non_realtime_input_change())
1347 in_set_state = false;
1353 MidiDiskstream::use_new_write_source (uint32_t n)
1355 if (!recordable()) {
1361 if (_write_source) {
1363 if (_write_source->is_empty ()) {
1364 _write_source->mark_for_remove ();
1365 _write_source.reset();
1367 _write_source.reset();
1372 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1373 if (!_write_source) {
1374 throw failed_constructor();
1378 catch (failed_constructor &err) {
1379 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1380 _write_source.reset();
1384 _write_source->set_allow_remove_if_empty (true);
1390 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1392 if (!recordable()) {
1396 if (_write_source && mark_write_complete) {
1397 _write_source->mark_streaming_write_completed ();
1400 use_new_write_source (0);
1402 if (record_enabled()) {
1403 //_capturing_sources.push_back (_write_source);
1408 MidiDiskstream::rename_write_sources ()
1410 if (_write_source != 0) {
1411 _write_source->set_source_name (_name, destructive());
1412 /* XXX what to do if this fails ? */
1418 MidiDiskstream::set_block_size (nframes_t nframes)
1423 MidiDiskstream::allocate_temporary_buffers ()
1428 MidiDiskstream::monitor_input (bool yn)
1431 _source_port->request_monitor_input (yn);
1433 cerr << "MidiDiskstream NO SOURCE PORT TO MONITOR\n";
1437 MidiDiskstream::set_align_style_from_io ()
1439 bool have_physical = false;
1445 get_input_sources ();
1447 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1448 have_physical = true;
1451 if (have_physical) {
1452 set_align_style (ExistingMaterial);
1454 set_align_style (CaptureTime);
1460 MidiDiskstream::playback_buffer_load () const
1462 return (float) ((double) _playback_buf->read_space()/
1463 (double) _playback_buf->capacity());
1467 MidiDiskstream::capture_buffer_load () const
1469 return (float) ((double) _capture_buf->write_space()/
1470 (double) _capture_buf->capacity());
1474 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1479 /** Writes playback events in the given range to \a dst, translating time stamps
1480 * so that an event at \a start has time = 0
1483 MidiDiskstream::get_playback(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset)
1487 assert(dst.size() == 0);
1490 // Reverse. ... We just don't do reverse, ok? Back off.
1495 // Check only events added this offset cycle
1496 MidiBuffer::iterator this_cycle_start = dst.end();
1498 // Translates stamps to be relative to start, but add offset.
1499 _playback_buf->read(dst, start, end, offset);
1501 gint32 data_read = end-start;
1502 //cout << "data read = " << data_read << " e=" << end << " s=" << start << "off= " << offset
1503 // << " readspace " << _playback_buf->read_space() << " writespace " << _playback_buf->write_space() << endl;
1504 g_atomic_int_add(&_frames_read_from_ringbuffer, data_read);
1506 // Now feed the data through the MidiStateTracker.
1507 // In case it detects a LoopEvent it will add necessary note
1510 if (_midistate_tracker.track(this_cycle_start, dst.end()))
1511 _midistate_tracker.resolve_notes(dst, end-start - 1 + offset);