Skip to content

Commit b48a6ec

Browse files
authored
Add more audio formats to audiolevel filter (#1287)
* Add more audio formats to audiolevel filter This avoids downsampling audio to s16 when used in a processing pipeline * Fix audio conversion math for u8 * Account for floating point rounding due to conversions * Increment filter version
1 parent fde7c02 commit b48a6ec

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

‎src/modules/normalize/filter_audiolevel.c‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ static inline double IEC_Scale(double dB)
8787
return fScale;
8888
}
8989

90+
static double get_sample(
91+
mlt_audio_format format, void *buffer, int channel, int sample, int channels, int samples)
92+
{
93+
if (format == mlt_audio_s16)
94+
return ((int16_t *) buffer)[channel + sample * channels] / -(double) INT16_MIN;
95+
if (format == mlt_audio_s32)
96+
return ((int32_t *) buffer)[channel * samples + sample] / -(double) INT32_MIN;
97+
if (format == mlt_audio_float)
98+
return ((float *) buffer)[channel * samples + sample];
99+
if (format == mlt_audio_s32le)
100+
return ((int32_t *) buffer)[channel + sample * channels] / -(double) INT32_MIN;
101+
if (format == mlt_audio_f32le)
102+
return ((float *) buffer)[channel + sample * channels];
103+
return (((uint8_t *) buffer)[channel + sample * channels] - 128) / 256.0;
104+
}
105+
90106
static int filter_get_audio(mlt_frame frame,
91107
void **buffer,
92108
mlt_audio_format *format,
@@ -102,7 +118,9 @@ static int filter_get_audio(mlt_frame frame,
102118
int iec_scale = mlt_properties_get_int(filter_props, "iec_scale");
103119
int dbPeak = mlt_properties_get_int(filter_props, "dbpeak");
104120
const char *prefix = mlt_properties_get(filter_props, "prefix");
105-
*format = mlt_audio_s16;
121+
if (*format != mlt_audio_s16 && *format != mlt_audio_s32 && *format != mlt_audio_float
122+
&& *format != mlt_audio_s32le && *format != mlt_audio_f32le && *format != mlt_audio_u8)
123+
*format = mlt_audio_float;
106124
int error = mlt_frame_get_audio(frame, buffer, format, frequency, channels, samples);
107125
if (error || !buffer || !buffer[0])
108126
return error;
@@ -112,30 +130,29 @@ static int filter_get_audio(mlt_frame frame,
112130
int num_oversample = 0;
113131
int c, s;
114132
char key[128];
115-
int16_t *pcm = (int16_t *) *buffer;
116133

117134
for (c = 0; c < *channels; c++) {
118135
double level = 0.0;
119136
if (dbPeak) {
120-
int16_t peakVal = 0;
137+
double peak = 0.0;
121138
for (s = 0; s < num_samples; s++) {
122-
int16_t sample = abs(pcm[c + s * num_channels]);
123-
if (sample > peakVal)
124-
peakVal = sample;
139+
double sample = fabs(get_sample(*format, *buffer, c, s, num_channels, *samples));
140+
if (sample > peak)
141+
peak = sample;
125142
}
126-
if (peakVal == 0)
143+
if (peak == 0)
127144
level = -100;
128145
else
129-
level = AMPTODBFS((double) peakVal / (double) INT16_MAX);
146+
level = AMPTODBFS(peak);
130147

131148
if (iec_scale)
132149
level = IEC_Scale(level);
133150
} else {
134151
double val = 0;
135152
for (s = 0; s < num_samples; s++) {
136-
double sample = fabs(pcm[c + s * num_channels] / 128.0);
153+
double sample = fabs(get_sample(*format, *buffer, c, s, num_channels, *samples));
137154
val += sample;
138-
if (sample == 128)
155+
if (sample > 0.9999)
139156
num_oversample++;
140157
else
141158
num_oversample = 0;
@@ -150,7 +167,7 @@ static int filter_get_audio(mlt_frame frame,
150167
}
151168
// max amplitude = 40/42, 3to10 oversamples=41, more then 10 oversamples=42
152169
if (level == 0.0 && num_samples > 0)
153-
level = val / num_samples * 40.0 / 42.0 / 127.0;
170+
level = val / num_samples * 40.0 / 42.0;
154171
if (iec_scale)
155172
level = IEC_Scale(AMPTODBFS(level));
156173
}

‎src/modules/normalize/filter_audiolevel.yml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schema_version: 7.2
22
type: filter
33
identifier: audiolevel
44
title: Audio Levels
5-
version: 4
5+
version: 5
66
copyright: Meltytech, LLC, Marco Gittler, and Steve Harris
77
creator: Dan Dennedy
88
contributor:
@@ -22,6 +22,11 @@ tags:
2222
- Audio
2323
audio_formats:
2424
- s16
25+
- s32
26+
- float
27+
- s32le
28+
- f32le
29+
- u8
2530
parameters:
2631
- identifier: prefix
2732
title: Frame Property Prefix

0 commit comments

Comments
 (0)