Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some silly issues =) #50

Merged
merged 6 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bevy Oddio

[![Bevy tracking](https://img.shields.io/badge/Bevy%20tracking-main-lightblue)](https://github.com/bevyengine/bevy/blob/main/docs/plugins_guidelines.md#main-branch-tracking) ![Crates.io](https://img.shields.io/crates/d/bevy_oddio) ![Crates.io](https://img.shields.io/crates/l/bevy_oddio) ![Crates.io](https://img.shields.io/crates/v/bevy_oddio) ![docs.rs](https://img.shields.io/docsrs/bevy_oddio) [![CI](https://github.com/harudagondi/bevy_oddio/actions/workflows/rust.yml/badge.svg)](https://github.com/harudagondi/bevy_oddio/actions/workflows/rust.yml)
[![Bevy tracking](https://img.shields.io/badge/Bevy%20tracking-main-lightblue)](https://github.com/bevyengine/bevy/blob/main/docs/plugins_guidelines.md#main-branch-tracking) [![Crates.io](https://img.shields.io/crates/d/bevy_oddio)](https://crates.io/crates/bevy_oddio) ![Crates.io](https://img.shields.io/crates/l/bevy_oddio) ![Crates.io](https://img.shields.io/crates/v/bevy_oddio) [![docs.rs](https://img.shields.io/docsrs/bevy_oddio)](https://docs.rs/bevy_oddio/latest/bevy_oddio/) [![CI](https://github.com/harudagondi/bevy_oddio/actions/workflows/rust.yml/badge.svg)](https://github.com/harudagondi/bevy_oddio/actions/workflows/rust.yml)

A third party Bevy plugin that integrates [`oddio`] into [Bevy].

Expand Down
7 changes: 2 additions & 5 deletions examples/gain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ fn change_volume(
mut sinks: ResMut<Assets<AudioSink<SineWithGain>>>,
time: Res<Time>,
) {
let sink = match sinks.get_mut(&sink_handle.0) {
Some(sink) => sink,
None => return,
};
let Some(sink) = sinks.get_mut(&sink_handle.0) else { return };

let factor = (time.elapsed_seconds_wrapped().sin() + 1.0) / 2.0;

sink.control::<oddio::Gain<_>, _>()
.set_amplitude_ratio(factor as f32);
.set_amplitude_ratio(factor);
}
7 changes: 2 additions & 5 deletions examples/spatial_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ fn change_velocity(
) {
let mut emitter = query.single_mut();

let normalized_time = time.elapsed_seconds_wrapped().sin() as f32 * 5.0;
let normalized_time = time.elapsed_seconds_wrapped().sin() * 5.0;
let delta = time.delta_seconds();

let sink = match sinks.get_mut(&sink.0) {
Some(sink) => sink,
None => return,
};
let Some(sink) = sinks.get_mut(&sink.0) else { return };

let prev_pos = emitter.translation;
let position = Vec3::new(normalized_time, prev_pos.y, prev_pos.z);
Expand Down
9 changes: 3 additions & 6 deletions examples/spatial_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ fn change_velocity(
) {
let mut emitter = query.single_mut();

let x = time.elapsed_seconds_wrapped().sin() as f32 * 3.0;
let z = time.elapsed_seconds_wrapped().cos() as f32 * 3.0;
let x = time.elapsed_seconds_wrapped().sin() * 3.0;
let z = time.elapsed_seconds_wrapped().cos() * 3.0;
let delta = time.delta_seconds();

let sink = match sinks.get_mut(&sink.0) {
Some(sink) => sink,
None => return,
};
let Some(sink) = sinks.get_mut(&sink.0) else { return };

let prev_pos = emitter.translation;
let position = Vec3::new(x, prev_pos.y, z);
Expand Down
60 changes: 60 additions & 0 deletions examples/stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use bevy::{
prelude::{
App, Assets, Commands, Deref, Handle, Input, KeyCode, Res, ResMut, Resource, StartupStage,
},
DefaultPlugins,
};
use bevy_oddio::{
builtins::sine::{self, Sine},
output::AudioSink,
Audio, AudioPlugin,
};
use oddio::Sample;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(AudioPlugin)
.add_startup_system(init_assets)
.add_startup_system_to_stage(StartupStage::PostStartup, play_sine)
.add_system(get_input)
.run();
}

#[derive(Resource, Deref)]
struct SineHandle(Handle<Sine>);
#[derive(Resource)]
struct SineSink(Handle<AudioSink<Sine>>);

fn init_assets(mut commands: Commands, mut assets: ResMut<Assets<Sine>>) {
let handle = assets.add(Sine);
commands.insert_resource(SineHandle(handle));
}

fn play_sine(
mut commands: Commands,
mut audio: ResMut<Audio<Sample, Sine>>,
sine: Res<SineHandle>,
) {
// Note is in A4.
let handle = audio.play(sine.clone(), sine::Settings::new(0.0, 440.0));
commands.insert_resource(SineSink(handle));
}

fn get_input(
keys: Res<Input<KeyCode>>,
sink: Res<SineSink>,
mut sinks: ResMut<Assets<AudioSink<Sine>>>,
) {
let Some(sink) = sinks.get_mut(&sink.0) else { return };

let control = sink.control::<oddio::Stop<_>, _>();

if keys.just_pressed(KeyCode::Space) {
if control.is_paused() {
control.resume();
} else {
control.pause();
}
}
}
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

//! A plugin that integrates [`oddio`] with [`bevy`].
//!
//! Note that audio must have two channels or it will not work.
//! Thus, non-wav files are more likely to break.
//! There is an issue with loading audio files.
//!
//! See [`#1`](https://github.com/harudagondi/bevy_oddio/issues/1).
//! See [`#4`](https://github.com/harudagondi/bevy_oddio/issues/4).

use std::{collections::VecDeque, marker::PhantomData, sync::Arc};

Expand All @@ -18,7 +17,7 @@ use bevy::{
reflect::TypeUuid,
};
use frames::{FromFrame, Mono, Stereo};
use oddio::{Frame, Frames, FramesSignal, Sample, Seek, Signal, SpatialOptions};
use oddio::{Frame, Frames, FramesSignal, Gain, Sample, Seek, Signal, SpatialOptions, Speed};

pub use oddio;
use output::{
Expand Down Expand Up @@ -139,10 +138,10 @@ pub trait ToSignal {

impl<F: Frame + Send + Sync + Copy> ToSignal for AudioSource<F> {
type Settings = f64;
type Signal = FramesSignal<F>;
type Signal = Gain<Speed<FramesSignal<F>>>;

fn to_signal(&self, settings: Self::Settings) -> Self::Signal {
FramesSignal::new(self.frames.clone(), settings)
Gain::new(Speed::new(FramesSignal::new(self.frames.clone(), settings)))
}
}

Expand Down