This repository was archived by the owner on Feb 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhound_stream.rs
More file actions
82 lines (71 loc) · 1.85 KB
/
hound_stream.rs
File metadata and controls
82 lines (71 loc) · 1.85 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::traits::{Sample, FP};
use std::{
fs::File,
io::{BufReader, BufWriter, Read, Seek, Write},
path::Path,
};
use hound::{Error as WavError, WavReader, WavWriter};
use crate::{
traits::{InStream, OutStream},
DefaultConfig,
};
pub struct HoundInStream<R: Read>(WavReader<R>);
pub struct HoundOutStream<W: Write + Seek>(WavWriter<W>);
impl<R: Read> HoundInStream<R> {
pub fn new(wav_reader: WavReader<R>) -> Self {
Self(wav_reader)
}
}
impl HoundInStream<BufReader<File>> {
pub fn open<P>(filename: P) -> HoundInStream<BufReader<File>>
where
P: AsRef<Path>,
{
HoundInStream::new(hound::WavReader::open(filename).unwrap())
}
}
impl<W: Write + Seek> HoundOutStream<W> {
pub fn new(wav_writer: WavWriter<W>) -> Self {
Self(wav_writer)
}
pub fn finalize(self) {
self.0.finalize().unwrap()
}
}
impl HoundOutStream<BufWriter<File>> {
pub fn create<P>(filename: P) -> HoundOutStream<BufWriter<File>>
where
P: AsRef<Path>,
{
let spec = DefaultConfig::new();
HoundOutStream::new(hound::WavWriter::create(filename, spec).unwrap())
}
}
impl<R: Read> InStream<FP, WavError> for HoundInStream<R> {
fn read(&mut self, buf: &mut [FP]) -> Result<usize, WavError> {
let mut n = 0;
for (x, sample) in buf.iter_mut().zip(self.0.samples()) {
*x = FP::from_f32(sample?);
n += 1;
}
Ok(n)
}
fn read_exact(&mut self, buf: &mut [FP]) -> Result<(), WavError> {
self.read(buf).map(|_| ())
}
}
impl<W: Write + Seek> OutStream<FP, WavError> for HoundOutStream<W> {
fn write(&mut self, buf: &[FP]) -> Result<usize, WavError> {
let mut n = 0;
for x in buf {
self.0.write_sample(FP::into_f32(*x))?;
n += 1;
}
Ok(n)
}
fn write_exact(&mut self, buf: &[FP]) -> Result<(), WavError> {
self.write(buf).map(|_| ())
}
/// do not need to wait
fn wait(&mut self) {}
}