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(¶ms)
.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>§
pub fn as_ptr(&self) -> *mut cubeb_stream
Methods from Deref<Target = StreamRef>§
pub fn as_ptr(&self) -> *mut cubeb_stream
sourcepub fn start(&self) -> Result<(), Error>
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(¶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");
stream.start().unwrap();
thread::sleep(Duration::from_millis(500));
stream.stop().unwrap();
}
sourcepub fn stop(&self) -> Result<(), Error>
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(¶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");
stream.start().unwrap();
thread::sleep(Duration::from_millis(500));
stream.stop().unwrap();
}
sourcepub fn latency(&self) -> Result<u32, Error>
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.
sourcepub fn input_latency(&self) -> Result<u32, Error>
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.
sourcepub fn current_device(&self) -> Result<&DeviceRef, Error>
pub fn current_device(&self) -> Result<&DeviceRef, Error>
Get the current output device for this stream.
sourcepub fn set_input_mute(&self, mute: bool) -> Result<(), Error>
pub fn set_input_mute(&self, mute: bool) -> Result<(), Error>
Set the mute state for an input stream.
sourcepub fn set_input_processing_params(
&self,
params: InputProcessingParams
) -> Result<(), Error>
pub fn set_input_processing_params( &self, params: InputProcessingParams ) -> Result<(), Error>
Set the processing parameters for an input stream.
sourcepub fn device_destroy(&self, device: DeviceRef) -> Result<(), Error>
pub fn device_destroy(&self, device: DeviceRef) -> Result<(), Error>
Destroy a cubeb_device structure.
sourcepub fn register_device_changed_callback(
&self,
callback: Option<unsafe extern "C" fn(_: *mut c_void)>
) -> Result<(), Error>
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.
pub fn user_ptr(&self) -> *mut c_void
Trait Implementations§
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> 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