summaryrefslogtreecommitdiff
path: root/libcodec2/src
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2022-07-18 14:20:37 +0300
committerYuval Adam <_@yuv.al>2022-07-18 14:20:37 +0300
commit42d63aac1b709fa371fdb055077423822c74098e (patch)
tree369431587bbcd154353b2fea3ff6cf509c392cfe /libcodec2/src
parent65eeda16498b2fb2ec4aaabf474843cac6771d26 (diff)
Implement encode()
Diffstat (limited to 'libcodec2/src')
-rw-r--r--libcodec2/src/lib.rs40
1 files changed, 37 insertions, 3 deletions
diff --git a/libcodec2/src/lib.rs b/libcodec2/src/lib.rs
index cedf9db..4a224e4 100644
--- a/libcodec2/src/lib.rs
+++ b/libcodec2/src/lib.rs
@@ -1,11 +1,45 @@
-extern crate codec2_sys;
+extern crate codec2_sys as ffi;
+
+pub enum Modes {
+ Mode1400 = ffi::CODEC2_MODE_1400 as isize,
+}
+
+pub struct Codec2(*mut ffi::CODEC2);
+
+impl Codec2 {
+ pub fn new() -> Self {
+ let mode = ffi::CODEC2_MODE_1400.try_into().unwrap();
+ let c = unsafe { ffi::codec2_create(mode) };
+ if c.is_null() {
+ panic!("error")
+ }
+ Codec2(c)
+ }
+
+ pub fn destroy(&self) {
+ unsafe { ffi::codec2_destroy(self.0) }
+ }
+
+ /// Encode an array of speech samples into an array of bits
+ pub fn encode(self, speech: &[i16], bits: &[u8]) {
+ unsafe {
+ ffi::codec2_encode(self.0, bits.as_ptr() as *mut _, speech.as_ptr() as *mut _);
+ }
+ }
+}
+
+impl Drop for Codec2 {
+ fn drop(&mut self) {
+ self.destroy();
+ }
+}
#[cfg(test)]
mod tests {
- use codec2_sys::CODEC2;
+ use super::Codec2;
#[test]
fn it_works() {
- let _c: CODEC2;
+ let _c = Codec2::new();
let result = 2 + 2;
assert_eq!(result, 4);
}