From e15b8bffec381ea648d668fdb8b80c8b6a7ec31d Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Thu, 4 Apr 2024 21:14:17 -0400 Subject: [PATCH 1/7] Add sine integration test --- examples/sine.rs | 2 +- tests/sine.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/sine.rs diff --git a/examples/sine.rs b/examples/sine.rs index 5d99b9660..2fb5a00e4 100644 --- a/examples/sine.rs +++ b/examples/sine.rs @@ -44,7 +44,7 @@ fn main() -> Result<(), coreaudio::Error> { // Read the input format. This is counterintuitive, but it's the format used when sending // audio data to the AudioUnit representing the output device. This is separate from the // format the AudioUnit later uses to send the data to the hardware device. - let stream_format = audio_unit.input_stream_format()?; + let stream_format = audio_unit.output_stream_format()?; println!("{:#?}", &stream_format); // For this example, our sine wave expects `f32` data. diff --git a/tests/sine.rs b/tests/sine.rs new file mode 100644 index 000000000..2e06c9393 --- /dev/null +++ b/tests/sine.rs @@ -0,0 +1,75 @@ + +//! A basic output stream example, using an Output AudioUnit to generate a sine wave. + +extern crate coreaudio; + +use coreaudio::audio_unit::render_callback::{self, data}; +use coreaudio::audio_unit::{AudioUnit, IOType, SampleFormat}; +use std::f64::consts::PI; + +struct SineWaveGenerator { + time: f64, + /// generated frequency in Hz + freq: f64, + /// magnitude of generated signal + volume: f64, +} + +impl SineWaveGenerator { + fn new(freq: f64, volume: f64) -> Self { + SineWaveGenerator { + time: 0., + freq, + volume, + } + } +} + +impl Iterator for SineWaveGenerator { + type Item = f32; + fn next(&mut self) -> Option { + self.time += 1. / 44_100.; + let output = ((self.freq * self.time * PI * 2.).sin() * self.volume) as f32; + Some(output) + } +} + +#[test] +fn sine() -> Result<(), coreaudio::Error> { + let frequency_hz = 440.; + let volume = 0.15; + let mut samples = SineWaveGenerator::new(frequency_hz, volume); + + // Construct an Output audio unit that delivers audio to the default output device. + let mut audio_unit = AudioUnit::new(IOType::DefaultOutput)?; + + // Read the input format. This is counterintuitive, but it's the format used when sending + // audio data to the AudioUnit representing the output device. This is separate from the + // format the AudioUnit later uses to send the data to the hardware device. + let stream_format = audio_unit.output_stream_format()?; + println!("{:#?}", &stream_format); + + // For this example, our sine wave expects `f32` data. + assert!(SampleFormat::F32 == stream_format.sample_format); + + type Args = render_callback::Args>; + audio_unit.set_render_callback(move |args| { + let Args { + num_frames, + mut data, + .. + } = args; + for i in 0..num_frames { + let sample = samples.next().unwrap(); + for channel in data.channels_mut() { + channel[i] = sample; + } + } + Ok(()) + })?; + audio_unit.start()?; + + std::thread::sleep(std::time::Duration::from_millis(50)); + + Ok(()) +} From 8105da228d6e02be94296160f87f8f6b2289fd5e Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Thu, 4 Apr 2024 23:59:36 -0400 Subject: [PATCH 2/7] Move to llvm action rather than homebrew line --- .github/workflows/coreaudio-rs.yml | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index 577ea8504..dabc9a817 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -6,8 +6,10 @@ jobs: runs-on: macOS-latest steps: - uses: actions/checkout@v4 - - name: Install llvm and clang - run: brew install llvm + - name: Install LLVM and Clang + uses: KyleMayes/install-llvm-action@v2.0 + with: + version: "15.0" - name: Install stable uses: actions-rs/toolchain@v1 with: @@ -34,8 +36,10 @@ jobs: runs-on: macOS-latest steps: - uses: actions/checkout@v4 - - name: Install llvm and clang - run: brew install llvm + - name: Install LLVM and Clang + uses: KyleMayes/install-llvm-action@v2.0 + with: + version: "15.0" - name: Install stable uses: actions-rs/toolchain@v1 with: @@ -54,8 +58,10 @@ jobs: runs-on: macOS-latest steps: - uses: actions/checkout@v4 - - name: Install llvm and clang - run: brew install llvm + - name: Install LLVM and Clang + uses: KyleMayes/install-llvm-action@v2.0 + with: + version: "15.0" - name: Install stable uses: actions-rs/toolchain@v1 with: @@ -74,8 +80,10 @@ jobs: runs-on: macOS-latest steps: - uses: actions/checkout@v4 - - name: Install llvm and clang - run: brew install llvm + - name: Install LLVM and Clang + uses: KyleMayes/install-llvm-action@v2.0 + with: + version: "15.0" - name: Install stable uses: actions-rs/toolchain@v1 with: From 9dc52be32f21399147bce6599f7aeeecd896184c Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Fri, 5 Apr 2024 00:02:28 -0400 Subject: [PATCH 3/7] Fix ci --- .github/workflows/coreaudio-rs.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index dabc9a817..20abbf999 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -7,7 +7,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang - uses: KyleMayes/install-llvm-action@v2.0 + uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - name: Install stable @@ -37,7 +37,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang - uses: KyleMayes/install-llvm-action@v2.0 + uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - name: Install stable @@ -59,7 +59,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang - uses: KyleMayes/install-llvm-action@v2.0 + uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - name: Install stable @@ -81,7 +81,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang - uses: KyleMayes/install-llvm-action@v2.0 + uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - name: Install stable From ad35b248cd3e897724627c48de62669e6516b1d9 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Fri, 5 Apr 2024 11:20:06 -0400 Subject: [PATCH 4/7] Update CI some more --- .github/workflows/coreaudio-rs.yml | 25 ++++++++----------------- tests/sine.rs | 12 +++--------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index 20abbf999..b2f66f391 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -3,19 +3,19 @@ on: [push, pull_request] jobs: # Run cargo test with default, no and all features. macos-test: - runs-on: macOS-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [macOS-latest, macOS-14] steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - - name: Install stable - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: stable - override: true - name: cargo test run: cargo test --verbose # TODO: These don't work as of 2020-12-06, but they should. @@ -40,12 +40,9 @@ jobs: uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - - name: Install stable - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: stable - override: true - name: Add iOS targets run: rustup target add aarch64-apple-ios x86_64-apple-ios - name: Install cargo lipo @@ -62,12 +59,9 @@ jobs: uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - - name: Install stable - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: stable - override: true - name: cargo doc - all features run: cargo doc --all-features --verbose @@ -84,12 +78,9 @@ jobs: uses: KyleMayes/install-llvm-action@v2.0.1 with: version: "15.0" - - name: Install stable - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: stable - override: true - name: cargo publish continue-on-error: true run: cargo publish --token $CRATESIO_TOKEN diff --git a/tests/sine.rs b/tests/sine.rs index 2e06c9393..b3aa88999 100644 --- a/tests/sine.rs +++ b/tests/sine.rs @@ -1,7 +1,4 @@ - -//! A basic output stream example, using an Output AudioUnit to generate a sine wave. - -extern crate coreaudio; +// This is from examples/sine.rs use coreaudio::audio_unit::render_callback::{self, data}; use coreaudio::audio_unit::{AudioUnit, IOType, SampleFormat}; @@ -37,15 +34,12 @@ impl Iterator for SineWaveGenerator { #[test] fn sine() -> Result<(), coreaudio::Error> { let frequency_hz = 440.; - let volume = 0.15; + let volume = 0.01; // Lets have this audio be very quiet. let mut samples = SineWaveGenerator::new(frequency_hz, volume); // Construct an Output audio unit that delivers audio to the default output device. let mut audio_unit = AudioUnit::new(IOType::DefaultOutput)?; - // Read the input format. This is counterintuitive, but it's the format used when sending - // audio data to the AudioUnit representing the output device. This is separate from the - // format the AudioUnit later uses to send the data to the hardware device. let stream_format = audio_unit.output_stream_format()?; println!("{:#?}", &stream_format); @@ -69,7 +63,7 @@ fn sine() -> Result<(), coreaudio::Error> { })?; audio_unit.start()?; - std::thread::sleep(std::time::Duration::from_millis(50)); + std::thread::sleep(std::time::Duration::from_millis(100)); Ok(()) } From fd0da4b008a24baace7f7c238c247b9b12e9c296 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Sat, 6 Apr 2024 12:46:53 -0400 Subject: [PATCH 5/7] Add macos 12 and 13 to ci --- .github/workflows/coreaudio-rs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index b2f66f391..68c406c94 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -6,7 +6,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macOS-latest, macOS-14] + os: [macOS-12, macOS-13 macOS-14] steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang From 2eb761108ad798c8efa87e50a144b2ecbf47b7b4 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Sat, 6 Apr 2024 12:53:15 -0400 Subject: [PATCH 6/7] fix ci --- .github/workflows/coreaudio-rs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index 68c406c94..077afe7bc 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -6,7 +6,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macOS-12, macOS-13 macOS-14] + os: [macOS-12, macOS-13, macOS-14] steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang From b829fb7de8cb62c9fe6aafd40afa33342cd95444 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 8 Apr 2024 17:23:59 -0400 Subject: [PATCH 7/7] Run sine example in ci --- .github/workflows/coreaudio-rs.yml | 4 +- Cargo.toml | 2 +- tests/sine.rs | 69 ------------------------------ 3 files changed, 4 insertions(+), 71 deletions(-) delete mode 100644 tests/sine.rs diff --git a/.github/workflows/coreaudio-rs.yml b/.github/workflows/coreaudio-rs.yml index 077afe7bc..dcbf6124a 100644 --- a/.github/workflows/coreaudio-rs.yml +++ b/.github/workflows/coreaudio-rs.yml @@ -6,7 +6,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macOS-12, macOS-13, macOS-14] + os: [macOS-latest] steps: - uses: actions/checkout@v4 - name: Install LLVM and Clang @@ -18,6 +18,8 @@ jobs: toolchain: stable - name: cargo test run: cargo test --verbose + - name: Run sign example + run: cargo run --example sine # TODO: These don't work as of 2020-12-06, but they should. # - name: cargo test - no features # run: cargo test --no-default-features --verbose diff --git a/Cargo.toml b/Cargo.toml index a5bdcde79..8ce822e7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "coreaudio-rs" -version = "0.11.3" +version = "0.12.0" authors = ["mitchmindtree ", "yupferris "] description = "A friendly rust interface for Apple's CoreAudio API." keywords = ["core", "audio", "unit", "osx", "ios"] diff --git a/tests/sine.rs b/tests/sine.rs deleted file mode 100644 index b3aa88999..000000000 --- a/tests/sine.rs +++ /dev/null @@ -1,69 +0,0 @@ -// This is from examples/sine.rs - -use coreaudio::audio_unit::render_callback::{self, data}; -use coreaudio::audio_unit::{AudioUnit, IOType, SampleFormat}; -use std::f64::consts::PI; - -struct SineWaveGenerator { - time: f64, - /// generated frequency in Hz - freq: f64, - /// magnitude of generated signal - volume: f64, -} - -impl SineWaveGenerator { - fn new(freq: f64, volume: f64) -> Self { - SineWaveGenerator { - time: 0., - freq, - volume, - } - } -} - -impl Iterator for SineWaveGenerator { - type Item = f32; - fn next(&mut self) -> Option { - self.time += 1. / 44_100.; - let output = ((self.freq * self.time * PI * 2.).sin() * self.volume) as f32; - Some(output) - } -} - -#[test] -fn sine() -> Result<(), coreaudio::Error> { - let frequency_hz = 440.; - let volume = 0.01; // Lets have this audio be very quiet. - let mut samples = SineWaveGenerator::new(frequency_hz, volume); - - // Construct an Output audio unit that delivers audio to the default output device. - let mut audio_unit = AudioUnit::new(IOType::DefaultOutput)?; - - let stream_format = audio_unit.output_stream_format()?; - println!("{:#?}", &stream_format); - - // For this example, our sine wave expects `f32` data. - assert!(SampleFormat::F32 == stream_format.sample_format); - - type Args = render_callback::Args>; - audio_unit.set_render_callback(move |args| { - let Args { - num_frames, - mut data, - .. - } = args; - for i in 0..num_frames { - let sample = samples.next().unwrap(); - for channel in data.channels_mut() { - channel[i] = sample; - } - } - Ok(()) - })?; - audio_unit.start()?; - - std::thread::sleep(std::time::Duration::from_millis(100)); - - Ok(()) -}