1 #include "config.h"
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <inttypes.h>
7 
8 #include "a52.h"
9 #include "audio_out.h"
10 #include "audio_out_internal.h"
11 
12 typedef struct peak_instance_s {
13     ao_instance_t ao;
14     int flags;
15     sample_t peak;
16 } peak_instance_t;
17 
peak_setup(ao_instance_t * _instance,int sample_rate,int * flags,sample_t * level,sample_t * bias)18 static int peak_setup (ao_instance_t * _instance, int sample_rate, int * flags,
19 		       sample_t * level, sample_t * bias)
20 {
21     peak_instance_t * instance = (peak_instance_t *) _instance;
22 
23     *flags = instance->flags;
24     *level = 1;
25     *bias = 0;
26 
27     return 0;
28 }
29 
peak_play(ao_instance_t * _instance,int flags,sample_t * samples)30 static int peak_play (ao_instance_t * _instance, int flags, sample_t * samples)
31 {
32     peak_instance_t * instance = (peak_instance_t *) _instance;
33     int i;
34 
35     for (i = 0; i < 256 * 2; i++) {
36 	if (instance->peak < fabs(samples[i]))
37 	    instance->peak = fabs(samples[i]);
38     }
39 
40     return 0;
41 }
42 
peak_close(ao_instance_t * _instance)43 static void peak_close (ao_instance_t * _instance)
44 {
45     peak_instance_t * instance = (peak_instance_t *) _instance;
46 
47     printf ("peak level = %.4f (%+.2f dB)\n",
48 	    instance->peak, 6 * log (instance->peak) / log (2));
49 }
50 
peak_open(int flags)51 static ao_instance_t * peak_open (int flags)
52 {
53     peak_instance_t * instance;
54 
55     instance = malloc (sizeof (peak_instance_t));
56     if (instance == NULL)
57 	return NULL;
58 
59     instance->ao.setup = peak_setup;
60     instance->ao.play = peak_play;
61     instance->ao.close = peak_close;
62     instance->flags = flags;
63     instance->peak = 0;
64 
65     return (ao_instance_t *) instance;
66 }
67 
ao_peak_open(void)68 ao_instance_t * ao_peak_open (void)
69 {
70     return peak_open (A52_STEREO);
71 }
72 
ao_peakdolby_open(void)73 ao_instance_t * ao_peakdolby_open (void)
74 {
75     return peak_open (A52_DOLBY);
76 }
77