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
impl Buffer
Sourcepub fn new(server: &Server) -> Result<Buffer>
pub fn new(server: &Server) -> Result<Buffer>
Creates a new audio buffer.
Examples found in repository?
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}
Sourcepub fn get_duration(&self) -> Result<f32>
pub fn get_duration(&self) -> Result<f32>
Get the duration of the buffer in seconds.
Sourcepub fn get_length_in_samples(&self) -> Result<i32>
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.
Sourcepub fn decode_from_array(&self, data: &mut [c_char]) -> Result<()>
pub fn decode_from_array(&self, data: &mut [c_char]) -> Result<()>
Takes an encoded array of audio data and decodes it.
Sourcepub fn load_from_array(
&self,
sampling_rate: i32,
channels: i32,
frames: i32,
data: &mut [f32],
) -> Result<()>
pub fn load_from_array( &self, sampling_rate: i32, channels: i32, frames: i32, data: &mut [f32], ) -> Result<()>
Load from an array of interleaved floats.
Sourcepub fn load_from_file(&self, path: &CString) -> Result<()>
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?
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}