Skip to content

Commit d7084b6

Browse files
committed
added new fucntions and improved code
1 parent ee825b5 commit d7084b6

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "silence-core"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
edition = "2021"
55
description = "Core audio I/O abstractions for the silence crate."
66
license = "Apache-2.0"

src/io/record.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Offers audio recording capabilities via being a middleware on [`cpal`].
22
3-
use std::{sync::Arc, thread::JoinHandle};
3+
use std::{collections::VecDeque, sync::Arc, thread::{sleep, JoinHandle}, time::Duration};
44

55
use cpal::{
66
traits::{DeviceTrait, StreamTrait},
@@ -22,8 +22,58 @@ use super::InputDevice;
2222
/// The `err_callback` callback is called if an error occurs in the [`DeviceTrait::build_input_stream`] whilst recording.
2323
/// The function returns an error if an error occured outside of the [`DeviceTrait::build_input_stream`] function.
2424
pub fn record_audio_with_interrupt<E>(
25+
input_device: InputDevice,
2526
interrupt: tokio::sync::oneshot::Receiver<()>,
27+
err_callback: E,
28+
config: StreamConfig,
29+
) -> anyhow::Result<Arc<Mutex<VecDeque<f32>>>>
30+
where
31+
E: FnMut(StreamError) + Send + 'static,
32+
{
33+
let buffer_handle: Arc<parking_lot::lock_api::Mutex<parking_lot::RawMutex, VecDeque<f32>>> =
34+
Arc::new(parking_lot::Mutex::new(VecDeque::new()));
35+
let buffer_handle_clone = buffer_handle.clone();
36+
37+
let _: JoinHandle<anyhow::Result<()>> = std::thread::spawn(move || {
38+
let stream: cpal::Stream = input_device.build_input_stream(
39+
&config,
40+
move |data: &[f32], _info: &cpal::InputCallbackInfo| {
41+
let mut buffer_handle = buffer_handle_clone.lock();
42+
for sample in data.iter() {
43+
buffer_handle.push_back(*sample);
44+
}
45+
},
46+
err_callback,
47+
None,
48+
)?;
49+
50+
//Start stream
51+
stream.play()?;
52+
53+
//Wait for interrupt
54+
interrupt.blocking_recv()?;
55+
56+
//Return from thread
57+
Ok(())
58+
});
59+
60+
Ok(buffer_handle)
61+
}
62+
63+
///
64+
/// Records audio until the user interrupts it.
65+
///
66+
/// # Behavior
67+
/// The recording thread is automaticly started at the creation of the (Input)[`cpal::Stream`].
68+
/// Records audio until (Pushes the samples into the [`Arc<Mutex<Vec<f32>>>`]) the [`tokio::sync::oneshot::Receiver`] receives a message.
69+
/// The [`Sync`] buffer is returned and can be accessed.
70+
///
71+
/// # Error
72+
/// The `err_callback` callback is called if an error occurs in the [`DeviceTrait::build_input_stream`] whilst recording.
73+
/// The function returns an error if an error occured outside of the [`DeviceTrait::build_input_stream`] function.
74+
pub fn record_audio_with_duration<E>(
2675
input_device: InputDevice,
76+
duration: Duration,
2777
err_callback: E,
2878
config: StreamConfig,
2979
) -> anyhow::Result<Arc<Mutex<Vec<f32>>>>
@@ -50,8 +100,8 @@ where
50100
//Start stream
51101
stream.play()?;
52102

53-
//Wait for interrupt
54-
interrupt.blocking_recv()?;
103+
//Sleep the thread
104+
sleep(duration);
55105

56106
//Return from thread
57107
Ok(())

src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
#[cfg(test)]
55
mod tests {
6-
use std::{fs, thread::sleep, time::Duration};
6+
use std::{collections::VecDeque, fs, thread::sleep, time::Duration};
77

88
use cpal::traits::{DeviceTrait, StreamTrait};
99
use opencv::videoio::{CAP_ANY, CAP_MSMF};
@@ -91,12 +91,12 @@ mod tests {
9191
sleep(Duration::from_secs(2));
9292
}
9393

94-
fn record_audio(input_device: cpal::Device, config: cpal::SupportedStreamConfig) -> Vec<f32> {
94+
fn record_audio(input_device: cpal::Device, config: cpal::SupportedStreamConfig) -> VecDeque<f32> {
9595
let (sender, receiver) = oneshot::channel::<()>();
9696
let err_callback = |err| eprintln!("an error occurred on stream: {}", err);
9797

9898
let buffer_handle =
99-
record_audio_with_interrupt(receiver, input_device, err_callback, config.into())
99+
record_audio_with_interrupt(input_device, receiver, err_callback, config.into())
100100
.unwrap();
101101

102102
sleep(Duration::from_secs(3));
@@ -125,7 +125,7 @@ mod tests {
125125
)
126126
.unwrap();
127127

128-
let sound_packets = encode_samples_opus(encoder, &sample, 20, channels).unwrap();
128+
let sound_packets = encode_samples_opus(encoder, &Into::<Vec<f32>>::into(sample), 20, channels).unwrap();
129129

130130
let decoder = create_opus_decoder(48000).unwrap();
131131

0 commit comments

Comments
 (0)