midi import in daw backend
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/// Audio effect processor trait
|
||||
///
|
||||
/// All effects must be Send to be usable in the audio thread.
|
||||
/// Effects should be real-time safe: no allocations, no blocking operations.
|
||||
pub trait Effect: Send {
|
||||
/// Process audio buffer in-place
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `buffer` - Interleaved audio samples to process
|
||||
/// * `channels` - Number of audio channels (2 for stereo)
|
||||
/// * `sample_rate` - Sample rate in Hz
|
||||
fn process(&mut self, buffer: &mut [f32], channels: usize, sample_rate: u32);
|
||||
|
||||
/// Set an effect parameter
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - Parameter identifier
|
||||
/// * `value` - Parameter value (normalized or specific units depending on parameter)
|
||||
fn set_parameter(&mut self, id: u32, value: f32);
|
||||
|
||||
/// Get an effect parameter value
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - Parameter identifier
|
||||
///
|
||||
/// # Returns
|
||||
/// Current parameter value
|
||||
fn get_parameter(&self, id: u32) -> f32;
|
||||
|
||||
/// Reset effect state (clear delays, resonances, etc.)
|
||||
fn reset(&mut self);
|
||||
|
||||
/// Get the effect name
|
||||
fn name(&self) -> &str;
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
use super::Effect;
|
||||
use crate::dsp::BiquadFilter;
|
||||
|
||||
/// Simple 3-band EQ (low shelf, mid peak, high shelf)
|
||||
///
|
||||
/// Parameters:
|
||||
/// - 0: Low gain in dB (-12.0 to +12.0)
|
||||
/// - 1: Mid gain in dB (-12.0 to +12.0)
|
||||
/// - 2: High gain in dB (-12.0 to +12.0)
|
||||
/// - 3: Low frequency in Hz (default: 250)
|
||||
/// - 4: Mid frequency in Hz (default: 1000)
|
||||
/// - 5: High frequency in Hz (default: 8000)
|
||||
pub struct SimpleEQ {
|
||||
low_gain: f32,
|
||||
mid_gain: f32,
|
||||
high_gain: f32,
|
||||
low_freq: f32,
|
||||
mid_freq: f32,
|
||||
high_freq: f32,
|
||||
|
||||
low_filter: BiquadFilter,
|
||||
mid_filter: BiquadFilter,
|
||||
high_filter: BiquadFilter,
|
||||
|
||||
sample_rate: f32,
|
||||
}
|
||||
|
||||
impl SimpleEQ {
|
||||
/// Create a new SimpleEQ with flat response
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
low_gain: 0.0,
|
||||
mid_gain: 0.0,
|
||||
high_gain: 0.0,
|
||||
low_freq: 250.0,
|
||||
mid_freq: 1000.0,
|
||||
high_freq: 8000.0,
|
||||
low_filter: BiquadFilter::new(),
|
||||
mid_filter: BiquadFilter::new(),
|
||||
high_filter: BiquadFilter::new(),
|
||||
sample_rate: 48000.0, // Default, will be updated on first process
|
||||
}
|
||||
}
|
||||
|
||||
/// Set low band gain in decibels
|
||||
pub fn set_low_gain(&mut self, gain_db: f32) {
|
||||
self.low_gain = gain_db.clamp(-12.0, 12.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Set mid band gain in decibels
|
||||
pub fn set_mid_gain(&mut self, gain_db: f32) {
|
||||
self.mid_gain = gain_db.clamp(-12.0, 12.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Set high band gain in decibels
|
||||
pub fn set_high_gain(&mut self, gain_db: f32) {
|
||||
self.high_gain = gain_db.clamp(-12.0, 12.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Set low band frequency
|
||||
pub fn set_low_freq(&mut self, freq: f32) {
|
||||
self.low_freq = freq.clamp(20.0, 500.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Set mid band frequency
|
||||
pub fn set_mid_freq(&mut self, freq: f32) {
|
||||
self.mid_freq = freq.clamp(200.0, 5000.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Set high band frequency
|
||||
pub fn set_high_freq(&mut self, freq: f32) {
|
||||
self.high_freq = freq.clamp(2000.0, 20000.0);
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
/// Update filter coefficients based on current parameters
|
||||
fn update_filters(&mut self) {
|
||||
// Only update if sample rate has been set
|
||||
if self.sample_rate > 0.0 {
|
||||
// Use peaking filters for all bands
|
||||
// Q factor of 1.0 gives a moderate bandwidth
|
||||
self.low_filter.set_peaking(self.low_freq, 1.0, self.low_gain, self.sample_rate);
|
||||
self.mid_filter.set_peaking(self.mid_freq, 1.0, self.mid_gain, self.sample_rate);
|
||||
self.high_filter.set_peaking(self.high_freq, 1.0, self.high_gain, self.sample_rate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SimpleEQ {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Effect for SimpleEQ {
|
||||
fn process(&mut self, buffer: &mut [f32], channels: usize, sample_rate: u32) {
|
||||
// Update sample rate if it changed
|
||||
let sr = sample_rate as f32;
|
||||
if (self.sample_rate - sr).abs() > 0.1 {
|
||||
self.sample_rate = sr;
|
||||
self.update_filters();
|
||||
}
|
||||
|
||||
// Process through each filter in series
|
||||
self.low_filter.process_buffer(buffer, channels);
|
||||
self.mid_filter.process_buffer(buffer, channels);
|
||||
self.high_filter.process_buffer(buffer, channels);
|
||||
}
|
||||
|
||||
fn set_parameter(&mut self, id: u32, value: f32) {
|
||||
match id {
|
||||
0 => self.set_low_gain(value),
|
||||
1 => self.set_mid_gain(value),
|
||||
2 => self.set_high_gain(value),
|
||||
3 => self.set_low_freq(value),
|
||||
4 => self.set_mid_freq(value),
|
||||
5 => self.set_high_freq(value),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_parameter(&self, id: u32) -> f32 {
|
||||
match id {
|
||||
0 => self.low_gain,
|
||||
1 => self.mid_gain,
|
||||
2 => self.high_gain,
|
||||
3 => self.low_freq,
|
||||
4 => self.mid_freq,
|
||||
5 => self.high_freq,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.low_filter.reset();
|
||||
self.mid_filter.reset();
|
||||
self.high_filter.reset();
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"SimpleEQ"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
use super::Effect;
|
||||
|
||||
/// Simple gain/volume effect
|
||||
///
|
||||
/// Parameters:
|
||||
/// - 0: Gain in dB (-60.0 to +12.0)
|
||||
pub struct GainEffect {
|
||||
gain_db: f32,
|
||||
gain_linear: f32,
|
||||
}
|
||||
|
||||
impl GainEffect {
|
||||
/// Create a new gain effect with 0 dB (unity) gain
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
gain_db: 0.0,
|
||||
gain_linear: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a gain effect with a specific dB value
|
||||
pub fn with_gain_db(gain_db: f32) -> Self {
|
||||
let gain_linear = db_to_linear(gain_db);
|
||||
Self {
|
||||
gain_db,
|
||||
gain_linear,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set gain in decibels
|
||||
pub fn set_gain_db(&mut self, gain_db: f32) {
|
||||
self.gain_db = gain_db.clamp(-60.0, 12.0);
|
||||
self.gain_linear = db_to_linear(self.gain_db);
|
||||
}
|
||||
|
||||
/// Get current gain in decibels
|
||||
pub fn gain_db(&self) -> f32 {
|
||||
self.gain_db
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for GainEffect {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Effect for GainEffect {
|
||||
fn process(&mut self, buffer: &mut [f32], _channels: usize, _sample_rate: u32) {
|
||||
for sample in buffer.iter_mut() {
|
||||
*sample *= self.gain_linear;
|
||||
}
|
||||
}
|
||||
|
||||
fn set_parameter(&mut self, id: u32, value: f32) {
|
||||
if id == 0 {
|
||||
self.set_gain_db(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_parameter(&self, id: u32) -> f32 {
|
||||
if id == 0 {
|
||||
self.gain_db
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
// Gain has no state to reset
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"Gain"
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert decibels to linear gain
|
||||
#[inline]
|
||||
fn db_to_linear(db: f32) -> f32 {
|
||||
if db <= -60.0 {
|
||||
0.0
|
||||
} else {
|
||||
10.0_f32.powf(db / 20.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert linear gain to decibels
|
||||
#[inline]
|
||||
#[allow(dead_code)]
|
||||
fn linear_to_db(linear: f32) -> f32 {
|
||||
if linear <= 0.0 {
|
||||
-60.0
|
||||
} else {
|
||||
20.0 * linear.log10()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
pub mod effect_trait;
|
||||
pub mod eq;
|
||||
pub mod gain;
|
||||
pub mod pan;
|
||||
pub mod synth;
|
||||
|
||||
pub use effect_trait::Effect;
|
||||
pub use eq::SimpleEQ;
|
||||
pub use gain::GainEffect;
|
||||
pub use pan::PanEffect;
|
||||
pub use synth::SimpleSynth;
|
||||
@@ -0,0 +1,98 @@
|
||||
use super::Effect;
|
||||
|
||||
/// Stereo panning effect using constant-power panning law
|
||||
///
|
||||
/// Parameters:
|
||||
/// - 0: Pan position (-1.0 = full left, 0.0 = center, +1.0 = full right)
|
||||
pub struct PanEffect {
|
||||
pan: f32,
|
||||
left_gain: f32,
|
||||
right_gain: f32,
|
||||
}
|
||||
|
||||
impl PanEffect {
|
||||
/// Create a new pan effect with center panning
|
||||
pub fn new() -> Self {
|
||||
let mut effect = Self {
|
||||
pan: 0.0,
|
||||
left_gain: 1.0,
|
||||
right_gain: 1.0,
|
||||
};
|
||||
effect.update_gains();
|
||||
effect
|
||||
}
|
||||
|
||||
/// Create a pan effect with a specific pan position
|
||||
pub fn with_pan(pan: f32) -> Self {
|
||||
let mut effect = Self {
|
||||
pan: pan.clamp(-1.0, 1.0),
|
||||
left_gain: 1.0,
|
||||
right_gain: 1.0,
|
||||
};
|
||||
effect.update_gains();
|
||||
effect
|
||||
}
|
||||
|
||||
/// Set pan position (-1.0 = left, 0.0 = center, +1.0 = right)
|
||||
pub fn set_pan(&mut self, pan: f32) {
|
||||
self.pan = pan.clamp(-1.0, 1.0);
|
||||
self.update_gains();
|
||||
}
|
||||
|
||||
/// Get current pan position
|
||||
pub fn pan(&self) -> f32 {
|
||||
self.pan
|
||||
}
|
||||
|
||||
/// Update left/right gains using constant-power panning law
|
||||
fn update_gains(&mut self) {
|
||||
use std::f32::consts::PI;
|
||||
|
||||
// Constant-power panning: pan from -1 to +1 maps to angle 0 to PI/2
|
||||
let angle = (self.pan + 1.0) * 0.5 * PI / 2.0;
|
||||
|
||||
self.left_gain = angle.cos();
|
||||
self.right_gain = angle.sin();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PanEffect {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Effect for PanEffect {
|
||||
fn process(&mut self, buffer: &mut [f32], channels: usize, _sample_rate: u32) {
|
||||
if channels == 2 {
|
||||
// Stereo processing
|
||||
for frame in buffer.chunks_exact_mut(2) {
|
||||
frame[0] *= self.left_gain;
|
||||
frame[1] *= self.right_gain;
|
||||
}
|
||||
}
|
||||
// Mono and other channel counts: no panning applied
|
||||
}
|
||||
|
||||
fn set_parameter(&mut self, id: u32, value: f32) {
|
||||
if id == 0 {
|
||||
self.set_pan(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_parameter(&self, id: u32) -> f32 {
|
||||
if id == 0 {
|
||||
self.pan
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
// Pan has no state to reset
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"Pan"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
use super::Effect;
|
||||
use crate::audio::midi::MidiEvent;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
/// Maximum number of simultaneous voices
|
||||
const MAX_VOICES: usize = 16;
|
||||
|
||||
/// A single synthesizer voice
|
||||
#[derive(Clone)]
|
||||
struct SynthVoice {
|
||||
active: bool,
|
||||
note: u8,
|
||||
channel: u8,
|
||||
velocity: u8,
|
||||
phase: f32,
|
||||
frequency: f32,
|
||||
age: u32, // For voice stealing
|
||||
}
|
||||
|
||||
impl SynthVoice {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
active: false,
|
||||
note: 0,
|
||||
channel: 0,
|
||||
velocity: 0,
|
||||
phase: 0.0,
|
||||
frequency: 0.0,
|
||||
age: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate frequency from MIDI note number
|
||||
fn note_to_frequency(note: u8) -> f32 {
|
||||
440.0 * 2.0_f32.powf((note as f32 - 69.0) / 12.0)
|
||||
}
|
||||
|
||||
/// Start playing a note
|
||||
fn note_on(&mut self, channel: u8, note: u8, velocity: u8) {
|
||||
self.active = true;
|
||||
self.channel = channel;
|
||||
self.note = note;
|
||||
self.velocity = velocity;
|
||||
self.frequency = Self::note_to_frequency(note);
|
||||
self.phase = 0.0;
|
||||
self.age = 0;
|
||||
}
|
||||
|
||||
/// Stop playing
|
||||
fn note_off(&mut self) {
|
||||
self.active = false;
|
||||
}
|
||||
|
||||
/// Generate one sample
|
||||
fn process_sample(&mut self, sample_rate: f32) -> f32 {
|
||||
if !self.active {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Simple sine wave
|
||||
let sample = (self.phase * 2.0 * PI).sin() * (self.velocity as f32 / 127.0) * 0.3;
|
||||
|
||||
// Update phase
|
||||
self.phase += self.frequency / sample_rate;
|
||||
if self.phase >= 1.0 {
|
||||
self.phase -= 1.0;
|
||||
}
|
||||
|
||||
self.age += 1;
|
||||
sample
|
||||
}
|
||||
}
|
||||
|
||||
/// Simple polyphonic synthesizer using sine waves
|
||||
pub struct SimpleSynth {
|
||||
voices: Vec<SynthVoice>,
|
||||
sample_rate: f32,
|
||||
pending_events: Vec<MidiEvent>,
|
||||
}
|
||||
|
||||
impl SimpleSynth {
|
||||
/// Create a new SimpleSynth
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
voices: vec![SynthVoice::new(); MAX_VOICES],
|
||||
sample_rate: 44100.0,
|
||||
pending_events: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Find a free voice, or steal the oldest one
|
||||
fn find_voice_for_note_on(&mut self) -> usize {
|
||||
// First, look for an inactive voice
|
||||
for (i, voice) in self.voices.iter().enumerate() {
|
||||
if !voice.active {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
// No free voices, steal the oldest one
|
||||
self.voices
|
||||
.iter()
|
||||
.enumerate()
|
||||
.max_by_key(|(_, v)| v.age)
|
||||
.map(|(i, _)| i)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Find the voice playing a specific note on a specific channel
|
||||
fn find_voice_for_note_off(&mut self, channel: u8, note: u8) -> Option<usize> {
|
||||
self.voices
|
||||
.iter()
|
||||
.position(|v| v.active && v.channel == channel && v.note == note)
|
||||
}
|
||||
|
||||
/// Handle a MIDI event
|
||||
pub fn handle_event(&mut self, event: &MidiEvent) {
|
||||
if event.is_note_on() {
|
||||
let voice_idx = self.find_voice_for_note_on();
|
||||
self.voices[voice_idx].note_on(event.channel(), event.data1, event.data2);
|
||||
} else if event.is_note_off() {
|
||||
if let Some(voice_idx) = self.find_voice_for_note_off(event.channel(), event.data1) {
|
||||
self.voices[voice_idx].note_off();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Queue a MIDI event to be processed
|
||||
pub fn queue_event(&mut self, event: MidiEvent) {
|
||||
self.pending_events.push(event);
|
||||
}
|
||||
|
||||
/// Process all queued events
|
||||
fn process_events(&mut self) {
|
||||
// Collect events first to avoid borrowing issues
|
||||
let events: Vec<MidiEvent> = self.pending_events.drain(..).collect();
|
||||
for event in events {
|
||||
self.handle_event(&event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Effect for SimpleSynth {
|
||||
fn process(&mut self, buffer: &mut [f32], channels: usize, sample_rate: u32) {
|
||||
self.sample_rate = sample_rate as f32;
|
||||
|
||||
// Process any queued MIDI events
|
||||
self.process_events();
|
||||
|
||||
// Generate audio from all active voices
|
||||
if channels == 1 {
|
||||
// Mono
|
||||
for sample in buffer.iter_mut() {
|
||||
let mut sum = 0.0;
|
||||
for voice in &mut self.voices {
|
||||
sum += voice.process_sample(self.sample_rate);
|
||||
}
|
||||
*sample += sum;
|
||||
}
|
||||
} else if channels == 2 {
|
||||
// Stereo (duplicate mono signal)
|
||||
for frame in buffer.chunks_exact_mut(2) {
|
||||
let mut sum = 0.0;
|
||||
for voice in &mut self.voices {
|
||||
sum += voice.process_sample(self.sample_rate);
|
||||
}
|
||||
frame[0] += sum;
|
||||
frame[1] += sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_parameter(&mut self, id: u32, value: f32) {
|
||||
// Parameter 0: Note on
|
||||
// Parameter 1: Note off
|
||||
// This is a simple interface for testing without proper MIDI routing
|
||||
match id {
|
||||
0 => {
|
||||
let note = value as u8;
|
||||
let voice_idx = self.find_voice_for_note_on();
|
||||
self.voices[voice_idx].note_on(0, note, 100);
|
||||
}
|
||||
1 => {
|
||||
let note = value as u8;
|
||||
if let Some(voice_idx) = self.find_voice_for_note_off(0, note) {
|
||||
self.voices[voice_idx].note_off();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_parameter(&self, _id: u32) -> f32 {
|
||||
0.0
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
for voice in &mut self.voices {
|
||||
voice.note_off();
|
||||
}
|
||||
self.pending_events.clear();
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"SimpleSynth"
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SimpleSynth {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user