Struct Buffer

Source
pub struct Buffer { /* private fields */ }
Expand description

Buffers store un-encoded float32 audio data at the sampling rate of the server. They can be loaded from files or arrays, and will resample the data exactly once when loaded. Buffers are most commonly used with buffer nodes. Save for the contained audio data, buffers are stateless; using them requires coupling them with a node. Since buffers are quite large, using a cache is recommended. Buffers may safely be used in more than one place at a time. Modifying a buffer’s audio data while it is in use will result in an error.

Implementations§

Source§

impl Buffer

Source

pub fn new(server: &Server) -> Result<Buffer>

Creates a new audio buffer.

Examples found in repository?
examples/file_playback.rs (line 14)
6fn main() {
7    libaudioverse::initialize().unwrap();
8    
9    // server initialization
10    let server = Server::new().unwrap();
11    server.set_output_device().expect("Could not create default audio device");
12    
13    // create a new buffer
14    let buf = Buffer::new(&server).unwrap();
15    let path = CString::new("test.ogg").unwrap();
16    
17    // load audio data from test.ogg into this buffer
18    buf.load_from_file(&path).unwrap();
19    
20    // create a Buffer node, associate it with the buffer just created, and play it
21    let buf_node = BufferNode::new(&server).unwrap();
22    buf_node.buffer().set(&buf).unwrap();
23    buf_node.connect_server(0).unwrap();
24    
25    // wait for the whole file to finish playing
26    let duration = buf_node.position().get_range().unwrap().1.ceil() as u64;
27    thread::sleep(time::Duration::from_secs(duration));
28    
29    libaudioverse::shutdown().unwrap();
30}
Source

pub fn get_duration(&self) -> Result<f32>

Get the duration of the buffer in seconds.

Source

pub fn get_length_in_samples(&self) -> Result<i32>

Get the length of the specified buffer in samples. The sample rate of a buffer is the sample rate of the server for which that buffer was created. This function is primarily useful for estimating ram usage in caching structures.

Source

pub fn decode_from_array(&self, data: &mut [c_char]) -> Result<()>

Takes an encoded array of audio data and decodes it.

Source

pub fn load_from_array( &self, sampling_rate: i32, channels: i32, frames: i32, data: &mut [f32], ) -> Result<()>

Load from an array of interleaved floats.

Source

pub fn load_from_file(&self, path: &CString) -> Result<()>

Loads data into this buffer from a file. The file will be resampled to the sampling rate of the server. This will happen synchronously.

Examples found in repository?
examples/file_playback.rs (line 18)
6fn main() {
7    libaudioverse::initialize().unwrap();
8    
9    // server initialization
10    let server = Server::new().unwrap();
11    server.set_output_device().expect("Could not create default audio device");
12    
13    // create a new buffer
14    let buf = Buffer::new(&server).unwrap();
15    let path = CString::new("test.ogg").unwrap();
16    
17    // load audio data from test.ogg into this buffer
18    buf.load_from_file(&path).unwrap();
19    
20    // create a Buffer node, associate it with the buffer just created, and play it
21    let buf_node = BufferNode::new(&server).unwrap();
22    buf_node.buffer().set(&buf).unwrap();
23    buf_node.connect_server(0).unwrap();
24    
25    // wait for the whole file to finish playing
26    let duration = buf_node.position().get_range().unwrap().1.ceil() as u64;
27    thread::sleep(time::Duration::from_secs(duration));
28    
29    libaudioverse::shutdown().unwrap();
30}
Source

pub fn normalize(&self) -> Result<()>

Normalizes the buffer.

Auto Trait Implementations§

§

impl Freeze for Buffer

§

impl RefUnwindSafe for Buffer

§

impl Send for Buffer

§

impl Sync for Buffer

§

impl Unpin for Buffer

§

impl UnwindSafe for Buffer

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.