@@ -20,26 +20,29 @@ pub struct Player {
2020 decoder : Decoder ,
2121 buffer : Arc < Mutex < Ringbuf < f32 > > > ,
2222 stream : Option < Stream > ,
23- frames_received : usize ,
2423 volume : Arc < Mutex < f32 > > ,
24+ last_frame : Option < u32 > ,
2525}
2626impl Player {
2727 pub fn new ( ) -> Self {
2828 Self {
2929 decoder : Decoder :: new ( 48000 , opus:: Channels :: Stereo ) . unwrap ( ) ,
3030 buffer : Arc :: new ( Mutex :: new ( Ringbuf :: < f32 > :: new ( PLAYER_BUFFER_SIZE ) ) ) ,
3131 stream : None ,
32- frames_received : 0 ,
32+ last_frame : None ,
3333 volume : Arc :: new ( Mutex :: new ( 1.0 ) ) ,
3434 }
3535 }
3636
3737 // decode opus data when received and buffer the samples
38- pub fn receive ( & mut self , frame : Vec < u8 > ) {
38+ pub fn receive ( & mut self , frame_data : Vec < u8 > , frame_idx : u32 ) {
39+ self . last_frame = Some ( frame_idx) ;
40+
3941 // allocate and decode into here
4042 let mut pcm = [ 0.0 ; 960 ] ;
41- self . decoder . decode_float ( & frame, & mut pcm, false ) . unwrap ( ) ;
42- self . frames_received += 1 ;
43+ self . decoder
44+ . decode_float ( & frame_data, & mut pcm, false )
45+ . unwrap ( ) ;
4346
4447 let mut buffer = self . buffer . lock ( ) . unwrap ( ) ;
4548 if buffer. len ( ) < buffer. free_len ( ) {
@@ -106,7 +109,7 @@ impl Player {
106109
107110 pub fn clear ( & mut self ) {
108111 self . buffer . lock ( ) . unwrap ( ) . clear ( ) ;
109- self . frames_received = 0 ;
112+ self . last_frame = None ;
110113 self . pause ( ) ;
111114 }
112115
@@ -132,17 +135,14 @@ impl Player {
132135 let buffer = self . buffer . lock ( ) . unwrap ( ) ;
133136 buffer. len ( ) > 48000 * 2 * SECONDS // 1s of stereo 48k
134137 }
135- pub fn get_seconds_elapsed ( & self ) -> usize {
136- const SECONDS : usize = 1 ;
138+ pub fn get_seconds_elapsed ( & self ) -> u32 {
139+ const SECONDS : u32 = 1 ;
137140
138141 // 960 = 2 channels * 480 samples per frame
139- let samples_received = self . frames_received * 480 ;
142+ let samples_received = self . last_frame . unwrap_or_default ( ) * 480 ;
140143 let current_sample = samples_received. saturating_sub ( 48000 * SECONDS ) ;
141144 current_sample. max ( 0 ) / 48000
142145 }
143- pub fn fake_frames_received ( & mut self , frames : usize ) {
144- self . frames_received = frames;
145- }
146146 pub fn get_visualizer_buffer ( & mut self ) -> Option < [ f32 ; 4096 ] > {
147147 let buf = self . buffer . lock ( ) . unwrap ( ) ;
148148
@@ -209,7 +209,6 @@ fn write_audio<T: Sample>(
209209#[ derive( Debug , Clone ) ]
210210pub enum AudioCommand {
211211 AudioData ( AudioData ) ,
212- StartLate ( usize ) ,
213212 Clear ,
214213 Volume ( f32 ) ,
215214 Shutdown ,
@@ -278,7 +277,7 @@ impl AudioThread {
278277 let _ = self . tx . send ( AudioStatus :: Buffer ( self . p . buffer_status ( ) ) ) ;
279278 }
280279
281- self . p . receive ( frame. data ) ;
280+ self . p . receive ( frame. data , frame . frame ) ;
282281
283282 if let Some ( samples) = self . p . get_visualizer_buffer ( ) {
284283 let bars = calculate_visualizer ( & samples) ;
@@ -287,7 +286,6 @@ impl AudioThread {
287286 }
288287 AudioData :: Start => {
289288 self . wants_play = true ;
290- self . p . fake_frames_received ( 0 ) ;
291289
292290 // if we don't have enough buffer yet, wait
293291 // otherwise we had extra so we can just keep playing i guess?
@@ -302,18 +300,14 @@ impl AudioThread {
302300 }
303301 }
304302 AudioData :: Finish => {
303+ // TODO: we probably shouldnt do this
305304 while !self . p . finish ( ) {
306305 std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 20 ) ) ;
307306 }
308307 self . p . pause ( ) ;
309308 let _ = self . tx . send ( AudioStatus :: Finished ) ;
310309 }
311310 } ,
312- AudioCommand :: StartLate ( frame_id) => {
313- self . wants_play = true ;
314- self . p . fake_frames_received ( frame_id) ;
315- }
316-
317311 AudioCommand :: Clear => {
318312 let _ = self . tx . send ( AudioStatus :: Elapsed ( 0 ) ) ;
319313 self . p . clear ( ) ;
0 commit comments