apply formatting
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use cpal::traits::{DeviceTrait, StreamTrait};
|
||||
use cpal::{BuildStreamError, Device, SampleFormat, SizedSample, Stream, SupportedStreamConfig};
|
||||
use std::fmt::Debug;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{path, thread};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
use hound::WavSpec;
|
||||
use log::{debug, error, info, trace};
|
||||
use std::fmt::Debug;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::Duration;
|
||||
use std::{path, thread};
|
||||
use thiserror::Error;
|
||||
|
||||
fn generic_sample_to_i16(sample: impl SizedSample + Into<f64>, format: SampleFormat) -> i16 {
|
||||
@@ -136,7 +136,7 @@ pub fn start(
|
||||
let thread_samples = samples.clone();
|
||||
let thread_recording_stop = recording_should_stop.clone();
|
||||
let thread_device_config = device_config.clone();
|
||||
let record_thread_handle = thread::spawn(move ||
|
||||
let record_thread_handle = thread::spawn(move || {
|
||||
start_recording_blocking_with_parameters(
|
||||
µphone,
|
||||
&thread_device_config,
|
||||
@@ -144,7 +144,7 @@ pub fn start(
|
||||
thread_samples,
|
||||
thread_recording_stop,
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
let spec = WavSpec {
|
||||
channels: device_config.channels(),
|
||||
@@ -163,25 +163,40 @@ pub fn start(
|
||||
})))
|
||||
}
|
||||
|
||||
pub fn stop_and_take_data(handler: Arc<Mutex<Option<RecordingHandler>>>) -> Result<(Vec<i16>, WavSpec), RecordingError> {
|
||||
let handler = handler.lock().map_err(|_|RecordingError::ThreadPoison)?.take().ok_or(RecordingError::RecordingAlreadyStopped)?;
|
||||
pub fn stop_and_take_data(
|
||||
handler: Arc<Mutex<Option<RecordingHandler>>>,
|
||||
) -> Result<(Vec<i16>, WavSpec), RecordingError> {
|
||||
let handler = handler
|
||||
.lock()
|
||||
.map_err(|_| RecordingError::ThreadPoison)?
|
||||
.take()
|
||||
.ok_or(RecordingError::RecordingAlreadyStopped)?;
|
||||
|
||||
let samples = handler.samples.clone();
|
||||
let spec = handler.spec.clone();
|
||||
|
||||
handler.stop_recording()?;
|
||||
|
||||
Ok((samples.lock().map_err(|_|RecordingError::ThreadPoison)?.to_vec(), spec))
|
||||
Ok((
|
||||
samples
|
||||
.lock()
|
||||
.map_err(|_| RecordingError::ThreadPoison)?
|
||||
.to_vec(),
|
||||
spec,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn samples_to_wav(
|
||||
samples: impl IntoIterator<Item = i16>,
|
||||
spec: &WavSpec,
|
||||
filepath: impl AsRef<path::Path>
|
||||
filepath: impl AsRef<path::Path>,
|
||||
) -> Result<Vec<u8>, hound::Error> {
|
||||
let wav_bytes = std::io::Cursor::new(Vec::<u8>::new());
|
||||
|
||||
trace!("creating a wav file writer to {}", filepath.as_ref().display());
|
||||
trace!(
|
||||
"creating a wav file writer to {}",
|
||||
filepath.as_ref().display()
|
||||
);
|
||||
let mut file_writer = hound::WavWriter::create(&filepath, *spec)?;
|
||||
|
||||
trace!("writing samples...");
|
||||
@@ -193,4 +208,4 @@ pub fn samples_to_wav(
|
||||
file_writer.finalize()?;
|
||||
|
||||
Ok(wav_bytes.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user