Skip to content

Commit

Permalink
Refactor into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
valsteen committed Dec 24, 2020
1 parent 97b1643 commit 7602b56
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 248 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions midi_delay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use vst::api;
use vst::buffer::{AudioBuffer, SendEventBuffer};
use vst::plugin::{CanDo, Category, HostCallback, Info, Plugin};
use std::sync::Arc;
use parameters::{MidiDelayParameters, Parameter};
use std::cell::RefCell;
use util::datastructures::{AbsoluteTimeMidiMessageVector, DelayedMessageConsumer};
use util::messages::{AbsoluteTimeMidiMessage, MidiMessageType};

use parameters::{MidiDelayParameters, Parameter};
use util::parameters::ParameterConversion;
use util::midi_message_type::MidiMessageType;
use util::absolute_time_midi_message_vector::AbsoluteTimeMidiMessageVector;
use util::delayed_message_consumer::DelayedMessageConsumer;
use util::absolute_time_midi_message::AbsoluteTimeMidiMessage;


plugin_main!(MidiDelay);
Expand Down
22 changes: 11 additions & 11 deletions note_fan_out/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ mod parameters;
#[macro_use]
extern crate vst;

use std::collections::HashSet;
use std::hash::{Hasher, Hash};
use std::sync::Arc;
use vst::api;
use vst::buffer::{AudioBuffer, SendEventBuffer};
use vst::event::{Event, MidiEvent};
use vst::plugin::{CanDo, Category, HostCallback, Info, Plugin};
use std::sync::Arc;
use util::parameters::ParameterConversion;
use util::messages::{MidiMessageType, RawMessage, ChannelMessage, NoteMessage, GenericChannelMessage, NoteOn, NoteOff};
use parameters::{NoteFanoutParameters, Parameter};
use std::collections::HashSet;
use std::hash::{Hasher, Hash};
use parameters::ChannelDistribution;

use parameters::ChannelDistribution;
use parameters::{NoteFanoutParameters, Parameter};
use util::messages::{ChannelMessage, GenericChannelMessage, NoteMessage, NoteOff, NoteOn};
use util::midi_message_type::MidiMessageType;
use util::parameters::ParameterConversion;
use util::raw_message::RawMessage;

plugin_main!(NoteFanOut);

