Skip to content

Commit

Permalink
audio recording final fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Jan 5, 2024
1 parent 33145ec commit 8ea06bb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.01.04. 22:44
2024.01.05. 13:39
5 changes: 4 additions & 1 deletion src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use egui::Color32;
use rand::rngs::ThreadRng;

use rodio::{OutputStream, OutputStreamHandle, Sink};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io;
use std::io::{Read, Seek, SeekFrom};
Expand Down Expand Up @@ -129,10 +130,12 @@ impl Default for TemplateApp {
let (itx, irx) = mpsc::channel::<String>();
let (audio_save_tx, audio_save_rx) = mpsc::channel::<String>();
Self {
audio_file: Arc::new(Mutex::new(PathBuf::from(format!(
audio_file:
Arc::new(Mutex::new(PathBuf::from(format!(
"{}\\Matthias\\Client\\voice_record.wav",
env!("APPDATA")
)))),

//fontbook
filter: Default::default(),
named_chars: Default::default(),
Expand Down
12 changes: 3 additions & 9 deletions src/app/ui/client_ui/client_actions/audio_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{FromSample, Sample};
use hound::WavWriter;
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
Expand Down Expand Up @@ -37,7 +36,7 @@ impl Default for Opt {
}
}

pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: PathBuf) {
pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: Arc<Mutex<PathBuf>>) {
std::thread::spawn(move || {
let opt = Opt::default();

Expand All @@ -52,25 +51,22 @@ pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: PathBuf) {
}
.expect("failed to find input device");

println!("Input device: {}", device.name()?);

let config = device
.default_input_config()
.expect("Failed to get default input config");
println!("Default input config: {:?}", config);

// The WAV file we're recording to.

let spec = wav_spec_from_config(&config);

let PATH = PATH.to_string_lossy().to_string();

let PATH = PATH.lock().unwrap().to_string_lossy().to_string().clone();
let writer = hound::WavWriter::create(PATH, spec)?;

let writer = Arc::new(Mutex::new(Some(writer)));

// A flag to indicate that recording is in progress.
println!("Begin recording...");

// Run the input stream on a separate thread.
let writer_2 = writer.clone();
Expand Down Expand Up @@ -116,8 +112,6 @@ pub fn audio_recroding(receiver: mpsc::Receiver<bool>, PATH: PathBuf) {
//Block until further notice by user
receiver.recv()?;

println!("Stop recording, channel updated! Move file");

Ok(())
});
}
Expand Down
17 changes: 7 additions & 10 deletions src/app/ui/client_ui/widgets/message_tray/message_tray_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,20 @@ impl TemplateApp {
atx.send(false).unwrap();

//Path to voice recording created by audio_recording.rs, Arc mutex to avoid data races
let mut should_send = (false, PathBuf::new());
match self.audio_file.try_lock() {
match self.audio_file.clone().try_lock() {
Ok(ok) => {
should_send = (true, ok.to_path_buf());

self.send_file(ok.to_path_buf());

let _ = fs::remove_file(ok.to_path_buf());
}
Err(error) => println!("{error}"),
};

if should_send.0 {
self.send_file(should_send.1.clone());
//clear temp files
let _ = fs::remove_file(should_send.1);
}

//Destroy state
self.atx = None;
}

});
} else if ui
.add(egui::ImageButton::new(egui::include_image!(
Expand All @@ -222,7 +219,7 @@ impl TemplateApp {

self.atx = Some(tx);

audio_recroding(rx, self.audio_file.lock().unwrap().to_path_buf());
audio_recroding(rx, self.audio_file.clone());
}
}
});
Expand Down

0 comments on commit 8ea06bb

Please sign in to comment.