Skip to content

Commit fcc63b7

Browse files
committed
sfx fixes
1 parent a3029ff commit fcc63b7

File tree

5 files changed

+42
-79
lines changed

5 files changed

+42
-79
lines changed

Cargo.lock

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

apps/desktop/src/routes/app/onboarding/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ function Component() {
4343

4444
useEffect(() => {
4545
sfxCommands.play("BGM").catch(console.error);
46+
return () => {
47+
sfxCommands.stop("BGM").catch(console.error);
48+
};
4649
}, []);
4750

4851
useEffect(() => {

plugins/sfx/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,3 @@ specta = { workspace = true }
2424
strum = { workspace = true, features = ["derive"] }
2525

2626
once_cell = { workspace = true }
27-
tracing = { workspace = true }

plugins/sfx/src/commands.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@ use crate::{AppSounds, SfxPluginExt};
33
#[tauri::command]
44
#[specta::specta]
55
pub async fn play<R: tauri::Runtime>(app: tauri::AppHandle<R>, sfx: AppSounds) {
6-
tracing::info!("sfx command: play called with {:?}", sfx);
76
app.sfx().play(sfx)
87
}
98

109
#[tauri::command]
1110
#[specta::specta]
1211
pub async fn stop<R: tauri::Runtime>(app: tauri::AppHandle<R>, sfx: AppSounds) {
13-
tracing::info!("sfx command: stop called with {:?}", sfx);
1412
app.sfx().stop(sfx)
1513
}
1614

1715
#[tauri::command]
1816
#[specta::specta]
1917
pub async fn set_volume<R: tauri::Runtime>(app: tauri::AppHandle<R>, sfx: AppSounds, volume: f32) {
20-
tracing::info!(
21-
"sfx command: set_volume called with {:?}, volume: {}",
22-
sfx,
23-
volume
24-
);
2518
app.sfx().set_volume(sfx, volume)
2619
}

plugins/sfx/src/ext.rs

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,85 +20,54 @@ pub enum AppSounds {
2020
BGM,
2121
}
2222

23-
pub fn to_speaker(bytes: &'static [u8], looping: bool) -> std::sync::mpsc::Sender<SoundControl> {
23+
pub(crate) fn to_speaker(
24+
bytes: &'static [u8],
25+
looping: bool,
26+
) -> std::sync::mpsc::Sender<SoundControl> {
2427
use rodio::source::Source;
2528
use rodio::{Decoder, OutputStream, Sink};
2629
let (tx, rx) = std::sync::mpsc::channel();
2730

2831
std::thread::spawn(move || {
29-
eprintln!(
30-
"[sfx] attempting to play audio, bytes len: {}, looping: {}",
31-
bytes.len(),
32-
looping
33-
);
34-
tracing::info!(
35-
"sfx: attempting to play audio, bytes len: {}, looping: {}",
36-
bytes.len(),
37-
looping
38-
);
39-
40-
match OutputStream::try_default() {
41-
Ok((stream, stream_handle)) => {
42-
eprintln!("[sfx] got output stream");
43-
tracing::info!("sfx: got output stream");
44-
let file = std::io::Cursor::new(bytes);
45-
46-
match Decoder::new(file) {
47-
Ok(source) => {
48-
eprintln!("[sfx] decoded audio source successfully");
49-
tracing::info!("sfx: decoded audio source");
50-
51-
match Sink::try_new(&stream_handle) {
52-
Ok(sink) => {
53-
eprintln!("[sfx] created sink, appending source and playing");
54-
tracing::info!("sfx: created sink, appending source");
55-
56-
if looping {
57-
sink.append(source.repeat_infinite());
58-
} else {
59-
sink.append(source);
60-
}
61-
62-
loop {
63-
match rx.recv_timeout(std::time::Duration::from_millis(100)) {
64-
Ok(SoundControl::Stop) => {
65-
eprintln!("[sfx] stopping playback");
66-
sink.stop();
67-
break;
68-
}
69-
Ok(SoundControl::SetVolume(volume)) => {
70-
eprintln!("[sfx] setting volume to {}", volume);
71-
sink.set_volume(volume);
72-
}
73-
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
74-
if !looping && sink.empty() {
75-
break;
76-
}
77-
}
78-
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
79-
break;
80-
}
81-
}
82-
}
83-
drop(stream);
84-
}
85-
Err(e) => {
86-
eprintln!("[sfx] ERROR: failed to create sink: {:?}", e);
87-
tracing::error!("sfx: failed to create sink: {:?}", e);
88-
}
89-
}
90-
}
91-
Err(e) => {
92-
eprintln!("[sfx] ERROR: failed to decode audio: {:?}", e);
93-
tracing::error!("sfx: failed to decode audio: {:?}", e);
32+
let Ok((stream, stream_handle)) = OutputStream::try_default() else {
33+
return;
34+
};
35+
36+
let file = std::io::Cursor::new(bytes);
37+
let Ok(source) = Decoder::new(file) else {
38+
return;
39+
};
40+
41+
let Ok(sink) = Sink::try_new(&stream_handle) else {
42+
return;
43+
};
44+
45+
if looping {
46+
sink.append(source.repeat_infinite());
47+
} else {
48+
sink.append(source);
49+
}
50+
51+
loop {
52+
match rx.recv_timeout(std::time::Duration::from_millis(100)) {
53+
Ok(SoundControl::Stop) => {
54+
sink.stop();
55+
break;
56+
}
57+
Ok(SoundControl::SetVolume(volume)) => {
58+
sink.set_volume(volume);
59+
}
60+
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
61+
if !looping && sink.empty() {
62+
break;
9463
}
9564
}
96-
}
97-
Err(e) => {
98-
eprintln!("[sfx] ERROR: failed to get output stream: {:?}", e);
99-
tracing::error!("sfx: failed to get output stream: {:?}", e);
65+
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
66+
break;
67+
}
10068
}
10169
}
70+
drop(stream);
10271
});
10372

10473
tx

0 commit comments

Comments
 (0)