-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsync.c
executable file
·1426 lines (1239 loc) · 47.6 KB
/
sync.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "sync.h"
#include "log.h"
#include "freq.h"
/*
There are the number of subdivisions to use.
The higher, the less precise in frequency but more in position and vice versa
*/
#define SYNC_LPF 22000 // Sync Low pass filter
#define FACTOR_LFEXPL 4
#define FACTOR_EXPLORE 8
#define FACTOR_DETECT 8
// Cut off for harmonic search
#define HARMONIC_TSHLD 6000
long int DetectPulse(double *AllSamples, wav_hdr header, int role, parameters *config)
{
int maxdetected = 0, AudioChannels = 0;
long int sampleOffset = 0, searchOffset = 0;
if(config->debugSync)
logmsgFileOnly("\nStarting Detect start pulse\n");
AudioChannels = header.fmt.NumOfChan;
sampleOffset = DetectPulseInternal(AllSamples, header, FACTOR_EXPLORE, 0, &maxdetected, role, AudioChannels, config);
if(sampleOffset == -1)
{
if(config->debugSync)
logmsgFileOnly("WARNING SYNC: First round start pulse failed\n");
// Find out a new starting point where some soudn starts
searchOffset = DetectSignalStart(AllSamples, header, 0, 0, 0, NULL, NULL, config);
if (searchOffset > 0)
{
long int MS_Samples = 0;
MS_Samples = SecondsToSamples(header.fmt.SamplesPerSec, 0.015, AudioChannels, NULL, NULL);
if (searchOffset >= MS_Samples)
searchOffset -= MS_Samples;
}
else
return -1;
sampleOffset = DetectPulseInternal(AllSamples, header, FACTOR_EXPLORE, searchOffset, &maxdetected, role, AudioChannels, config);
if(sampleOffset == -1)
return -1;
}
searchOffset = AdjustPulseSampleStartByLength(AllSamples, header, sampleOffset, role, AudioChannels, config);
if (searchOffset != -1 && searchOffset != sampleOffset)
{
if (config->debugSync)
logmsg(" SYNC: Adjusted pulse sample start from %ld to %ld",
SamplesForDisplay(sampleOffset, AudioChannels), SamplesForDisplay(searchOffset, AudioChannels));
sampleOffset = searchOffset;
}
if(config->debugSync)
logmsgFileOnly(" Start pulse return value %ld\n", SamplesForDisplay(sampleOffset, AudioChannels));
return sampleOffset;
}
/*
positions relative to the expected one
Start with common sense ones, then search all around the place
2.1 and above were added for PAL MD at 60 detection. Yes, that is 2.1
*/
#define END_SYNC_MAX_TRIES 86
#define END_SYNC_VALUES { 0.50, 0.25, 0.0, 1.25, 1.50,\
0.9, 0.8, 0.7, 0.6, 1.6, 1.7, 1.8, 1.9,\
0.4, 0.3, 0.2, 0.1, 1.1, 1.3, 1.2, 1.4,\
1.0, -1,0, 2.0, -2.0,\
2.1, 2.2, 2.3, 2.4, 2.5, 2.5, 2.7, 2.8, 2.9, 3.0,\
-2.1, -2.2, -2.3, -2.4, -2.5, -2.5, -2.7, -2.8, -2.9, -3.0,\
3.1, 3.2, 3.3, 3.4, 3.5, 3.5, 3.7, 3.8, 3.9, 4.0,\
-3.1, -3.2, -3.3, -3.4, -3.5, -3.5, -3.7, -3.8, -3.9, -4.0,\
-0.50, -0.25, -1.25, -1.50,\
-0.9, -0.8, -0.7, -0.6, -1.6, -1.7, -1.8, -1.9,\
-0.4, -0.3, -0.2, -0.1, -1.1, -1.2, -1.3, -1.4 }
long int DetectEndPulse(double *AllSamples, long int startpulse, wav_hdr header, int role, parameters *config)
{
int maxdetected = 0, frameAdjust = 0, tries = 0, maxtries = END_SYNC_MAX_TRIES;
int factor = 0, AudioChannels = 0, bytesPerSample = 0;
long int sampleOffset = 0, searchOffset = -1, totalSamples = 0, secondSilenceStart = 0;
double silenceOffset[END_SYNC_MAX_TRIES] = END_SYNC_VALUES;
bytesPerSample = header.fmt.bitsPerSample / 8;
totalSamples = header.data.DataSize / bytesPerSample;
AudioChannels = header.fmt.NumOfChan;
if(GetPulseSyncFreq(role, config) < HARMONIC_TSHLD)
factor = FACTOR_LFEXPL;
else
factor = FACTOR_EXPLORE;
/* Try a clean detection */
sampleOffset = GetSecondSyncSilenceSampleOffset(GetMSPerFrameRole(role, config), header, 0, 1, config);
if(!sampleOffset)
{
logmsg("ERROR: Invalid profile, Closing sync has no pre-silence block\n");
return -1;
}
sampleOffset += startpulse;
secondSilenceStart = sampleOffset;
if(sampleOffset < totalSamples)
{
if(config->debugSync)
logmsgFileOnly("\nStarting CLEAN Detect end pulse with sample offset %ld\n", SamplesForDisplay(sampleOffset, AudioChannels));
searchOffset = DetectPulseInternal(AllSamples, header, factor, sampleOffset, &maxdetected, role, AudioChannels, config);
if(searchOffset != -1)
{
sampleOffset = searchOffset;
searchOffset = AdjustPulseSampleStartByLength(AllSamples, header, searchOffset, role, AudioChannels, config);
if (searchOffset != -1 && searchOffset != sampleOffset)
{
if (config->debugSync)
logmsg(" SYNC: Adjusted pulse sample start from %ld to %ld",
SamplesForDisplay(sampleOffset, AudioChannels), SamplesForDisplay(searchOffset, AudioChannels));
sampleOffset = searchOffset;
}
return sampleOffset;
}
/* We try to figure out position of the pulses, things are not fine with this recording, profile selected, framerate, etc */
if(config->debugSync)
logmsgFileOnly("End pulse CLEAN detection failed started search at %ld samples\n", SamplesForDisplay(sampleOffset, AudioChannels));
}
else
{
if(config->debugSync)
logmsgFileOnly("End pulse CLEAN detection out of bounds at %ld samples\n", SamplesForDisplay(sampleOffset, AudioChannels));
}
do
{
/* Use defaults to calculate real frame rate but don't go further than silence*/
sampleOffset = GetSecondSyncSilenceSampleOffset(GetMSPerFrameRole(role, config), header, frameAdjust, silenceOffset[tries], config) + startpulse;
if(sampleOffset < secondSilenceStart && sampleOffset < totalSamples)
{
if(config->debugSync)
logmsgFileOnly("\nFile %s\nStarting Detect end pulse #%d with sample offset %ld [%g silence]\n\tMaxDetected %d frameAdjust: %d\n",
GetFileName(role, config), tries + 1, SamplesForDisplay(sampleOffset, AudioChannels), silenceOffset[tries], maxdetected, frameAdjust);
frameAdjust = 0;
maxdetected = 0;
searchOffset = DetectPulseInternal(AllSamples, header, factor, sampleOffset, &maxdetected, role, AudioChannels, config);
if(searchOffset == -1 && !maxdetected)
{
if(config->debugSync)
logmsgFileOnly("End pulse failed try #%d, started search at %ld samples [%g silence]\n",
tries+1, SamplesForDisplay(sampleOffset, AudioChannels), silenceOffset[tries]);
}
}
else
{
if(sampleOffset > totalSamples && config->debugSync)
logmsgFileOnly("End pulse try #%d detection out of bounds at %ld samples\n", tries+1, SamplesForDisplay(sampleOffset, AudioChannels));
}
tries ++;
}while(searchOffset == -1 && tries < maxtries);
if(searchOffset == -1)
{
tries = 0;
do
{
/* Use defaults to calculate real frame rate further back than silence... */
sampleOffset = GetSecondSyncSilenceSampleOffset(GetMSPerFrameRole(role, config), header, frameAdjust, silenceOffset[tries], config) + startpulse;
if(sampleOffset >= secondSilenceStart && sampleOffset < totalSamples)
{
if(config->debugSync)
logmsgFileOnly("\nFile %s\nStarting Detect end pulse #%d with sample offset %ld [%g silence]\n\tMaxDetected %d frameAdjust: %d\n",
GetFileName(role, config), tries + 1, SamplesForDisplay(sampleOffset, AudioChannels), silenceOffset[tries], maxdetected, frameAdjust);
frameAdjust = 0;
maxdetected = 0;
searchOffset = DetectPulseInternal(AllSamples, header, factor, sampleOffset, &maxdetected, role, AudioChannels, config);
if(searchOffset == -1 && !maxdetected)
{
if(config->debugSync)
logmsgFileOnly("End pulse failed try #%d, started search at %ld samples [%g silence]\n",
tries+1, SamplesForDisplay(sampleOffset, AudioChannels), silenceOffset[tries]);
}
}
else
{
if(sampleOffset > totalSamples && config->debugSync)
logmsgFileOnly("End pulse #%d detection out of bounds at %ld samples\n", tries+1, SamplesForDisplay(sampleOffset, AudioChannels));
}
tries ++;
}while(searchOffset == -1 && tries < maxtries);
}
if(tries >= maxtries)
return -1;
sampleOffset = searchOffset;
searchOffset = AdjustPulseSampleStartByLength(AllSamples, header, sampleOffset, role, AudioChannels, config);
if (searchOffset != -1 && searchOffset != sampleOffset)
{
if (config->debugSync)
logmsg(" SYNC: Adjusted pulse sample start from %ld to %ld",
SamplesForDisplay(sampleOffset, AudioChannels), SamplesForDisplay(searchOffset, AudioChannels));
sampleOffset = searchOffset;
}
if(config->debugSync)
logmsgFileOnly("End pulse return value %ld\n", SamplesForDisplay(sampleOffset, AudioChannels));
return sampleOffset;
}
#define LOGCASE(x, y) { x; if(config->debugSync) logmsgFileOnly("Case #%d\n", y); }
double findAverageAmplitudeForTarget(Pulses *pulseArray, double targetFrequency, double *targetFrequencyHarmonic, long int TotalMS, long int start, int factor, int AudioChannels, parameters *config)
{
long count = 0, i = 0, countNoise = 0;
double averageAmplitude = 0, standardDeviation = 0, useAmplitude = 0, percent = 0;
if(config->debugSync)
logmsgFileOnly("Searching for in range: %ld-%ld samples\n",
SamplesForDisplay(pulseArray[start].samples, AudioChannels),
SamplesForDisplay(pulseArray[TotalMS-1].samples, AudioChannels));
for(i = start; i < TotalMS; i++)
{
if(pulseArray[i].hertz == targetFrequency
|| (targetFrequencyHarmonic[0] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[0])
|| (targetFrequencyHarmonic[1] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[1]))
{
averageAmplitude += fabs(pulseArray[i].amplitude);
count ++;
}
}
if(!count)
{
logmsgFileOnly("WARNING! Average Amplitude values for sync not found in range (NULL from digital/emu)\n");
return 0;
}
averageAmplitude /= count;
// Calculate Standard Deviatioon
count = 0;
for(i = start; i < TotalMS; i++)
{
if(pulseArray[i].hertz == targetFrequency
|| (targetFrequencyHarmonic[0] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[0])
|| (targetFrequencyHarmonic[1] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[1]))
{
standardDeviation += pow(fabs(pulseArray[i].amplitude) - averageAmplitude, 2);
count ++;
}
}
if(!count)
{
logmsgFileOnly("WARNING! Standard Deviation values for sync not found in range (NULL from digital/emu)\n");
return 0;
}
standardDeviation = sqrt(standardDeviation/(count-1));
percent = (double)count/(double)(TotalMS-start)*100;
if(factor == FACTOR_EXPLORE) { // first round detection
if(averageAmplitude <= 40) { //this is the regular case for the above
if(averageAmplitude >= 10) {
if(standardDeviation < averageAmplitude) {
if(percent > 5 && !(config->syncTolerance >= 2))
LOGCASE(useAmplitude = -1*averageAmplitude, 0)
else
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation/2), 1)
} else
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation/4), 2)
} else {
LOGCASE(useAmplitude = -1*(averageAmplitude + 2*standardDeviation), 3)
}
} else {
// these are special cases, too much difference
if(standardDeviation < averageAmplitude)
LOGCASE(useAmplitude = -1*(averageAmplitude - standardDeviation), 4)
else
LOGCASE(useAmplitude = -1*(averageAmplitude - standardDeviation/2), 5)
}
}
else if(factor == FACTOR_DETECT) { // second round
if(percent > 55) { // Too much noise
if(percent > 90) { // ridiculous noise
if(percent == 100)
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation/4), 6)
else
LOGCASE(useAmplitude = -1*averageAmplitude, 7)
} else if(averageAmplitude <= 40) {
if(standardDeviation < averageAmplitude)
LOGCASE(useAmplitude = -1*averageAmplitude, 8)
else
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation/4), 9)
} else {
// these are special cases, too much difference
if(standardDeviation < averageAmplitude)
LOGCASE(useAmplitude = -1*(averageAmplitude - standardDeviation), 10)
else
LOGCASE(useAmplitude = -1*(averageAmplitude - standardDeviation/2), 11)
}
} else { // lower that 55%, pulses hopefully
if(standardDeviation < averageAmplitude || (fabs(standardDeviation) + fabs(averageAmplitude) <= 20)) // this is the ideal case
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation), 12)
else // This is trying to adjust it
LOGCASE(useAmplitude = -1*(averageAmplitude + standardDeviation/4), 13)
}
}
if(count && useAmplitude == 0)
{
if(config->debugSync)
logmsgFileOnly("Signal is too perfect, matching to -1dbfs\n");
useAmplitude = -1;
}
for(i = start; i < TotalMS; i++)
{
if(pulseArray[i].hertz == targetFrequency)
countNoise++;
}
if(config->syncTolerance >= 2 || countNoise >= (TotalMS - start)*.70)
{
// clean noise at sync frequency
for(i = start; i < TotalMS; i++)
{
if((pulseArray[i].hertz == targetFrequency
|| (targetFrequencyHarmonic[0] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[0])
|| (targetFrequencyHarmonic[1] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[1]))
&& pulseArray[i].amplitude != NO_AMPLITUDE && pulseArray[i].amplitude < floor(useAmplitude))
pulseArray[i].hertz = 0;
}
}
if(config->debugSync)
logmsgFileOnly("AVG Amp: %g STD Dev: %g Use Amp: %g count %ld total %d %% %g (Noise %g%%)\n",
averageAmplitude, standardDeviation, useAmplitude,
TotalMS-start, count, (double)count/(double)(TotalMS-start)*100,
(double)countNoise/(double)(TotalMS-start)*100);
if(config->debugSync)
logmsgFileOnly("Searching for Average amplitude in block: F %g Total Start sample: %ld milliseconds to check: %ld\n",
targetFrequency,
SamplesForDisplay(pulseArray[start].samples, AudioChannels),
TotalMS);
return useAmplitude;
}
long int DetectPulseTrainSequence(Pulses *pulseArray, double targetFrequency, double *targetFrequencyHarmonic, long int TotalMS, int factor, int *maxdetected, long int start, int role, int AudioChannels, parameters *config)
{
long i = 0, sequence_start = 0;
int frame_pulse_count = 0, frame_silence_count = 0,
pulse_count = 0, silence_count = 0, lastcountedWasPulse = 0, lookingfor = 0;
double averageAmplitude = 0;
*maxdetected = 0;
lookingfor = getPulseFrameLen(role, config)*factor-factor/2;
lookingfor = lookingfor * 0.95;
//smoothAmplitudes(pulseArray, targetFrequency, TotalMS, start);
averageAmplitude = findAverageAmplitudeForTarget(pulseArray, targetFrequency, targetFrequencyHarmonic, TotalMS, start, factor, AudioChannels, config);
if(averageAmplitude == 0)
return -1;
if(config->debugSync)
logmsgFileOnly("== Searching for %g/%g/%g Average Amplitude %g looking for %d (%d*%d)\n",
targetFrequency,
NO_FREQ != targetFrequencyHarmonic[0] ? targetFrequencyHarmonic[0] : 0,
NO_FREQ != targetFrequencyHarmonic[1] ? targetFrequencyHarmonic[1] : 0, averageAmplitude, lookingfor,
getPulseFrameLen(role, config), factor);
for(i = start; i < TotalMS; i++)
{
int checkSilence = 0;
if(pulseArray[i].hertz)
{
if(config->syncTolerance >= 3)
targetFrequency = pulseArray[i].hertz;
if(pulseArray[i].amplitude >= averageAmplitude
&& (pulseArray[i].hertz == targetFrequency ||
(targetFrequencyHarmonic[0] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[0]) ||
(targetFrequencyHarmonic[1] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[1])))
{
int linefeedNeeded = 1;
frame_pulse_count++;
lastcountedWasPulse = 1;
if(config->debugSync)
logmsgFileOnly("[i:%ld] Sample:%7ld [%5gHz %0.2f dBFS] <<Pulse Frame counted %d>>",
i, SamplesForDisplay(pulseArray[i].samples, AudioChannels),
pulseArray[i].hertz, pulseArray[i].amplitude,
frame_pulse_count);
if(!sequence_start)
{
if(config->debugSync)
logmsgFileOnly(" This starts the sequence\n");
sequence_start = pulseArray[i].samples;
frame_silence_count = 0;
linefeedNeeded = 0;
}
if(frame_silence_count >= lookingfor*3/5) /* allow silence to have some stray noise */
{
silence_count++;
if(config->debugSync)
logmsgFileOnly("Closed a silence cycle %d\n", silence_count);
linefeedNeeded = 0;
frame_silence_count = 0;
}
//if(frame_silence_count)
//frame_silence_count = 0;
if(config->debugSync && linefeedNeeded)
logmsgFileOnly("\n");
}
else
checkSilence = 1;
}
else
checkSilence = 1;
// Fix issues with Digital recordings
if(!pulseArray[i].hertz && pulseArray[i].amplitude == NO_AMPLITUDE &&
frame_pulse_count > 2 && frame_pulse_count < lookingfor/10)
{
if(config->debugSync)
logmsgFileOnly("[i:%ld] Sample:%7ld [%5gHz %0.2f dBFS] Undefined Amplitude found (digital silence), resetting the sequence FC: %d SC: %d\n",
i, SamplesForDisplay(pulseArray[i].samples, AudioChannels),
pulseArray[i].hertz, pulseArray[i].amplitude,
frame_pulse_count, frame_silence_count);
if(config->syncTolerance >= 2)
frame_pulse_count = 0;
checkSilence = 0;
}
// 1 tick tolerance within a pulse
if(checkSilence && pulseArray[i].amplitude < averageAmplitude)
{
if(frame_pulse_count >= lookingfor/3 && (lastcountedWasPulse && (pulseArray[i].hertz == targetFrequency ||
(targetFrequencyHarmonic[0] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[0]) ||
(targetFrequencyHarmonic[1] != NO_FREQ && pulseArray[i].hertz == targetFrequencyHarmonic[1]))))
{
if(config->debugSync)
logmsgFileOnly("[i:%ld] Sample:%7ld [%5gHz %0.2f dBFS] Silence Frame found due to AVG but skipped due to Hz SC: %d\n",
i, SamplesForDisplay(pulseArray[i].samples, AudioChannels),
pulseArray[i].hertz, pulseArray[i].amplitude,
frame_silence_count);
lastcountedWasPulse = 0;
checkSilence = 0;
}
}
if(checkSilence)
{
if(pulseArray[i].amplitude < averageAmplitude)
{
if(config->debugSync)
logmsgFileOnly("[i:%ld] sample:%7ld [%5gHz %0.2f dBFS] %s %d\n",
i, SamplesForDisplay(pulseArray[i].samples, AudioChannels),
pulseArray[i].hertz, pulseArray[i].amplitude,
sequence_start ? " Silence Frame counted" : "--", frame_silence_count);
frame_silence_count ++;
}
else
{
int linefeedNeeded = 1;
if(config->debugSync)
logmsgFileOnly("[i:%ld] sample:%7ld [%5gHz %0.2f dBFS] Non Frame skipped %d LC:%d",
i, SamplesForDisplay(pulseArray[i].samples, AudioChannels),
pulseArray[i].hertz, pulseArray[i].amplitude,
frame_silence_count, lastcountedWasPulse);
if(lastcountedWasPulse == 0)
{
if(config->debugSync)
logmsgFileOnly(" Counting as silence\n");
frame_silence_count++;
linefeedNeeded = 0;
}
if(lastcountedWasPulse == 1 && frame_pulse_count >= lookingfor)
{
if(config->debugSync)
logmsgFileOnly(" Counting as silence due to pulse count\n");
frame_silence_count++;
linefeedNeeded = 0;
}
if(config->debugSync && linefeedNeeded)
logmsgFileOnly("\n");
}
if(frame_pulse_count >= lookingfor && (pulseArray[i].hertz != targetFrequency
|| (config->syncTolerance && pulseArray[i].amplitude < averageAmplitude && frame_silence_count > (int)(lookingfor*1.5) )))
{
pulse_count++;
if(config->debugSync)
logmsgFileOnly("Closed pulse #%d cycle. Silence count %d. Frame: Pulse %d Silence: %d (look reg %d/ used %d)\n",
pulse_count, silence_count, frame_pulse_count, frame_silence_count, lookingfor, (int)(lookingfor*1.5));
if(config->syncTolerance)
silence_count = pulse_count - 1;
if(pulse_count == getPulseCount(role, config) && silence_count >= pulse_count/2) /* silence_count == pulse_count - 1 */
{
if(config->debugSync)
logmsgFileOnly("Completed the sequence that started at sample %ld\n", SamplesForDisplay(sequence_start, AudioChannels));
return sequence_start;
}
lastcountedWasPulse = 0;
}
if(frame_pulse_count > 0 && pulseArray[i].hertz != targetFrequency)
{
if(!pulse_count && sequence_start)
{
if(config->debugSync)
logmsgFileOnly("Resets the sequence (no pulse count)\n");
sequence_start = 0;
}
if(!lastcountedWasPulse) // Allow 1 frame of silence/invalid frequency in between
{
frame_pulse_count = 0;
lastcountedWasPulse = 0;
}
}
if(frame_silence_count > lookingfor*2)
{
if(pulse_count)
{
if(config->debugSync)
logmsgFileOnly("Resets the sequence (silence too long > %d)\n", lookingfor * 2);
sequence_start = 0;
pulse_count = 0;
}
}
}
}
if(config->debugSync)
logmsgFileOnly("Failed\n");
*maxdetected = pulse_count;
return -1;
}
long int AdjustPulseSampleStartByLength(double* Samples, wav_hdr header, long int offset, int role, int AudioChannels, parameters* config)
{
int samplesNeeded = 0, frequency = 0, startDetectPos = -1, endDetectPos = -1, bytesPerSample = 0;
long int startSearch = 0, endSearch = 0, pos = 0, count = 0, foundPos = -1, totalSamples = 0;
long int synLenInSamples = 0, matchCount = 0, tolerance = 0;
double* buffer = NULL, percentSTD = 0;
Pulses* pulseArray = NULL;
double targetFrequency = 0;
double syncLen = 0, averageMag = 0, standardDeviation = 0, compareMag = 0;
bytesPerSample = header.fmt.bitsPerSample / 8;
totalSamples = header.data.DataSize / bytesPerSample;
/* check for the time duration in ms of a single sync pulses */
syncLen = FramesToSeconds(1, GetMSPerFrameRole(role, config))*1000.0;
if(!syncLen)
{
logmsgFileOnly("\tERROR: No Sync Length\n");
return(foundPos);
}
frequency = GetPulseSyncFreq(role, config);
samplesNeeded = (double)header.fmt.SamplesPerSec/frequency*AudioChannels;
targetFrequency = FindFrequencyBracketForSync(frequency,
samplesNeeded, AudioChannels, header.fmt.SamplesPerSec, config);
synLenInSamples = RoundToNsamples(((double)header.fmt.SamplesPerSec*syncLen*AudioChannels) / 1000.0, AudioChannels, NULL, NULL);
buffer = (double*)malloc(samplesNeeded * sizeof(double));
if (!buffer)
{
logmsgFileOnly("\tERROR: Sync Adjust malloc failed\n");
return(foundPos);
}
if (offset >= synLenInSamples)
{
startSearch = offset - synLenInSamples;
startSearch = RoundToNsamples((double)startSearch, AudioChannels, NULL, NULL);
}
else
startSearch = 0;
endSearch = offset + synLenInSamples*1.5;
endSearch = RoundToNsamples((double)endSearch, AudioChannels, NULL, NULL);
if (config->debugSync)
logmsgFileOnly("\nSearching at %ld samples/%ld bytes End At: %ld samples/%ld bytes (%d bytes per sample)\n looking for %d->%ghz samples needed: %d\n",
SamplesForDisplay(startSearch, AudioChannels), SamplesToBytes(startSearch, bytesPerSample),
SamplesForDisplay(endSearch, AudioChannels), SamplesToBytes(endSearch, bytesPerSample),
bytesPerSample,
frequency, targetFrequency,
SamplesForDisplay(samplesNeeded, AudioChannels));
pulseArray = (Pulses*)malloc(sizeof(Pulses) * (endSearch - startSearch));
if (!pulseArray)
{
free(buffer);
logmsgFileOnly("\tPulse malloc failed!\n");
return(foundPos);
}
memset(pulseArray, 0, sizeof(Pulses) * (endSearch - startSearch));
// we are counting inn samples, not bytes
for (pos = startSearch; pos < endSearch; pos += AudioChannels)
{
memset(buffer, 0, samplesNeeded * sizeof(double));
if (pos + samplesNeeded > totalSamples)
{
//logmsg("\tUnexpected end of File, please record the full Audio Test from the 240p Test Suite\n");
break;
}
pulseArray[count].samples = pos;
memcpy(buffer, Samples + pos, samplesNeeded * sizeof(double));
ProcessChunkForSyncPulse(buffer, samplesNeeded,
header.fmt.SamplesPerSec, &pulseArray[count],
CHANNEL_LEFT, AudioChannels, config);
count++;
}
// Calculate Average
for (pos = 0; pos < count; pos++)
{
if (pulseArray[pos].hertz == targetFrequency &&
pulseArray[pos].samples >= offset && pulseArray[pos].samples <= offset + synLenInSamples)
{
averageMag += pulseArray[pos].magnitude;
matchCount++;
}
}
if (!matchCount)
{
logmsgFileOnly("\tERROR: Sync Adjustment, no matches at %g\n", targetFrequency);
free(buffer);
free(pulseArray);
return(foundPos);
}
averageMag = averageMag / (double)matchCount;
// Calculate Standard Deviatioon
matchCount = 0;
for (pos = 0; pos < count; pos++)
{
if (pulseArray[pos].hertz == targetFrequency
&& pulseArray[pos].samples >= offset && pulseArray[pos].samples <= offset + synLenInSamples)
{
standardDeviation += pow(fabs(pulseArray[pos].magnitude) - averageMag, 2);
matchCount++;
}
}
if (!matchCount)
{
logmsgFileOnly("\tERROR: Sync Adjustment, no matches at for std dev %g\n", targetFrequency);
free(buffer);
free(pulseArray);
return(foundPos);
}
standardDeviation = sqrt(standardDeviation / (matchCount - 1));
// Estimate magnitude search
percentSTD = standardDeviation * 100 / averageMag;
if(percentSTD < 10) // strict one
compareMag = averageMag*0.999;
if(percentSTD >= 10 && percentSTD < 15) // works fine, common
compareMag = averageMag - standardDeviation;
if(percentSTD >= 15 && percentSTD < 25) // works fine, common
compareMag = averageMag - standardDeviation/2;
if(percentSTD >= 25 && percentSTD < 90) // works fine, less common
compareMag = averageMag - standardDeviation/3;
if(percentSTD >= 90 && percentSTD < 100) // least common
compareMag = averageMag - standardDeviation/4;
if(percentSTD >= 100) // least common
compareMag = averageMag - standardDeviation/5;
config->syncAlignPct[config->syncAlignIterator] = percentSTD;
if (config->debugSync)
logmsgFileOnly("Adjust Sync %g%% AVG: %g STD: %g Used: %g\n", percentSTD, averageMag, standardDeviation, compareMag);
for (pos = 0; pos < count; pos++)
{
// pulseArray[pos].hertz is 0 if not a harmonic or the Target frequency
if (config->debugSync)
logmsgFileOnly("[%0.4d/%0.4d]Sample: %ld/Byte:%ld %gHz Mag %g Phase %g Search for: %g\n", pos, count,
SamplesForDisplay(pulseArray[pos].samples, AudioChannels),
SamplesToBytes(pulseArray[pos].samples, bytesPerSample),
pulseArray[pos].hertz, pulseArray[pos].magnitude, pulseArray[pos].phase,
compareMag);
if (pulseArray[pos].hertz != 0 && pulseArray[pos].magnitude >= compareMag)
{
if (startDetectPos == -1)
{
startDetectPos = pos;
if (config->debugSync)
logmsgFileOnly("== match start == using Sample: %ld\n",
SamplesForDisplay(pulseArray[startDetectPos].samples, AudioChannels));
}
}
else
{
// Only do something if Start Sync was detected
if (startDetectPos != -1)
{
double adjustEnd = 1.0;
long int foundPulseLength = 0;
// Adjust tolerance if we still are not within the expected pulse length
foundPulseLength = pulseArray[pos].samples - pulseArray[startDetectPos].samples;
if(foundPulseLength < synLenInSamples)
adjustEnd = 0.1;
// Check for end of the pulse
if ((pulseArray[pos].hertz == 0 ||
(pulseArray[pos].hertz != 0 && pulseArray[pos].magnitude < compareMag*adjustEnd)))
{
// abort in 16khz 8 bit attempts that have very low matching due to small errors
if(samplesNeeded <= 2 && foundPulseLength < synLenInSamples)
{
logmsgFileOnly("== Aborted centering, probably 16khz 8 bit matching\n");
return(offset);
}
//endDetectPos = pos + RoundToNsamples((double)samplesNeeded/4.0, AudioChannels, NULL, NULL); // Add the tail since we are doing overlapped starts
if(pulseArray[pos].hertz == 0)
pos --;
endDetectPos = pos;
if (config->debugSync)
logmsgFileOnly("== match end == using Sample: %ld (compared %g)\n",
SamplesForDisplay(pulseArray[endDetectPos].samples, AudioChannels),
compareMag*adjustEnd);
break;
}
}
}
}
if (startDetectPos != -1 && endDetectPos != -1)
{
double percent = 0;
long int foundPulseLength = 0;
foundPulseLength = pulseArray[endDetectPos].samples - pulseArray[startDetectPos].samples;
foundPos = pulseArray[startDetectPos].samples;
percent = (double)labs(foundPulseLength-synLenInSamples)*100.0/(double)synLenInSamples;
if (config->debugSync)
logmsgFileOnly("Detected at: %ld Percent outside %g%% detected length in samples %ld expected length in samples: %ld\n",
SamplesForDisplay(foundPos, AudioChannels),
percent,
SamplesForDisplay(foundPulseLength, AudioChannels),
SamplesForDisplay(synLenInSamples, AudioChannels));
if (percent > 10 && percent < 75)
{
long int newFoundPos = 0;
// in this special case we allow errors in order to better center the sync pulse
// we re-run the algorithm
tolerance = 10;
startDetectPos = endDetectPos = -1;
compareMag = compareMag * 0.5;
for (pos = 0; pos < count; pos++)
{
if (config->debugSync)
logmsgFileOnly("*Sample: %ld %gHz Mag %g Phase %g Tol: %g\n",
SamplesForDisplay(pulseArray[pos].samples, AudioChannels),
pulseArray[pos].hertz, pulseArray[pos].magnitude, pulseArray[pos].phase, tolerance);
if (targetFrequency == pulseArray[pos].hertz && pulseArray[pos].magnitude >= compareMag)
{
if (startDetectPos == -1)
startDetectPos = pos;
}
if (startDetectPos != -1 &&
(targetFrequency != pulseArray[pos].hertz ||
(targetFrequency == pulseArray[pos].hertz && pulseArray[pos].magnitude < compareMag*0.5)))
{
if (tolerance)
tolerance--;
else
{
endDetectPos = pos + RoundToNsamples((double)samplesNeeded / 4.0, AudioChannels, NULL, NULL); // Add the tail since we are doing overlapped starts
break;
}
}
}
if(startDetectPos != -1 && endDetectPos != -1)
{
foundPulseLength = pulseArray[endDetectPos].samples - pulseArray[startDetectPos].samples;
foundPulseLength = RoundToNsamples((double)foundPulseLength, AudioChannels, NULL, NULL);
newFoundPos = RoundToNsamples((double)pulseArray[startDetectPos].samples + ((double)foundPulseLength/2.0 - (double)synLenInSamples/2.0), AudioChannels, NULL, NULL);
if (newFoundPos != foundPos)
{
foundPos = newFoundPos;
config->syncAlignTolerance[config->syncAlignIterator] = 1;
config->syncAlignPct[config->syncAlignIterator] = percent;
if (config->debugSync)
logmsg("WARNING: Had to adjust sync pulse start due to %g%% pulse length difference from %ld samples to %ld samples\n", percent,
SamplesForDisplay(pulseArray[startDetectPos].samples, AudioChannels), SamplesForDisplay(foundPos, AudioChannels));
}
if (config->debugSync)
logmsgFileOnly("FOUND: Item-%ld Sample: %ld %gHz Mag %g Phase %g\n",
startDetectPos,
SamplesForDisplay(pulseArray[startDetectPos].samples, AudioChannels),
pulseArray[startDetectPos].hertz, pulseArray[startDetectPos].magnitude, pulseArray[startDetectPos].phase);
}
}
else
{
if(foundPulseLength > synLenInSamples)
{
if (config->debugSync)
logmsgFileOnly("Starting from sample: %ld, using %g Mag (Start Mag %g -> End Mag%g)\n",
SamplesForDisplay(foundPos, AudioChannels),
compareMag,
pulseArray[startDetectPos].magnitude,
pulseArray[endDetectPos].magnitude);
// Center the first pulse within the expected pulse sync length
foundPos += ((foundPulseLength-synLenInSamples)/2.0)*AudioChannels;
if (config->debugSync)
logmsgFileOnly("Corrected to sample: %ld\n",
SamplesForDisplay(foundPos, AudioChannels));
}
}
}
free(buffer);
free(pulseArray);
return foundPos;
}
// Searches using 1ms/factor blocks
long int DetectPulseInternal(double *Samples, wav_hdr header, int factor, long int offset, int *maxdetected, int role, int AudioChannels, parameters *config)
{
int bytesPerSample = 0, executeCleanSilence = 0;
long int i = 0, TotalMS = 0, totalSamples = 0;
double *sampleBuffer = NULL;
long int sampleBufferSize = 0, pos = 0, startPos = 0;
Pulses *pulseArray = NULL;
double targetFrequency = 0, targetFrequencyHarmonic[2] = { NO_FREQ, NO_FREQ }, origFrequency = 0, MaxMagnitude = 0;
bytesPerSample = header.fmt.bitsPerSample/8;
/* Not a real ms, just approximate */
sampleBufferSize = SecondsToSamples(header.fmt.SamplesPerSec, 1.0/((double)factor*1000.0), AudioChannels, NULL, NULL);
if(sampleBufferSize < 2*AudioChannels){
if(header.fmt.SamplesPerSec < 44100)
logmsg("ERROR: Invalid parameters for sync detection (sample rate too low)\n");
else
logmsg("ERROR: Invalid parameters for sync detection\n");
return -1;
}
sampleBuffer = (double*)malloc(sampleBufferSize*sizeof(double));
if(!sampleBuffer)
{
logmsgFileOnly("\tERROR: malloc failed for sample buffer during DetectPulseInternal\n");
return -1;
}
totalSamples = header.data.DataSize/bytesPerSample;
// calculate how many sampleBufferSize units fit in the available samples from the file
TotalMS = totalSamples/sampleBufferSize-1;
pos = offset;
if(offset)
{
double syncLen = 0;
i = offset;
startPos = i;
/* check for the time duration in ms*factor of the sync pulses */
syncLen = GetLastSyncDuration(GetMSPerFrameRole(role, config), config)*1000*factor;
if(!syncLen)
{
logmsg("\tERROR: No sync length\n");
return -1;
}
if(factor == FACTOR_EXPLORE) /* are we exploring? */
syncLen *= 2.0; /* widen so that the silence offset is compensated for */
else
syncLen *= 1.2; /* widen so that the silence offset is compensated for */
//if(i+syncLen > TotalMS)
TotalMS = i+syncLen;
if(config->debugSync)
logmsgFileOnly("Started detecting with offset %ld. Changed to:\n\tSamplesBufferSize: %ld, Samples:%ld-%ld/ms:%ld-%ld]\n\tms len: %g Bytes: %g Factor: %d\n",
offset/AudioChannels, sampleBufferSize, i*factor, TotalMS*factor, i, TotalMS,
syncLen, syncLen/factor, factor);
}
else
{
double expectedlen = 0, seconds = 0, syncLenSeconds = 0, syncLen = 0, silenceLen = 0, silenceLenSeconds = 0;
seconds = GetSignalTotalDuration(GetMSPerFrameRole(role, config), config);
expectedlen = SecondsToSamples(header.fmt.SamplesPerSec, seconds, header.fmt.NumOfChan, NULL, NULL);
expectedlen = floor(expectedlen/sampleBufferSize) - 1;
syncLenSeconds = GetFirstSyncDuration(GetMSPerFrameRole(role, config), config);
syncLen = SecondsToSamples(header.fmt.SamplesPerSec, syncLenSeconds, header.fmt.NumOfChan, NULL, NULL);
syncLen = floor(syncLen/sampleBufferSize) - 1;
silenceLenSeconds = GetFirstSilenceDuration(GetMSPerFrameRole(role, config), config);
silenceLen = SecondsToSamples(header.fmt.SamplesPerSec, silenceLenSeconds, header.fmt.NumOfChan, NULL, NULL);
silenceLen = floor(silenceLen/sampleBufferSize) - 1;
// check if it is long enough
if(expectedlen + syncLen + silenceLen / 2 < TotalMS)
TotalMS = TotalMS - expectedlen + syncLen + silenceLen/2;
if(expectedlen*1.5 < TotalMS) // long file
config->trimmingNeeded = 1;
}
pulseArray = (Pulses*)malloc(sizeof(Pulses)*TotalMS);
if(!pulseArray)
{
logmsgFileOnly("\tPulse malloc failed!\n");
return -1;
}
memset(pulseArray, 0, sizeof(Pulses)*TotalMS);
origFrequency = GetPulseSyncFreq(role, config);
targetFrequency = FindFrequencyBracketForSync(origFrequency,
sampleBufferSize, AudioChannels, header.fmt.SamplesPerSec, config);
if(origFrequency < HARMONIC_TSHLD) //default behavior for around 8khz, harmonic is NO_FREQ
{
targetFrequencyHarmonic[0] = FindFrequencyBracketForSync(targetFrequency*2,
sampleBufferSize, AudioChannels, header.fmt.SamplesPerSec, config);
targetFrequencyHarmonic[1] = FindFrequencyBracketForSync(targetFrequency*3,
sampleBufferSize, AudioChannels, header.fmt.SamplesPerSec, config);
if(config->debugSync)
logmsgFileOnly("\n - Using %gHz and harmonics %g/%gHz for sync detection\n",
targetFrequency, targetFrequencyHarmonic[0], targetFrequencyHarmonic[1]);
executeCleanSilence = 1;