@@ -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