Fix compile issues in wasm-beep example (#723)

Fixes #721

Changes to a dependency library broke compilation of the example. Changes needed to get the example working and warning-free include:
 
1. specifying additional type constraints in local implementations
2. using the `FromSample` trait to convert samples in the handler
3. adding a default matcher in response to the non-exhaustive attribute on `SampleFormat`

Also adds a github action to ensure the wasm-beep example compiles.
This commit is contained in:
Greg Wiley
2022-11-05 20:34:08 -07:00
committed by GitHub
parent a9b34ea8bc
commit edecb26ff3
2 changed files with 19 additions and 3 deletions

View File

@@ -313,3 +313,17 @@ jobs:
- name: Build iphonesimulator feedback example
run: cd examples/ios-feedback && xcodebuild -scheme cpal-ios-example -configuration Debug -derivedDataPath build -sdk iphonesimulator
wasm-beep-build:
# this only confirms that the Rust source builds
# and checks to prevent regressions like #721.
#
# It does not test the javascript/web integration
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Target
run: rustup target add wasm32-unknown-unknown
- name: Cargo Build
working-directory: ./examples/wasm-beep
run: cargo build --target wasm32-unknown-unknown

View File

@@ -38,12 +38,13 @@ pub fn beep() -> Handle {
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
_ => panic!("unsupported sample format"),
})
}
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Stream
where
T: cpal::Sample,
T: cpal::Sample + cpal::SizedSample + cpal::FromSample<f32>,
{
let sample_rate = config.sample_rate.0 as f32;
let channels = config.channels as usize;
@@ -71,10 +72,11 @@ where
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
where
T: cpal::Sample,
T: cpal::Sample + cpal::FromSample<f32>,
{
for frame in output.chunks_mut(channels) {
let value: T = cpal::Sample::from::<f32>(&next_sample());
let sample = next_sample();
let value = T::from_sample::<f32>(sample);
for sample in frame.iter_mut() {
*sample = value;
}