22
33use download_db:: download_lichess_db;
44use eval:: { Engine , EngineStatus } ;
5- use iced:: widget:: Id ;
65use iced:: advanced:: widget:: Id as GenericId ;
76use iced:: widget:: svg:: Handle ;
87use iced:: widget:: text:: LineHeight ;
@@ -25,10 +24,9 @@ use rfd::AsyncFileDialog;
2524use iced_aw:: { TabLabel , Tabs } ;
2625use chess:: { Board , BoardStatus , ChessMove , Color , File , Game , Piece , Rank , Square , ALL_SQUARES } ;
2726
28- use rodio:: { Decoder , OutputStream , OutputStreamHandle } ;
29- use rodio:: source:: { Source , Buffered } ;
27+ use rodio:: { OutputStream , OutputStreamBuilder } ;
3028
31- use rand:: thread_rng ;
29+ use rand:: rng ;
3230use rand:: seq:: SliceRandom ;
3331
3432mod config;
@@ -128,43 +126,41 @@ pub enum Message {
128126}
129127
130128struct SoundPlayback {
131- // it's not directly used, but we need to keep it: https://github.com/RustAudio/rodio/issues/330
132- stream : OutputStream ,
133- handle : OutputStreamHandle ,
134- one_piece_sound : Buffered < Decoder < BufReader < StdFile > > > ,
135- two_pieces_sound : Buffered < Decoder < BufReader < StdFile > > > ,
129+ handle : OutputStream ,
136130}
137131
138132impl SoundPlayback {
139133 pub const ONE_PIECE_SOUND : u8 = 0 ;
140134 pub const TWO_PIECE_SOUND : u8 = 1 ;
141135 pub fn init_sound ( ) -> Option < Self > {
142136 let mut sound_playback = None ;
143- if let Ok ( ( stream, handle) ) = OutputStream :: try_default ( ) {
144- let one_pieces_sound = StdFile :: open ( ONE_PIECE_SOUND_FILE ) ;
145- let two_pieces_sound = StdFile :: open ( TWO_PIECES_SOUND_FILE ) ;
146-
147- if let ( Ok ( one_piece) , Ok ( two_piece) ) = ( one_pieces_sound, two_pieces_sound) {
148- sound_playback = Some (
149- SoundPlayback {
150- stream : stream,
151- handle : handle,
152- one_piece_sound : Decoder :: new ( BufReader :: new ( one_piece) ) . unwrap ( ) . buffered ( ) ,
153- two_pieces_sound : Decoder :: new ( BufReader :: new ( two_piece) ) . unwrap ( ) . buffered ( )
154- }
155- ) ;
156- }
157- }
137+ if let Ok ( handle) = OutputStreamBuilder :: open_default_stream ( ) {
138+ sound_playback = Some (
139+ SoundPlayback {
140+ handle : handle,
141+ } ) ;
142+ }
158143 sound_playback
159144 }
160145 pub fn play_audio ( & self , audio : u8 ) {
161- let audio = match audio {
162- SoundPlayback :: ONE_PIECE_SOUND => self . one_piece_sound . clone ( ) ,
163- _ => self . two_pieces_sound . clone ( ) ,
146+ let sink = match audio {
147+ SoundPlayback :: ONE_PIECE_SOUND => {
148+ rodio:: play (
149+ & self . handle . mixer ( ) ,
150+ BufReader :: new (
151+ StdFile :: open ( ONE_PIECE_SOUND_FILE ) . unwrap ( )
152+ ) ) . unwrap ( )
153+ } ,
154+ _ => {
155+ rodio:: play (
156+ & self . handle . mixer ( ) ,
157+ BufReader :: new (
158+ StdFile :: open ( TWO_PIECES_SOUND_FILE ) . unwrap ( )
159+ ) ) . unwrap ( )
160+ } ,
164161 } ;
165- if let Err ( e) = self . handle . play_raw ( audio. convert_samples ( ) ) {
166- eprintln ! ( "{e}" ) ;
167- }
162+ sink. play ( ) ;
163+ sink. detach ( ) ;
168164 }
169165}
170166
@@ -597,7 +593,7 @@ impl OfflinePuzzles {
597593 if let Some ( puzzles_vec) = puzzles_vec {
598594 if !puzzles_vec. is_empty ( ) {
599595 self . puzzle_tab . puzzles = puzzles_vec;
600- self . puzzle_tab . puzzles . shuffle ( & mut thread_rng ( ) ) ;
596+ self . puzzle_tab . puzzles . shuffle ( & mut rng ( ) ) ;
601597 self . puzzle_tab . current_puzzle = 0 ;
602598 self . puzzle_number_ui = String :: from ( "1" ) ;
603599 self . load_puzzle ( false ) ;
0 commit comments