Skip to content

Commit

Permalink
Loops
Browse files Browse the repository at this point in the history
  • Loading branch information
zeozeozeo committed Nov 30, 2023
1 parent 164caa6 commit 8b33c69
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 54 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn main() {
* Optionally use [Symphonia](https://github.com/pdeljanov/Symphonia) to support most audio formats
* Feature to disable audio playback support, if you want to use kittyaudio purely as an audio library
* Commands to change volume, playback rate and position in the sound with easings
* Loops, and commands to change them with easings

# Roadmap

Expand Down
35 changes: 35 additions & 0 deletions examples/loops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use kittyaudio::{include_sound, Change, Command, Easing, Mixer, PlaybackRate};

fn main() {
let sound = include_sound!("../assets/drozerix_-_crush.ogg").unwrap();

let mut mixer = Mixer::new();
mixer.init();

let sound = mixer.play(sound);

// set the loop points to be from 2 to 4 seconds
sound.set_loop_enabled(true);
sound.set_loop(2.0..=4.0);

// after 6 seconds (the loop has ran 1 time), change the loop region to
// 4.0..=4.1 seconds in the span on 5 seconds
let command = Command::new(Change::LoopSeconds(4.0..=4.1), Easing::Linear, 6.0, 5.0);
sound.add_command(command);

// reverse the sound gradually after 11 seconds upto 16 seconds (11+5)
let command = Command::new(
Change::PlaybackRate(PlaybackRate::Factor(-1.0)),
Easing::Linear,
11.0,
5.0,
);
sound.add_command(command);

// change the loop to be 4-5 seconds from 13 secons in the span of 5 seconds
// (the playback rate change command is applied simultaneously)
let command = Command::new(Change::LoopSeconds(4.0..=6.0), Easing::Linear, 13.0, 5.0);
sound.add_command(command);

mixer.wait();
}
11 changes: 7 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::f32::consts::PI;

use crate::PlaybackRate;
use std::{f32::consts::PI, ops::RangeInclusive};

/// https://github.com/Michaelangel007/easing#the-magic-of-170158
const C1: f32 = 1.70158;
Expand Down Expand Up @@ -433,7 +432,7 @@ impl Easing {
}

/// Specifies what change to make to a [`crate::Sound`]. Used with [`Command`].
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Change {
/// Change volume value.
Expand All @@ -447,11 +446,15 @@ pub enum Change {
Index(usize),
/// Change the position in seconds.
Position(f64),
/// Change the loop points in seconds.
LoopSeconds(RangeInclusive<f64>),
/// Change the loop points in samples.
LoopIndex(RangeInclusive<usize>),
}

/// A command that specifies an action that is applied on a [`crate::Sound`]
/// with an optional tween.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Command {
/// What variable to change.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
//! * Optionally use [Symphonia](https://github.com/pdeljanov/Symphonia) to support most audio formats
//! * Feature to disable audio playback support, if you want to use kittyaudio purely as an audio library
//! * Commands to change volume, playback rate and position in the sound with easings
//! * Loops, and commands to change them with easings
//!
//! # Roadmap
//!
Expand Down
Loading

0 comments on commit 8b33c69

Please sign in to comment.