Skip to content

Commit

Permalink
Add next module for new data-oriented API design
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Dec 23, 2022
1 parent 00dfee3 commit 8c2d45a
Show file tree
Hide file tree
Showing 11 changed files with 781 additions and 94 deletions.
19 changes: 19 additions & 0 deletions examples/ease.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::next::{Synth, Wave};

mod wav;

fn main() {
// Define waveform
const RAMP: Wave = Wave::sig(440.0).ramp(Wave::MIN);

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
let mut synth = Synth::new(RAMP);

// Synthesize 5 seconds of audio
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "ease.wav").expect("Failed to write WAV file");
}
19 changes: 19 additions & 0 deletions examples/ramp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::next::{Synth, Wave};

mod wav;

fn main() {
// Define waveform
const RAMP: Wave = Wave::sig(440.0).ramp(Wave::MAX);

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
let mut synth = Synth::new(RAMP);

// Synthesize 5 seconds of audio
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "ramp.wav").expect("Failed to write WAV file");
}
19 changes: 19 additions & 0 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::next::{Synth, Wave};

mod wav;

fn main() {
// Define waveform
const RECT: Wave = Wave::sig(440.0).rect(&Wave::sig(0.5));

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
let mut synth = Synth::new(RECT);

// Synthesize 5 seconds of audio
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "rectangle.wav").expect("Failed to write WAV file");
}
29 changes: 9 additions & 20 deletions examples/sawtooth.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::osc::Sawtooth;
use twang::Synth;
use twang::next::{Synth, Wave};

mod wav;

// State of the synthesizer.
struct Processors {
saw: Sawtooth,
}

fn main() {
// Initialize audio
// Define waveform
const SAW: Wave = Wave::sig(440.0).saw();

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
// Create audio processors
let proc = Processors {
saw: Sawtooth::new(),
};
// Build synthesis algorithm
let mut synth = Synth::new(proc, |proc, frame: Frame<_, 2>| {
// Calculate the next sample for each processor
let saw = proc.saw.step(440.0);
// Pan the generated audio center
frame.pan(saw, 0.0)
});
let mut synth = Synth::new(SAW);

// Synthesize 5 seconds of audio
synth.stream(audio.sink());
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "sawtooth.wav").expect("Failed to write WAV file");
}
30 changes: 10 additions & 20 deletions examples/sine.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::osc::Sine;
use twang::Synth;
use twang::next::{Synth, Wave};

mod wav;

// State of the synthesizer.
struct Processors {
sine: Sine,
}

fn main() {
// Initialize audio
// Define waveform
const SINE: Wave = Wave::sig(440.0).sine();

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
// Create audio processors
let proc = Processors { sine: Sine::new() };
// Build synthesis algorithm
let mut synth = Synth::new(proc, |proc, frame: Frame<_, 2>| {
// Calculate the next sample for each processor
let sine = proc.sine.step(440.0);
// Pan the generated audio center
frame.pan(sine, 0.0)
});
let mut synth = Synth::new(SINE);

// Synthesize 5 seconds of audio
synth.stream(audio.sink());
// Plot synthesized audio
synth.stream(audio.sink(), &[]);

// Plot synthesized audio, and write to a WAV file
// plot::write(&audio);
// Write synthesized audio to WAV file
wav::write(audio, "sine.wav").expect("Failed to write WAV file");
}
29 changes: 9 additions & 20 deletions examples/square.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::osc::Pulse;
use twang::Synth;
use twang::next::{Synth, Wave};

mod wav;

// State of the synthesizer.
struct Processors {
pulse: Pulse,
}

fn main() {
// Initialize audio
// Define waveform
const SQUARE: Wave = Wave::sig(440.0).sq();

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
// Create audio processors
let proc = Processors {
pulse: Pulse::new(),
};
// Build synthesis algorithm
let mut synth = Synth::new(proc, |proc, frame: Frame<_, 2>| {
// Calculate the next sample for each processor
let square = proc.pulse.step(440.0, Default::default());
// Pan the generated audio center
frame.pan(square, 0.0)
});
let mut synth = Synth::new(SQUARE);

// Synthesize 5 seconds of audio
synth.stream(audio.sink());
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "square.wav").expect("Failed to write WAV file");
}
19 changes: 19 additions & 0 deletions examples/trap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::next::{Synth, Wave};

mod wav;

fn main() {
// Define waveform
const TRAP: Wave = Wave::sig(440.0).trap(&Wave::sig(0.5), Wave::ZERO);

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
let mut synth = Synth::new(TRAP);

// Synthesize 5 seconds of audio
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "trap.wav").expect("Failed to write WAV file");
}
29 changes: 9 additions & 20 deletions examples/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
use fon::{chan::Ch16, Audio, Frame};
use twang::osc::Triangle;
use twang::Synth;
use twang::next::{Synth, Wave};

