libaudioverse/
buffer.rs

1//! Storage for audio data.
2use std::ffi::{CString };
3use std :: os :: raw;
4use super::*;
5use super::{libaudioverse_sys, server};
6use check;
7
8/// 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.
9/// 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.
10pub struct Buffer {
11    // make handle visible for  BufferProperty's usage
12    pub(crate) handle: libaudioverse_sys::LavHandle,
13}
14
15impl Buffer {
16    /// Creates a new audio buffer.
17    pub fn new(server : &server::Server) -> Result<Buffer> {
18        let mut buf_handle : libaudioverse_sys::LavHandle = 0;
19        check(unsafe { libaudioverse_sys::Lav_createBuffer(server.handle, &mut buf_handle) })?;
20        Ok(Buffer { handle : buf_handle })
21    }
22    
23    /// Get the duration of the buffer in seconds.
24    pub fn get_duration(&self) -> Result<f32> {
25        let mut duration : f32 = 0.0;
26        check(unsafe { libaudioverse_sys::Lav_bufferGetDuration(self.handle, &mut duration) })?;
27        Ok(duration)
28    }
29    
30    /// 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. 
31    /// This function is primarily useful for estimating ram usage in caching structures.
32    pub fn get_length_in_samples(&self) -> Result<i32> {
33        let mut samples : i32 = 0;
34        check(unsafe { libaudioverse_sys::Lav_bufferGetLengthInSamples(self.handle, &mut samples) })?;
35        Ok(samples)
36    }
37    
38    /// Takes an encoded array of audio data and decodes it.
39    pub fn decode_from_array(&self, data : &mut [raw::c_char]) -> Result<()> {
40        check(unsafe { libaudioverse_sys::Lav_bufferDecodeFromArray(self.handle, data.as_mut_ptr(), data.len() as i32) })?;
41        Ok(())
42    }
43    
44    /// Load from an array of interleaved floats.
45    pub fn load_from_array(&self, sampling_rate : i32, channels : i32, frames : i32, data : &mut [f32]) -> Result<()> {
46        check(unsafe { libaudioverse_sys::Lav_bufferLoadFromArray(self.handle, sampling_rate, channels, frames, data.as_mut_ptr()) })?;
47        Ok(())
48    }
49    
50    /// Loads data into this buffer from a file. The file will be resampled to the sampling rate of the server. This will happen synchronously.
51    pub fn load_from_file(&self, path : &CString) -> Result<()> {
52        check(unsafe { libaudioverse_sys::Lav_bufferLoadFromFile(self.handle, path.as_ptr()) })?;
53        Ok(())
54    }
55    
56    /// Normalizes the buffer.
57    pub fn normalize(&self) -> Result<()> {
58        check(unsafe { libaudioverse_sys::Lav_bufferNormalize(self.handle) })
59    }
60    
61}