-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmdfourier.h
747 lines (618 loc) · 16.3 KB
/
mdfourier.h
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* 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/
*
*/
#ifndef MDFOURIER_H
#define MDFOURIER_H
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <libgen.h>
#include "incbeta.h"
#define MDVERSION "1.14"
#if INTPTR_MAX == INT64_MAX
#define BITS_MDF "64-bit"
#elif INTPTR_MAX == INT32_MAX
#define BITS_MDF "32-bit"
#else
#error Unknown pointer size or missing size macros!
#endif
#define MAX_FREQ_COUNT 96000 /* Number of frequencies to compare(MAX) */
#define FREQ_COUNT 2000 /* Number of frequencies to compare(default) */
#define DBL_PERFECT_MATCH 0.00001 // double difference to be considered a "perfect" match
#define SIGNIFICANT_AMPLITUDE -66.0
#define NS_LOWEST_AMPLITUDE -200
#define PCM_8BIT_MIN_AMPLITUDE -48.16
#define PCM_16BIT_MIN_AMPLITUDE -96.33
#define PCM_24BIT_MIN_AMPLITUDE -144.49
#define PCM_32BIT_MIN_AMPLITUDE -192.66
#define HIGHEST_NOISEFLOOR_ALLOWED -40.0
#define NOISE_FLOOR_DIGITAL_FAIR -120.0
#define STEREO_TOLERANCE_REPORT 8.5
#define TYPE_NOTYPE -1000
#define TYPE_SILENCE -1
#define TYPE_SYNC -2
#define TYPE_INTERNAL_KNOWN -4
#define TYPE_INTERNAL_UNKNOWN -5
#define TYPE_SKIP -6
#define TYPE_TIMEDOMAIN -7
#define TYPE_SILENCE_OVERRIDE -8
#define TYPE_WATERMARK -9
#define TYPE_CLK_ANALYSIS -10 // used internally
#define TYPE_CONTROL TYPE_SILENCE
#define TYPE_NULLTYPE_C '\0'
#define TYPE_SILENCE_C 'n'
#define TYPE_SYNC_C 's'
#define TYPE_NOTYPE_C '-'
#define TYPE_INTERNAL_KNOWN_C 'i'
#define TYPE_INTERNAL_UNKNOWN_C 'I'
#define TYPE_SKIP_C 'k'
#define TYPE_TIMEDOMAIN_C 't'
#define TYPE_SILENCE_OVER_C 'N'
#define TYPE_WATERMARK_C 'W'
#define TYPE_SILENCE_TRIM_C 'X'
#define BAR_DIFF_DB_TOLERANCE 1.0
#define NO_INDEX -100
#define NO_AMPLITUDE -10000
#define NO_FREQ -10000
#define START_HZ 20.0
#define END_HZ 20000.0
#define MAX_HZ 192000.0
#define SILENCE_LIMIT -220 //dBFS
#define START_HZ_PLOT 0
#define DB_HEIGHT 18.0
#define DB_DIFF DB_HEIGHT/2.0
#define MAXINT8 127
#define MININT8 -128.0
#define MAXINT16 32767.0
#define MININT16 -32768.0
#define MAXINT24 8388607.0
#define MININT24 -8388608.0
#define MAXINT32 2147483647.0
#define MININT32 -2147483648.0
// This value is the pitch difference at which it will be reported
// 0.25 cents is around 7hz in a 48khz signal
// 0.05 cents is around 2hz in a 48khz signal
#define MAX_CENTS_DIFF 0.25
#define MIN_CENTS_DIFF 0.08
#define BUFFER_SIZE 4096
#define T_BUFFER_SIZE BUFFER_SIZE*2+256
#define NO_ROLE 0
#define ROLE_REF 1
#define ROLE_COMP 2
#define CHANNEL_NONE '-' //Invalid state
#define CHANNEL_MONO 'm' //Is mono, used for balance
#define CHANNEL_STEREO 'S' //Requires Stereo
#define CHANNEL_PSTEREO 's' //Allows mono vs Stereo
#define CHANNEL_NOISE 'n' //Allows "deeper" fundamental frequency noise analysis
#define CHANNEL_LEFT 'l'
#define CHANNEL_RIGHT 'r'
#define NO_SYNC_AUTO_C 'A'
#define NO_SYNC_MANUAL_C 'M'
#define NO_SYNC_DIGITAL_C 'D'
#define NO_SYNC_LENGTH_C 'L'
#define NO_SYNC_AUTO 0
#define NO_SYNC_MANUAL 1
#define NO_SYNC_DIGITAL 2
#define NO_SYNC_LENGTH 3
#define INVALID_CHANNELS -1
#define WATERMARK_NONE 0
#define WATERMARK_VALID 1
#define WATERMARK_INVALID 2
#define WATERMARK_INDETERMINATE 3
#define AMPL_HIDIFF 1.0
#define MISS_HIDIFF 1.0
#define EXTRA_HIDIFF 1.0
#define NO_CLK -1
#define MASK_USE_WINDOW '*'
#define MASK_NONE '-'
#define MASK_DEFAULT MASK_NONE
#if defined (WIN32)
#define MAX_FOLDER_NAME 50
#define MAX_FILE_NAME 25
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define FOLDERCHAR '\\'
#else
#define FOLDERCHAR '/'
#endif
#if INTPTR_MAX == INT64_MAX
#define BITS_MDF "64-bit"
#elif INTPTR_MAX == INT32_MAX
#define BITS_MDF "32-bit"
#else
#error Unknown pointer size or missing size macros!
#endif
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
#define MAX_SYNC 9
#define FREQDOMTRIES 500
#define FREQDOMRATIO -30.0 // dBFS
#define DELAYCOUNT 10
#define PCNT_VISIBLE_WRN 10.0
#define PCNT_VISIBLE_ACT 25.0
enum normalize
{
max_time,
max_frequency,
average,
none
};
#define OUTPUT_FOLDER "MDFResults"
#define OUTPUT_FOLDER_MDW "MDWResults"
typedef struct max_vol {
double maxSample;
uint32_t offset;
double samplerate;
double framerate;
} MaxSample;
typedef struct max_mag {
double magnitude;
double hertz;
long int block;
char channel;
} MaxMagn;
typedef struct abt_st {
char typeName[128];
char typeDisplayName[128];
int type;
int elementCount;
int frames;
int cutFrames;
char color[20];
char channel;
int syncTone;
double syncLen;
int IsaddOnData;
char maskType;
} AudioBlockType;
typedef struct sync_st {
char syncName[255];
double MSPerFrame;
double LineCount;
int pulseSyncFreq;
int pulseFrameLen;
int pulseCount;
} VideoBlockDef;
typedef struct abd_st {
char Name[256];
int totalBlocks;
int regularBlocks;
VideoBlockDef SyncFormat[MAX_SYNC];
int syncCount;
AudioBlockType *typeArray;
int typeCount;
int useWatermark;
int watermarkValidFreq;
int watermarkInvalidFreq;
char watermarkDisplayName[128];
} AudioBlockDef;
/********************************************************/
/* WAV data structures */
typedef struct RIFF_HEADER
{
/* RIFF Chunk Descriptor */
uint8_t RIFF[4]; /* RIFF Header Magic header */
uint32_t ChunkSize; /* RIFF Chunk Size */
uint8_t WAVE[4]; /* WAVE Header */
} riff_hdr;
typedef struct SYB_CHUNK
{
/* sub-chunk */
uint8_t chunkID[4]; /* Chunk ID */
uint32_t Size; /* Size of the SubChunk */
} sub_chunk;
#define WAVE_FORMAT_PCM 0x0001
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
#define WAVE_FORMAT_ALAW 0x0006
#define WAVE_FORMAT_MULAW 0x0007
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE
#define FMT_TYPE_1 16
#define FMT_TYPE_2 18
#define FMT_TYPE_3 40
#define FMT_TYPE_1_SIZE 0
#define FMT_TYPE_2_SIZE 2
#define FMT_TYPE_3_SIZE 24
#define FMT_EXTRA_SIZE 24
typedef struct FMT_HEADER
{
/* "fmt" sub-chunk*/
uint8_t fmt[4]; /* FMT header */
uint32_t Subchunk1Size; /* Size of the fmt chunk */
uint16_t AudioFormat; /* Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM */
uint16_t NumOfChan; /* Number of channels 1=Mono 2=Sterio */
uint32_t SamplesPerSec; /* Sampling Frequency in Hz */
uint32_t bytesPerSec; /* bytes per second */
uint16_t blockAlign; /* 2=16-bit mono, 4=16-bit stereo */
uint16_t bitsPerSample; /* Number of bits per sample */
} fmt_hdr;
typedef struct FMT_HEADER_EXT1
{
/* "fmt" sub-chunk */
uint16_t extSize; /* Size of the extension: 18 */
} fmt_hdr_ext1;
typedef struct FMT_HEADER_EXT2
{
/* "fmt" sub-chunk */
uint16_t extSize; /* Size of the extension: 22 */
uint16_t wValidBitsPerSample; /* at most 8*M */
uint32_t dwChannelMask; /* Speaker position mask */
/* GUID (16 bytes, first two bytes are the data format code) */
uint16_t formatCode; /* formatcode */
uint8_t SubFormat[14];
} fmt_hdr_ext2;
typedef struct DATA_HEADER
{
/* "data" sub-chunk */
uint8_t DataID[4]; /* "data" string */
uint32_t DataSize; /* Sampled data length */
} data_hdr;
typedef struct FACT_CK
{
/* "fact" sub-chunk for WAVE_FORMAT_EXTENSIBLE */
uint8_t DataID[4]; /* "fact" string */
uint32_t DataSize; /* Chunk size: minimum 4 */
uint32_t dwSampleLength; /* Number of samples (per channel) */
} fact_ck;
typedef struct WAV_HEADER
{
riff_hdr riff;
fmt_hdr fmt;
data_hdr data;
} wav_hdr;
/********************************************************/
typedef struct FrequencySt {
double hertz;
double magnitude;
double amplitude;
double phase;
short matched;
} Frequency;
typedef struct fftw_spectrum_st {
fftw_complex *spectrum;
size_t size;
size_t ENBW;
} FFTWSpectrum;
typedef struct samples_st {
double *samples;
double *windowed_samples;
long int size;
long int difference;
long int padding;
long int sampleOffset;
} BlockSamples;
typedef struct AudioBlock_st {
Frequency *freq;
FFTWSpectrum fftwValues;
BlockSamples audio;
Frequency *freqRight;
FFTWSpectrum fftwValuesRight;
BlockSamples audioRight;
long int SilenceSizeLeft;
long int SilenceSizeRight;
BlockSamples *internalSync;
int internalSyncCount;
int index;
int type;
int frames;
double seconds;
char channel;
char maskType;
double AverageDifference;
double missingPercent;
double extraPercent;
} AudioBlocks;
typedef struct AudioSt {
char SourceFile[BUFFER_SIZE];
int AudioChannels;
int role;
int hasSilenceBlock;
double floorFreq;
double floorAmplitude;
double *Samples;
double SampleRate;
int bytesPerSample;
long int numSamples;
long int SamplesStart;
long int samplesPosFLAC;
int errorFLAC;
double framerate;
wav_hdr header;
uint8_t fmtExtra[24];
int fmtType;
fact_ck fact;
int factExists;
long int startOffset;
long int endOffset;
MaxMagn MaxMagnitude;
double gridFrequency;
double gridAmplitude;
double scanrateFrequency;
double scanrateAmplitude;
double crossFrequency;
double crossAmplitude;
double SilenceBinSize;
int nyquistLimit;
int watermarkStatus;
double startHz;
double endHz;
double delayArray[DELAYCOUNT];
int delayElemCount;
double balance;
AudioBlocks clkFrequencies;
double originalCLK;
double EstimatedSR_CLK;
double originalSR_CLK;
double EstimatedSR;
double originalSR;
double originalFrameRate;
AudioBlocks *Blocks;
} AudioSignal;
/********************************************************/
typedef struct window_unit_st {
double *window;
long int frames;
double seconds;
long int size;
long int sizePadding;
int trimSize;
} windowUnit;
typedef struct window_st {
windowUnit *windowArray;
int windowCount;
int MaxWindow;
double SampleRate;
char winType;
} windowManager;
/********************************************************/
typedef struct freq_diff_st {
double hertz;
double amplitude;
char channel;
} FreqDifference;
typedef struct ampl_diff_st {
double hertz;
double refAmplitude;
double diffAmplitude;
char channel;
} AmplDifference;
typedef struct phase_diff_st {
double hertz;
double diffPhase;
char channel;
} PhaseDifference;
typedef struct blk_diff_st {
FreqDifference *freqMissArray;
long int cntFreqBlkDiff;
long int cmpFreqBlkDiff;
AmplDifference *amplDiffArray;
long int cntAmplBlkDiff;
long int cmpAmplBlkDiff;
long int perfectAmplMatch;
// Left
long int cntAmplBlkDiffLeft;
long int cmpAmplBlkDiffLeft;
long int perfectAmplMatchLeft;
// Right
long int cntAmplBlkDiffRight;
long int cmpAmplBlkDiffRight;
long int perfectAmplMatchRight;
PhaseDifference *phaseDiffArray;
long int cntPhaseBlkDiff;
long int cmpPhaseBlkDiff;
int type;
char channel;
} BlockDifference;
typedef struct block_diff_st {
BlockDifference *BlockDiffArray;
long int cntPerfectAmplMatch;
long int cntFreqAudioDiff;
long int cntAmplAudioDiff;
long int cntPhaseAudioDiff;
long int cmpPhaseAudioDiff;
long int cntTotalCompared;
long int cntTotalAudioDiff;
// Left
long int cntPerfectAmplMatchLeft;
long int cntFreqAudioDiffLeft;
long int cntAmplAudioDiffLeft;
long int cntPhaseAudioDiffLeft;
long int cmpPhaseAudioDiffLeft;
long int cntTotalComparedLeft;
long int cntTotalAudioDiffLeft;
// Right
long int cntPerfectAmplMatchRight;
long int cntFreqAudioDiffRight;
long int cntAmplAudioDiffRight;
long int cntPhaseAudioDiffRight;
long int cmpPhaseAudioDiffRight;
long int cntTotalComparedRight;
long int cntTotalAudioDiffRight;
} AudioDifference;
/********************************************************/
typedef struct parameters_st {
char referenceFile[BUFFER_SIZE];
char comparisonFile[BUFFER_SIZE];
char folderName[BUFFER_SIZE*2];
char compareName[BUFFER_SIZE];
char profileFile[BUFFER_SIZE];
char outputFolder[BUFFER_SIZE];
char outputPath[BUFFER_SIZE];
double startHz, endHz;
double startHzPlot, endHzPlot;
double maxDbPlotZC;
int maxDbPlotZCChanged;
int showAll;
int extendedResults;
int verbose;
char window;
int MaxFreq;
int clock;
int ignoreFloor;
int outputFilterFunction;
AudioBlockDef types;
AudioDifference Differences;
double origSignificantAmplitude;
double significantAmplitude;
double referenceNoiseFloor;
double smallerFramerate;
double biggerFramerate;
double referenceFramerate;
int logScale;
int logScaleTS;
int debugSync;
int timeDomainSync;
int ZeroPad;
enum normalize normType;
int channelBalance;
int showPercent;
int noSyncProfile;
int noSyncProfileType;
double NoSyncTotalFrames;
int ignoreFrameRateDiff;
int labelNames;
double zoomWaveForm;
double thresholdAmplitudeHiDif;
double thresholdMissingHiDif;
double thresholdExtraHiDif;
// Keep Power/Amplitude levels matched even with uneven blocks
long int maxBlockFrameCount;
int padBlockSizes;
double maxBlockSeconds;
int plotDifferences;
int plotMissing;
int plotSpectrogram;
int plotTimeSpectrogram;
int plotNoiseFloor;
int plotTimeDomain;
int plotAllNotes;
int plotAllNotesWindowed;
int plotTimeDomainHiDiff;
int plotPhase;
double plotRatio;
int averagePlot;
int weightedAveragePlot;
int drawWindows;
int outputCSV;
int whiteBG;
int smallFile;
int syncTolerance;
int usesStereo;
int allowStereoVsMono;
double AmpBarRange;
int FullTimeSpectroScale;
int hasTimeDomain;
int hasSilenceOverRide;
int hasAddOnData;
int frequencyNormalizationTries;
double frequencyNormalizationTolerant;
int noiseFloorAutoAdjust;
int noiseFloorTooHigh;
int noiseFloorBigDifference;
int channelWithLowFundamentals;
double notVisible;
int stereoNotFound;
int SRNoMatch;
int diffClkNoMatch;
int internalSyncTolerance;
int changedCLKFrom;
double centsDifferenceCLK;
double RefCentsDifferenceSR;
double ComCentsDifferenceSR;
int pErrorReport;
int noBalance;
double highestValueBitDepth;
double lowestValueBitDepth;
double lowestDBFS;
int stereoBalanceBlock;
int warningStereoReversed;
double warningRatioTooHigh;
double syncAlignPct[4];
int syncAlignTolerance[4];
int syncAlignIterator;
int substractAveragePlot;
double averageDifference;
double averageDifferenceOrig;
double plotResX;
double plotResY;
fftw_plan sync_plan;
fftw_plan model_plan;
fftw_plan reverse_plan;
double refNoiseMin;
double refNoiseMax;
int videoFormatRef;
int videoFormatCom;
int nyquistLimit;
int useExtraData;
int compressToBlocks;
int drawPerfect;
int drawMissExtraFreq;
int trimmingNeeded;
/* Manual sync*/
int ManualSyncRef;
long ManualSyncRefStart;
long ManualSyncRefEnd;
int ManualSyncComp;
long ManualSyncCompStart;
long ManualSyncCompEnd;
/* Bisection Lines */
int SetBisectionLines;
double BisectionHertz;
double BisectionAmplitude;
/* Values only used for clock frequency */
char clkName[20];
int clkMeasure;
int clkBlock;
int clkFreq;
int clkRatio;
int clkNotFound;
int clkWarning;
double clkRef;
double clkCom;
int doClkAdjust;
int doSamplerateAdjust;
AudioSignal *referenceSignal;
AudioSignal *comparisonSignal;
// MDWave stuff
int maxBlanked;
int discardMDW;
int chunks;
int useCompProfile;
int executefft;
} parameters;
#endif