Skip to content

Commit

Permalink
Optionally use serde as a feature (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackMIDIScore committed Aug 25, 2024
1 parent e894a84 commit 98044ce
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ symphonia = "0.5.4"
biquad = "0.4.2"
simdeez = "2.0.0-dev3"
proc-macro2 = "1.0.86"
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
serde = ["dep:serde"]

[dev-dependencies]
midi-toolkit-rs = "0.1.0"
Expand Down
6 changes: 4 additions & 2 deletions core/src/audio_stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Number of audio channels.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ChannelCount {
Mono,
Stereo,
Expand Down Expand Up @@ -30,7 +31,8 @@ impl From<u16> for ChannelCount {
}

/// Parameters of the output audio.
#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct AudioStreamParams {
pub sample_rate: u32,
pub channels: ChannelCount,
Expand Down
15 changes: 10 additions & 5 deletions core/src/channel/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::sync::Arc;
use crate::soundfont::SoundfontBase;

/// MIDI events for a single key in a channel.
#[derive(Debug, Clone)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum KeyNoteEvent {
/// Starts a new note voice with a velocity
On(u8),
Expand All @@ -19,7 +20,8 @@ pub enum KeyNoteEvent {
}

/// Events to modify parameters of a channel.
#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ChannelConfigEvent {
/// Sets the soundfonts for the channel
SetSoundfonts(Vec<Arc<dyn SoundfontBase>>),
Expand All @@ -29,7 +31,8 @@ pub enum ChannelConfigEvent {
}

/// MIDI events for a channel.
#[derive(Debug, Clone)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ChannelAudioEvent {
/// Starts a new note voice
NoteOn { key: u8, vel: u8 },
Expand All @@ -54,7 +57,8 @@ pub enum ChannelAudioEvent {
}

/// Wrapper enum for various events for a channel.
#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ChannelEvent {
/// Audio event
Audio(ChannelAudioEvent),
Expand All @@ -64,7 +68,8 @@ pub enum ChannelEvent {
}

/// MIDI control events for a channel.
#[derive(Debug, Clone)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ControlEvent {
/// A raw control change event
Raw(u8, u8),
Expand Down
3 changes: 2 additions & 1 deletion core/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ impl ControlEventData {
}

/// Options for initializing a new VoiceChannel.
#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ChannelInitOptions {
/// If set to true, the voices killed due to the voice limit will fade out.
/// If set to false, they will be killed immediately, usually causing clicking
Expand Down
9 changes: 6 additions & 3 deletions core/src/channel_group/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{channel::ChannelInitOptions, AudioStreamParams};

/// Defines the multithreading options for each task that supports it.
#[derive(Clone)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ThreadCount {
/// No multithreading. Run everything on the same thread.
None,
Expand Down Expand Up @@ -30,7 +31,8 @@ pub enum ThreadCount {
/// - However, per-key multithreading adds some overhead, so if the synth is invoked to
/// render very small sample counts each time (e.g. sub 1 millisecond), not using per-key
/// multithreading becomes more efficient.
#[derive(Clone)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ParallelismOptions {
/// Render the MIDI channels parallel in a threadpool with the specified
/// thread count.
Expand Down Expand Up @@ -60,7 +62,8 @@ impl Default for ParallelismOptions {
}

/// Options for initializing a new ChannelGroup.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ChannelGroupConfig {
/// Channel initialization options (same for all channels).
/// See the `ChannelInitOptions` documentation for more information.
Expand Down
2 changes: 2 additions & 0 deletions core/src/channel_group/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::channel::{ChannelAudioEvent, ChannelConfigEvent};

/// Wrapper enum for various events to be sent to a MIDI synthesizer.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum SynthEvent {
/// An audio event to be sent to the specified channel.
/// See `ChannelAudioEvent` documentation for more information.
Expand Down
2 changes: 1 addition & 1 deletion core/src/channel_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl ChannelGroup {
}
SynthEvent::AllChannels(event) => {
for channel in self.channel_events_cache.iter_mut() {
channel.push(event.clone());
channel.push(event);
}
self.cached_event_count += self.channel_events_cache.len() as u32;
if self.cached_event_count > MAX_EVENT_CACHE_SIZE {
Expand Down
6 changes: 4 additions & 2 deletions core/src/soundfont/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Type of the audio sample interpolation algorithm.
#[derive(Clone, PartialEq, Eq, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Interpolator {
/// Nearest neighbor interpolation
///
Expand All @@ -13,7 +14,8 @@ pub enum Interpolator {
}

/// Options for initializing/loading a new sample soundfont.
#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct SoundfontInitOptions {
/// The bank number (0-128) to extract and use from the soundfont.
/// `None` means to use all available banks (bank 0 for SFZ).
Expand Down
4 changes: 4 additions & 0 deletions realtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ spin_sleep = "1.2.1"
to_vec = "0.1.0"
wav = "1.0.1"
xsynth-core = { workspace = true }
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
serde = ["dep:serde", "xsynth-core/serde"]

[dev-dependencies]
midi-toolkit-rs = "0.1.0"
Expand Down
2 changes: 2 additions & 0 deletions realtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::ops::RangeInclusive;
pub use xsynth_core::{channel::ChannelInitOptions, channel_group::ThreadCount};

/// Options for initializing a new RealtimeSynth.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct XSynthRealtimeConfig {
/// Channel initialization options (same for all channels).
/// See the `ChannelInitOptions` documentation for more information.
Expand Down
2 changes: 1 addition & 1 deletion realtime/src/event_senders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl RealtimeEventSender {
}
SynthEvent::AllChannels(event) => {
for sender in self.senders.iter_mut() {
sender.send_audio(event.clone());
sender.send_audio(event);
}
}
SynthEvent::ChannelConfig(event) => {
Expand Down
4 changes: 4 additions & 0 deletions render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ midi-toolkit-rs = "0.1.0"
spin_sleep = "1.2.1"
atomic_float = "1.0.0"
thiserror = "1.0.63"
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
serde = ["dep:serde", "xsynth-core/serde"]
6 changes: 4 additions & 2 deletions render/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ pub use xsynth_core::{
};

/// Supported audio formats of XSynthRender.
#[derive(PartialEq, Clone, Copy)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum XSynthRenderAudioFormat {
Wav,
}

/// Options for initializing a new XSynthRender object.
#[derive(Clone)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct XSynthRenderConfig {
/// Synthesizer initialization options.
/// See the `ChannelGroupConfig` documentation for more information.
Expand Down

0 comments on commit 98044ce

Please sign in to comment.