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/midi_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/smf_source.h>
45 #include <ardour/send.h>
46 #include <ardour/region_factory.h>
47 #include <ardour/midi_playlist.h>
48 #include <ardour/playlist_factory.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/midi_region.h>
51 #include <ardour/midi_port.h>
57 using namespace ARDOUR;
60 MidiDiskstream::MidiDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
61 : Diskstream(sess, name, flag)
65 , _last_flush_frame(0)
66 , _note_mode(Sustained)
68 /* prevent any write sources from being created */
77 assert(!destructive());
80 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
81 : Diskstream(sess, node)
85 , _last_flush_frame(0)
86 , _note_mode(Sustained)
91 if (set_state (node)) {
93 throw failed_constructor();
99 use_destructive_playlist ();
104 MidiDiskstream::init (Diskstream::Flag f)
108 /* there are no channels at this point, so these
109 two calls just get speed_buffer_size and wrap_buffer
110 size setup without duplicating their code.
113 set_block_size (_session.get_block_size());
114 allocate_temporary_buffers ();
116 _playback_buf = new MidiRingBuffer (_session.midi_diskstream_buffer_size());
117 _capture_buf = new MidiRingBuffer (_session.midi_diskstream_buffer_size());
119 _n_channels = ChanCount(DataType::MIDI, 1);
121 assert(recordable());
124 MidiDiskstream::~MidiDiskstream ()
126 Glib::Mutex::Lock lm (state_lock);
131 MidiDiskstream::non_realtime_locate (nframes_t position)
133 assert(_write_source);
134 _write_source->set_timeline_position (position);
135 seek(position, false);
140 MidiDiskstream::non_realtime_input_change ()
143 Glib::Mutex::Lock lm (state_lock);
145 if (input_change_pending == NoChange) {
149 if (input_change_pending & ConfigurationChanged) {
150 assert(_io->n_inputs() == _n_channels);
153 get_input_sources ();
154 set_capture_offset ();
156 if (first_input_change) {
157 set_align_style (_persistent_alignment_style);
158 first_input_change = false;
160 set_align_style_from_io ();
163 input_change_pending = NoChange;
165 /* implicit unlock */
168 /* reset capture files */
170 reset_write_sources (false);
172 /* now refill channel buffers */
174 if (speed() != 1.0f || speed() != -1.0f) {
175 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
178 seek (_session.transport_frame());
181 _last_flush_frame = _session.transport_frame();
185 MidiDiskstream::get_input_sources ()
187 uint32_t ni = _io->n_inputs().n_midi();
193 // This is all we do for now at least
196 _source_port = _io->midi_input(0);
202 MidiDiskstream::find_and_use_playlist (const string& name)
204 boost::shared_ptr<MidiPlaylist> playlist;
206 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (_session.playlist_by_name (name))) == 0) {
207 playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (DataType::MIDI, _session, name));
211 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't an midi playlist"), name) << endmsg;
215 return use_playlist (playlist);
219 MidiDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
221 assert(boost::dynamic_pointer_cast<MidiPlaylist>(playlist));
223 Diskstream::use_playlist(playlist);
229 MidiDiskstream::use_new_playlist ()
232 boost::shared_ptr<MidiPlaylist> playlist;
234 if (!in_set_state && destructive()) {
239 newname = Playlist::bump_name (_playlist->name(), _session);
241 newname = Playlist::bump_name (_name, _session);
244 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist> (PlaylistFactory::create (
245 DataType::MIDI, _session, newname, hidden()))) != 0) {
247 playlist->set_orig_diskstream_id (id());
248 return use_playlist (playlist);
256 MidiDiskstream::use_copy_playlist ()
258 assert(midi_playlist());
264 if (_playlist == 0) {
265 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
270 boost::shared_ptr<MidiPlaylist> playlist;
272 newname = Playlist::bump_name (_playlist->name(), _session);
274 if ((playlist = boost::dynamic_pointer_cast<MidiPlaylist>(PlaylistFactory::create (midi_playlist(), newname))) != 0) {
275 playlist->set_orig_diskstream_id (id());
276 return use_playlist (playlist);
282 /** Overloaded from parent to die horribly
285 MidiDiskstream::set_destructive (bool yn)
287 assert( ! destructive());
293 MidiDiskstream::set_note_mode (NoteMode m)
296 midi_playlist()->set_note_mode(m);
297 if (_write_source && _write_source->model())
298 _write_source->model()->set_note_mode(m);
302 MidiDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
304 // FIXME: waaay too much code to duplicate (AudioDiskstream)
306 int possibly_recording;
309 const int transport_rolling = 0x4;
310 const int track_rec_enabled = 0x2;
311 const int global_rec_enabled = 0x1;
313 /* merge together the 3 factors that affect record status, and compute
317 rolling = _session.transport_speed() != 0.0f;
318 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
319 change = possibly_recording ^ last_possibly_recording;
321 if (possibly_recording == last_possibly_recording) {
327 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
329 if (((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record)) ||
330 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
332 /* starting to record: compute first+last frames */
334 first_recordable_frame = transport_frame + _capture_offset;
335 last_recordable_frame = max_frames;
336 capture_start_frame = transport_frame;
338 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
340 /* was stopped, now rolling (and recording) */
342 if (_alignment_style == ExistingMaterial) {
343 first_recordable_frame += _session.worst_output_latency();
345 first_recordable_frame += _roll_delay;
350 /* was rolling, but record state changed */
352 if (_alignment_style == ExistingMaterial) {
355 if (!Config->get_punch_in()) {
357 /* manual punch in happens at the correct transport frame
358 because the user hit a button. but to get alignment correct
359 we have to back up the position of the new region to the
360 appropriate spot given the roll delay.
363 capture_start_frame -= _roll_delay;
365 /* XXX paul notes (august 2005): i don't know why
369 first_recordable_frame += _capture_offset;
373 /* autopunch toggles recording at the precise
374 transport frame, and then the DS waits
375 to start recording for a time that depends
376 on the output latency.
379 first_recordable_frame += _session.worst_output_latency();
384 if (Config->get_punch_in()) {
385 first_recordable_frame += _roll_delay;
387 capture_start_frame -= _roll_delay;
393 } else if (!record_enabled() || !can_record) {
397 last_recordable_frame = transport_frame + _capture_offset;
399 if (_alignment_style == ExistingMaterial) {
400 last_recordable_frame += _session.worst_output_latency();
402 last_recordable_frame += _roll_delay;
406 last_possibly_recording = possibly_recording;
410 MidiDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
412 // FIXME: waay too much code to duplicate (AudioDiskstream::process)
414 nframes_t rec_offset = 0;
415 nframes_t rec_nframes = 0;
416 bool nominally_recording;
417 bool re = record_enabled ();
418 bool collect_playback = false;
420 /* if we've already processed the frames corresponding to this call,
421 just return. this allows multiple routes that are taking input
422 from this diskstream to call our ::process() method, but have
423 this stuff only happen once. more commonly, it allows both
424 the AudioTrack that is using this AudioDiskstream *and* the Session
425 to call process() without problems.
432 commit_should_unlock = false;
434 check_record_status (transport_frame, nframes, can_record);
436 nominally_recording = (can_record && re);
443 /* This lock is held until the end of ::commit, so these two functions
444 must always be called as a pair. The only exception is if this function
445 returns a non-zero value, in which case, ::commit should not be called.
448 // If we can't take the state lock return.
449 if (!state_lock.trylock()) {
452 commit_should_unlock = true;
453 adjust_capture_position = 0;
455 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
458 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
465 case OverlapInternal:
466 /* ---------- recrange
469 rec_nframes = nframes;
474 /* |--------| recrange
477 rec_nframes = transport_frame + nframes - first_recordable_frame;
479 rec_offset = first_recordable_frame - transport_frame;
484 /* |--------| recrange
487 rec_nframes = last_recordable_frame - transport_frame;
491 case OverlapExternal:
492 /* |--------| recrange
493 -------------- transrange
495 rec_nframes = last_recordable_frame - last_recordable_frame;
496 rec_offset = first_recordable_frame - transport_frame;
500 if (rec_nframes && !was_recording) {
501 capture_captured = 0;
502 was_recording = true;
507 if (can_record && !_last_capture_regions.empty()) {
508 _last_capture_regions.clear ();
511 if (nominally_recording || rec_nframes) {
513 assert(_source_port);
515 // Pump entire port buffer into the ring buffer (FIXME: split cycles?)
516 //_capture_buf->write(_source_port->get_midi_buffer(), transport_frame);
517 size_t num_events = _source_port->get_midi_buffer().size();
518 size_t to_write = std::min(_capture_buf->write_space(), num_events);
520 MidiBuffer::iterator port_iter = _source_port->get_midi_buffer().begin();
522 for (size_t i=0; i < to_write; ++i) {
523 const MIDI::Event& ev = *port_iter;
525 _capture_buf->write(ev.time() + transport_frame, ev.size(), ev.buffer());
532 finish_capture (rec_monitors_input);
539 /* XXX XXX XXX XXX XXX XXX XXX XXX */
541 /* data will be written to disk */
543 if (rec_nframes == nframes && rec_offset == 0) {
545 playback_distance = nframes;
548 collect_playback = true;
551 adjust_capture_position = rec_nframes;
553 } else if (nominally_recording) {
555 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
557 playback_distance = nframes;
561 collect_playback = true;
564 if (collect_playback) {
566 /* we're doing playback */
568 nframes_t necessary_samples;
570 /* no varispeed playback if we're recording, because the output .... TBD */
572 if (rec_nframes == 0 && _actual_speed != 1.0f) {
573 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
575 necessary_samples = nframes;
578 // XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
579 // Write into playback buffer here, and whatnot?
580 //cerr << "MDS FIXME: collect playback" << endl;
590 /* we're exiting with failure, so ::commit will not
591 be called. unlock the state lock.
594 commit_should_unlock = false;
602 MidiDiskstream::commit (nframes_t nframes)
604 bool need_butler = false;
606 if (_actual_speed < 0.0) {
607 playback_sample -= playback_distance;
609 playback_sample += playback_distance;
612 if (adjust_capture_position != 0) {
613 capture_captured += adjust_capture_position;
614 adjust_capture_position = 0;
618 * can't do this with midi: write space is in bytes, chunk_frames is in frames
620 need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
622 need_butler = _playback_buf->write_space() >= disk_io_chunk_frames
623 || _capture_buf->read_space() >= disk_io_chunk_frames;
626 /* we'll just keep the playback buffer full for now.
627 * this should be decreased to reduce edit latency */
628 need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
630 if (commit_should_unlock) {
640 MidiDiskstream::set_pending_overwrite (bool yn)
642 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
644 pending_overwrite = yn;
646 overwrite_frame = playback_sample;
650 MidiDiskstream::overwrite_existing_buffers ()
652 //cerr << "MDS: overwrite_existing_buffers() (does nothing)" << endl;
657 MidiDiskstream::seek (nframes_t frame, bool complete_refill)
659 Glib::Mutex::Lock lm (state_lock);
662 _playback_buf->reset();
663 _capture_buf->reset();
665 playback_sample = frame;
668 if (complete_refill) {
669 while ((ret = do_refill_with_alloc ()) > 0) ;
671 ret = do_refill_with_alloc ();
678 MidiDiskstream::can_internal_playback_seek (nframes_t distance)
680 if (_playback_buf->read_space() < distance) {
688 MidiDiskstream::internal_playback_seek (nframes_t distance)
690 cerr << "MDS: internal_playback_seek " << distance << endl;
692 first_recordable_frame += distance;
693 playback_sample += distance;
698 /** @a start is set to the new frame position (TIME) read up to */
700 MidiDiskstream::read (nframes_t& start, nframes_t dur, bool reversed)
702 nframes_t this_read = 0;
704 nframes_t loop_end = 0;
705 nframes_t loop_start = 0;
706 nframes_t loop_length = 0;
710 /* Make the use of a Location atomic for this read operation.
712 Note: Locations don't get deleted, so all we care about
713 when I say "atomic" is that we are always pointing to
714 the same one and using a start/length values obtained
718 if ((loc = loop_location) != 0) {
719 loop_start = loc->start();
720 loop_end = loc->end();
721 loop_length = loop_end - loop_start;
724 /* if we are looping, ensure that the first frame we read is at the correct
725 position within the loop.
728 if (loc && start >= loop_end) {
729 //cerr << "start adjusted from " << start;
730 start = loop_start + ((start - loop_start) % loop_length);
731 //cerr << "to " << start << endl;
733 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
738 /* take any loop into account. we can't read past the end of the loop. */
740 if (loc && (loop_end - start < dur)) {
741 this_read = loop_end - start;
742 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
749 if (this_read == 0) {
753 this_read = min(dur,this_read);
755 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
756 error << string_compose(_("MidiDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
761 _read_data_count = _playlist->read_data_count();
765 // Swap note ons with note offs here. etc?
766 // Fully reversing MIDI required look-ahead (well, behind) to find previous
767 // CC values etc. hard.
771 /* if we read to the end of the loop, go back to the beginning */
781 //offset += this_read;
788 MidiDiskstream::do_refill_with_alloc ()
794 MidiDiskstream::do_refill ()
797 size_t write_space = _playback_buf->write_space();
798 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
800 if (write_space == 0) {
808 /* at end: nothing to do */
809 if (file_frame == max_frames) {
813 // At this point we...
814 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
815 assert(file_frame <= max_frames); // ... something to write
817 nframes_t to_read = min(disk_io_chunk_frames, (max_frames - file_frame));
819 if (read (file_frame, to_read, reversed)) {
826 /** Flush pending data to disk.
828 * Important note: this function will write *AT MOST* disk_io_chunk_frames
829 * of data to disk. it will never write more than that. If it writes that
830 * much and there is more than that waiting to be written, it will return 1,
831 * otherwise 0 on success or -1 on failure.
833 * If there is less than disk_io_chunk_frames to be written, no data will be
834 * written at all unless @a force_flush is true.
837 MidiDiskstream::do_flush (Session::RunContext context, bool force_flush)
843 _write_data_count = 0;
845 if (_last_flush_frame > _session.transport_frame()
846 || _last_flush_frame < capture_start_frame) {
847 _last_flush_frame = _session.transport_frame();
850 total = _session.transport_frame() - _last_flush_frame;
852 if (total == 0 || (_capture_buf->read_space() == 0 && _session.transport_speed() == 0) || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
856 /* if there are 2+ chunks of disk i/o possible for
857 this track, let the caller know so that it can arrange
858 for us to be called again, ASAP.
860 if we are forcing a flush, then if there is* any* extra
861 work, let the caller know.
863 if we are no longer recording and there is any extra work,
864 let the caller know too.
867 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
871 to_write = disk_io_chunk_frames;
873 assert(!destructive());
875 if (record_enabled() && _session.transport_frame() - _last_flush_frame > disk_io_chunk_frames) {
876 if ((!_write_source) || _write_source->midi_write (*_capture_buf, to_write) != to_write) {
877 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
880 _last_flush_frame = _session.transport_frame();
889 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
891 uint32_t buffer_position;
892 bool more_work = true;
894 boost::shared_ptr<MidiRegion> region;
895 nframes_t total_capture;
896 MidiRegion::SourceList srcs;
897 MidiRegion::SourceList::iterator src;
898 vector<CaptureInfo*>::iterator ci;
899 bool mark_write_completed = false;
901 finish_capture (true);
903 /* butler is already stopped, but there may be work to do
904 to flush remaining data to disk.
907 while (more_work && !err) {
908 switch (do_flush (Session::TransportContext, true)) {
915 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
920 /* XXX is there anything we can do if err != 0 ? */
921 Glib::Mutex::Lock lm (capture_info_lock);
923 if (capture_info.empty()) {
931 _write_source->mark_for_remove ();
932 _write_source->drop_references ();
933 _write_source.reset();
936 /* new source set up in "out" below */
940 assert(_write_source);
942 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
943 total_capture += (*ci)->frames;
946 /* figure out the name for this take */
948 srcs.push_back (_write_source);
949 _write_source->set_timeline_position (capture_info.front()->start);
950 _write_source->set_captured_for (_name);
952 string whole_file_region_name;
953 whole_file_region_name = region_name_from_path (_write_source->name(), true);
955 /* Register a new region with the Session that
956 describes the entire source. Do this first
957 so that any sub-regions will obviously be
958 children of this one (later!)
962 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, _write_source->last_capture_start_frame(), total_capture,
963 whole_file_region_name,
964 0, Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile)));
966 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
967 region->special_set_position (capture_info.front()->start);
971 catch (failed_constructor& err) {
972 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
976 _last_capture_regions.push_back (region);
978 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
980 XMLNode &before = _playlist->get_state();
981 _playlist->freeze ();
983 for (buffer_position = _write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
987 _session.region_name (region_name, _write_source->name(), false);
989 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
992 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
993 region = boost::dynamic_pointer_cast<MidiRegion> (rx);
996 catch (failed_constructor& err) {
997 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
998 continue; /* XXX is this OK? */
1001 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1003 _last_capture_regions.push_back (region);
1005 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1007 i_am_the_modifier++;
1008 _playlist->add_region (region, (*ci)->start);
1009 i_am_the_modifier--;
1011 buffer_position += (*ci)->frames;
1015 XMLNode &after = _playlist->get_state();
1016 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1020 mark_write_completed = true;
1022 reset_write_sources (mark_write_completed);
1024 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1028 capture_info.clear ();
1029 capture_start_frame = 0;
1033 MidiDiskstream::transport_looped (nframes_t transport_frame)
1035 if (was_recording) {
1037 // adjust the capture length knowing that the data will be recorded to disk
1038 // only necessary after the first loop where we're recording
1039 if (capture_info.size() == 0) {
1040 capture_captured += _capture_offset;
1042 if (_alignment_style == ExistingMaterial) {
1043 capture_captured += _session.worst_output_latency();
1045 capture_captured += _roll_delay;
1049 finish_capture (true);
1051 // the next region will start recording via the normal mechanism
1052 // we'll set the start position to the current transport pos
1053 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1054 capture_start_frame = transport_frame;
1055 first_recordable_frame = transport_frame; // mild lie
1056 last_recordable_frame = max_frames;
1057 was_recording = true;
1062 MidiDiskstream::finish_capture (bool rec_monitors_input)
1064 was_recording = false;
1066 if (capture_captured == 0) {
1070 // Why must we destroy?
1071 assert(!destructive());
1073 CaptureInfo* ci = new CaptureInfo;
1075 ci->start = capture_start_frame;
1076 ci->frames = capture_captured;
1078 /* XXX theoretical race condition here. Need atomic exchange ?
1079 However, the circumstances when this is called right
1080 now (either on record-disable or transport_stopped)
1081 mean that no actual race exists. I think ...
1082 We now have a capture_info_lock, but it is only to be used
1083 to synchronize in the transport_stop and the capture info
1084 accessors, so that invalidation will not occur (both non-realtime).
1087 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1089 capture_info.push_back (ci);
1090 capture_captured = 0;
1094 MidiDiskstream::set_record_enabled (bool yn)
1096 if (!recordable() || !_session.record_enabling_legal()) {
1100 assert(!destructive());
1102 if (yn && _source_port == 0) {
1104 /* pick up connections not initiated *from* the IO object
1105 we're associated with.
1108 get_input_sources ();
1111 /* yes, i know that this not proof against race conditions, but its
1112 good enough. i think.
1115 if (record_enabled() != yn) {
1117 engage_record_enable ();
1119 disengage_record_enable ();
1125 MidiDiskstream::engage_record_enable ()
1127 bool rolling = _session.transport_speed() != 0.0f;
1129 g_atomic_int_set (&_record_enabled, 1);
1131 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1132 _source_port->request_monitor_input (!(Config->get_auto_input() && rolling));
1135 // FIXME: Why is this necessary? Isn't needed for AudioDiskstream...
1137 use_new_write_source();
1139 _write_source->mark_streaming_midi_write_started (_note_mode, _session.transport_frame());
1141 RecordEnableChanged (); /* EMIT SIGNAL */
1145 MidiDiskstream::disengage_record_enable ()
1147 g_atomic_int_set (&_record_enabled, 0);
1148 if (_source_port && Config->get_monitoring_model() == HardwareMonitoring) {
1150 _source_port->request_monitor_input (false);
1154 RecordEnableChanged (); /* EMIT SIGNAL */
1158 MidiDiskstream::get_state ()
1160 XMLNode* node = new XMLNode ("MidiDiskstream");
1162 LocaleGuard lg (X_("POSIX"));
1164 snprintf (buf, sizeof(buf), "0x%x", _flags);
1165 node->add_property ("flags", buf);
1167 node->add_property("channel-mode", enum_2_string(get_channel_mode()));
1169 snprintf (buf, sizeof(buf), "0x%x", get_channel_mask());
1170 node->add_property("channel-mask", buf);
1172 node->add_property ("playlist", _playlist->name());
1174 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1175 node->add_property ("speed", buf);
1177 node->add_property("name", _name);
1178 id().print(buf, sizeof(buf));
1179 node->add_property("id", buf);
1181 if (_write_source && _session.get_record_enabled()) {
1183 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1184 XMLNode* cs_grandchild;
1186 cs_grandchild = new XMLNode (X_("file"));
1187 cs_grandchild->add_property (X_("path"), _write_source->path());
1188 cs_child->add_child_nocopy (*cs_grandchild);
1190 /* store the location where capture will start */
1194 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1195 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1197 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1200 cs_child->add_property (X_("at"), buf);
1201 node->add_child_nocopy (*cs_child);
1205 node->add_child_copy (*_extra_xml);
1212 MidiDiskstream::set_state (const XMLNode& node)
1214 const XMLProperty* prop;
1215 XMLNodeList nlist = node.children();
1216 XMLNodeIterator niter;
1217 uint32_t nchans = 1;
1218 XMLNode* capture_pending_node = 0;
1219 LocaleGuard lg (X_("POSIX"));
1221 in_set_state = true;
1223 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1224 /*if ((*niter)->name() == IO::state_node_name) {
1225 deprecated_io_node = new XMLNode (**niter);
1227 assert ((*niter)->name() != IO::state_node_name);
1229 if ((*niter)->name() == X_("CapturingSources")) {
1230 capture_pending_node = *niter;
1234 /* prevent write sources from being created */
1236 in_set_state = true;
1238 if ((prop = node.property ("name")) != 0) {
1239 _name = prop->value();
1242 if ((prop = node.property ("id")) != 0) {
1243 _id = prop->value ();
1246 if ((prop = node.property ("flags")) != 0) {
1247 _flags = Flag (string_2_enum (prop->value(), _flags));
1250 ChannelMode channel_mode = AllChannels;
1251 if ((prop = node.property ("channel-mode")) != 0) {
1252 channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode));
1255 unsigned int channel_mask = 0xFFFF;
1256 if ((prop = node.property ("channel-mask")) != 0) {
1257 sscanf (prop->value().c_str(), "0x%x", &channel_mask);
1258 if (channel_mask & (~0xFFFF)) {
1259 warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg;
1263 set_channel_mode(channel_mode, channel_mask);
1265 if ((prop = node.property ("channels")) != 0) {
1266 nchans = atoi (prop->value().c_str());
1269 if ((prop = node.property ("playlist")) == 0) {
1274 bool had_playlist = (_playlist != 0);
1276 if (find_and_use_playlist (prop->value())) {
1280 if (!had_playlist) {
1281 _playlist->set_orig_diskstream_id (_id);
1284 if (capture_pending_node) {
1285 use_pending_capture_data (*capture_pending_node);
1290 if ((prop = node.property ("speed")) != 0) {
1291 double sp = atof (prop->value().c_str());
1293 if (realtime_set_speed (sp, false)) {
1294 non_realtime_set_speed ();
1298 in_set_state = false;
1300 /* make sure this is clear before we do anything else */
1303 //_capturing_source = 0;
1305 /* write sources are handled when we handle the input set
1306 up of the IO that owns this DS (::non_realtime_input_change())
1309 in_set_state = false;
1315 MidiDiskstream::use_new_write_source (uint32_t n)
1317 if (!recordable()) {
1323 if (_write_source) {
1325 if (_write_source->is_empty ()) {
1326 _write_source->mark_for_remove ();
1327 _write_source.reset();
1329 _write_source.reset();
1334 _write_source = boost::dynamic_pointer_cast<SMFSource>(_session.create_midi_source_for_session (*this));
1335 if (!_write_source) {
1336 throw failed_constructor();
1340 catch (failed_constructor &err) {
1341 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1342 _write_source.reset();
1346 _write_source->set_allow_remove_if_empty (true);
1352 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1354 if (!recordable()) {
1358 if (_write_source && mark_write_complete) {
1359 _write_source->mark_streaming_write_completed ();
1362 use_new_write_source (0);
1364 if (record_enabled()) {
1365 //_capturing_sources.push_back (_write_source);
1370 MidiDiskstream::rename_write_sources ()
1372 if (_write_source != 0) {
1373 _write_source->set_source_name (_name, destructive());
1374 /* XXX what to do if this fails ? */
1380 MidiDiskstream::set_block_size (nframes_t nframes)
1385 MidiDiskstream::allocate_temporary_buffers ()
1390 MidiDiskstream::monitor_input (bool yn)
1393 _source_port->request_monitor_input (yn);
1395 cerr << "MidiDiskstream NO SOURCE PORT TO MONITOR\n";
1399 MidiDiskstream::set_align_style_from_io ()
1401 bool have_physical = false;
1407 get_input_sources ();
1409 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1410 have_physical = true;
1413 if (have_physical) {
1414 set_align_style (ExistingMaterial);
1416 set_align_style (CaptureTime);
1422 MidiDiskstream::playback_buffer_load () const
1424 return (float) ((double) _playback_buf->read_space()/
1425 (double) _playback_buf->capacity());
1429 MidiDiskstream::capture_buffer_load () const
1431 return (float) ((double) _capture_buf->write_space()/
1432 (double) _capture_buf->capacity());
1437 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1442 /** Writes playback events in the given range to \a dst, translating time stamps
1443 * so that an event at \a start has time = 0
1446 MidiDiskstream::get_playback(MidiBuffer& dst, nframes_t start, nframes_t end)
1449 assert(dst.size() == 0);
1451 // Reverse. ... We just don't do reverse, ok? Back off.
1456 // Translates stamps to be relative to start
1457 _playback_buf->read(dst, start, end);