Skip to content

Commit

Permalink
Rename core::Synth to Core
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 28, 2024
1 parent e9d5fa8 commit c1811e6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 111 deletions.
104 changes: 101 additions & 3 deletions oxisynth/src/core/midi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,105 @@ use crate::core::soundfont::{
InstrumentZone, PresetZone,
};
use crate::core::voice_pool::{Voice, VoiceAddMode, VoiceDescriptor, VoicePool};
use crate::OxiError;
use crate::{MidiEvent, OxiError};

use super::Core;

pub(crate) fn handle_event(synth: &mut Core, event: MidiEvent) -> Result<(), OxiError> {
match event.check()? {
MidiEvent::NoteOn { channel, key, vel } => {
self::noteon(
synth.channels.get(channel as usize)?,
&mut synth.voices,
synth.ticks,
synth.min_note_length_ticks,
synth.settings.gain,
key,
vel,
)?;
}
MidiEvent::NoteOff { channel, key } => {
synth.voices.noteoff(
synth.channels.get(channel as usize)?,
synth.min_note_length_ticks,
key,
);
}
MidiEvent::ControlChange {
channel,
ctrl,
value,
} => {
self::cc(
synth.channels.get_mut(channel as usize)?,
&mut synth.voices,
synth.min_note_length_ticks,
synth.settings.drums_channel_active,
ctrl,
value,
);
}
MidiEvent::AllNotesOff { channel } => {
synth.voices.all_notes_off(
synth.channels.get_mut(channel as usize)?,
synth.min_note_length_ticks,
);
}
MidiEvent::AllSoundOff { channel } => {
synth.voices.all_sounds_off(channel as usize);
}
MidiEvent::PitchBend { channel, value } => {
let channel = synth.channels.get_mut(channel as usize)?;

const MOD_PITCHWHEEL: u8 = 14;
channel.set_pitch_bend(value);
synth.voices.modulate_voices(channel, false, MOD_PITCHWHEEL);
}
MidiEvent::ProgramChange {
channel,
program_id,
} => {
self::program_change(
synth.channels.get_mut(channel as usize)?,
&synth.font_bank,
program_id,
synth.settings.drums_channel_active,
);
}
MidiEvent::ChannelPressure { channel, value } => {
let channel = synth.channels.get_mut(channel as usize)?;

const MOD_CHANNELPRESSURE: u8 = 13;
channel.set_channel_pressure(value);
synth
.voices
.modulate_voices(channel, false, MOD_CHANNELPRESSURE);
}
MidiEvent::PolyphonicKeyPressure {
channel,
key,
value,
} => {
let channel = synth.channels.get_mut(channel as usize)?;
channel.set_key_pressure(key as usize, value as i8);
synth.voices.key_pressure(channel, key);
}
MidiEvent::SystemReset => {
synth.voices.system_reset();

let preset = synth.font_bank.find_preset(0, 0).map(|p| p.1);
for channel in synth.channels.iter_mut() {
channel.init(preset.clone());
channel.init_ctrl(0);
}

synth.chorus.reset();
synth.reverb.reset();
}
};

Ok(())
}

type MidiControlChange = u32;
const RPN_MSB: MidiControlChange = 101;
Expand Down Expand Up @@ -35,7 +133,7 @@ pub(crate) fn set_gen(
}

