diff options
Diffstat (limited to 'libs')
-rw-r--r-- | libs/ardour/ardour/midi_diskstream.h | 25 | ||||
-rw-r--r-- | libs/ardour/ardour/midi_ring_buffer.h | 156 | ||||
-rw-r--r-- | libs/ardour/ardour/types.h | 6 | ||||
-rw-r--r-- | libs/ardour/enums.cc | 6 | ||||
-rw-r--r-- | libs/ardour/midi_buffer.cc | 5 | ||||
-rw-r--r-- | libs/ardour/midi_diskstream.cc | 27 | ||||
-rw-r--r-- | libs/ardour/midi_model.cc | 3 | ||||
-rw-r--r-- | libs/ardour/midi_track.cc | 22 | ||||
-rw-r--r-- | libs/ardour/smf_source.cc | 6 | ||||
-rw-r--r-- | libs/midi++2/jack_midiport.cc | 31 |
10 files changed, 160 insertions, 127 deletions
diff --git a/libs/ardour/ardour/midi_diskstream.h b/libs/ardour/ardour/midi_diskstream.h index f52f66dc6d..3e8e9c4156 100644 --- a/libs/ardour/ardour/midi_diskstream.h +++ b/libs/ardour/ardour/midi_diskstream.h @@ -90,28 +90,27 @@ class MidiDiskstream : public Diskstream void set_note_mode (NoteMode m); - void set_channel_mask(uint16_t channel_mask) { - _playback_buf->set_channel_mask(channel_mask); - _capture_buf->set_channel_mask(channel_mask); - } - uint16_t get_channel_mask() { uint16_t playback_mask = _playback_buf->get_channel_mask(); +#ifndef NDEBUG uint16_t capture_mask = _capture_buf->get_channel_mask(); assert(playback_mask == capture_mask); +#endif return playback_mask; } - void set_force_channel(int8_t force_channel) { - _playback_buf->set_force_channel(force_channel); - _capture_buf->set_force_channel(force_channel); + void set_channel_mode(ChannelMode mode, uint16_t mask) { + _playback_buf->set_channel_mode(mode, mask); + _capture_buf->set_channel_mode(mode, mask); } - int8_t get_force_channel() { - int8_t playback_force_channel = _playback_buf->get_force_channel(); - int8_t capture_force_channel = _capture_buf->get_force_channel(); - assert(playback_force_channel == capture_force_channel); - return playback_force_channel; + ChannelMode get_channel_mode() { + ChannelMode playback_mode = _playback_buf->get_channel_mode(); +#ifndef NDEBUG + ChannelMode capture_mode = _capture_buf->get_channel_mode(); + assert(playback_mode == capture_mode); +#endif + return playback_mode; } protected: diff --git a/libs/ardour/ardour/midi_ring_buffer.h b/libs/ardour/ardour/midi_ring_buffer.h index 680e2a7b61..ea0013fe46 100644 --- a/libs/ardour/ardour/midi_ring_buffer.h +++ b/libs/ardour/ardour/midi_ring_buffer.h @@ -75,7 +75,7 @@ public: if (w > r) { return ((r - w + _size) % _size) - 1; - } else if(w < r) { + } else if (w < r) { return (r - w) - 1; } else { return _size - 1; @@ -101,15 +101,17 @@ public: size_t read(size_t size, T* dst); bool full_read(size_t size, T* dst); + + bool skip(size_t size); void write(size_t size, const T* src); protected: - mutable gint _write_ptr; - mutable gint _read_ptr; + mutable int _write_ptr; + mutable int _read_ptr; size_t _size; ///< Size (capacity) in bytes - T* _buf; ///< size, event, size, event... + T* _buf; ///< size, event, size, event... }; @@ -130,7 +132,7 @@ MidiRingBufferBase<T>::peek(size_t size, T* dst) : _size - priv_read_ptr; memcpy(dst, &_buf[priv_read_ptr], read_size); - + return read_size; } @@ -168,7 +170,7 @@ MidiRingBufferBase<T>::read(size_t size, T* dst) : _size - priv_read_ptr; memcpy(dst, &_buf[priv_read_ptr], read_size); - + g_atomic_int_set(&_read_ptr, (priv_read_ptr + read_size) % _size); return read_size; @@ -192,6 +194,23 @@ MidiRingBufferBase<T>::full_read(size_t size, T* dst) template<typename T> +bool +MidiRingBufferBase<T>::skip(size_t size) +{ + if (read_space() < size) { + std::cerr << "WARNING: Attempt to skip past end of MIDI ring buffer" << std::endl; + return false; + } + + const size_t priv_read_ptr = g_atomic_int_get(&_read_ptr); + g_atomic_int_set(&_read_ptr, (priv_read_ptr + size) % _size); + + return true; +} + + + +template<typename T> inline void MidiRingBufferBase<T>::write(size_t size, const T* src) { @@ -199,14 +218,14 @@ MidiRingBufferBase<T>::write(size_t size, const T* src) if (priv_write_ptr + size <= _size) { memcpy(&_buf[priv_write_ptr], src, size); - g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size); + g_atomic_int_set(&_write_ptr, (priv_write_ptr + size) % _size); } else { const size_t this_size = _size - priv_write_ptr; assert(this_size < size); assert(priv_write_ptr + this_size <= _size); memcpy(&_buf[priv_write_ptr], src, this_size); memcpy(&_buf[0], src+this_size, size - this_size); - g_atomic_int_set(&_write_ptr, size - this_size); + g_atomic_int_set(&_write_ptr, size - this_size); } } @@ -223,11 +242,10 @@ MidiRingBufferBase<T>::write(size_t size, const T* src) */ class MidiRingBuffer : public MidiRingBufferBase<Byte> { public: - /** @param size Size in bytes. */ MidiRingBuffer(size_t size) - : MidiRingBufferBase<Byte>(size), _channel_mask(0xFFFF), _force_channel(-1) + : MidiRingBufferBase<Byte>(size), _channel_mask(0x0000FFFF) {} size_t write(double time, size_t size, const Byte* buf); @@ -238,21 +256,24 @@ public: size_t read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t offset=0); - /** - * @param channel_mask each bit in channel_mask represents a midi channel: bit 0 = channel 0, - * bit 1 = channel 1 etc. the read and write methods will only allow - * events to pass, whose channel bit is 1. + /** Set the channel filtering mode. + * @param mask If mode is FilterChannels, each bit represents a midi channel: + * bit 0 = channel 0, bit 1 = channel 1 etc. the read and write methods will only + * process events whose channel bit is 1. + * If mode is ForceChannel, mask is simply a channel number which all events will + * be forced to while reading. */ - void set_channel_mask(uint16_t channel_mask) { g_atomic_int_set(&_channel_mask, channel_mask); } - uint16_t get_channel_mask() { return g_atomic_int_get(&_channel_mask); } + void set_channel_mode(ChannelMode mode, uint16_t mask) { + g_atomic_int_set(&_channel_mask, ((uint16_t)mode << 16) | mask); + } + + ChannelMode get_channel_mode() const { + return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0xFFFF0000) >> 16); + } - /** - * @param channel if negative, forcing channels is deactivated and filtering channels - * is activated, if positive, the LSB of channel is the channel number - * of the channel all events are forced into and filtering is deactivated - */ - void set_force_channel(int8_t channel) { g_atomic_int_set(&_force_channel, channel); } - int8_t get_force_channel() { return g_atomic_int_get(&_force_channel); } + uint16_t get_channel_mask() const { + return static_cast<ChannelMode>((g_atomic_int_get(&_channel_mask) & 0x0000FFFF)); + } protected: inline bool is_channel_event(Byte event_type_byte) { @@ -263,8 +284,7 @@ protected: } private: - volatile uint16_t _channel_mask; - volatile int8_t _force_channel; + volatile uint32_t _channel_mask; // 16 bits mode, 16 bits mask }; @@ -308,17 +328,18 @@ MidiRingBuffer::read_contents(size_t size, Byte* buf) inline size_t MidiRingBuffer::write(double time, size_t size, const Byte* buf) { - //printf("MRB - write %#X %d %d with time %lf\n", - // buf[0], buf[1], buf[2], time); + /*fprintf(stderr, "MRB %p write (t = %f) ", this, time); + for (size_t i = 0; i < size; ++i) + fprintf(stderr, "%X", (char)buf[i]); + fprintf(stderr, "\n");*/ assert(size > 0); - if(is_channel_event(buf[0]) && (g_atomic_int_get(&_force_channel) < 0)) { - // filter events for channels - Byte channel_nr = buf[0] & 0x0F; - if( !(g_atomic_int_get(&_channel_mask) & (1L << channel_nr)) ) { + // Don't write event if it doesn't match channel filter + if (is_channel_event(buf[0]) && get_channel_mode() == FilterChannels) { + Byte channel = buf[0] & 0x0F; + if ( !(get_channel_mask() & (1L << channel)) ) return 0; - } } if (write_space() < (sizeof(double) + sizeof(size_t) + size)) { @@ -326,11 +347,11 @@ MidiRingBuffer::write(double time, size_t size, const Byte* buf) } else { MidiRingBufferBase<Byte>::write(sizeof(double), (Byte*)&time); MidiRingBufferBase<Byte>::write(sizeof(size_t), (Byte*)&size); - if(is_channel_event(buf[0]) && (g_atomic_int_get(&_force_channel) >= 0)) { + if (is_channel_event(buf[0]) && get_channel_mode() == ForceChannel) { assert(size == 3); Byte tmp_buf[3]; - //force event into channel - tmp_buf[0] = (buf[0] & 0xF0) | (g_atomic_int_get(&_force_channel) & 0x0F); + // Force event to channel + tmp_buf[0] = (buf[0] & 0xF0) | (get_channel_mask() & 0x0F); tmp_buf[1] = buf[1]; tmp_buf[2] = buf[2]; MidiRingBufferBase<Byte>::write(size, tmp_buf); @@ -354,22 +375,23 @@ MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t if (read_space() == 0) return 0; - MIDI::Event ev; + double ev_time; + uint32_t ev_size; size_t count = 0; - //printf("MRB - read %u .. %u + %u\n", start, end, offset); + //printf("---- MRB read %u .. %u + %u\n", start, end, offset); while (read_space() > sizeof(double) + sizeof(size_t)) { - full_peek(sizeof(double), (Byte*)&ev.time()); + full_peek(sizeof(double), (Byte*)&ev_time); - if (ev.time() > end) + if (ev_time > end) break; - bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)&ev.time()); + bool success = MidiRingBufferBase<Byte>::full_read(sizeof(double), (Byte*)&ev_time); if (success) { - success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev.size()); + success = MidiRingBufferBase<Byte>::full_read(sizeof(size_t), (Byte*)&ev_size); } if (!success) { @@ -377,51 +399,47 @@ MidiRingBuffer::read(MidiBuffer& dst, nframes_t start, nframes_t end, nframes_t continue; } - Byte first_event_byte; - if(success) { - success = full_peek(sizeof(Byte), &first_event_byte); - } - - // could this ever happen??? - if (!success) { - std::cerr << "MRB: PEEK ERROR (first event byte)" << std::endl; - continue; - } + Byte status; + success = full_peek(sizeof(Byte), &status); + assert(success); // If this failed, buffer is corrupt, all hope is lost - // filter events for channels - // filtering is only active, if forcing channels is not active - if(is_channel_event(first_event_byte) && (g_atomic_int_get(&_force_channel) < 0)) { - Byte channel_nr = first_event_byte & 0x0F; - if( !(g_atomic_int_get(&_channel_mask) & (1L << channel_nr)) ) { + // Ignore event if it doesn't match channel filter + if (is_channel_event(status) && get_channel_mode() == FilterChannels) { + const Byte channel = status & 0x0F; + if ( !(get_channel_mask() & (1L << channel)) ) { + skip(ev_size); // Advance read pointer to next event continue; } } - if (ev.time() >= start) { - ev.time() -= start; - // TODO: Right now there come MIDI Events with empty buffer - if(!ev.buffer()) { - std::cerr << "MidiRingBuffer::read WARNING: Skipping MIDI Event with NULL buffer pointer " - << " and length " << int(ev.size()) << std::endl; + if (ev_time >= start) { + + /*std::cerr << "MRB " << this << " - Reading event, time = " + << ev_time << " - " << start << " => " << ev_time - start + << ", size = " << ev_size << std::endl;*/ + + ev_time -= start; + + Byte* write_loc = dst.reserve(ev_time, ev_size); + if (write_loc == NULL) { + std::cerr << "MRB: Unable to reserve space in buffer, event skipped"; continue; } - Byte* write_loc = dst.reserve(ev.time(), ev.size()); - - success = MidiRingBufferBase<Byte>::full_read(ev.size(), write_loc); + success = MidiRingBufferBase<Byte>::full_read(ev_size, write_loc); if (success) { - if(is_channel_event(first_event_byte) && (g_atomic_int_get(&_force_channel) >= 0)) { - write_loc[0] = (write_loc[0] & 0xF0) | (g_atomic_int_get(&_force_channel) & 0x0F); + if (is_channel_event(status) && get_channel_mode() == ForceChannel) { + write_loc[0] = (write_loc[0] & 0xF0) | (get_channel_mask() & 0x0F); } ++count; - //printf("MRB - read event at time %lf\n", ev.time()); + //printf("MRB - read event at time %lf\n", ev_time); } else { std::cerr << "MRB: READ ERROR (data)" << std::endl; } } else { - printf("MRB - SKIPPING EVENT AT TIME %f\n", ev.time()); + printf("MRB (start %u) - Skipping event at (too early) time %f\n", start, ev_time); } } diff --git a/libs/ardour/ardour/types.h b/libs/ardour/ardour/types.h index 9195b1075c..55655e9e26 100644 --- a/libs/ardour/ardour/types.h +++ b/libs/ardour/ardour/types.h @@ -135,6 +135,12 @@ namespace ARDOUR { Sustained, Percussive }; + + enum ChannelMode { + AllChannels = 0, ///< Pass through all channel information unmodified + FilterChannels, ///< Ignore events on certain channels + ForceChannel ///< Force all events to a certain channel + }; enum EventTimeUnit { Frames, diff --git a/libs/ardour/enums.cc b/libs/ardour/enums.cc index d64d3a2f99..71b699396b 100644 --- a/libs/ardour/enums.cc +++ b/libs/ardour/enums.cc @@ -46,6 +46,7 @@ setup_enum_writer () MeterPoint _MeterPoint; TrackMode _TrackMode; NoteMode _NoteMode; + ChannelMode _ChannelMode; MeterFalloff _MeterFalloff; MeterHold _MeterHold; EditMode _EditMode; @@ -140,6 +141,11 @@ setup_enum_writer () REGISTER_ENUM (Sustained); REGISTER_ENUM (Percussive); REGISTER (_NoteMode); + + REGISTER_ENUM (AllChannels); + REGISTER_ENUM (FilterChannels); + REGISTER_ENUM (ForceChannel); + REGISTER (_ChannelMode); REGISTER_ENUM (MeterFalloffOff); REGISTER_ENUM (MeterFalloffSlowest); diff --git a/libs/ardour/midi_buffer.cc b/libs/ardour/midi_buffer.cc index 042f9529ad..4628de4a6a 100644 --- a/libs/ardour/midi_buffer.cc +++ b/libs/ardour/midi_buffer.cc @@ -195,7 +195,10 @@ MidiBuffer::push_back(const jack_midi_event_t& ev) Byte* MidiBuffer::reserve(double time, size_t size) { - assert(size <= MAX_EVENT_SIZE); + if (size > MAX_EVENT_SIZE) { + cerr << "WARNING: Failed to reserve " << size << " bytes for event"; + return 0; + } if (_size == _capacity) return 0; diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index 6efa1abbae..acc8f8117a 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -1227,11 +1227,10 @@ MidiDiskstream::get_state () snprintf (buf, sizeof(buf), "0x%x", _flags); node->add_property ("flags", buf); + node->add_property("channel-mode", enum_2_string(get_channel_mode())); + snprintf (buf, sizeof(buf), "0x%x", get_channel_mask()); - node->add_property("channel_mask", buf); - - snprintf (buf, sizeof(buf), "%d", get_force_channel()); - node->add_property("force_channel", buf); + node->add_property("channel-mask", buf); node->add_property ("playlist", _playlist->name()); @@ -1310,19 +1309,21 @@ MidiDiskstream::set_state (const XMLNode& node) if ((prop = node.property ("flags")) != 0) { _flags = Flag (string_2_enum (prop->value(), _flags)); } - - if ((prop = node.property ("channel_mask")) != 0) { - unsigned int channel_mask; - sscanf (prop->value().c_str(), "0x%x", &channel_mask); - set_channel_mask(channel_mask); + + ChannelMode channel_mode = AllChannels; + if ((prop = node.property ("channel-mode")) != 0) { + channel_mode = ChannelMode (string_2_enum(prop->value(), channel_mode)); } - if ((prop = node.property ("force_channel")) != 0) { - int force_channel; - sscanf (prop->value().c_str(), "%d", &force_channel); - set_force_channel(force_channel); + unsigned int channel_mask = 0xFFFF; + if ((prop = node.property ("channel-mask")) != 0) { + sscanf (prop->value().c_str(), "0x%x", &channel_mask); + if (channel_mask & 0xFFFF) + warning << _("MidiDiskstream: XML property channel-mask out of range") << endmsg; } + set_channel_mode(channel_mode, channel_mask); + if ((prop = node.property ("channels")) != 0) { nchans = atoi (prop->value().c_str()); } diff --git a/libs/ardour/midi_model.cc b/libs/ardour/midi_model.cc index 01f0ecc436..2b8755b0f9 100644 --- a/libs/ardour/midi_model.cc +++ b/libs/ardour/midi_model.cc @@ -491,9 +491,6 @@ void MidiModel::append(const MIDI::Event& ev) write_lock(); _edited = true; - cerr << "MidiModel append event type: " - << hex << "0x" << (int)ev.type() << endl; - assert(_notes.empty() || ev.time() >= _notes.back()->time()); assert(_writing); diff --git a/libs/ardour/midi_track.cc b/libs/ardour/midi_track.cc index ab13ad410c..d22c2171e7 100644 --- a/libs/ardour/midi_track.cc +++ b/libs/ardour/midi_track.cc @@ -428,9 +428,6 @@ MidiTrack::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_fram passthru (start_frame, end_frame, nframes, offset, 0, (_meter_point == MeterInput)); } - // stop all sounds - midi_panic(); - return 0; } @@ -571,10 +568,8 @@ MidiTrack::process_output_buffers (BufferSet& bufs, IO::silence(nframes, offset); } else { - MidiBuffer& output_buf = bufs.get_midi(0); - // TODO this crashes: (sends events with NULL buffer pointer) - // Is this necessary anyway here? Dont know. - //write_controller_messages(output_buf, start_frame, end_frame, nframes, offset); + // Write 'automation' controllers (e.g. CC events from a UI slider) + write_controller_messages(bufs.get_midi(0), start_frame, end_frame, nframes, offset); deliver_output(bufs, start_frame, end_frame, nframes, offset); } @@ -706,13 +701,13 @@ MidiTrack::set_note_mode (NoteMode m) void MidiTrack::midi_panic() { - for(uint8_t channel = 0; channel <= 0xF; channel++) { + for (uint8_t channel = 0; channel <= 0xF; channel++) { Byte ev[3] = { MIDI_CMD_CONTROL | channel, MIDI_CTL_SUSTAIN, 0 }; - write_immediate_event(3, ev); + write_immediate_event(3, ev); ev[1] = MIDI_CTL_ALL_NOTES_OFF; - write_immediate_event(3, ev); + write_immediate_event(3, ev); ev[1] = MIDI_CTL_RESET_CONTROLLERS; - write_immediate_event(3, ev); + write_immediate_event(3, ev); } } @@ -721,6 +716,11 @@ MidiTrack::midi_panic() bool MidiTrack::write_immediate_event(size_t size, const Byte* buf) { + printf("Write immediate event: "); + for (size_t i=0; i < size; ++i) { + printf("%X ", buf[i]); + } + printf("\n"); return (_immediate_events.write(0, size, buf) == size); } diff --git a/libs/ardour/smf_source.cc b/libs/ardour/smf_source.cc index 4ab7a9b973..389224eb3b 100644 --- a/libs/ardour/smf_source.cc +++ b/libs/ardour/smf_source.cc @@ -361,11 +361,11 @@ SMFSource::read_event(uint32_t* delta_t, uint32_t* size, Byte** buf) const if (event_size > 1) fread((*buf) + 1, 1, *size - 1, _fd); - printf("SMFSource %s read event: delta = %u, size = %u, data = ", _name.c_str(), *delta_t, *size); + /*printf("SMFSource %s read event: delta = %u, size = %u, data = ", _name.c_str(), *delta_t, *size); for (size_t i=0; i < *size; ++i) { printf("%X ", (*buf)[i]); } - printf("\n"); + printf("\n");*/ return (int)*size; } @@ -374,7 +374,7 @@ SMFSource::read_event(uint32_t* delta_t, uint32_t* size, Byte** buf) const nframes_t SMFSource::read_unlocked (MidiRingBuffer& dst, nframes_t start, nframes_t cnt, nframes_t stamp_offset, nframes_t negative_stamp_offset) const { - cerr << "SMF read_unlocked " << name() << " read " << start << ", count=" << cnt << ", offset=" << stamp_offset << endl; + //cerr << "SMF read_unlocked " << name() << " read " << start << ", count=" << cnt << ", offset=" << stamp_offset << endl; // 64 bits ought to be enough for anybody uint64_t time = 0; // in SMF ticks, 1 tick per _ppqn diff --git a/libs/midi++2/jack_midiport.cc b/libs/midi++2/jack_midiport.cc index 311bbb98f6..0b49517212 100644 --- a/libs/midi++2/jack_midiport.cc +++ b/libs/midi++2/jack_midiport.cc @@ -95,24 +95,27 @@ JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp) } else { - assert(_currently_in_cycle); assert(_jack_output_port); assert(timestamp < _nframes_this_cycle); - if (timestamp == 0) { - timestamp = _last_write_timestamp; - } - - if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), - timestamp, msg, msglen) == 0) { - ret = msglen; - _last_write_timestamp = timestamp; - + if (_currently_in_cycle) { + if (timestamp == 0) { + timestamp = _last_write_timestamp; + } + + if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), + timestamp, msg, msglen) == 0) { + ret = msglen; + _last_write_timestamp = timestamp; + + } else { + ret = 0; + cerr << "write of " << msglen << " failed, port holds " + << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle)) + << endl; + } } else { - ret = 0; - cerr << "write of " << msglen << " failed, port holds " - << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle)) - << endl; + cerr << "write to JACK midi port failed: not currently in a process cycle." << endl; } } |