Skip to content

Commit

Permalink
Merge pull request #113 from solaoi/fix_add-desktop-audio-part3
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
solaoi authored Apr 4, 2024
2 parents 5954987 + 111c382 commit b5059c1
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 130 deletions.
81 changes: 39 additions & 42 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
windows_subsystem = "windows"
)]

use crossbeam_channel::Sender;
use module::model_type_vosk::ModelTypeVosk;
use module::model_type_whisper::ModelTypeWhisper;
use module::transcription::TraceCompletion;
use tauri::http::{HttpRange, ResponseBuilder};
use tauri::{Manager, State, Window};
use tauri::{
http::{HttpRange, ResponseBuilder},
Manager, State, Window,
};
use tauri_plugin_sql::{Migration, MigrationKind};

use crossbeam_channel::unbounded;
use std::cmp::min;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::{
cmp::min,
io::{Read, Seek, SeekFrom},
path::PathBuf,
str::FromStr,
sync::{Arc, Mutex},
};

use crossbeam_channel::{unbounded, Sender};
use urlencoding::decode;

mod module;
use module::device::Device;
use module::{
chat_online::ChatOnline,
deleter::NoteDeleter,
device::{self, Device},
downloader::{vosk::VoskModelDownloader, whisper::WhisperModelDownloader},
model_type_vosk::ModelTypeVosk,
model_type_whisper::ModelTypeWhisper,
permissions,
record::Record,
record_desktop::RecordDesktop,
transcription::{TraceCompletion, Transcription},
transcription_online::TranscriptionOnline,
};

struct RecordState(Arc<Mutex<Option<Sender<()>>>>);

Expand All @@ -30,46 +42,45 @@ const BUNDLE_IDENTIFIER: &str = "blog.aota.Lycoris";
#[tauri::command]
fn delete_note_command(window: Window, note_id: u64) {
std::thread::spawn(move || {
let deleter = module::deleter::NoteDeleter::new(window.app_handle().clone());
let deleter = NoteDeleter::new(window.app_handle().clone());
deleter.delete(note_id)
});
}

#[tauri::command]
fn download_whisper_model_command(window: Window, model: String) {
std::thread::spawn(move || {
let dl =
module::downloader::whisper::WhisperModelDownloader::new(window.app_handle().clone());
let dl = WhisperModelDownloader::new(window.app_handle().clone());
dl.download(ModelTypeWhisper::from_str(&model).unwrap())
});
}

#[tauri::command]
fn download_vosk_model_command(window: Window, model: String) {
std::thread::spawn(move || {
let dl = module::downloader::vosk::VoskModelDownloader::new(window.app_handle().clone());
let dl = VoskModelDownloader::new(window.app_handle().clone());
dl.download(ModelTypeVosk::from_str(&model).unwrap())
});
}

#[tauri::command]
fn list_devices_command() -> Vec<Device> {
module::device::list_devices()
device::list_devices()
}

#[tauri::command]
fn has_accessibility_permission_command() -> bool {
module::permissions::has_accessibility_permission()
permissions::has_accessibility_permission()
}

#[tauri::command]
fn has_screen_capture_permission_command(window: Window) -> bool {
module::permissions::has_screen_capture_permission(window)
permissions::has_screen_capture_permission(window)
}

#[tauri::command]
fn has_microphone_permission_command(window: Window) -> bool {
module::permissions::has_microphone_permission(window)
permissions::has_microphone_permission(window)
}

