You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
930 B

#include "audio_gain.h"
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <log/log.h>
static inline int16_t float_to_short(float f)
{
static const float scale = 1 << 15;
return roundf(fmaxf(fminf(f * scale, scale - 1.f), -scale));
}
static inline float short_to_float(int16_t ival)
{
static const float scale = 1. / (float)(1UL << 15);
return ival * scale;
}
void audio_gain_process(int16_t *src, size_t count, int mute, float db_level)
{
float db_gain;
float value;
size_t i;
if (src == NULL) {
ALOGE("audio_gain_process fail, src is NULL");
return;
}
db_gain = powf(10, db_level / 20.0f); /* 10 ,20.0 are used to cauculate db */
if (mute != 0) {
db_gain = 0.0f;
}
for (i = 0; i < count; i++) {
value = short_to_float(src[i]) * db_gain;
src[i] = float_to_short(value);
}
}