Skip to content

Commit

Permalink
Fix for invalid delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
twitchax committed Dec 22, 2022
1 parent 89fb3e7 commit 111c441
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn start(args: Args) -> Void {
Some(Command::Play { symbol, octave, inversion, delay, length, fade_in }) => {
let chord = Chord::parse(&symbol)?.with_octave(Octave::Zero + octave).with_inversion(inversion);

play(&chord, delay, length, fade_in);
play(&chord, delay, length, fade_in)?;
}
None => {
println!("No command given.");
Expand All @@ -82,19 +82,28 @@ fn describe(chord: &Chord) {
println!("{}", chord);
}

fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) {
fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) -> Void {
describe(chord);

let (_stream, stream_handle) = OutputStream::try_default().unwrap();

let mut sinks = vec![];

for (k, n) in chord.chord().into_iter().enumerate() {
let chord_tones = chord.chord();

if length <= chord_tones.len() as f32 * delay {
return Err(anyhow::Error::msg("The delay is too long for the length of play (i.e., the number of chord tones times the delay is longer than the length)."));
}

for (k, n) in chord_tones.into_iter().enumerate() {
let sink = Sink::try_new(&stream_handle).unwrap();

let d = k as f32 * delay;

let source = SineWave::new(n.frequency())
.take_duration(Duration::from_secs_f32(length - k as f32 * delay))
.delay(Duration::from_secs_f32(k as f32 * delay))
.take_duration(Duration::from_secs_f32(length - d))
.buffered()
.delay(Duration::from_secs_f32(d))
.fade_in(Duration::from_secs_f32(fade_in))
.amplify(0.20);

Expand All @@ -104,6 +113,8 @@ fn play(chord: &Chord, delay: f32, length: f32, fade_in: f32) {
}

std::thread::sleep(Duration::from_secs_f32(length));

Ok(())
}

// Tests.
Expand Down
1 change: 0 additions & 1 deletion src/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,6 @@ mod tests {
assert_eq!(C.into_chord().with_inversion(1).chord(), vec![E, G, CFive]);
assert_eq!(C.into_chord().with_inversion(2).chord(), vec![G, CFive, EFive]);
assert_eq!(C.into_chord().maj7().with_inversion(3).chord(), vec![B, CFive, EFive, GFive]);

}

#[test]
Expand Down

0 comments on commit 111c441

Please sign in to comment.