-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement part of pulse waves in new API
- Loading branch information
1 parent
f54bb5f
commit 3b66f6e
Showing
6 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
use fon::{chan::Ch16, Audio, Frame}; | ||
use twang::next::{Synth, Wave}; | ||
use fon::{chan::Ch16, Audio}; | ||
use twang::tree::{line::Line, Synth}; | ||
|
||
mod wav; | ||
//mod plot; | ||
|
||
fn main() { | ||
// Define waveform | ||
const TRIANGLE: Wave = Wave::sig(440.0).trap(Wave::ZERO, Wave::MAX); | ||
|
||
let waveform = const { Line(440.0).osc().pulse(Line(0.0), Line(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(TRIANGLE); | ||
let mut synth = Synth::new(waveform); | ||
|
||
// Synthesize 5 seconds of audio | ||
synth.stream(audio.sink(), &[]); | ||
|
||
// Write synthesized audio to WAV file | ||
// Plot synthesized audio, and write to a WAV file | ||
//plot::write(&audio); | ||
wav::write(audio, "triangle.wav").expect("Failed to write WAV file"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::tree::{Chunk, Wave}; | ||
|
||
/// Pulse wave | ||
/// | ||
/// Takes phase (-1 to 1), duty (-1 to 1) and alias (0 to 1) as input | ||
#[derive(Debug)] | ||
pub struct Pulse<I, J, K>(pub I, pub J, pub K); | ||
|
||
impl<I, J, K> Wave for Pulse<I, J, K> | ||
where | ||
I: Wave, | ||
J: Wave, | ||
K: Wave, | ||
{ | ||
fn synthesize(&self, elapsed: u64, interval: u64, vars: &[f32]) -> Chunk { | ||
let chunk = self.0.synthesize(elapsed, interval, vars); | ||
let _cycle = self.1.synthesize(elapsed, interval, vars); | ||
let alias = self.2.synthesize(elapsed, interval, vars); | ||
let clip = alias.recip(); | ||
|
||
chunk.abs().gain(2.0).offset(-1.0).amplify(clip).clip() | ||
} | ||
} |