Skip to main content

StreamBuilder

Struct StreamBuilder 

Source
pub struct StreamBuilder<'a, F> { /* private fields */ }
Expand description

Stream builder

use cubeb::{Context, MonoFrame, Sample};
use std::f32::consts::PI;
use std::thread;
use std::time::Duration;

const SAMPLE_FREQUENCY: u32 = 48_000;
const STREAM_FORMAT: cubeb::SampleFormat = cubeb::SampleFormat::S16LE;
type Frame = MonoFrame<i16>;

let ctx = Context::init(None, None).unwrap();

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");

Implementations§

Source§

impl<'a, F> StreamBuilder<'a, F>

Source

pub fn new() -> StreamBuilder<'a, F>

Examples found in repository?
examples/tone.rs (line 33)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn data_callback<D>(&mut self, cb: D) -> &mut Self
where D: FnMut(&[F], &mut [F]) -> isize + Send + Sync + 'static,

User supplied data callback, see DataCallback

Examples found in repository?
examples/tone.rs (lines 38-50)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn state_callback<S>(&mut self, cb: S) -> &mut Self
where S: FnMut(State) + Send + Sync + 'static,

User supplied state callback, see StateCallback

Examples found in repository?
examples/tone.rs (lines 51-53)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn name<T: Into<Vec<u8>>>(&mut self, name: T) -> &mut Self

A name for this stream.

Examples found in repository?
examples/tone.rs (line 35)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn default_input(&mut self, params: &'a StreamParamsRef) -> &mut Self

Use the default input device with params

Optional if the stream is output only

Source

pub fn input( &mut self, device: DeviceId, params: &'a StreamParamsRef, ) -> &mut Self

Use a specific input device with params

Optional if the stream is output only

Source

pub fn default_output(&mut self, params: &'a StreamParamsRef) -> &mut Self

Use the default output device with params

Optional if the stream is input only

Examples found in repository?
examples/tone.rs (line 36)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn output( &mut self, device: DeviceId, params: &'a StreamParamsRef, ) -> &mut Self

Use a specific output device with params

Optional if the stream is input only

Source

pub fn latency(&mut self, latency: u32) -> &mut Self

Stream latency in frames.

Valid range is [1, 96000].

Examples found in repository?
examples/tone.rs (line 37)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}
Source

pub fn device_changed_cb<CB>(&mut self, cb: CB) -> &mut Self
where CB: FnMut() + Send + Sync + 'static,

User supplied callback called when the underlying device changed.

See StateCallback

Optional

Source

pub fn init(self, ctx: &ContextRef) -> Result<Stream<F>>

Build the stream

Examples found in repository?
examples/tone.rs (line 55)
21fn main() {
22    let ctx = common::init("Cubeb tone example").expect("Failed to create cubeb context");
23
24    let params = cubeb::StreamParamsBuilder::new()
25        .format(STREAM_FORMAT)
26        .rate(SAMPLE_FREQUENCY)
27        .channels(1)
28        .layout(cubeb::ChannelLayout::MONO)
29        .take();
30
31    let mut position = 0u32;
32
33    let mut builder = cubeb::StreamBuilder::<Frame>::new();
34    builder
35        .name("Cubeb tone (mono)")
36        .default_output(&params)
37        .latency(0x1000)
38        .data_callback(move |_, output| {
39            // generate our test tone on the fly
40            for f in output.iter_mut() {
41                // North American dial tone
42                let t1 = (2.0 * PI * 350.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
43                let t2 = (2.0 * PI * 440.0 * position as f32 / SAMPLE_FREQUENCY as f32).sin();
44
45                f.m = i16::from_float(0.5 * (t1 + t2));
46
47                position += 1;
48            }
49            output.len() as isize
50        })
51        .state_callback(|state| {
52            println!("stream {:?}", state);
53        });
54
55    let stream = builder.init(&ctx).expect("Failed to create cubeb stream");
56
57    stream.start().unwrap();
58    thread::sleep(Duration::from_millis(500));
59    stream.stop().unwrap();
60}

Trait Implementations§

Source§

impl<F> Default for StreamBuilder<'_, F>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, F> Freeze for StreamBuilder<'a, F>

§

impl<'a, F> !RefUnwindSafe for StreamBuilder<'a, F>

§

impl<'a, F> !Send for StreamBuilder<'a, F>

§

impl<'a, F> !Sync for StreamBuilder<'a, F>

§

impl<'a, F> Unpin for StreamBuilder<'a, F>

§

impl<'a, F> UnsafeUnpin for StreamBuilder<'a, F>

§

impl<'a, F> !UnwindSafe for StreamBuilder<'a, F>

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>,

Source§

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>,

Source§

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.