-
Notifications
You must be signed in to change notification settings - Fork 7
/
balance.c
executable file
·408 lines (340 loc) · 10.9 KB
/
balance.c
1
2
3
4
5
6
7
8
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
36
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* MDFourier
* A Fourier Transform analysis tool to compare game console audio
* http://junkerhq.net/MDFourier/
*
* Copyright (C)2019-2020 Artemio Urbina
*
* This file is part of the 240p Test Suite
*
* You can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Requires the FFTW library:
* http://www.fftw.org/
*
*/
#include "mdfourier.h"
#include "balance.h"
#include "freq.h"
#include "windows.h"
#include "log.h"
#include "cline.h"
#include "profile.h"
int CheckBalance(AudioSignal *Signal, int block, parameters *config)
{
long int pos = 0;
double longest = 0;
double *buffer;
long int buffersize = 0;
windowManager windows;
double *windowUsed = NULL;
long int loadedBlockSize = 0, i = 0, matchIndex = 0;
struct timespec start, end;
int discardBytes = 0;
double leftDecimals = 0, MaxMagLeft = 0, MaxMagRight = 0;
AudioBlocks Channels[2];
if(Signal->AudioChannels != 2)
{
logmsg(" - %s signal is mono\n", getRoleText(Signal));
if(config->allowStereoVsMono)
return -1;
else
{
if(config->usesStereo)
{
logmsg("ERROR: Stereo vs Mono not allowed by profile\n");
return 0;
}
else // All fine
return -1;
}
}
memset(&Channels, 0, sizeof(AudioBlocks)*2);
pos = Signal->startOffset;
longest = FramesToSeconds(Signal->framerate, GetLongestElementFrames(config));
if(!longest)
{
logmsg("Block definitions are invalid, total length is 0.\n");
return 0;
}
buffersize = SecondsToSamples(Signal->SampleRate, longest, Signal->AudioChannels, NULL, NULL);
buffer = (double*)malloc(sizeof(double)*buffersize);
if(!buffer)
{
logmsg("\tmalloc failed\n");
return(0);
}
// Use flattop for Amplitude accuracy
if(!initWindows(&windows, Signal->SampleRate, 'f', config))
return 0;
if(config->clock)
clock_gettime(CLOCK_MONOTONIC, &start);
while(i <= block)
{
double duration = 0;
long int frames = 0, difference = 0, cutFrames = 0;
frames = GetBlockFrames(config, i);
cutFrames = GetBlockCutFrames(config, i);
duration = FramesToSeconds(Signal->framerate, frames);
loadedBlockSize = SecondsToSamples(Signal->SampleRate, duration, Signal->AudioChannels, &discardBytes, &leftDecimals);
difference = GetSampleSizeDifferenceByFrameRate(Signal->framerate, frames, Signal->SampleRate, Signal->AudioChannels, config);
windowUsed = getWindowByLength(&windows, frames, cutFrames, config->smallerFramerate, config);
if(i == block)
{
Channels[0].index = GetBlockSubIndex(config, i);
Channels[0].type = GetBlockType(config, i);
Channels[0].seconds = 0;
Channels[1].index = Channels[0].index;
Channels[1].type = Channels[0].type;
Channels[1].seconds = 0;
memset(buffer, 0, buffersize);
if((uint32_t)(pos + loadedBlockSize) > Signal->header.data.DataSize)
{
logmsg("\tunexpected end of File, please record the full Audio Test from the 240p Test Suite\n");
break;
}
memcpy(buffer, Signal->Samples + pos, sizeof(double)*(loadedBlockSize-difference));
if(!ExecuteBalanceDFFT(&Channels[0], buffer, (loadedBlockSize-difference), Signal->SampleRate, windowUsed, CHANNEL_LEFT, config))
return 0;
if(!ExecuteBalanceDFFT(&Channels[1], buffer, (loadedBlockSize-difference), Signal->SampleRate, windowUsed, CHANNEL_RIGHT, config))
return 0;
Channels[0].freq = (Frequency*)malloc(sizeof(Frequency)*config->MaxFreq);
if(!Channels[0].freq)
{
logmsg("ERROR: Not enough memory for Data Structures\n");
return 0;
}
memset(Channels[0].freq, 0, sizeof(Frequency)*config->MaxFreq);
Channels[1].freq = (Frequency*)malloc(sizeof(Frequency)*config->MaxFreq);
if(!Channels[1].freq)
{
ReleaseBlock(&Channels[0]);
logmsg("ERROR: Not enough memory for Data Structures\n");
return 0;
}
memset(Channels[1].freq, 0, sizeof(Frequency)*config->MaxFreq);
if(!FillFrequencyStructures(Signal, &Channels[0], config))
{
ReleaseBlock(&Channels[0]);
ReleaseBlock(&Channels[1]);
logmsg("- Could not detect Stereo channel balance.\n");
return 0;
}
if(!FillFrequencyStructures(Signal, &Channels[1], config))
{
ReleaseBlock(&Channels[0]);
ReleaseBlock(&Channels[1]);
logmsg("- Could not detect Stereo channel balance.\n");
return 0;
}
}
pos += loadedBlockSize;
pos += discardBytes;
i++;
}
if(!Channels[0].freq || !Channels[1].freq)
{
ReleaseBlock(&Channels[0]);
ReleaseBlock(&Channels[1]);
logmsg("- Could not detect Stereo channel balance.\n");
return 0;
}
if(!areDoublesEqual(Channels[0].freq[0].hertz, Channels[1].freq[matchIndex].hertz))
matchIndex = 1; // Allow one bin difference
if(!areDoublesEqual(Channels[0].freq[0].hertz, Channels[1].freq[matchIndex].hertz))
{
logmsg("\nERROR: Channel balance block has different frequency content. (use -B to ignore)\n");
logmsg("\tNot a MONO signal for balance check. %s# %d (%d) at [%g Hz/%g] vs [%g Hz/%g]\n",
GetBlockName(config, block), GetBlockSubIndex(config, block), block,
Channels[0].freq[0].hertz, Channels[0].freq[0].magnitude,
Channels[1].freq[0].hertz, Channels[1].freq[0].magnitude);
if(config->verbose)
{
logmsgFileOnly("Left Channel:\n");
PrintFrequenciesBlockMagnitude(NULL, Channels[0].freq, config);
logmsgFileOnly("Right Channel:\n");
PrintFrequenciesBlockMagnitude(NULL, Channels[1].freq, config);
}
ReleaseBlock(&Channels[0]);
ReleaseBlock(&Channels[1]);
config->noBalance |= Signal->role;
return 0;
}
for(i = 0; i < config->MaxFreq; i++)
{
if(!Channels[0].freq[i].hertz && Channels[1].freq[i].hertz)
break;
if(Channels[0].freq[i].hertz && Channels[0].freq[i].magnitude > MaxMagLeft)
MaxMagLeft = Channels[0].freq[i].magnitude;
if(Channels[1].freq[i].hertz && Channels[1].freq[i].magnitude > MaxMagRight)
MaxMagRight = Channels[1].freq[i].magnitude;
}
if(!areDoublesEqual(Channels[0].freq[0].magnitude, Channels[1].freq[matchIndex].magnitude))
{
double ratio = 0;
double amplLeft = 0, amplRight = 0, amplDiff = 0;
char diffNam = '\0';
if(Channels[0].freq[0].magnitude > Channels[1].freq[matchIndex].magnitude)
{
diffNam = CHANNEL_LEFT;
ratio = Channels[1].freq[matchIndex].magnitude/Channels[0].freq[0].magnitude;
amplLeft = CalculateAmplitude(Channels[0].freq[0].magnitude, MaxMagLeft);
amplRight = CalculateAmplitude(Channels[1].freq[0].magnitude, MaxMagLeft);
amplDiff = amplLeft - amplRight;
}
else
{
diffNam = CHANNEL_RIGHT;
ratio = Channels[0].freq[0].magnitude/Channels[1].freq[matchIndex].magnitude;
amplLeft = CalculateAmplitude(Channels[0].freq[0].magnitude, MaxMagRight);
amplRight = CalculateAmplitude(Channels[1].freq[0].magnitude, MaxMagRight);
amplDiff = amplRight - amplLeft;
}
//logmsg("Amplitudes: Left %gdBFS Right %gdBFS, diff: %gdBFS\n", amplLeft, amplRight, amplDiff);
if(fabs(amplDiff) >= 0.0001)
{
logmsg(" - %s signal stereo imbalance: %s channel is higher by %g dBFS",
getRoleText(Signal),
diffNam == CHANNEL_LEFT ? "left" : "right", amplDiff);
}
else
{
logmsg(" - %s signal stereo imbalance: %s channel is higher by less than 0.0001 dBFS",
getRoleText(Signal),
diffNam == CHANNEL_LEFT ? "left" : "right");
}
if(config->verbose)
{
double percent_higher;
percent_higher = 100.0 * (pow(10, amplDiff / 20)-1.0);
logmsg(" (%0.5f%%)",
percent_higher);
}
logmsg("\n");
Signal->balance = amplDiff;
if(diffNam == CHANNEL_LEFT)
Signal->balance *= -1;
if(config->channelBalance)
BalanceAudioChannel(Signal, diffNam, ratio);
}
else
logmsg(" - %s signal has no stereo imbalance\n",
getRoleText(Signal));
if(config->clock)
{
double elapsedSeconds;
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedSeconds = TimeSpecToSeconds(&end) - TimeSpecToSeconds(&start);
logmsg(" - clk: Audio Channel Balancing took %0.2fs\n", elapsedSeconds);
}
ReleaseBlock(&Channels[0]);
ReleaseBlock(&Channels[1]);
free(buffer);
freeWindows(&windows);
return 1;
}
int ExecuteBalanceDFFT(AudioBlocks *AudioArray, double *samples, size_t size, double samplerate, double *window, char channel, parameters *config)
{
fftw_plan p = NULL;
long stereoSignalSize = 0;
long i = 0, monoSignalSize = 0, zeropadding = 0;
double *signal = NULL;
fftw_complex *spectrum = NULL;
double seconds = 0, S2 = 0;
if(!AudioArray)
{
logmsg("No Array for results\n");
return 0;
}
stereoSignalSize = (long)size;
monoSignalSize = stereoSignalSize/2;
seconds = (double)size/(samplerate*2);
if(config->ZeroPad) /* disabled by default */
zeropadding = GetZeroPadValues(&monoSignalSize, &seconds, samplerate);
signal = (double*)malloc(sizeof(double)*(monoSignalSize+1));
if(!signal)
{
logmsg("Not enough memory\n");
return(0);
}
spectrum = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*(monoSignalSize/2+1));
if(!spectrum)
{
logmsg("Not enough memory\n");
return(0);
}
if(!config->model_plan)
{
config->model_plan = fftw_plan_dft_r2c_1d(monoSignalSize, signal, spectrum, FFTW_MEASURE);
if(!config->model_plan)
{
logmsg("FFTW failed to create FFTW_MEASURE plan\n");
free(signal);
signal = NULL;
return 0;
}
}
p = fftw_plan_dft_r2c_1d(monoSignalSize, signal, spectrum, FFTW_MEASURE);
if(!p)
{
logmsg("FFTW failed to create FFTW_MEASURE plan\n");
free(signal);
signal = NULL;
return 0;
}
memset(signal, 0, sizeof(double)*(monoSignalSize+1));
memset(spectrum, 0, sizeof(fftw_complex)*(monoSignalSize/2+1));
for(i = 0; i < monoSignalSize - zeropadding; i++)
{
if(channel == CHANNEL_LEFT)
signal[i] = (double)samples[i*2];
if(channel == CHANNEL_RIGHT)
signal[i] = (double)samples[i*2+1];
if(window)
{
signal[i] *= window[i];
S2 += window[i]*window[i];
}
}
fftw_execute(p);
fftw_destroy_plan(p);
p = NULL;
free(signal);
signal = NULL;
AudioArray->fftwValues.spectrum = spectrum;
AudioArray->fftwValues.size = monoSignalSize;
AudioArray->fftwValues.ENBW = samplerate*S2;
AudioArray->seconds = seconds;
return(1);
}
void BalanceAudioChannel(AudioSignal *Signal, char channel, double ratio)
{
long int i = 0, start = 0, end = 0;
double *samples = NULL;
if(!Signal)
return;
if(!Signal->Samples)
return;
samples = Signal->Samples;
start = Signal->startOffset;
end = Signal->endOffset;
for(i = start; i < end; i+=2)
{
if(channel == CHANNEL_LEFT)
samples[i] = samples[i]*ratio;
if(channel == CHANNEL_RIGHT)
samples[i+1] = samples[i+1]*ratio;
}
}