Expand Down Expand Up @@ -110,9 +112,7 @@ impl Plugin for NoteFanOut {

for e in events.events() {
if let Event::Midi(e) = e {
let raw_message = RawMessage::from(e.data);
let midi_message = MidiMessageType::from(raw_message);
let channel_message = GenericChannelMessage::from(raw_message);
let midi_message = MidiMessageType::from(&e.data);

match midi_message {
MidiMessageType::NoteOnMessage(midi_message) => {
Expand All @@ -123,7 +123,7 @@ impl Plugin for NoteFanOut {
target_channel
}
ChannelDistribution::Off => {
channel_message.get_channel()
GenericChannelMessage::from(&e.data).get_channel()
}
};

Expand Down
26 changes: 20 additions & 6 deletions note_off_delay/src/datastructures.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::collections::HashMap;
use util::messages::{AbsoluteTimeMidiMessage, NoteOn, MidiMessageType, NoteOff, ChannelMessage, NoteMessage};
use std::fmt::{Display, Formatter};
use std::fmt;
use std::hash::{Hash, Hasher};

use util::absolute_time_midi_message::AbsoluteTimeMidiMessage;
use util::messages::{NoteOff, NoteOn, ChannelMessage, NoteMessage};
use util::midi_message_type::MidiMessageType;

#[derive(Eq)]

#[derive(Eq, Clone, Copy)]
pub struct CurrentPlayingNotesIndex([u8; 2]);

impl From<&AbsoluteTimeMidiMessage> for CurrentPlayingNotesIndex {
Expand Down Expand Up @@ -58,6 +61,11 @@ impl CurrentPlayingNotes {
Some(*oldest_note)
}

// TODO
fn get(&self, index: &CurrentPlayingNotesIndex) -> Option<&AbsoluteTimeMidiMessage> {
self.0.get(index)
}

fn add_message(&mut self, message: AbsoluteTimeMidiMessage, max_notes: u8) -> Option<AbsoluteTimeMidiMessage> {
let play_time_in_samples = message.play_time_in_samples;

Expand All @@ -78,12 +86,16 @@ impl CurrentPlayingNotes {
}
};

// TODO don't remove the entry here. When sending the corresponding note off,
// we remove the playing note from currentplayingnotes by using the ID
self.0.remove_entry(&(CurrentPlayingNotesIndex::from(&oldest.unwrap())));

return Some(AbsoluteTimeMidiMessage {
data: NoteOff::from(oldest_note).into(),
play_time_in_samples
});
// TODO here use the ID of the note on we will remove
// then just skip sending it
return Some(AbsoluteTimeMidiMessage::new(
NoteOff::from(oldest_note).into(),
play_time_in_samples,
));
}
None
}
Expand All @@ -94,6 +106,8 @@ impl CurrentPlayingNotes {
for message in messages {
match MidiMessageType::from(message) {
MidiMessageType::NoteOffMessage(m) => {
// find corresponding note on, create this note off with the same ID, remove from
// playing notes when sending
self.0.remove(&CurrentPlayingNotesIndex([m.get_channel(), m.get_pitch()]));
}
MidiMessageType::NoteOnMessage(_) => {
Expand Down
32 changes: 25 additions & 7 deletions note_off_delay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ use std::sync::Arc;
use vst::api;
use vst::buffer::{AudioBuffer, SendEventBuffer};
use vst::plugin::{CanDo, Category, HostCallback, Info, Plugin};
use std::cell::RefCell;

use util::messages::{MidiMessageType, AbsoluteTimeMidiMessage};
use util::datastructures::{AbsoluteTimeMidiMessageVector, DelayedMessageConsumer};
use datastructures::CurrentPlayingNotes;
use parameters::NoteOffDelayPluginParameters;
use parameters::Parameter;
use util::absolute_time_midi_message::AbsoluteTimeMidiMessage;
use util::absolute_time_midi_message_vector::AbsoluteTimeMidiMessageVector;
use util::debug::DebugSocket;
use util::delayed_message_consumer::DelayedMessageConsumer;
use util::messages::format_event;
use util::midi_message_type::MidiMessageType;
use util::parameters::ParameterConversion;
use parameters::Parameter;
use std::cell::RefCell;
use datastructures::CurrentPlayingNotes;
use util::messages;

plugin_main!(NoteOffDelayPlugin);

Expand Down Expand Up @@ -55,6 +57,13 @@ impl NoteOffDelayPlugin {
};

let mut messages: Vec<AbsoluteTimeMidiMessage> = message_consumer.collect();

// TODO decouple limiting and updating playing notes. first we generate the note off necessary to cut notes that
// exceeds the limit. this will generate duplicate note off messages with the same ID
// then right when doing send_events, only then we update current playing notes, skipping
// note off if the corresponding ID is not found in current playing notes. this sorts out the
// duplicate note off issue, two same ID can live there as long as we know we skip them right at sending.

let notes_off = self
.current_playing_notes
.update(&messages, self.parameters.get_max_notes());
Expand All @@ -80,7 +89,7 @@ impl NoteOffDelayPlugin {
fn debug_events_in(&mut self, events: &api::Events) {
for e in events.events() {
DebugSocket::send(
&*(messages::format_event(&e)
&*(format_event(&e)
+ &*format!(" current time={}", self.current_time_in_samples)),
);
}
Expand Down Expand Up @@ -193,14 +202,23 @@ impl Plugin for NoteOffDelayPlugin {
for event in events.events() {
// TODO: minimum time, maximum time ( with delay )

// TODO we can't just create from midi message anymore, first match on type, then create AbsoluteTimeMidiMessage with
// a specific ID when it's a note off


if let Some(mut absolute_time_midi_message) = AbsoluteTimeMidiMessage::from_event(&event, self.current_time_in_samples) {
let midi_message = MidiMessageType::from(&absolute_time_midi_message);
match midi_message {
MidiMessageType::NoteOffMessage(_) => {
// TODO find the corresponding note on ID in current playing notes and assign it
// maybe using a constructor that uses the original note on
notes_off.insert_message(absolute_time_midi_message)
}
MidiMessageType::Unsupported => {}
MidiMessageType::NoteOnMessage(_) => {
// TODO find the ID of the note on this one replaces, get the corresponding note off by id,
// replace it at the absolute time location of this new note on, this note on then receives time+1

// find any pending note off that was planned after this note on, and place
// it just before. This is in order to still trigger the note off message.
if let Some(delayed_note_off_position) = self.message_queue.iter().position(
Expand Down
1 change: 1 addition & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"

[dependencies]
vst = { git = "https://github.com/rustaudio/vst-rs" }
global_counter = "0.2.1"
Loading

0 comments on commit 7602b56

Please sign in to comment.