mod wav;

// State of the synthesizer.
struct Processors {
triangle: Triangle,
}

fn main() {
// Initialize audio
// Define waveform
const TRIANGLE: Wave = Wave::sig(440.0).trap(Wave::ZERO, Wave::MAX);

// Initialize audio, and create synthesizer
let mut audio = Audio::<Ch16, 2>::with_silence(48_000, 48_000 * 5);
// Create audio processors
let proc = Processors {
triangle: Triangle::new(),
};
// Build synthesis algorithm
let mut synth = Synth::new(proc, |proc, frame: Frame<_, 2>| {
// Calculate the next sample for each processor
let triangle = proc.triangle.step(440.0);
// Pan the generated audio center
frame.pan(triangle, 0.0)
});
let mut synth = Synth::new(TRIANGLE);

// Synthesize 5 seconds of audio
synth.stream(audio.sink());
synth.stream(audio.sink(), &[]);

// Write synthesized audio to WAV file
wav::write(audio, "triangle.wav").expect("Failed to write WAV file");
}
25 changes: 12 additions & 13 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! Twang synthesis file format
use alloc::vec::Vec;
use fon::chan::{Ch32, Channel};
use fon::{Audio, Sink};
use fon::chan::{Channel, Ch32};

/*
/// A twang synthesis operation
Expand All @@ -20,7 +20,7 @@ enum Op {
Pop,
/// Push value onto audio stack
Psh(Ch32),
/// Swap by index
/// Swap by index
Swp(u32),
/// Swap and pop by index
Sap(u32),
Expand Down Expand Up @@ -92,7 +92,7 @@ enum Node {
WaveT(Table, Const),
WaveC(Table, Chunk),
WaveV(Table, Value),

WaypointT(Table, Const),
WaypointC(Table, Chunk),
WaypointV(Table, Value),
Expand All @@ -107,7 +107,6 @@ enum Node {
BezierVC(Value, Chunk),
BezierVV(Value, Value),


/// Frequency Counter
///
/// A frequency counter is a sawtooth wave.
Expand Down Expand Up @@ -175,7 +174,6 @@ mod seal {

use self::seal::{Any, Sampler};


/// Builder for a synth
///
/// Inputs -> Program -> Output
Expand All @@ -190,7 +188,8 @@ pub struct SynthBuilder {
impl SynthBuilder {
/// Add chunked audio from an external source
pub fn mix_source(mut self, chunk: Chunk) -> Self {
self.input_buffers.resize((chunk.0 + 1).try_into().unwrap(), [Ch32::default(); 32]);
self.input_buffers
.resize((chunk.0 + 1).try_into().unwrap(), [Ch32::default(); 32]);
self.nodes.push(Node::Source(chunk));
self
}
Expand All @@ -199,17 +198,19 @@ impl SynthBuilder {
///
/// A line wave is a horizontal line, silence to human ears.
pub fn mix_line(mut self, value: Value) -> Self {
self.input_samples.resize((value.0 + 1).try_into().unwrap(), 0.0);
self.input_samples
.resize((value.0 + 1).try_into().unwrap(), 0.0);
self.nodes.push(Node::Line(value));
self
}

/// Add wavetable
///
/// A wave table is a collection of samples that are slowed down or sped up
/// to make the pitch higher or lower.
pub fn mix_wave(mut self, table: Table, freq: impl Sampler) -> Self {
self.input_wtables.resize((table.0 + 1).try_into().unwrap(), Vec::new());
self.input_wtables
.resize((table.0 + 1).try_into().unwrap(), Vec::new());
self.nodes.push(match freq.to_any() {
Any::Value(x) => Node::WaveV(table, x),
Any::Chunk(x) => Node::WaveC(table, x),
Expand All @@ -223,7 +224,8 @@ impl SynthBuilder {
/// A ways table is almost the same thing as a wavetable, except allows
/// aliasing.
pub fn mix_ways(mut self, table: Table, freq: impl Sampler) -> Self {
self.input_wtables.resize((table.0 + 1).try_into().unwrap(), Vec::new());
self.input_wtables
.resize((table.0 + 1).try_into().unwrap(), Vec::new());
self.nodes.push(match freq.to_any() {
Any::Value(x) => Node::WaypointV(table, x),
Any::Chunk(x) => Node::WaypointC(table, x),
Expand Down Expand Up @@ -257,9 +259,6 @@ impl SynthBuilder {
}
}




/*
#[derive(Debug)]
struct EnvelopeComponent {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ pub mod noise;
pub mod ops;
pub mod osc;
// FIXME
// pub mod file;
pub mod file;
// FIXME
pub mod next;

pub use synth::Synth;
Loading

0 comments on commit 8c2d45a

Please sign in to comment.