/// Send a noteon message.
pub(in super::super) fn noteon(
fn noteon(
channel: &Channel,
voices: &mut VoicePool,
start_time: usize,
Expand Down Expand Up @@ -307,7 +405,7 @@ fn inner_noteon(
}

/// Send a control change message.
pub(in super::super) fn cc(
fn cc(
channel: &mut Channel,
voices: &mut VoicePool,
min_note_length_ticks: usize,
Expand Down
105 changes: 4 additions & 101 deletions oxisynth/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod font_bank;

use crate::chorus::Chorus;
use crate::reverb::Reverb;
use crate::{MidiEvent, OxiError};

pub mod soundfont;
use soundfont::SoundFont;
Expand All @@ -29,7 +28,7 @@ struct FxBuf {
pub chorus: [f32; 64],
}

pub struct Synth {
pub(crate) struct Core {
ticks: usize,
pub font_bank: FontBank,

Expand All @@ -55,13 +54,13 @@ pub struct Synth {
i16_output: write::i16_write::I16OutputState,
}

impl Default for Synth {
impl Default for Core {
fn default() -> Self {
Self::new(Default::default()).unwrap()
}
}

impl Synth {
impl Core {
pub fn new(desc: SynthDescriptor) -> Result<Self, SettingsError> {
let chorus_active = desc.chorus_active;
let reverb_active = desc.reverb_active;
Expand All @@ -77,13 +76,12 @@ impl Synth {
settings.audio_channels
};

let midi_channels = settings.midi_channels;
let mut synth = Self {
ticks: 0,

font_bank: FontBank::new(),

channels: ChannelPool::new(midi_channels as usize, None),
channels: ChannelPool::new(settings.midi_channels as usize, None),
voices: VoicePool::new(settings.polyphony as usize, settings.sample_rate),
left_buf: vec![[0.0; 64]; nbuf as usize],
right_buf: vec![[0.0; 64]; nbuf as usize],
Expand Down Expand Up @@ -115,99 +113,4 @@ impl Synth {

Ok(synth)
}

pub fn send_event(&mut self, event: MidiEvent) -> Result<(), OxiError> {
match event.check()? {
MidiEvent::NoteOn { channel, key, vel } => {
midi::noteon(
self.channels.get(channel as usize)?,
&mut self.voices,
self.ticks,
self.min_note_length_ticks,
self.settings.gain,
key,
vel,
)?;
}
MidiEvent::NoteOff { channel, key } => {
self.voices.noteoff(
self.channels.get(channel as usize)?,
self.min_note_length_ticks,
key,
);
}
MidiEvent::ControlChange {
channel,
ctrl,
value,
} => {
midi::cc(
self.channels.get_mut(channel as usize)?,
&mut self.voices,
self.min_note_length_ticks,
self.settings.drums_channel_active,
ctrl,
value,
);
}
MidiEvent::AllNotesOff { channel } => {
self.voices.all_notes_off(
self.channels.get_mut(channel as usize)?,
self.min_note_length_ticks,
);
}
MidiEvent::AllSoundOff { channel } => {
self.voices.all_sounds_off(channel as usize);
}
MidiEvent::PitchBend { channel, value } => {
let channel = self.channels.get_mut(channel as usize)?;

const MOD_PITCHWHEEL: u8 = 14;
channel.set_pitch_bend(value);
self.voices.modulate_voices(channel, false, MOD_PITCHWHEEL);
}
MidiEvent::ProgramChange {
channel,
program_id,
} => {
midi::program_change(
self.channels.get_mut(channel as usize)?,
&self.font_bank,
program_id,
self.settings.drums_channel_active,
);
}
MidiEvent::ChannelPressure { channel, value } => {
let channel = self.channels.get_mut(channel as usize)?;

const MOD_CHANNELPRESSURE: u8 = 13;
channel.set_channel_pressure(value);
self.voices
.modulate_voices(channel, false, MOD_CHANNELPRESSURE);
}
MidiEvent::PolyphonicKeyPressure {
channel,
key,
value,
} => {
let channel = self.channels.get_mut(channel as usize)?;
channel.set_key_pressure(key as usize, value as i8);
self.voices.key_pressure(channel, key);
}
MidiEvent::SystemReset => {
self.voices.system_reset();

let preset = self.font_bank.find_preset(0, 0).map(|p| p.1);
for channel in self.channels.iter_mut() {
channel.init(preset.clone());
channel.init_ctrl(0);
}

self.chorus.reset();
self.reverb.reset();
}
};

Ok(())
}
}
4 changes: 2 additions & 2 deletions oxisynth/src/core/write.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::core::Synth;
use crate::core::Core;

#[cfg(feature = "i16-out")]
pub(crate) mod i16_write;

impl Synth {
impl Core {
/// clean the audio buffers
fn clear_buffers(&mut self) {
self.left_buf.iter_mut().for_each(|buff| buff.fill(0.0));
Expand Down
4 changes: 2 additions & 2 deletions oxisynth/src/core/write/i16_write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::LazyLock;

use crate::core::Synth;
use crate::core::Core;

static RAND_TABLE: LazyLock<[[f32; 48000]; 2]> = LazyLock::new(|| {
let mut rand: [[f32; 48000]; 2] = [[0.0; 48000]; 2];
Expand Down Expand Up @@ -29,7 +29,7 @@ pub(crate) struct I16OutputState {

#[inline]
pub fn write_i16(
synth: &mut Synth,
synth: &mut Core,
len: usize,
loff: usize,
lincr: usize,
Expand Down
6 changes: 3 additions & 3 deletions oxisynth/src/synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The API for sending MIDI events is probably what you expect:
*/
#[derive(Default)]
pub struct Synth {
core: crate::core::Synth,
core: crate::core::Core,
}

impl Synth {
Expand All @@ -39,7 +39,7 @@ impl Synth {
*/
pub fn new(desc: SynthDescriptor) -> Result<Self, SettingsError> {
Ok(Synth {
core: crate::core::Synth::new(desc)?,
core: crate::core::Core::new(desc)?,
})
}

Expand All @@ -52,7 +52,7 @@ impl Synth {
}

pub fn send_event(&mut self, event: MidiEvent) -> Result<(), OxiError> {
self.core.send_event(event)
crate::core::midi::handle_event(&mut self.core, event)
}

pub fn font_bank(&self) -> &FontBank {
Expand Down

0 comments on commit c1811e6

Please sign in to comment.