@@ -127,7 +127,7 @@ static void app_free(WavPlayerApp* app) {
127127
128128// TODO: that works only with 8-bit 2ch audio
129129static bool fill_data (WavPlayerApp * app , size_t index ) {
130- if (app -> num_channels == 1 ) {
130+ if (app -> num_channels == 1 && app -> bits_per_sample == 8 ) {
131131 uint16_t * sample_buffer_start = & app -> sample_buffer [index ];
132132 size_t count = stream_read (app -> stream , app -> tmp_buffer , app -> samples_count_half );
133133
@@ -166,7 +166,108 @@ static bool fill_data(WavPlayerApp* app, size_t index) {
166166 return count != app -> samples_count_half ;
167167 }
168168
169- if (app -> num_channels == 2 ) {
169+ if (app -> num_channels == 1 && app -> bits_per_sample == 16 ) {
170+ uint16_t * sample_buffer_start = & app -> sample_buffer [index ];
171+ size_t count = stream_read (app -> stream , app -> tmp_buffer , app -> samples_count );
172+
173+ for (size_t i = count ; i < app -> samples_count ; i ++ ) {
174+ //app->tmp_buffer[i] = 0;
175+ }
176+
177+ for (size_t i = 0 ; i < app -> samples_count ; i += 2 ) {
178+ int16_t int_16 =
179+ (((int16_t )app -> tmp_buffer [i + 1 ] << 8 ) + (int16_t )app -> tmp_buffer [i ]);
180+
181+ float data = ((float )int_16 / 256.0 + 127.0 );
182+ data -= UINT8_MAX / 2 ; // to signed
183+ data /= UINT8_MAX / 2 ; // scale -1..1
184+
185+ data *= app -> volume ; // volume
186+ data = tanhf (data ); // hyperbolic tangent limiter
187+
188+ data *= UINT8_MAX / 2 ; // scale -128..127
189+ data += UINT8_MAX / 2 ; // to unsigned
190+
191+ if (data < 0 ) {
192+ data = 0 ;
193+ }
194+
195+ if (data > 255 ) {
196+ data = 255 ;
197+ }
198+
199+ sample_buffer_start [i / 2 ] = data ;
200+ }
201+
202+ wav_player_view_set_data (app -> view , sample_buffer_start , app -> samples_count_half );
203+
204+ return count != app -> samples_count ;
205+ }
206+
207+ if (app -> num_channels == 2 && app -> bits_per_sample == 16 ) {
208+ uint16_t * sample_buffer_start = & app -> sample_buffer [index ];
209+ size_t count = stream_read (app -> stream , app -> tmp_buffer , app -> samples_count );
210+
211+ for (size_t i = 0 ; i < app -> samples_count ; i += 4 ) {
212+ int16_t L = (((int16_t )app -> tmp_buffer [i + 1 ] << 8 ) + (int16_t )app -> tmp_buffer [i ]);
213+ int16_t R = (((int16_t )app -> tmp_buffer [i + 3 ] << 8 ) + (int16_t )app -> tmp_buffer [i + 2 ]);
214+ int32_t int_16 = L / 2 + R / 2 ; // (L + R) / 2
215+
216+ float data = ((float )int_16 / 256.0 + 127.0 );
217+ data -= UINT8_MAX / 2 ; // to signed
218+ data /= UINT8_MAX / 2 ; // scale -1..1
219+
220+ data *= app -> volume ; // volume
221+ data = tanhf (data ); // hyperbolic tangent limiter
222+
223+ data *= UINT8_MAX / 2 ; // scale -128..127
224+ data += UINT8_MAX / 2 ; // to unsigned
225+
226+ if (data < 0 ) {
227+ data = 0 ;
228+ }
229+
230+ if (data > 255 ) {
231+ data = 255 ;
232+ }
233+
234+ sample_buffer_start [i / 4 ] = data ;
235+ }
236+
237+ count = stream_read (app -> stream , app -> tmp_buffer , app -> samples_count );
238+
239+ for (size_t i = 0 ; i < app -> samples_count ; i += 4 ) {
240+ int16_t L = (((int16_t )app -> tmp_buffer [i + 1 ] << 8 ) + (int16_t )app -> tmp_buffer [i ]);
241+ int16_t R = (((int16_t )app -> tmp_buffer [i + 3 ] << 8 ) + (int16_t )app -> tmp_buffer [i + 2 ]);
242+ int32_t int_16 = L / 2 + R / 2 ; // (L + R) / 2
243+
244+ float data = ((float )int_16 / 256.0 + 127.0 );
245+ data -= UINT8_MAX / 2 ; // to signed
246+ data /= UINT8_MAX / 2 ; // scale -1..1
247+
248+ data *= app -> volume ; // volume
249+ data = tanhf (data ); // hyperbolic tangent limiter
250+
251+ data *= UINT8_MAX / 2 ; // scale -128..127
252+ data += UINT8_MAX / 2 ; // to unsigned
253+
254+ if (data < 0 ) {
255+ data = 0 ;
256+ }
257+
258+ if (data > 255 ) {
259+ data = 255 ;
260+ }
261+
262+ sample_buffer_start [i / 4 + app -> samples_count / 4 ] = data ;
263+ }
264+
265+ wav_player_view_set_data (app -> view , sample_buffer_start , app -> samples_count_half );
266+
267+ return count != app -> samples_count ;
268+ }
269+
270+ if (app -> num_channels == 2 && app -> bits_per_sample == 8 ) {
170271 uint16_t * sample_buffer_start = & app -> sample_buffer [index ];
171272 size_t count = stream_read (app -> stream , app -> tmp_buffer , app -> samples_count );
172273
@@ -270,6 +371,9 @@ static void app_run(WavPlayerApp* app) {
270371 while (1 ) {
271372 if (furi_message_queue_get (app -> queue , & event , FuriWaitForever ) == FuriStatusOk ) {
272373 if (event .type == WavPlayerEventHalfTransfer ) {
374+ wav_player_view_set_chans (app -> view , app -> num_channels );
375+ wav_player_view_set_bits (app -> view , app -> bits_per_sample );
376+
273377 eof = fill_data (app , 0 );
274378 wav_player_view_set_current (app -> view , stream_tell (app -> stream ));
275379 if (eof ) {
@@ -280,6 +384,9 @@ static void app_run(WavPlayerApp* app) {
280384 }
281385
282386 } else if (event .type == WavPlayerEventFullTransfer ) {
387+ wav_player_view_set_chans (app -> view , app -> num_channels );
388+ wav_player_view_set_bits (app -> view , app -> bits_per_sample );
389+
283390 eof = fill_data (app , app -> samples_count_half );
284391 wav_player_view_set_current (app -> view , stream_tell (app -> stream ));
285392 if (eof ) {
0 commit comments