Struct oxcable::wrappers::Messaged [] [src]

pub struct Messaged<D> where D: MessageReceiver {
    // some fields omitted
}

Bundles a MessageReceiver with threaded message passing.

Sending messages

Once a device is wrapped with Messaged, one or more Senders can be returned from it. These represent the send end of standard Rust channels, and can be sent between threads freely.

As an AudioDevice

The wrapper implements AudioDevice if the contained device does as well. The implementation first checks for any incoming messages, then applies them to the interior device. Once the messages have been handled, it calls the normal tick function of the device.

This allows one to wrap a device in Messaged, move the sender to another thread, then drop the Messaged device into a normal container, such as DeviceChain or DeviceGraph. It will automatically receive and handle its own messages while processing.

Example

use std::thread;
use oxcable::chain::{DeviceChain, Tick};
use oxcable::oscillator::{Oscillator, Sine, SetFreq};
use oxcable::io::audio::AudioEngine;
use oxcable::wrappers::Messaged;

fn main() {
    // Initialize signal chain...
    let tx;
    let engine = AudioEngine::with_buffer_size(256).unwrap();
    let mut chain = DeviceChain::from({
        let msgd = Messaged::from(Oscillator::new(Sine).freq(440.0));
        tx = msgd.get_sender();
        msgd
    }).into(
        engine.default_output(1).unwrap()
    );

    // Send the messager to a new thread. It will wait for one second, then
    // set the oscillator to a higher frequency.
    thread::spawn(move || {
        thread::sleep_ms(1000);
        tx.send(SetFreq(880.0));
    });

    // Generate two seconds of audio, then exit...
    chain.tick_n_times(88200);
}

Methods

impl<D> Messaged<D> where D: MessageReceiver

fn get_sender(&self) -> Sender<D::Msg>

Return the sending half of our communication channel.

Trait Implementations

impl<D> From<D> for Messaged<D> where D: MessageReceiver

fn from(device: D) -> Self

impl<D> AudioDevice for Messaged<D> where D: AudioDevice + MessageReceiver

fn num_inputs(&self) -> usize

fn num_outputs(&self) -> usize

fn tick(&mut self, t: Time, inputs: &[Sample], outputs: &mut [Sample])

impl<D> Deref for Messaged<D> where D: AudioDevice + MessageReceiver

type Target = D

fn deref(&self) -> &D

impl<D> DerefMut for Messaged<D> where D: AudioDevice + MessageReceiver

fn deref_mut(&mut self) -> &mut D