From 7e1a63f388e1dd652d2534031b18f43c09555f7e Mon Sep 17 00:00:00 2001 From: harudagondi Date: Mon, 14 Nov 2022 12:53:40 +0800 Subject: [PATCH] Big refactor for 0.2 (#6) # Objective - This is a very big refactor that allows better ergonomics, better refactorability, and better support for streaming audio. - Partially complete #1 for `bevy_audio`, - bevyengine/bevy#5819 ~needs to be merged first.~ is now merged! Now waiting for bevy 0.9. - NiklasEi/bevy_kira_audio#63 is blocking for `kira`. - Partially complete #2 for `oddio` - bevyengine/bevy#5828 needs to be resolved first - NiklasEi/bevy_kira_audio#63 is blocking for `kira` - `oddio` isn't even implemented yet for this PR - By virtue of solving #2, then #3 will be unblocked - Unblocks #4 by using `DspGraph` - Fixes #5 ## Solution - [x] Add support for - [x] `bevy_audio`, ~blocked by bevyengine/bevy#5819~ Now waiting for bevy 0.9. - [x] `bevy_kira_audio`, ~blocked by NiklasEi/bevy_kira_audio#72~ Now waiting for bevy 0.9. - [x] `bevy_oddio` - [ ] Add support for streaming audio in - [x] `bevy_audio`, ~blocked by bevyengine/bevy#5819~ - [ ] `bevy_kira_audio`, blocked by NiklasEi/bevy_kira_audio#63 - [x] `bevy_oddio` - [ ] Allow setting of parameters in - [ ] `bevy_audio`, blocked by bevyengine/bevy#5828 - [ ] `bevy_kira_audio`, blocked by NiklasEi/bevy_kira_audio#63 - [x] `bevy_oddio` ## Note for Users of this Branch If you are a user of `bevy_fundsp`, please try to test this PR! This currently only works with bevy_audio (EDIT: and bevy_oddio!), because this relies on a branch I made on bevy. --- ## Changelog Reworked the majority of the code. ## Added - A way to play streaming DSP sources. See `SourceType::Dynamic`. - You can play DSP sources using `Audio::play_dsp`. - Two iterators on streaming audio sources: `Iter` and `IterMono`. ### Changed - Adding the DSP plugin. - You must now call `DspPlugin::default()`. - The method on adding DSP sources. - No more initializing using `DspAssets`! - Just add your DSP function using `app.add_dsp_source` - Playing DSP sources require `Audio` to be mutable. (Use `ResMut`) - A lot of internals are shuffled around. ### Removed - `DspAssets` - `FnDspGraph` - `StreamingDspSource` --- .github/workflows/rust.yml | 136 +++------ CHANGELOG.md | 41 +++ Cargo.toml | 58 ++-- README.md | 36 +-- examples/bevy_audio/interactive.rs | 26 +- examples/bevy_audio/noise.rs | 26 +- examples/kira/interactive.rs | 39 +-- examples/kira/noise.rs | 24 +- examples/oddio/controlled.rs | 52 ++++ examples/oddio/interactive.rs | 44 +++ examples/oddio/noise.rs | 30 ++ justfile | 24 +- src/backend.rs | 64 +++++ src/backend/bevy_audio.rs | 95 ++++++ src/backend/kira.rs | 65 +++++ src/backend/oddio.rs | 182 ++++++++++++ src/dsp_graph.rs | 43 +++ src/dsp_manager.rs | 51 ++++ src/dsp_source.rs | 314 ++++++++++++++++++++ src/kira_impl.rs | 71 ----- src/lib.rs | 444 ++++++++--------------------- src/oddio_impl.rs | 43 --- src/rodio_impl.rs | 13 - 23 files changed, 1276 insertions(+), 645 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 examples/oddio/controlled.rs create mode 100644 examples/oddio/interactive.rs create mode 100644 examples/oddio/noise.rs create mode 100644 src/backend.rs create mode 100644 src/backend/bevy_audio.rs create mode 100644 src/backend/kira.rs create mode 100644 src/backend/oddio.rs create mode 100644 src/dsp_graph.rs create mode 100644 src/dsp_manager.rs create mode 100644 src/dsp_source.rs delete mode 100644 src/kira_impl.rs delete mode 100644 src/oddio_impl.rs delete mode 100644 src/rodio_impl.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 115dd2b..d3a968e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -1,107 +1,44 @@ -on: [push, pull_request] +name: CI -name: Continuous integration +on: + push: + branches: [main] + pull_request: + branches: [main] -jobs: - check_bevy_audio: - name: check bevy_audio - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - name: Install alsa and udev - run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev - - uses: actions-rs/cargo@v1 - with: - command: check - - test_bevy_audio: - name: test bevy_audio - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - name: Install alsa and udev - run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev - - uses: actions-rs/cargo@v1 - with: - command: test +env: + CARGO_TERM_COLOR: always - check_kira: - name: check kira - runs-on: ubuntu-latest +jobs: + test: + strategy: + fail-fast: false + matrix: + feature: [bevy_audio, kira, oddio] + os: [windows-latest, ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - name: Install alsa and udev - run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev - - uses: actions-rs/cargo@v1 + - uses: actions/checkout@v2 + - uses: actions/cache@v2 with: - command: check - args: --no-default-features --features kira - - test_kira: - name: test kira - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-build-${{ matrix.toolchain }}-${{ hashFiles('**/Cargo.toml') }} - uses: actions-rs/toolchain@v1 with: - profile: minimal toolchain: stable override: true - name: Install alsa and udev run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev + if: runner.os == 'linux' - uses: actions-rs/cargo@v1 with: command: test - args: --no-default-features --features kira - - # check_oddio: - # name: check oddio - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v3 - # - uses: actions-rs/toolchain@v1 - # with: - # profile: minimal - # toolchain: stable - # override: true - # - name: Install alsa and udev - # run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev - # - uses: actions-rs/cargo@v1 - # with: - # command: check - # args: --no-default-features --features oddio - - # test_oddio: - # name: test oddio - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v3 - # - uses: actions-rs/toolchain@v1 - # with: - # profile: minimal - # toolchain: stable - # override: true - # - name: Install alsa and udev - # run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev - # - uses: actions-rs/cargo@v1 - # with: - # command: test - # args: --no-default-features --features oddio + args: --no-default-features --features ${{ matrix.feature }} fmt: name: fmt @@ -138,3 +75,22 @@ jobs: with: command: clippy args: -- -D warnings + + miri: + name: miri + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + feature: [bevy_audio, kira, oddio] + steps: + - uses: actions/checkout@v3 + - name: Install Miri + run: | + rustup toolchain install nightly --component miri + rustup override set nightly + cargo miri setup + - name: Install alsa and udev + run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev + - name: Test with Miri + run: cargo miri test --no-default-features --features ${{ matrix.feature }} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..65fa299 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,41 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.2.0] - 14-11-2022 + +Reworked the majority of the internals. + +## Added + +- A way to play streaming DSP sources. See `SourceType::Dynamic`. +- You can play DSP sources using `Audio::play_dsp`. +- Two iterators on streaming audio sources: `Iter` and `IterMono`. + +### Changed + +- Adding the DSP plugin. + - You must now call `DspPlugin::default()`. +- The method on adding DSP sources. + - No more initializing using `DspAssets`! + - Just add your DSP function using `app.add_dsp_source` +- Playing DSP sources require `Audio` to be mutable. (Use `ResMut`) +- A lot of internals are shuffled around. + +### Removed + +- `DspAssets`. Initialize DSP graphs using `App::add_dsp_source` instead. +- `FnDspGraph`. This is now reworked to the trait `DspGraph` and can work with any type now. +- `StreamingDspSource`. This is now reworked to `Iter` (`bevy_kira_audio` and `bevy_oddio`) and `IterMono` (`bevy_audio`). + +## [0.1.0] - 01-08-22 + +- New bevy plugin! `bevy_fundsp` integrates fundsp with bevy. + +[Unreleased]: https://github.com/harudagondi/bevy_fundsp/compare/v0.2.0...HEAD +[0.2.0]: https://github.com/harudagondi/bevy_fundsp/compare/v0.1.0...v0.2.0 +[0.1.0]: https://github.com/harudagondi/bevy_fundsp/releases/tag/v0.1.0 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index f472392..b97ecd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bevy_fundsp" authors = ["Gio Genre De Asis"] -version = "0.1.0" +version = "0.2.0" edition = "2021" description = "A Bevy plugin that integrates FunDSP into Bevy." homepage = "https://github.com/harudagondi/bevy_fundsp" @@ -12,36 +12,52 @@ categories = ["game-development", "multimedia::audio"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["bevy_audio"] +kira = ["dep:kira", "bevy_kira_audio"] +bevy_audio = ["bevy/bevy_audio", "bevy/wav", "rodio"] +oddio = ["bevy_oddio"] + [dependencies] fundsp = "0.9" -kira = { version = "0.6", default-features = false, features = ["wav"], optional = true } -# bevy_oddio = { git = "https://github.com/harudagondi/bevy_oddio.git", optional = true } +cpal = "0.14" +once_cell = "1.13" +rodio = { version = "0.16", default-features = false, features = ["wav"], optional = true } +kira = { version = "0.7", default-features = false, features = ["wav"], optional = true } +ringbuf = "0.3" +parking_lot = "0.12" -atomic = "0.5" +[dependencies.uuid] +version = "1.1" +features = [ + "v5" +] [dependencies.bevy] # git = "https://github.com/bevyengine/bevy" -version = "0.8" +version = "0.9" default-features = false features = ["bevy_asset"] [dependencies.bevy_kira_audio] # git = "https://github.com/NiklasEi/bevy_kira_audio.git" # branch = "bevy_main" -version = "0.11" +version = "0.13" default-features = false features = ["wav"] optional = true -[features] -default = ["bevy_audio"] -kira = ["dep:kira", "bevy_kira_audio"] -bevy_audio = ["bevy/bevy_audio", "bevy/wav"] -# oddio = ["bevy_oddio"] +[dependencies.bevy_oddio] +# git = "https://github.com/harudagondi/bevy_oddio" +# branch = "bevy_main" +version = "0.3" +default-features = false +optional = true +features = ["wav"] [dev-dependencies.bevy] # git = "https://github.com/bevyengine/bevy" -version = "0.8" +version = "0.9" default-features = false features = [ "render", @@ -76,7 +92,17 @@ name = "kira_interactive" path = "examples/kira/interactive.rs" required-features = ["kira"] -# [[example]] -# name = "oddio_noise" -# path = "examples/oddio/noise.rs" -# required-features = ["oddio"] \ No newline at end of file +[[example]] +name = "oddio_noise" +path = "examples/oddio/noise.rs" +required-features = ["oddio"] + +[[example]] +name = "oddio_interactive" +path = "examples/oddio/interactive.rs" +required-features = ["oddio"] + +[[example]] +name = "oddio_controlled" +path = "examples/oddio/controlled.rs" +required-features = ["oddio"] \ No newline at end of file diff --git a/README.md b/README.md index 941ac52..535304d 100644 --- a/README.md +++ b/README.md @@ -20,18 +20,15 @@ or multiplying your DSP graph with a low constant (lower than 1.0). ```rust no_run #![allow(clippy::precedence)] -use bevy_fundsp::prelude::*; use bevy::prelude::*; +use bevy_fundsp::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugin(DspPlugin) - .add_startup_system(init_dsp) - .add_startup_system_to_stage( - StartupStage::PostStartup, - play_noise - ) + .add_plugin(DspPlugin::default()) + .add_dsp_source(white_noise, SourceType::Dynamic) + .add_startup_system_to_stage(StartupStage::PostStartup, play_noise) .run(); } @@ -39,23 +36,26 @@ fn white_noise() -> impl AudioUnit32 { white() >> split::() * 0.2 } -fn init_dsp(mut dsp_manager: ResMut) { - dsp_manager.add_graph(white_noise, 30.0); // length is in seconds -} - -fn play_noise(dsp_assets: Res, audio: Res