Struct cubeb::Stream

source ·
pub struct Stream<F>(/* private fields */);
Expand description

Audio input/output stream

§Example

extern crate cubeb;
use std::thread;
use std::time::Duration;

type Frame = cubeb::MonoFrame<f32>;

fn main() {
    let ctx = cubeb::init("Cubeb tone example").unwrap();

    let params = cubeb::StreamParamsBuilder::new()
        .format(cubeb::SampleFormat::Float32LE)
        .rate(44_100)
        .channels(1)
        .layout(cubeb::ChannelLayout::MONO)
        .prefs(cubeb::StreamPrefs::NONE)
        .take();

    let phase_inc = 440.0 / 44_100.0;
    let mut phase = 0.0;
    let volume = 0.25;

    let mut builder = cubeb::StreamBuilder::<Frame>::new();
    builder
        .name("Cubeb Square Wave")
        .default_output(&params)
        .latency(0x1000)
        .data_callback(move |_, output| {
            // Generate a square wave
            for x in output.iter_mut() {
                x.m = if phase < 0.5 { volume } else { -volume };
                phase = (phase + phase_inc) % 1.0;
            }

            output.len() as isize
        })
        .state_callback(|state| {
            println!("stream {:?}", state);
        });
    let stream = builder.init(&ctx).expect("Failed to create stream.");

    // Start playback
    stream.start().unwrap();

    // Play for 1/2 second
    thread::sleep(Duration::from_millis(500));

    // Shutdown
    stream.stop().unwrap();
}

Methods from Deref<Target = Stream>§

Methods from Deref<Target = StreamRef>§

source

pub fn as_ptr(&self) -> *mut cubeb_stream

source

pub fn start(&self) -> Result<(), Error>

Start playback.

Examples found in repository?
examples/tone.rs (line 57)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
fn main() {
    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");

    let params = cubeb::StreamParamsBuilder::new()
        .format(STREAM_FORMAT)
        .rate(SAMPLE_FREQUENCY)
        .channels(1)
        .layout(cubeb::ChannelLayout::MONO)
        .take();

    let mut position = 0u32;

    let mut builder = cubeb::StreamBuilder::<Frame>::new();
    builder
        .name("Cubeb tone (mono)")
        .default_output(&params)
        .latency(0x1000)
        .data_callback(move |_, output| {
            // generate our test tone on the fly
            for f in output.iter_mut() {
                // North American dial tone
                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();

                f.m = i16::from_float(0.5 * (t1 + t2));

                position += 1;
            }
            output.len() as isize
        })
        .state_callback(|state| {
            println!("stream {:?}", state);
        });

    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");

    stream.start().unwrap();
    thread::sleep(Duration::from_millis(500));
    stream.stop().unwrap();
}
source

pub fn stop(&self) -> Result<(), Error>

Stop playback.

Examples found in repository?
examples/tone.rs (line 59)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
fn main() {
    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");

    let params = cubeb::StreamParamsBuilder::new()
        .format(STREAM_FORMAT)
        .rate(SAMPLE_FREQUENCY)
        .channels(1)
        .layout(cubeb::ChannelLayout::MONO)
        .take();

    let mut position = 0u32;

    let mut builder = cubeb::StreamBuilder::<Frame>::new();
    builder
        .name("Cubeb tone (mono)")
        .default_output(&params)
        .latency(0x1000)
        .data_callback(move |_, output| {
            // generate our test tone on the fly
            for f in output.iter_mut() {
                // North American dial tone
                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();

                f.m = i16::from_float(0.5 * (t1 + t2));

                position += 1;
            }
            output.len() as isize
        })
        .state_callback(|state| {
            println!("stream {:?}", state);
        });

    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");

    stream.start().unwrap();
    thread::sleep(Duration::from_millis(500));
    stream.stop().unwrap();
}
source

pub fn position(&self) -> Result<u64, Error>

Get the current stream playback position.

source

pub fn latency(&self) -> Result<u32, Error>

Get the latency for this stream, in frames. This is the number of frames between the time cubeb acquires the data in the callback and the listener can hear the sound.

source

pub fn input_latency(&self) -> Result<u32, Error>

Get the input latency for this stream, in frames. This is the number of frames between the time the audio input device records the audio, and the cubeb callback delivers it. This returns an error if the stream is output-only.

source

pub fn set_volume(&self, volume: f32) -> Result<(), Error>

Set the volume for a stream.

source

pub fn set_name(&self, name: &CStr) -> Result<(), Error>

Change a stream’s name

source

pub fn current_device(&self) -> Result<&DeviceRef, Error>

Get the current output device for this stream.

source

pub fn set_input_mute(&self, mute: bool) -> Result<(), Error>

Set the mute state for an input stream.

source

pub fn set_input_processing_params( &self, params: InputProcessingParams ) -> Result<(), Error>

Set the processing parameters for an input stream.

source

pub fn device_destroy(&self, device: DeviceRef) -> Result<(), Error>

Destroy a cubeb_device structure.

source

pub fn register_device_changed_callback( &self, callback: Option<unsafe extern "C" fn(_: *mut c_void)> ) -> Result<(), Error>

Set a callback to be notified when the output device changes.

source

pub fn user_ptr(&self) -> *mut c_void

Trait Implementations§

source§

impl<F> Deref for Stream<F>

§

type Target = Stream

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<F> Drop for Stream<F>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<F> Freeze for Stream<F>

§

impl<F> RefUnwindSafe for Stream<F>
where F: RefUnwindSafe,

§

impl<F> !Send for Stream<F>

§

impl<F> !Sync for Stream<F>

§

impl<F> Unpin for Stream<F>

§

impl<F> UnwindSafe for Stream<F>
where F: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.