[][src]Struct wavy::Speakers

pub struct Speakers<S: Sample + Unpin> where
    Ch16: From<S::Chan>, 
{ /* fields omitted */ }

Play audio samples through speaker system.

440 HZ Sine Wave Example

note: This example depends on twang = "0.3" to synthesize the sine wave.

use fon::mono::Mono64;
use pasts::{prelude::*, CvarExec};
use std::cell::RefCell;
use twang::Synth;
use wavy::Speakers;

/// The program's shared state.
struct State {}

/// Speakers task (play sine wave).
async fn speakers(state: &RefCell<State>) {
    // Connect to system's speaker(s)
    let mut speakers = Speakers::<Mono64>::new();
    // Create a new synthesizer
    let mut synth = Synth::new();

    loop {
        // 1. Wait for speaker to need more samples.
        let audio = speakers.play().await;
        // 2. Borrow shared state mutably
        let _state = state.borrow_mut();
        // 3. Generate and write samples into speaker buffer.
        synth.gen(audio, |fc| fc.freq(440.0).sine().amp(0.7));
    }
}

/// Program start.
async fn start() {
    // Initialize shared state.
    let state = RefCell::new(State {});
    // Create speaker task.
    let mut speakers = speakers(&state);
    // Wait for first task to complete.
    [speakers.fut()].select().await;
}

/// Start the async executor.
fn main() {
    static EXECUTOR: CvarExec = CvarExec::new();
    EXECUTOR.block_on(start())
}

Implementations

impl<S: Sample + Unpin> Speakers<S> where
    Ch16: From<S::Chan>,
    Ch32: From<S::Chan>,
    Ch64: From<S::Chan>, 
[src]

pub fn new() -> Self[src]

Connect to the speaker system.

Panics

  • If already connected to the speaker system.

pub fn sample_rate(&self) -> u32[src]

Get the speakers' sample rate.

pub async fn play<'_, '_>(&'_ mut self) -> impl Sink<S> + '_[src]

Play audio through speakers. Returns mutable reference to next audio buffer to play. If you don't overwrite the buffer, it will keep playing whatever was last written into it.

Auto Trait Implementations

impl<S> RefUnwindSafe for Speakers<S> where
    S: RefUnwindSafe

impl<S> !Send for Speakers<S>

impl<S> !Sync for Speakers<S>

impl<S> Unpin for Speakers<S>

impl<S> UnwindSafe for Speakers<S> where
    S: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.