audio_sdk 0.1.1

Creating Simple SDk
Documentation
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use std::time::Duration;
use std::f32::consts::PI;
use std::sync::{Arc, Mutex};
use std::thread;

/// Function to play a tone with specified frequency, duration, volume, and latency using cpal
pub fn play_tone(frequency: u32, duration_ms: u64, volume: f32, latency: Option<Duration>) {
    // Get the default host and device
    let host = cpal::default_host();
    let device = host.default_output_device().expect("No output device available");

    // Set up a default configuration
    let config = device.default_output_config().unwrap();
    let config: cpal::StreamConfig = config.into();

    // Prepare the sine wave data
    let sample_rate = config.sample_rate.0 as f32;
    let sample_count = (sample_rate * duration_ms as f32 / 1000.0) as usize;
    let mut sample_clock = 0f32;

    let samples: Vec<f32> = (0..sample_count)
        .map(|_| {
            let value = (sample_clock * frequency as f32 * 2.0 * PI / sample_rate).sin();
            sample_clock = (sample_clock + 1.0) % sample_rate;
            value * volume // Apply the provided volume
        })
        .collect();

    // Shared state between the audio callback and the main thread
    let samples = Arc::new(Mutex::new(samples));
    let samples_for_thread = samples.clone();

    // Create the audio stream with additional latency parameter
    let stream = device.build_output_stream(
        &config,
        move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
            let mut samples = samples_for_thread.lock().unwrap();
            for sample in data.iter_mut() {
                if let Some(value) = samples.pop() {
                    *sample = value;
                } else {
                    *sample = 0.0;
                }
            }
        },
        err_fn,
        latency // Add latency as the fourth parameter
    ).unwrap();

    // Play the stream
    stream.play().unwrap();

    // Sleep for the duration of the tone
    thread::sleep(Duration::from_millis(duration_ms));
}

// Error callback function
fn err_fn(err: cpal::StreamError) {
    eprintln!("An error occurred on the audio stream: {}", err);
}