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(¶ms)
.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>
impl<'a, F> StreamBuilder<'a, F>
Sourcepub fn new() -> StreamBuilder<'a, F>
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(¶ms)
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}Sourcepub fn data_callback<D>(&mut self, cb: D) -> &mut Self
pub fn data_callback<D>(&mut self, cb: D) -> &mut Self
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(¶ms)
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}Sourcepub fn state_callback<S>(&mut self, cb: S) -> &mut Self
pub fn state_callback<S>(&mut self, cb: S) -> &mut Self
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(¶ms)
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}Sourcepub fn name<T: Into<Vec<u8>>>(&mut self, name: T) -> &mut Self
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(¶ms)
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}Sourcepub fn default_input(&mut self, params: &'a StreamParamsRef) -> &mut Self
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
Sourcepub fn input(
&mut self,
device: DeviceId,
params: &'a StreamParamsRef,
) -> &mut Self
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
Sourcepub fn default_output(&mut self, params: &'a StreamParamsRef) -> &mut Self
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(¶ms)
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}Sourcepub fn output(
&mut self,
device: DeviceId,
params: &'a StreamParamsRef,
) -> &mut Self
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
Sourcepub fn latency(&mut self, latency: u32) -> &mut Self
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(¶ms)
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}Sourcepub fn device_changed_cb<CB>(&mut self, cb: CB) -> &mut Self
pub fn device_changed_cb<CB>(&mut self, cb: CB) -> &mut Self
Sourcepub fn init(self, ctx: &ContextRef) -> Result<Stream<F>>
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(¶ms)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more