@@ -210,10 +210,18 @@ int gen_tone_init (struct audio_s *audio_config_p, int amp)
210
210
a = ((double )(j ) / 256.0 ) * (2 * M_PI );
211
211
s = (int ) (sin (a ) * 32767 * amp / 100.0 );
212
212
213
- /* 16 bit sound sample is in range of -32768 .. +32767. */
214
-
215
- assert (s >= -32768 && s <= 32767 );
213
+ /* 16 bit sound sample must fit in range of -32768 .. +32767. */
216
214
215
+ if (s < -32768 ) {
216
+ text_color_set (DW_COLOR_ERROR );
217
+ dw_printf ("gen_tone_init: Excessive amplitude is being clipped.\n" );
218
+ s = -32768 ;
219
+ }
220
+ else if (s > 32767 ) {
221
+ text_color_set (DW_COLOR_ERROR );
222
+ dw_printf ("gen_tone_init: Excessive amplitude is being clipped.\n" );
223
+ s = 32767 ;
224
+ }
217
225
sine_table [j ] = s ;
218
226
}
219
227
@@ -235,7 +243,8 @@ int gen_tone_init (struct audio_s *audio_config_p, int amp)
235
243
/* These numbers were by trial and error. Need more investigation here. */
236
244
237
245
float filter_len_bits = 88 * 9600.0 / (44100.0 * 2.0 );
238
- /* Filter length in number of data bits. */
246
+ /* Filter length in number of data bits. */
247
+ /* Currently 9.58 */
239
248
240
249
float lpf_baud = 0.8 ; /* Lowpass cutoff freq as fraction of baud rate */
241
250
@@ -250,10 +259,15 @@ int gen_tone_init (struct audio_s *audio_config_p, int amp)
250
259
251
260
lp_filter_size [chan ] = (int ) (( filter_len_bits * (float )samples_per_sec / baud ) + 0.5 );
252
261
253
- if (lp_filter_size [chan ] < 10 || lp_filter_size [chan ] > MAX_FILTER_SIZE ) {
254
- text_color_set (DW_COLOR_ERROR );
255
- dw_printf ("gen_tone_init: INTERNAL ERROR, chan %d, lp_filter_size %d\n" , chan , lp_filter_size [chan ]);
256
- lp_filter_size [chan ] = MAX_FILTER_SIZE / 2 ;
262
+ if (lp_filter_size [chan ] < 10 ) {
263
+ text_color_set (DW_COLOR_DEBUG );
264
+ dw_printf ("gen_tone_init: unexpected, chan %d, lp_filter_size %d < 10\n" , chan , lp_filter_size [chan ]);
265
+ lp_filter_size [chan ] = 10 ;
266
+ }
267
+ else if (lp_filter_size [chan ] > MAX_FILTER_SIZE ) {
268
+ text_color_set (DW_COLOR_DEBUG );
269
+ dw_printf ("gen_tone_init: unexpected, chan %d, lp_filter_size %d > %d\n" , chan , lp_filter_size [chan ], MAX_FILTER_SIZE );
270
+ lp_filter_size [chan ] = MAX_FILTER_SIZE ;
257
271
}
258
272
259
273
fc = (float )baud * lpf_baud / (float )samples_per_sec ;
@@ -358,6 +372,8 @@ void gen_tone_put_sample (int chan, int a, int sam) {
358
372
359
373
assert (save_audio_config_p -> adev [a ].bits_per_sample == 16 );
360
374
375
+ // TODO: Should print message telling user to reduce output level.
376
+
361
377
if (sam < -32767 ) sam = -32767 ;
362
378
else if (sam > 32767 ) sam = 32767 ;
363
379
0 commit comments