-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Pitch as an alternative sound source (#9225)
# Objective My attempt at implementing #7515 ## Solution Added struct `Pitch` and implemented on it `Source` trait. ## Changelog ### Added - File pitch.rs to bevy_audio crate - Struct `Pitch` and type aliases for `AudioSourceBundle<Pitch>` and `SpatialAudioSourceBundle<Pitch>` - New example showing how to use `PitchBundle` ### Changed - `AudioPlugin` now adds system for `Pitch` audio --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
- Loading branch information
1 parent
a88b9fc
commit fb9c5a6
Showing
5 changed files
with
110 additions
and
2 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
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,38 @@ | ||
use crate::{AudioSourceBundle, Decodable, SpatialAudioSourceBundle}; | ||
use bevy_reflect::{TypePath, TypeUuid}; | ||
use rodio::{source::SineWave, source::TakeDuration, Source}; | ||
|
||
/// A source of sine wave sound | ||
#[derive(Debug, Clone, TypeUuid, TypePath)] | ||
#[uuid = "cbc63be3-b0b9-4d2c-a03c-88b58f1a19ef"] | ||
pub struct Pitch { | ||
/// Frequency at which sound will be played | ||
pub frequency: f32, | ||
/// Duration for which sound will be played | ||
pub duration: std::time::Duration, | ||
} | ||
|
||
impl Pitch { | ||
/// Creates a new note | ||
pub fn new(frequency: f32, duration: std::time::Duration) -> Self { | ||
Pitch { | ||
frequency, | ||
duration, | ||
} | ||
} | ||
} | ||
|
||
impl Decodable for Pitch { | ||
type DecoderItem = <SineWave as Iterator>::Item; | ||
type Decoder = TakeDuration<SineWave>; | ||
|
||
fn decoder(&self) -> Self::Decoder { | ||
SineWave::new(self.frequency).take_duration(self.duration) | ||
} | ||
} | ||
|
||
/// Bundle for playing a bevy note sound | ||
pub type PitchBundle = AudioSourceBundle<Pitch>; | ||
|
||
/// Bundle for playing a bevy note sound with a 3D position | ||
pub type SpatialPitchBundle = SpatialAudioSourceBundle<Pitch>; |
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,55 @@ | ||
//! This example illustrates how to play a single-frequency sound (aka a pitch) | ||
|
||
use bevy::prelude::*; | ||
use std::time::Duration; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_event::<PlayPitch>() | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, (play_pitch, keyboard_input_system)) | ||
.run(); | ||
} | ||
|
||
#[derive(Event, Default)] | ||
struct PlayPitch; | ||
|
||
#[derive(Resource)] | ||
struct PitchFrequency(f32); | ||
|
||
fn setup(mut commands: Commands) { | ||
commands.insert_resource(PitchFrequency(220.0)); | ||
} | ||
|
||
fn play_pitch( | ||
mut pitch_assets: ResMut<Assets<Pitch>>, | ||
frequency: Res<PitchFrequency>, | ||
mut events: EventReader<PlayPitch>, | ||
mut commands: Commands, | ||
) { | ||
for _ in events.iter() { | ||
info!("playing pitch with frequency: {}", frequency.0); | ||
commands.spawn(PitchBundle { | ||
source: pitch_assets.add(Pitch::new(frequency.0, Duration::new(1, 0))), | ||
settings: PlaybackSettings::DESPAWN, | ||
}); | ||
info!("number of pitch assets: {}", pitch_assets.len()); | ||
} | ||
} | ||
|
||
fn keyboard_input_system( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
mut frequency: ResMut<PitchFrequency>, | ||
mut events: EventWriter<PlayPitch>, | ||
) { | ||
if keyboard_input.just_pressed(KeyCode::Up) { | ||
frequency.0 *= 2.0f32.powf(1.0 / 12.0); | ||
} | ||
if keyboard_input.just_pressed(KeyCode::Down) { | ||
frequency.0 /= 2.0f32.powf(1.0 / 12.0); | ||
} | ||
if keyboard_input.just_pressed(KeyCode::Space) { | ||
events.send(PlayPitch); | ||
} | ||
} |