#[tauri::command]
Expand All @@ -87,47 +98,38 @@ fn start_command(
*lock = Some(stop_record_tx);
std::thread::spawn(move || {
if device_type == "microphone" {
let record = module::record::Record::new(window.app_handle().clone());
let record = Record::new(window.app_handle().clone());
record.start(
device_label,
speaker_language,
transcription_accuracy,
note_id,
stop_record_rx,
Arc::new(Mutex::new(false)),
);
} else if device_type == "desktop" {
let record_desktop =
module::record_desktop::RecordDesktop::new(window.app_handle().clone());
let record_desktop = RecordDesktop::new(window.app_handle().clone());
record_desktop.start(
speaker_language,
transcription_accuracy,
note_id,
stop_record_rx,
None,
Arc::new(Mutex::new(false)),
);
} else {
let record = module::record::Record::new(window.app_handle().clone());
let record_desktop =
module::record_desktop::RecordDesktop::new(window.app_handle().clone());
let record = Record::new(window.app_handle().clone());
let record_desktop = RecordDesktop::new(window.app_handle().clone());

let (stop_record_clone_tx, stop_record_clone_rx) = unbounded();
let speaker_language_clone = speaker_language.clone();
let transcription_accuracy_clone = transcription_accuracy.clone();

let should_stop_other_transcription = Arc::new(Mutex::new(false));
let should_stop_other_transcription_clone =
Arc::clone(&should_stop_other_transcription);

std::thread::spawn(move || {
record_desktop.start(
speaker_language_clone,
transcription_accuracy_clone,
note_id,
stop_record_rx,
Some(stop_record_clone_tx),
should_stop_other_transcription_clone,
);
});
record.start(
Expand All @@ -136,7 +138,6 @@ fn start_command(
transcription_accuracy,
note_id,
stop_record_clone_rx.clone(),
should_stop_other_transcription,
);
}
});
Expand Down Expand Up @@ -164,22 +165,18 @@ fn start_trace_command(

std::thread::spawn(move || {
if transcription_accuracy.starts_with("online-transcript") {
let mut transcription_online = module::transcription_online::TranscriptionOnline::new(
let mut transcription_online = TranscriptionOnline::new(
window.app_handle(),
transcription_accuracy,
speaker_language,
note_id,
);
transcription_online.start(stop_convert_rx, true);
} else if transcription_accuracy.starts_with("online-chat") {
let mut chat_online = module::chat_online::ChatOnline::new(
window.app_handle(),
speaker_language,
note_id,
);
let mut chat_online = ChatOnline::new(window.app_handle(), speaker_language, note_id);
chat_online.start(stop_convert_rx, true);
} else {
let mut transcription = module::transcription::Transcription::new(
let mut transcription = Transcription::new(
window.app_handle(),
transcription_accuracy,
speaker_language,
Expand Down
10 changes: 7 additions & 3 deletions src-tauri/src/module/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ pub fn list_devices() -> Vec<Device> {
.input_devices()
.unwrap()
.filter_map(|device| {
Some(Device {
label: device.name().unwrap(),
})
if device.name().is_ok() && device.name().unwrap().contains("ZoomAudioDevice") {
None
} else {
Some(Device {
label: device.name().unwrap(),
})
}
})
.collect();
}
11 changes: 6 additions & 5 deletions src-tauri/src/module/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ extern crate objc;
extern crate objc_foundation;
extern crate objc_id;

use objc::msg_send;
use objc::runtime::{Class, Object};
use objc::sel;
use objc::sel_impl;
use objc::{
msg_send,
runtime::{Class, Object},
sel, sel_impl,
};
use objc_id::Id;

use core_graphics::access::ScreenCaptureAccess;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub fn has_microphone_permission(window: Window) -> bool {

pub fn has_screen_capture_permission(window: Window) -> bool {
let access = ScreenCaptureAccess::default();
let trusted = access.request();
let trusted = access.preflight();
if !trusted {
let func = |ok: bool| {
if ok {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/module/recognizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ impl MyRecognizer {

pub fn recognize<T: Sample + ToSample<i16>>(
app_handle: AppHandle,
last_partial: &mut String,
recognizer: &mut Recognizer,
data: &[T],
channels: ChannelCount,
notify_decoding_state_is_finalized_tx: SyncSender<String>,
is_desktop: bool,
) {
let mut last_partial = String::new();
let data: Vec<i16> = data.iter().map(|v| v.to_sample()).collect();
let data = if channels != 1 {
Self::stereo_to_mono(&data)
Expand Down
30 changes: 9 additions & 21 deletions src-tauri/src/module/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crossbeam_channel::{unbounded, Receiver};
use tauri::{api::path::data_dir, AppHandle, Manager};

use super::{
chat_online::ChatOnline, recognizer::MyRecognizer, sqlite::Sqlite,
transcription::Transcription, transcription_online::TranscriptionOnline, writer::Writer,
chat_online::ChatOnline, recognizer::MyRecognizer, sqlite::Sqlite, transcription,
transcription_online::TranscriptionOnline, writer::Writer,
};

pub struct Record {
Expand All @@ -43,16 +43,7 @@ impl Record {
transcription_accuracy: String,
note_id: u64,
stop_record_rx: Receiver<()>,
should_stop_other_transcription: Arc<Mutex<bool>>,
) {
let should_stop_other_transcription_on_record =
*should_stop_other_transcription.lock().unwrap();
if !should_stop_other_transcription_on_record {
let mut lock = should_stop_other_transcription.lock().unwrap();
*lock = true;
drop(lock);
}

let host = cpal::default_host();
let device = host
.input_devices()
Expand All @@ -75,7 +66,6 @@ impl Record {
);
let recognizer = Arc::new(Mutex::new(recognizer));
let recognizer_clone = recognizer.clone();
let mut last_partial = String::new();

let spec = Writer::wav_spec_from_config(&config);
let data_dir = data_dir().unwrap_or(PathBuf::from("./"));
Expand All @@ -97,7 +87,6 @@ impl Record {
move |data: &[f32], _| {
MyRecognizer::recognize(
app_handle.clone(),
&mut last_partial,
&mut recognizer_clone.lock().unwrap(),
data,
channels,
Expand All @@ -113,7 +102,6 @@ impl Record {
move |data: &[u16], _| {
MyRecognizer::recognize(
app_handle.clone(),
&mut last_partial,
&mut recognizer_clone.lock().unwrap(),
data,
channels,
Expand All @@ -129,7 +117,6 @@ impl Record {
move |data: &[i16], _| {
MyRecognizer::recognize(
app_handle.clone(),
&mut last_partial,
&mut recognizer_clone.lock().unwrap(),
data,
channels,
Expand Down Expand Up @@ -184,10 +171,7 @@ impl Record {
.lock()
.unwrap()
.replace(Writer::build(&audio_path.to_str().expect("error"), spec));
if !is_no_transcription
&& !*is_converting.lock().unwrap()
&& !should_stop_other_transcription_on_record
{
if !is_no_transcription && !*is_converting.lock().unwrap() {
let is_converting_clone = Arc::clone(&is_converting);
let app_handle_clone = app_handle.clone();
let stop_convert_rx_clone = stop_convert_rx.clone();
Expand All @@ -214,13 +198,16 @@ impl Record {
);
chat_online.start(stop_convert_rx_clone, false);
} else {
let mut transcription = Transcription::new(
transcription::initialize_transcription(
app_handle_clone,
transcription_accuracy_clone,
speaker_language_clone,
note_id,
);
transcription.start(stop_convert_rx_clone, false);
let mut lock = transcription::SINGLETON_INSTANCE.lock().unwrap();
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
}

let mut lock = is_converting_clone.lock().unwrap();
Expand All @@ -245,6 +232,7 @@ impl Record {
stop_writer_tx.send(()).unwrap();
if !is_no_transcription {
stop_convert_tx.send(()).unwrap();
transcription::drop_transcription();
} else {
drop(stop_convert_tx)
}
Expand Down
Loading

0 comments on commit b5059c1

Please sign in to comment.