Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Aug 27, 2024
1 parent 3e9f333 commit 9348c15
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cfg-if = "1.0.0"
wasm-bindgen = "0.2"
tsify-next = "0.5"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand Down
31 changes: 18 additions & 13 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;
use cpal::{traits::{DeviceTrait, HostTrait, StreamTrait}, SampleFormat, Stream, StreamError, SupportedStreamConfig};

use cpal::{traits::{DeviceTrait, HostTrait, StreamTrait}, BuildStreamError, SampleFormat, Stream, SupportedStreamConfig};
use crate::errors::BeeperError;

pub struct Beeper {
device: cpal::Device,
Expand All @@ -9,9 +9,9 @@ pub struct Beeper {
vol: f32
}
impl Beeper {
pub fn new(vol: f32) -> Result<Self, Box<dyn Error>> {
pub fn new(vol: f32) -> Result<Self, BeeperError> {
let host = cpal::default_host();
let device = host.default_output_device().ok_or(std::fmt::Error {})?;
let device = host.default_output_device().ok_or(BeeperError::NoDefaultOutputDevice)?;
let supported_config = device.default_output_config().unwrap();
let config = supported_config.config();
let sample_format = supported_config.sample_format();
Expand All @@ -28,7 +28,7 @@ impl Beeper {
vol,
})
}
pub fn set_vol(&mut self, vol: f32) -> Result<(), Box<dyn Error>> {
pub fn set_vol(&mut self, vol: f32) -> Result<(), BeeperError> {
if self.vol != vol {
let sample_format = self.supported_config.sample_format();
let config = self.supported_config.config();
Expand All @@ -49,7 +49,7 @@ impl Beeper {
}
}

pub fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig, vol: f32) -> Result<Stream, BuildStreamError>
pub fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig, vol: f32) -> Result<Stream, BeeperError>
where
T: cpal::Sample,
{
Expand All @@ -63,15 +63,20 @@ where
((sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin() / 6.0) * vol
};

let err_fn = |err| eprintln!("an error occurred on stream: {}", err);
#[cfg(not(target_arch = "wasm32"))]
let err_fn = |err: StreamError| eprintln!("an error occurred on stream: {}", err);

#[cfg(target_arch = "wasm32")]
let err_fn = |err: StreamError| { gloo_console::log!("an error occurred on stream: {}", err.to_string()) };

device.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
Ok(
device.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
)?
)
}

Expand Down
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cpal::BuildStreamError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum BeeperError {
#[error("No default output device found")]
NoDefaultOutputDevice,
#[error("Error occured whilst building stream: {0}")]
BuildStream(#[from] BuildStreamError),
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod chip8;
pub mod errors;
pub mod input;
pub mod audio;
pub mod options;
Expand Down

0 comments on commit 9348c15

Please sign in to comment.