summaryrefslogtreecommitdiff
path: root/c2enc/src/main.rs
blob: 9df5b6a87518ee56eb5310a7f9a0b6d3ce155546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
extern crate byteorder;
extern crate libcodec2;

use byteorder::{LittleEndian, ReadBytesExt};
use std::fs::File;
use std::io::prelude::*;
use std::io::Cursor;
use std::path::Path;

struct Cli {
    in_path: std::path::PathBuf,
    out_path: std::path::PathBuf,
}

fn main() {
    let in_path_arg = std::env::args().nth(1).expect("no input path given");
    let out_path_arg = std::env::args().nth(2).expect("no output path given");

    let args = Cli {
        in_path: std::path::PathBuf::from(in_path_arg),
        out_path: std::path::PathBuf::from(out_path_arg),
    };

    let in_path = Path::new(&args.in_path);
    let mut file = File::open(&in_path).unwrap();
    let mut buf = Vec::new();
    file.read_to_end(&mut buf).unwrap();

    let mut rdr = Cursor::new(buf);
    let mut speech = Vec::new();
    while let Ok(i) = rdr.read_i16::<LittleEndian>() {
        speech.push(i);
    }

    let c = libcodec2::Codec2::new();
    let nsam = c.samples_per_frame();
    let nbits = c.bytes_per_frame();

    let mut file = File::create(&args.out_path).unwrap();

    for samples in speech.chunks_exact(nsam) {
        // remainder frame will be dropped
        let mut bits = vec![0u8; nbits];
        c.encode(&samples, &mut bits);
        file.write(&bits).unwrap();
    }
    file.flush().unwrap();
}