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.
18 $Id: diskstream.cc 567 2006-06-07 14:54:12Z trutkin $
34 #include <pbd/error.h>
35 #include <pbd/basename.h>
36 #include <glibmm/thread.h>
37 #include <pbd/xml++.h>
38 #include <pbd/memento_command.h>
40 #include <ardour/ardour.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/midi_diskstream.h>
43 #include <ardour/utils.h>
44 #include <ardour/configuration.h>
45 #include <ardour/smf_source.h>
46 #include <ardour/destructive_filesource.h>
47 #include <ardour/send.h>
48 #include <ardour/midi_playlist.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)
64 //, _current_playback_buffer(0)
65 //, _current_capture_buffer(0)
66 //, _playback_wrap_buffer(0)
67 //, _capture_wrap_buffer(0)
70 , _capture_transition_buf(0)
71 , _last_flush_frame(0)
73 /* prevent any write sources from being created */
82 assert(!destructive());
83 DiskstreamCreated (this); /* EMIT SIGNAL */
86 MidiDiskstream::MidiDiskstream (Session& sess, const XMLNode& node)
87 : Diskstream(sess, node)
90 //, _current_playback_buffer(0)
91 //, _current_capture_buffer(0)
92 //, _playback_wrap_buffer(0)
93 //, _capture_wrap_buffer(0)
96 , _capture_transition_buf(0)
97 , _last_flush_frame(0)
102 if (set_state (node)) {
103 in_set_state = false;
104 throw failed_constructor();
107 in_set_state = false;
110 use_destructive_playlist ();
113 DiskstreamCreated (this); /* EMIT SIGNAL */
117 MidiDiskstream::init (Diskstream::Flag f)
121 /* there are no channels at this point, so these
122 two calls just get speed_buffer_size and wrap_buffer
123 size setup without duplicating their code.
126 set_block_size (_session.get_block_size());
127 allocate_temporary_buffers ();
129 //_playback_wrap_buffer = new RawMidi[wrap_buffer_size];
130 //_capture_wrap_buffer = new RawMidi[wrap_buffer_size];
131 _playback_buf = new MidiRingBuffer (_session.diskstream_buffer_size());
132 _capture_buf = new MidiRingBuffer (_session.diskstream_buffer_size());
133 _capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
135 _n_channels = ChanCount(DataType::MIDI, 1);
137 assert(recordable());
140 MidiDiskstream::~MidiDiskstream ()
142 Glib::Mutex::Lock lm (state_lock);
146 MidiDiskstream::non_realtime_input_change ()
149 Glib::Mutex::Lock lm (state_lock);
151 if (input_change_pending == NoChange) {
155 if (input_change_pending & ConfigurationChanged) {
157 assert(_io->n_inputs() == _n_channels);
160 get_input_sources ();
161 set_capture_offset ();
163 if (first_input_change) {
164 set_align_style (_persistent_alignment_style);
165 first_input_change = false;
167 set_align_style_from_io ();
170 input_change_pending = NoChange;
173 /* reset capture files */
175 reset_write_sources (false);
177 /* now refill channel buffers */
179 if (speed() != 1.0f || speed() != -1.0f) {
180 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
183 seek (_session.transport_frame());
186 _last_flush_frame = _session.transport_frame();
190 MidiDiskstream::get_input_sources ()
192 uint32_t ni = _io->n_inputs().get(DataType::MIDI);
198 // This is all we do for now at least
201 _source_port = _io->midi_input(0);
203 /* I don't get it....
204 const char **connections = _io->input(0)->get_connections ();
206 if (connections == 0 || connections[0] == 0) {
209 // _source_port->disable_metering ();
215 _source_port = dynamic_cast<MidiPort*>(
216 _session.engine().get_port_by_name (connections[0]) );
225 MidiDiskstream::find_and_use_playlist (const string& name)
228 MidiPlaylist* playlist;
230 if ((pl = _session.playlist_by_name (name)) == 0) {
231 playlist = new MidiPlaylist(_session, name);
235 if ((playlist = dynamic_cast<MidiPlaylist*> (pl)) == 0) {
236 error << string_compose(_("MidiDiskstream: Playlist \"%1\" isn't a midi playlist"), name) << endmsg;
240 return use_playlist (playlist);
244 MidiDiskstream::use_playlist (Playlist* playlist)
246 assert(dynamic_cast<MidiPlaylist*>(playlist));
248 return Diskstream::use_playlist(playlist);
252 MidiDiskstream::use_new_playlist ()
255 MidiPlaylist* playlist;
257 if (!in_set_state && destructive()) {
262 newname = Playlist::bump_name (_playlist->name(), _session);
264 newname = Playlist::bump_name (_name, _session);
267 if ((playlist = new MidiPlaylist (_session, newname, hidden())) != 0) {
268 playlist->set_orig_diskstream_id (id());
269 return use_playlist (playlist);
276 MidiDiskstream::use_copy_playlist ()
282 if (_playlist == 0) {
283 error << string_compose(_("MidiDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
288 MidiPlaylist* playlist;
290 newname = Playlist::bump_name (_playlist->name(), _session);
292 if ((playlist = new MidiPlaylist (*midi_playlist(), newname)) != 0) {
293 playlist->set_orig_diskstream_id (id());
294 return use_playlist (playlist);
300 /** Overloaded from parent to die horribly
303 MidiDiskstream::set_destructive (bool yn)
305 assert( ! destructive());
310 MidiDiskstream::check_record_status (jack_nframes_t transport_frame, jack_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 (!_session.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 (_session.get_punch_in()) {
393 first_recordable_frame += _roll_delay;
395 capture_start_frame -= _roll_delay;
401 if (_flags & Recordable) {
402 RingBufferNPT<CaptureTransition>::rw_vector transvec;
403 _capture_transition_buf->get_write_vector(&transvec);
405 if (transvec.len[0] > 0) {
406 transvec.buf[0]->type = CaptureStart;
407 transvec.buf[0]->capture_val = capture_start_frame;
408 _capture_transition_buf->increment_write_ptr(1);
411 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
416 } else if (!record_enabled() || !can_record) {
420 last_recordable_frame = transport_frame + _capture_offset;
422 if (_alignment_style == ExistingMaterial) {
423 last_recordable_frame += _session.worst_output_latency();
425 last_recordable_frame += _roll_delay;
429 last_possibly_recording = possibly_recording;
433 MidiDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
435 // FIXME: waay too much code to duplicate (AudioDiskstream::process)
437 jack_nframes_t rec_offset = 0;
438 jack_nframes_t rec_nframes = 0;
439 bool nominally_recording;
440 bool re = record_enabled ();
441 bool collect_playback = false;
443 /*_current_capture_buffer = 0;
444 _current_playback_buffer = 0;*/
446 /* if we've already processed the frames corresponding to this call,
447 just return. this allows multiple routes that are taking input
448 from this diskstream to call our ::process() method, but have
449 this stuff only happen once. more commonly, it allows both
450 the AudioTrack that is using this AudioDiskstream *and* the Session
451 to call process() without problems.
458 check_record_status (transport_frame, nframes, can_record);
460 nominally_recording = (can_record && re);
467 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
468 must always be called as a pair. The only exception is if this function
469 returns a non-zero value, in which case, ::commit should not be called.
472 // If we can't take the state lock return.
473 if (!state_lock.trylock()) {
477 adjust_capture_position = 0;
479 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
482 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
489 case OverlapInternal:
490 /* ---------- recrange
493 rec_nframes = nframes;
498 /* |--------| recrange
501 rec_nframes = transport_frame + nframes - first_recordable_frame;
503 rec_offset = first_recordable_frame - transport_frame;
508 /* |--------| recrange
511 rec_nframes = last_recordable_frame - transport_frame;
515 case OverlapExternal:
516 /* |--------| recrange
517 -------------- transrange
519 rec_nframes = last_recordable_frame - last_recordable_frame;
520 rec_offset = first_recordable_frame - transport_frame;
524 if (rec_nframes && !was_recording) {
525 capture_captured = 0;
526 was_recording = true;
531 if (can_record && !_last_capture_regions.empty()) {
532 _last_capture_regions.clear ();
535 if (nominally_recording || rec_nframes) {
537 assert(_source_port);
539 // Pump entire port buffer into the ring buffer (FIXME!)
540 _capture_buf->write(_source_port->get_midi_buffer(), transport_frame);
542 // FIXME: hackitty hack, don't come back
543 //_write_source->ViewDataRangeReady (_write_source->length(), rec_nframes); /* EMIT SIGNAL */
545 for (size_t i=0; i < _source_port->size(); ++i) {
546 cerr << "DISKSTREAM GOT EVENT(1) " << i << "!!\n";
549 if (_source_port->size() == 0)
550 cerr << "No events :/ (1)\n";
555 finish_capture (rec_monitors_input);
562 /* XXX XXX XXX XXX XXX XXX XXX XXX */
563 /* data will be written to disk */
565 adjust_capture_position = rec_nframes;
567 } else if (nominally_recording) {
569 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
571 // Ummm.. well, I suppose we'll just hang out for a bit?
573 playback_distance = nframes;
577 collect_playback = true;
580 if (collect_playback) {
582 /* we're doing playback */
584 jack_nframes_t necessary_samples;
586 /* no varispeed playback if we're recording, because the output .... TBD */
588 if (rec_nframes == 0 && _actual_speed != 1.0f) {
589 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
591 necessary_samples = nframes;
594 // XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX
595 // Write into playback buffer here, and whatnot
605 /* we're exiting with failure, so ::commit will not
606 be called. unlock the state lock.
620 MidiDiskstream::commit (jack_nframes_t nframes)
622 bool need_butler = false;
624 if (_actual_speed < 0.0) {
625 playback_sample -= playback_distance;
627 playback_sample += playback_distance;
630 /* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
633 _playback_buf->increment_read_ptr (playback_distance);
635 if (adjust_capture_position) {
636 _capture_buf->increment_write_ptr (adjust_capture_position);
639 if (adjust_capture_position != 0) {
640 capture_captured += adjust_capture_position;
641 adjust_capture_position = 0;
645 need_butler = _playback_buf->write_space() >= _playback_buf->capacity() / 2;
647 need_butler = _playback_buf->write_space() >= disk_io_chunk_frames
648 || _capture_buf->read_space() >= disk_io_chunk_frames;
659 MidiDiskstream::set_pending_overwrite (bool yn)
661 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
663 pending_overwrite = yn;
665 overwrite_frame = playback_sample;
666 //overwrite_offset = channels.front().playback_buf->get_read_ptr();
670 MidiDiskstream::overwrite_existing_buffers ()
676 MidiDiskstream::seek (jack_nframes_t frame, bool complete_refill)
678 Glib::Mutex::Lock lm (state_lock);
681 _playback_buf->reset();
682 _capture_buf->reset();
684 playback_sample = frame;
686 _last_flush_frame = frame;
688 if (complete_refill) {
689 while ((ret = do_refill_with_alloc ()) > 0) ;
691 ret = do_refill_with_alloc ();
698 MidiDiskstream::can_internal_playback_seek (jack_nframes_t distance)
700 if (_playback_buf->read_space() < distance) {
708 MidiDiskstream::internal_playback_seek (jack_nframes_t distance)
710 first_recordable_frame += distance;
711 playback_sample += distance;
716 /** @a start is set to the new frame position (TIME) read up to */
718 MidiDiskstream::read (jack_nframes_t& start, jack_nframes_t dur, bool reversed)
720 jack_nframes_t this_read = 0;
722 jack_nframes_t loop_end = 0;
723 jack_nframes_t loop_start = 0;
724 jack_nframes_t loop_length = 0;
728 /* Make the use of a Location atomic for this read operation.
730 Note: Locations don't get deleted, so all we care about
731 when I say "atomic" is that we are always pointing to
732 the same one and using a start/length values obtained
736 if ((loc = loop_location) != 0) {
737 loop_start = loc->start();
738 loop_end = loc->end();
739 loop_length = loop_end - loop_start;
742 /* if we are looping, ensure that the first frame we read is at the correct
743 position within the loop.
746 if (loc && start >= loop_end) {
747 //cerr << "start adjusted from " << start;
748 start = loop_start + ((start - loop_start) % loop_length);
749 //cerr << "to " << start << endl;
751 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
756 /* take any loop into account. we can't read past the end of the loop. */
758 if (loc && (loop_end - start < dur)) {
759 this_read = loop_end - start;
760 //cerr << "reloop true: thisread: " << this_read << " dur: " << dur << endl;
767 if (this_read == 0) {
771 this_read = min(dur,this_read);
773 if (midi_playlist()->read (*_playback_buf, start, this_read) != this_read) {
774 error << string_compose(_("MidiDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
779 _read_data_count = _playlist->read_data_count();
783 cerr << "Reversed MIDI.. that's just crazy talk." << endl;
784 // Swap note ons with note offs here
788 /* if we read to the end of the loop, go back to the beginning */
804 MidiDiskstream::do_refill_with_alloc ()
810 MidiDiskstream::do_refill ()
813 size_t write_space = _playback_buf->write_space();
815 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
817 if (write_space == 0) {
821 /* if there are 2+ chunks of disk i/o possible for
822 this track, let the caller know so that it can arrange
823 for us to be called again, ASAP.
826 // FIXME: using disk_io_chunk_frames as an event count, not good
827 if (_playback_buf->write_space() >= (_slaved?3:2) * disk_io_chunk_frames) {
831 /* if we're running close to normal speed and there isn't enough
832 space to do disk_io_chunk_frames of I/O, then don't bother.
834 at higher speeds, just do it because the sync between butler
835 and audio thread may not be good enough.
838 if ((write_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
839 cerr << "No refill 1\n";
843 /* when slaved, don't try to get too close to the read pointer. this
844 leaves space for the buffer reversal to have something useful to
848 if (_slaved && write_space < (_playback_buf->capacity() / 2)) {
849 cerr << "No refill 2\n";
854 cerr << "No refill 3 (reverse)\n";
858 if (file_frame == max_frames) {
859 //cerr << "No refill 4 (EOF)\n";
861 /* at end: nothing to do */
868 if (file_frame > max_frames - total_space) {
870 /* to close to the end: read what we can, and zero fill the rest */
872 zero_fill = total_space - (max_frames - file_frame);
873 total_space = max_frames - file_frame;
881 assert(_playback_buf->write_space() > 0); // ... have something to write to, and
882 assert(file_frame <= max_frames); // ... something to write
884 // So (read it, then) write it:
886 jack_nframes_t file_frame_tmp = file_frame;
887 jack_nframes_t to_read = min(disk_io_chunk_frames, (max_frames - file_frame));
889 // FIXME: read count?
890 if (read (file_frame_tmp, to_read, reversed)) {
895 file_frame = file_frame_tmp;
902 /** Flush pending data to disk.
904 * Important note: this function will write *AT MOST* disk_io_chunk_frames
905 * of data to disk. it will never write more than that. If it writes that
906 * much and there is more than that waiting to be written, it will return 1,
907 * otherwise 0 on success or -1 on failure.
909 * If there is less than disk_io_chunk_frames to be written, no data will be
910 * written at all unless @a force_flush is true.
913 MidiDiskstream::do_flush (Session::RunContext context, bool force_flush)
917 // FIXME: I'd be lying if I said I knew what this thing was
918 //RingBufferNPT<CaptureTransition>::rw_vector transvec;
919 jack_nframes_t total;
921 _write_data_count = 0;
923 if (_last_flush_frame > _session.transport_frame()) {
924 _last_flush_frame = _session.transport_frame();
927 total = _session.transport_frame() - _last_flush_frame;
930 // FIXME: put this condition back in! (removed for testing)
931 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
932 //cerr << "MDS - no flush 1\n";
936 /* if there are 2+ chunks of disk i/o possible for
937 this track, let the caller know so that it can arrange
938 for us to be called again, ASAP.
940 if we are forcing a flush, then if there is* any* extra
941 work, let the caller know.
943 if we are no longer recording and there is any extra work,
944 let the caller know too.
947 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
951 //to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
952 to_write = disk_io_chunk_frames;
954 assert(!destructive());
956 if ((!_write_source) || _write_source->write (*_capture_buf, to_write) != to_write) {
957 //cerr << "MDS - no flush 2\n";
958 error << string_compose(_("MidiDiskstream %1: cannot write to disk"), _id) << endmsg;
961 _last_flush_frame = _session.transport_frame();
962 //cerr << "MDS - flushed\n";
965 //(*chan).curr_capture_cnt += to_write;
973 MidiDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
975 uint32_t buffer_position;
976 bool more_work = true;
978 MidiRegion* region = 0;
979 jack_nframes_t total_capture;
980 MidiRegion::SourceList srcs;
981 MidiRegion::SourceList::iterator src;
982 vector<CaptureInfo*>::iterator ci;
983 bool mark_write_completed = false;
985 finish_capture (true);
987 /* butler is already stopped, but there may be work to do
988 to flush remaining data to disk.
991 while (more_work && !err) {
992 switch (do_flush (Session::TransportContext, true)) {
999 error << string_compose(_("MidiDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1004 /* XXX is there anything we can do if err != 0 ? */
1005 Glib::Mutex::Lock lm (capture_info_lock);
1007 if (capture_info.empty()) {
1011 if (abort_capture) {
1013 list<Source*>* deletion_list = new list<Source*>;
1015 if (_write_source) {
1016 _write_source->mark_for_remove ();
1017 _write_source->release ();
1019 deletion_list->push_back (_write_source);
1024 /* new source set up in "out" below */
1026 if (!deletion_list->empty()) {
1027 DeleteSources (deletion_list);
1029 delete deletion_list;
1034 assert(_write_source);
1036 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1037 total_capture += (*ci)->frames;
1040 /* figure out the name for this take */
1042 SMFSource* s = _write_source;
1048 cerr << "MidiDiskstream: updating source after capture\n";
1049 s->update_header (capture_info.front()->start, when, twhen);
1051 s->set_captured_for (_name);
1055 /* Register a new region with the Session that
1056 describes the entire source. Do this first
1057 so that any sub-regions will obviously be
1058 children of this one (later!)
1061 assert(_write_source);
1062 region = new MidiRegion (srcs, _write_source->last_capture_start_frame(), total_capture,
1063 region_name_from_path (_write_source->name()),
1064 0, Region::Flag (Region::DefaultFlags|Region::Automatic|Region::WholeFile));
1066 region->special_set_position (capture_info.front()->start);
1070 catch (failed_constructor& err) {
1071 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1075 _last_capture_regions.push_back (region);
1077 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1079 XMLNode &before = _playlist->get_state();
1080 _playlist->freeze ();
1082 for (buffer_position = _write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1085 _session.region_name (region_name, _write_source->name(), false);
1087 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1090 region = new MidiRegion (srcs, buffer_position, (*ci)->frames, region_name);
1093 catch (failed_constructor& err) {
1094 error << _("MidiDiskstream: could not create region for captured audio!") << endmsg;
1095 continue; /* XXX is this OK? */
1098 _last_capture_regions.push_back (region);
1100 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1102 i_am_the_modifier++;
1103 _playlist->add_region (*region, (*ci)->start);
1104 i_am_the_modifier--;
1106 buffer_position += (*ci)->frames;
1110 XMLNode &after = _playlist->get_state();
1111 _session.add_command (new MementoCommand<Playlist>(*_playlist, before, after));
1113 mark_write_completed = true;
1115 reset_write_sources (mark_write_completed);
1119 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1123 capture_info.clear ();
1124 capture_start_frame = 0;
1128 MidiDiskstream::finish_capture (bool rec_monitors_input)
1130 was_recording = false;
1132 if (capture_captured == 0) {
1136 // Why must we destroy?
1137 assert(!destructive());
1139 CaptureInfo* ci = new CaptureInfo;
1141 ci->start = capture_start_frame;
1142 ci->frames = capture_captured;
1144 /* XXX theoretical race condition here. Need atomic exchange ?
1145 However, the circumstances when this is called right
1146 now (either on record-disable or transport_stopped)
1147 mean that no actual race exists. I think ...
1148 We now have a capture_info_lock, but it is only to be used
1149 to synchronize in the transport_stop and the capture info
1150 accessors, so that invalidation will not occur (both non-realtime).
1153 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1155 capture_info.push_back (ci);
1156 capture_captured = 0;
1160 MidiDiskstream::set_record_enabled (bool yn)
1162 if (!recordable() || !_session.record_enabling_legal()) {
1166 assert(!destructive());
1168 if (yn && _source_port == 0) {
1170 /* pick up connections not initiated *from* the IO object
1171 we're associated with.
1174 get_input_sources ();
1177 /* yes, i know that this not proof against race conditions, but its
1178 good enough. i think.
1181 if (record_enabled() != yn) {
1183 engage_record_enable ();
1185 disengage_record_enable ();
1191 MidiDiskstream::engage_record_enable ()
1193 bool rolling = _session.transport_speed() != 0.0f;
1195 g_atomic_int_set (&_record_enabled, 1);
1197 if (Config->get_use_hardware_monitoring() && _source_port) {
1198 _source_port->request_monitor_input (!(_session.get_auto_input() && rolling));
1201 RecordEnableChanged (); /* EMIT SIGNAL */
1205 MidiDiskstream::disengage_record_enable ()
1207 g_atomic_int_set (&_record_enabled, 0);
1208 if (Config->get_use_hardware_monitoring()) {
1210 _source_port->request_monitor_input (false);
1214 RecordEnableChanged (); /* EMIT SIGNAL */
1218 MidiDiskstream::get_state ()
1220 XMLNode* node = new XMLNode ("MidiDiskstream");
1222 LocaleGuard lg (X_("POSIX"));
1224 snprintf (buf, sizeof(buf), "0x%x", _flags);
1225 node->add_property ("flags", buf);
1227 node->add_property ("playlist", _playlist->name());
1229 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1230 node->add_property ("speed", buf);
1232 node->add_property("name", _name);
1234 node->add_property("id", buf);
1236 if (_write_source && _session.get_record_enabled()) {
1238 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1239 XMLNode* cs_grandchild;
1241 cs_grandchild = new XMLNode (X_("file"));
1242 cs_grandchild->add_property (X_("path"), _write_source->path());
1243 cs_child->add_child_nocopy (*cs_grandchild);
1245 /* store the location where capture will start */
1249 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1250 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1252 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1255 cs_child->add_property (X_("at"), buf);
1256 node->add_child_nocopy (*cs_child);
1260 node->add_child_copy (*_extra_xml);
1267 MidiDiskstream::set_state (const XMLNode& node)
1269 const XMLProperty* prop;
1270 XMLNodeList nlist = node.children();
1271 XMLNodeIterator niter;
1272 uint32_t nchans = 1;
1273 XMLNode* capture_pending_node = 0;
1274 LocaleGuard lg (X_("POSIX"));
1276 in_set_state = true;
1278 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1279 /*if ((*niter)->name() == IO::state_node_name) {
1280 deprecated_io_node = new XMLNode (**niter);
1282 assert ((*niter)->name() != IO::state_node_name);
1284 if ((*niter)->name() == X_("CapturingSources")) {
1285 capture_pending_node = *niter;
1289 /* prevent write sources from being created */
1291 in_set_state = true;
1293 if ((prop = node.property ("name")) != 0) {
1294 _name = prop->value();
1297 if ((prop = node.property ("id")) != 0) {
1298 _id = prop->value ();
1301 if ((prop = node.property ("flags")) != 0) {
1302 _flags = strtol (prop->value().c_str(), 0, 0);
1305 if ((prop = node.property ("channels")) != 0) {
1306 nchans = atoi (prop->value().c_str());
1309 if ((prop = node.property ("playlist")) == 0) {
1314 bool had_playlist = (_playlist != 0);
1316 if (find_and_use_playlist (prop->value())) {
1320 if (!had_playlist) {
1321 _playlist->set_orig_diskstream_id (_id);
1324 if (capture_pending_node) {
1325 use_pending_capture_data (*capture_pending_node);
1330 if ((prop = node.property ("speed")) != 0) {
1331 double sp = atof (prop->value().c_str());
1333 if (realtime_set_speed (sp, false)) {
1334 non_realtime_set_speed ();
1338 in_set_state = false;
1340 /* make sure this is clear before we do anything else */
1343 //_capturing_source = 0;
1345 /* write sources are handled when we handle the input set
1346 up of the IO that owns this DS (::non_realtime_input_change())
1349 in_set_state = false;
1355 MidiDiskstream::use_new_write_source (uint32_t n)
1357 if (!recordable()) {
1363 if (_write_source) {
1365 if (SMFSource::is_empty (_write_source->path())) {
1366 _write_source->mark_for_remove ();
1367 _write_source->release();
1368 delete _write_source;
1370 _write_source->release();
1376 _write_source = dynamic_cast<SMFSource*>(_session.create_midi_source_for_session (*this));
1377 if (!_write_source) {
1378 throw failed_constructor();
1382 catch (failed_constructor &err) {
1383 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1388 _write_source->use ();
1389 _write_source->set_allow_remove_if_empty (true);
1395 MidiDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1397 if (!recordable()) {
1401 if (_write_source && mark_write_complete) {
1402 _write_source->mark_streaming_write_completed ();
1405 if (!_write_source) {
1406 use_new_write_source ();
1411 MidiDiskstream::rename_write_sources ()
1413 if (_write_source != 0) {
1414 _write_source->set_name (_name, destructive());
1415 /* XXX what to do if this fails ? */
1421 MidiDiskstream::set_block_size (jack_nframes_t nframes)
1426 MidiDiskstream::allocate_temporary_buffers ()
1431 MidiDiskstream::monitor_input (bool yn)
1434 _source_port->request_monitor_input (yn);
1436 cerr << "MidiDiskstream NO SOURCE PORT TO MONITOR\n";
1440 MidiDiskstream::set_align_style_from_io ()
1442 bool have_physical = false;
1448 get_input_sources ();
1450 if (_source_port && _source_port->flags() & JackPortIsPhysical) {
1451 have_physical = true;
1454 if (have_physical) {
1455 set_align_style (ExistingMaterial);
1457 set_align_style (CaptureTime);
1463 MidiDiskstream::playback_buffer_load () const
1465 return (float) ((double) _playback_buf->read_space()/
1466 (double) _playback_buf->capacity());
1470 MidiDiskstream::capture_buffer_load () const
1472 return (float) ((double) _capture_buf->write_space()/
1473 (double) _capture_buf->capacity());
1478 MidiDiskstream::use_pending_capture_data (XMLNode& node)
1483 /** Writes playback events in the given range to dst, translating time stamps
1484 * so that an event at start has time = 0
1487 MidiDiskstream::get_playback(MidiBuffer& dst, jack_nframes_t start, jack_nframes_t end)
1490 assert(dst.size() == 0);
1492 // I think this happens with reverse varispeed? maybe?
1498 cerr << "MIDI Diskstream pretending to read" << endl;
1503 const char note = rand()%30 + 30;
1516 ev.time = (end - start) / 2;
1523 _playback_buf->read(dst, start, end);
1525 // Translate time stamps to be relative to the start of this cycle
1526 for (size_t i=0; i < dst.size(); ++i) {
1527 assert(dst[i].time >= start);
1528 assert(dst[i].time <= end);
1529 cerr << "Translating event stamp " << dst[i].time << " to ";
1530 dst[i].time -= start;
1531 cerr << dst[i].time << endl;