audio_out_peak.c 1.68 KB
Newer Older
Gildas Bazin's avatar
Gildas Bazin committed
1 2 3 4 5 6 7
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <inttypes.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
8
#include "dca.h"
Gildas Bazin's avatar
Gildas Bazin committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "audio_out.h"
#include "audio_out_internal.h"

typedef struct peak_instance_s {
    ao_instance_t ao;
    int flags;
    float peak;
} peak_instance_t;

static int peak_setup (ao_instance_t * _instance, int sample_rate, int * flags,
		       level_t * level, sample_t * bias)
{
    peak_instance_t * instance = (peak_instance_t *) _instance;

    *flags = instance->flags;
    *level = CONVERT_LEVEL;
    *bias = 0;

    return 0;
}

static int peak_play (ao_instance_t * _instance, int flags, sample_t * samples)
{
    peak_instance_t * instance = (peak_instance_t *) _instance;
    int i;

    for (i = 0; i < 256 * 2; i++) {
36
#ifdef LIBDCA_FIXED
Gildas Bazin's avatar
Gildas Bazin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	float f = fabs (samples[i] * (1.0 / (1 << 30)));
#else
	float f = fabs (samples[i]);
#endif
	if (instance->peak < f)
	    instance->peak = f;
    }

    return 0;
}

static void peak_close (ao_instance_t * _instance)
{
    peak_instance_t * instance = (peak_instance_t *) _instance;

    printf ("peak level = %.4f (%+.2f dB)\n",
	    instance->peak, 6 * log (instance->peak) / log (2));
}

static ao_instance_t * peak_open (int flags)
{
    peak_instance_t * instance;

    instance = (peak_instance_t *) malloc (sizeof (peak_instance_t));
    if (instance == NULL)
	return NULL;

    instance->ao.setup = peak_setup;
    instance->ao.play = peak_play;
    instance->ao.close = peak_close;
    instance->flags = flags;
    instance->peak = 0;

    return (ao_instance_t *) instance;
}

ao_instance_t * ao_peak_open (void)
{
    return peak_open (DTS_STEREO);
}

ao_instance_t * ao_peakdolby_open (void)
{
    return peak_open (DTS_DOLBY);
}