feat: modularize sin wave generator

This commit is contained in:
2026-10-18 13:52:00 +02:00
parent 27ee2efd00
commit 1682a26c61
6 changed files with 78 additions and 21 deletions
+3 -3
View File
@@ -8,7 +8,7 @@
pub struct AudioPlayer {
stream: Stream,
}
use super::Generator;
use crate::generators::GeneratorFn;
use cpal::{
Device, Error, ErrorKind, FromSample, I24, OutputCallbackInfo, Sample, SampleFormat, SizedSample, Stream, StreamConfig, SupportedStreamConfig, traits::{DeviceTrait, StreamTrait},
};
@@ -27,7 +27,7 @@ impl AudioPlayer {
/// Returns an error if:
/// - the device reports a sample format `raust` does not support, or
/// - CPAL fails to build the output stream.
pub fn try_new(device: &Device, config: SupportedStreamConfig, generator: Generator) -> anyhow::Result<Self> {
pub fn try_new(device: &Device, config: SupportedStreamConfig, generator: GeneratorFn) -> anyhow::Result<Self> {
let stream = match config.sample_format() {
SampleFormat::I8 => Self::create_stream::<i8>(device, config.into(), generator),
SampleFormat::I16 => Self::create_stream::<i16>(device, config.into(), generator),
@@ -57,7 +57,7 @@ impl AudioPlayer {
pub fn create_stream<T>(
device: &Device,
config: StreamConfig,
mut generator: Generator,
mut generator: GeneratorFn,
) -> Result<Stream, anyhow::Error>
where
T: SizedSample + FromSample<f32>,