-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwav2ay.c
1102 lines (967 loc) · 32.7 KB
/
wav2ay.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
/***
latest official release at: https://github.com/EdouardBERGE/wav2ay
-----------------------------------------------------------------------------------------------------
This software is using MIT "expat" license
« Copyright © BERGE Edouard (roudoudou)
Permission is hereby granted, free of charge,to any person obtaining a copy of this software
and associated documentation/source files of RASM, to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
The Software is provided "as is", without warranty of any kind, express or implied,
including but not limited to the warranties of merchantability, fitness for a particular
purpose and noninfringement. In no event shall the authors or copyright holders be liable for
any claim, damages or other liability, whether in an action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use or other dealings in the
Software. »
-----------------------------------------------------------------------------------------------------
***/
#ifdef _WIN32
#define OS_WIN 1
#endif
#ifdef _WIN64
#define OS_WIN 1
#endif
#ifdef OS_WIN
#define _USE_MATH_DEFINES
#include<io.h>
#include<fcntl.h>
#endif
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<errno.h>
#include<math.h>
#define MAXCHANNEL 3
enum
{
O32_LITTLE_ENDIAN = 0x03020100ul,
O32_BIG_ENDIAN = 0x00010203ul,
O32_PDP_ENDIAN = 0x01000302ul, /* DEC PDP-11 (aka ENDIAN_LITTLE_WORD) */
O32_HONEYWELL_ENDIAN = 0x02030001ul /* Honeywell 316 (aka ENDIAN_BIG_WORD) */
};
/*************************************************************************
CSV Export Func
*************************************************************************/
void export_csv(char *filename,double *data,int n)
{
char temp[1024];
FILE *f;
int i;
f=fopen(filename,"wb");
for (i=0;i<n;i++) {
sprintf(temp,"%.1lf\n",data[i]);
fputs(temp,f);
}
fclose(f);
}
/*************************************************************************
WAV Loading Func
*************************************************************************/
struct s_wav_header {
char ChunkID[4];
unsigned char ChunkSize[4];
char Format[4];
char SubChunk1ID[4];
unsigned char SubChunk1Size[4];
unsigned char AudioFormat[2];
unsigned char NumChannels[2];
unsigned char SampleRate[4];
unsigned char ByteRate[4];
unsigned char BlockAlign[2];
unsigned char BitsPerSample[2];
unsigned char SubChunk2ID[4];
unsigned char SubChunk2Size[4];
};
/*
* strange WAV header
*
00000000 52 49 46 46 b2 83 c8 00 57 41 56 45 66 6d 74 20 |RIFF....WAVEfmt |
00000010 12 00 00 00 01 00 01 00 22 56 00 00 44 ac 00 00 |........"V..D...|
00000020 02 00 10 00 00 00 66 61 63 74 04 00 00 00 c0 41 |......fact.....A|
00000030 64 00 64 61 74 61 80 83 c8 00 00 00 00 00 00 00 |d.data..........|
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*/
void push_wav(char *outputfilename, short int *data, int n) {
struct s_wav_header wav_header={0};
unsigned int datasize,riffsize;
unsigned char chunksize[4];
char RIFF[5];
FILE *f;
#ifdef OS_WIN
int sr;
#endif
datasize=n*2;
memcpy(wav_header.ChunkID,"RIFF",4);
riffsize=datasize+16+8+datasize+8;
wav_header.ChunkSize[0]=riffsize&0xFF;
wav_header.ChunkSize[1]=(riffsize>>8)&0xFF;
wav_header.ChunkSize[2]=(riffsize>>16)&0xFF;
wav_header.ChunkSize[3]=(riffsize>>24)&0xFF;
memcpy(wav_header.Format,"WAVE",4);
memcpy(wav_header.SubChunk1ID,"fmt ",4);
wav_header.SubChunk1Size[0]=16;
wav_header.AudioFormat[0]=1; // integer
wav_header.NumChannels[0]=1;
wav_header.SampleRate[0]=0x44;
wav_header.SampleRate[1]=0xAC; // 44.1KHz
wav_header.ByteRate[0]=0x88;
wav_header.ByteRate[1]=0x58;
wav_header.ByteRate[2]=0x01; // 88.2Kbio
wav_header.BlockAlign[0]=2;
wav_header.BitsPerSample[0]=16;
memcpy(wav_header.SubChunk2ID,"data",4);
wav_header.SubChunk2Size[0]=datasize&0xFF;
wav_header.SubChunk2Size[1]=(datasize>>8)&0xFF;
wav_header.SubChunk2Size[2]=(datasize>>16)&0xFF;
wav_header.SubChunk2Size[3]=(datasize>>24)&0xFF;
// dataaaaa output
f=fopen(outputfilename,"wb");
#ifdef OS_WIN
sr=_setmode(_fileno(f), _O_BINARY );
if (sr==-1) {
fprintf(stderr,"wavoutput windows binary mode got problem!\n");
exit(1);
}
#endif
if (!f) {
fprintf(stderr,"wavoutput got problem!\n");
exit(1);
}
fwrite(&wav_header,1,sizeof(struct s_wav_header),f);
fwrite(data,1,datasize,f);
fclose(f);
}
double (*_internal_getsample)(unsigned char *data, int *idx);
double __internal_getsample8(unsigned char *data, int *idx) {
double v;
v=data[*idx]-128;*idx=*idx+1;return v;
}
double __internal_getsample16(unsigned char *data, int *idx) {
char *most;
double v;
most=(char*)data;
v=most[*idx+1];v+=data[*idx]/256.0;*idx=*idx+2;return v;
}
double __internal_getsample32(unsigned char *data, int *idx) {
float v,*peteher;
peteher=(float*)(&data[*idx]);
v=*peteher;
*idx=*idx+4;
return v*128.0;
}
double *load_wav(char *filename, int *n, double *acqui) {
struct s_wav_header *wav_header;
int controlsize,nbchannel,wFormat,frequency,bitspersample,nbsample;
unsigned char *subchunk;
unsigned char *data;
int subchunksize;
int filesize,idx;
int i,num;
FILE *f;
double *wav;
f=fopen(filename,"rb");
if (!f) {
fprintf(stderr,"file [%s] not found\n",filename);
return NULL;
}
fseek(f,0,SEEK_END);
filesize=ftell(f);
fseek(f,0,SEEK_SET);
if (filesize<sizeof(struct s_wav_header)) {
fprintf(stderr,"WAV import - this file is too small to be a valid WAV!\n");
return NULL;
}
data=(unsigned char *)malloc(filesize);
fread(data,1,filesize,f);
fclose(f);
wav_header=(struct s_wav_header *)data;
if (strncmp(wav_header->Format,"WAVE",4)) {
fprintf(stderr,"WAV import - unsupported audio sample type (format must be 'WAVE')\n");
free(data);
return NULL;
}
controlsize=wav_header->SubChunk1Size[0]+wav_header->SubChunk1Size[1]*256+wav_header->SubChunk1Size[2]*65536+wav_header->SubChunk1Size[3]*256*65536;
if (controlsize!=16) {
fprintf(stderr,"WAV import - invalid wav chunk size (subchunk1 control) => %d!=16\n",controlsize);
}
if (strncmp(wav_header->SubChunk1ID,"fmt",3)) {
fprintf(stderr,"WAV import - unsupported audio sample type (subchunk1id must be 'fmt')\n");
free(data);
return NULL;
}
if (controlsize==16) {
subchunk=(unsigned char *)&wav_header->SubChunk2ID;
} else {
subchunk=(unsigned char *)&wav_header->SubChunk2ID-16+controlsize;
}
// tant qu on n est pas sur le chunk data, on avance dans le "TIFF"
while (strncmp((char *)subchunk,"data",4)) {
subchunksize=8+subchunk[4]+subchunk[5]*256+subchunk[6]*65536+subchunk[7]*256*65536;
if (subchunksize>=filesize) {
fprintf(stderr,"WAV import - data subchunk not found\n");
free(data);
return NULL;
}
subchunk+=subchunksize;
}
subchunksize=subchunk[4]+subchunk[5]*256+subchunk[6]*65536+subchunk[7]*256*65536;
controlsize=subchunksize; // taille des samples
nbchannel=wav_header->NumChannels[0]+wav_header->NumChannels[1]*256;
if (nbchannel<1) {
fprintf(stderr,"WAV import - invalid number of audio channel\n");
free(data);
return NULL;
}
wFormat=wav_header->AudioFormat[0]+wav_header->AudioFormat[1]*256;
if (wFormat!=1 && wFormat!=3) {
fprintf(stderr,"WAV import - invalid or unsupported wFormatTag (%04X) ",wFormat);
switch (wFormat) {
case -2:fprintf(stderr,"Extensible Format (user defined) \n");
default:
case 0:fprintf(stderr," Unknown Format\n");
case 1:fprintf(stderr," PCM format (8 or 16 bit), Microsoft Corporation\n");
case 2:fprintf(stderr," AD PCM Format, Microsoft Corporation\n");
case 3:fprintf(stderr," IEEE PCM Float format (32 bit)\n");
case 48:fprintf(stderr,"AC2, Dolby Laboratories\n");
case 49:fprintf(stderr,"GSM 6.10, Microsoft Corporation\n");
case 50:fprintf(stderr,"MSN Audio, Microsoft Corporation\n");
case 80:fprintf(stderr,"MPEG format\n");
case 85:fprintf(stderr,"ISO/MPEG Layer3 Format\n");
case 146:fprintf(stderr,"AC3 Digital, Sonic Foundry\n");
case 255:fprintf(stderr,"Raw AAC\n");
case 352:fprintf(stderr,"Microsoft Corporation\n");
case 353:fprintf(stderr,"Windows Media Audio. This format is valid for versions 2 through 9\n");
case 354:fprintf(stderr,"Windows Media Audio 9 Professional\n");
case 355:fprintf(stderr,"Windows Media Audio 9 Lossless\n");
case 356:fprintf(stderr,"Windows Media SPDIF Digital Audio\n");
case 5632:fprintf(stderr,"ADTS AAC Audio\n");
case 5633:fprintf(stderr,"Raw AAC\n");
case 5634:fprintf(stderr,"MPEG-4 audio transport stream with a synchronization layer (LOAS) and a multiplex layer (LATM)\n");
case 5648:fprintf(stderr,"High-Efficiency Advanced Audio Coding (HE-AAC) stream\n");
}
free(data);
return NULL;
}
frequency=wav_header->SampleRate[0]+wav_header->SampleRate[1]*256+wav_header->SampleRate[2]*65536+wav_header->SampleRate[3]*256*65536;
bitspersample=wav_header->BitsPerSample[0]+wav_header->BitsPerSample[1]*256;
switch (bitspersample) {
case 8:_internal_getsample=__internal_getsample8;break;
case 16:_internal_getsample=__internal_getsample16;break;
case 32:_internal_getsample=__internal_getsample32;break;
default:fprintf(stderr,"unsupported bits per sample size %d (valid are PCM-8, PCM-16 and FLOAT-32)\n",bitspersample); free(data);return NULL;
}
nbsample=controlsize/nbchannel/(bitspersample/8);
if (controlsize+sizeof(struct s_wav_header)>filesize) {
fprintf(stderr,"WAV import - cannot read %d byte%s of audio whereas the file is %d bytes big!\n",controlsize,controlsize>1?"s":"",filesize);
free(data);
return NULL;
}
*n=nbsample;
fprintf(stderr,"nbchannel=%d | bitpersample=%d | Format=%s | frequency=%d\n",nbchannel,bitspersample,wFormat==1?"PCM":"IEEE Float",frequency);
*acqui=frequency;
wav=(double *)malloc(nbsample*sizeof(double));
idx=subchunk+8-data;
for (i=0;i<nbsample;i++) {
/* downmixing */
double accumulator=0.0;
for (num=0;num<nbchannel;num++) {
accumulator+=_internal_getsample(data,&idx);
}
wav[i]=accumulator/nbchannel;
}
return wav;
}
/*****************************************************
*
* Fourier stuff (basic transform)
*
*****************************************************/
/* slow but understandable Fourier transform */
void calcule_fourier(double *data, int n, double *dataout, int clean) {
double rel,img,inverse;
int i,j;
inverse=1.0/n;
// skip everything too low according to lowcut filter
for (i=0;i<clean;i++) dataout[i]=0.0;
for (;i<=n/2;i++) {
rel=img=0.0;
// cumul
for (j=0;j<n;j++) {
rel+=data[j]*cos((2*M_PI*j*i)/n);
img+=data[j]*sin((2*M_PI*j*i)/n);
}
// norme
rel*=inverse;
img*=inverse;
dataout[i]=2.0*sqrt(rel*rel+img*img);
}
}
/* partial Fourier trick to compute only peak neighborhood */
double calcule_fourier_precision(double *data, int n, int peak, double cutlow, double peakHz) {
double v[16384],rel,img,vmax,inverse;
int i,j,imax;
int lp,mp,ws,bornemin;
bornemin=(double)peak*cutlow/peakHz*16384/n;
if (bornemin<1) bornemin=1;
// [lp:mp] let us zoom on previous (small) fourier buffer
ws=16384/n+1;
lp=peak*16384/n-ws/2;
if (lp<bornemin) lp=bornemin; // may happend with strong bass
mp=peak*16384/n+ws/2;
if (mp>=8192) mp=8191;
inverse=1.0/n;
// do Fourier on that segment
for (i=lp;i<=mp;i++) {
rel=img=0.0;
for (j=0;j<n;j++) {
rel+=data[j]*cos((2*M_PI*j*i)/16384.0);
img+=data[j]*sin((2*M_PI*j*i)/16384.0);
}
// energy
rel*=inverse;
img*=inverse;
v[i]=2.0*sqrt(rel*rel+img*img);
}
// max is max
vmax=v[lp];imax=lp;
for (i=lp+1;i<=mp;i++) {
if (v[i]>vmax) {
vmax=v[i];
imax=i;
}
}
// precise peak
return imax*n/16384.0;
}
void passe_bande(double acqui,double basse,double haute,double *datain,double *dataout,int nb)
{
double *datatmp=NULL;
double A1,A2,B0,B1,B2;
double a,b;
int i;
/***/
double e,old_e,old_old_e;
double s,old_s,old_old_s;
if (nb<=0) return;
if (basse<0.0001) basse=0.0001;
if (haute<0.0001) haute=0.0001;
if (basse>acqui) basse=acqui;
if (haute>acqui) haute=acqui;
datatmp=(double *)malloc(sizeof(double)*nb);
if (!datatmp) return;
a=M_PI*basse/acqui;
b=M_PI*haute/acqui;
a=sin(a)/cos(a);
b=sin(b)/cos(b);
B0= -b / ((1 + a) * (1 + b));
B1= 0;
B2= b / ((1 + a) * (1 + b));
A1= ((1 + a) * (1 - b) + (1 - a) * (1 + b)) / ((1 + a) * (1 + b));
A2= -(1 - a) * (1 - b) / ((1 + a) * (1 + b));
/* init */
old_e=old_old_e=datain[0];
old_s=old_old_s=0;
/* a filter phases out signal */
for (i=0;i<nb;i++) {
e=datain[i];
s=e*B0+old_e*B1+old_old_e*B2+old_s*A1+old_old_s*A2;
datatmp[i]=s;
old_old_s=old_s;
old_s=s;
old_old_e=old_e;
old_e=e;
}
old_e=old_old_e=e;
old_s=old_old_s=0;
/* so we filter again reverse */
while (i>0) {
i--;
e=datatmp[i];
s=e*B0+old_e*B1+old_old_e*B2+old_s*A1+old_old_s*A2;
dataout[i]=s;
old_old_s=old_s;
old_s=s;
old_old_e=old_e;
old_e=e;
}
free(datatmp);
}
// AY period related to a frequency
struct s_ay_period {
double *fourier;
};
double * compute_AY(int ws, int replay, int clean, double cuthigh, double cutlow,int periode, double workingfreq) {
double *retfour;
double *minisignal;
double freq,target;
int i,j,k;
double vtic,tic,mtic;
freq=replay*ws; // echantillons/seconde
minisignal=(double *)malloc(ws*sizeof(double));
i=periode;
// generate AY signal for this period, windows size and replay freq
if (!i) target=workingfreq; else target=workingfreq/i;
// keep shanon compliant
if (target*2>freq) {
retfour=(double *)malloc(ws*sizeof(double));
memset(retfour,0,ws*sizeof(double));
free(minisignal);
return retfour;
}
tic=mtic=0.5*freq/target;
vtic=80.0;
for (j=0;j<ws;j++) {
minisignal[j]=vtic;
tic-=1.0;
if (tic<=0.0) {
vtic=-vtic;
tic+=mtic;
}
}
retfour=(double *)malloc(ws*sizeof(double));
calcule_fourier(minisignal,ws,retfour,clean);
free(minisignal);
return retfour;
}
int getpeak(double *fourier, int ws) {
double vmax;
int imax;
int j;
imax=0;
vmax=fourier[0];
for (j=1;j<=ws/2;j++) {
if (fourier[j]>vmax) {
vmax=fourier[j];
imax=j;
}
}
return imax;
}
int getvolume(double level) {
int volume;
if (level>=64) volume=15; else
if (level>=45) volume=14; else
if (level>=32) volume=13; else
if (level>=22) volume=12; else
if (level>=16) volume=11; else
if (level>=11) volume=10; else
if (level>=8) volume=9; else
if (level>=6) volume=8; else
if (level>=4) volume=7; else
if (level>=3.3) volume=6; else
if (level>=2) volume=5; else
if (level>=1.7) volume=4; else
if (level>=1) volume=3; else
if (level>=0.7) volume=2; else
volume=1;
return volume;
}
void do_sample(double *data,int n, double pw, double cutlow, double cuthigh, double acqui, double replay, double preamp, int info, double workingfreq, int nbchannel, double treshold, int dmalist, char *wavout_filename, int *channel_list, int cpclist, char *akifilename, int noiseblocker) {
double *fourier,*oldfourier;
double *newdata,subcoef;
double vmax,resolution,picfreq;
int nbwin,i,j,k,imax,ws,clean;
struct s_ay_period *ay=NULL;
short int *wavout;
int wavout_n=0;
// Amstrad registers
int psgperiod,psgvolume;
int nbpausedma;
int channel;
int AYprevperiod[MAXCHANNEL];
int AYprevvolume[MAXCHANNEL];
int AYperiod[MAXCHANNEL];
int AYvolume[MAXCHANNEL];
int nbchanges;
int makenoise=0;
int hadnoise=0;
FILE *akifile;
#ifdef OS_WIN
int sr;
#endif
if (pw<0.0 || pw>0.75) {
fprintf(stderr,"previous weight not in [0.0:0.75] interval. Default value is 0.25\n");
pw=0.25;
}
if (acqui<4000 || acqui>48000) {
fprintf(stderr,"wrong acquisition frequency. Default value is 15.6KHz\n");
acqui=15600.0;
}
if (cuthigh<cutlow || cutlow<0.0 || cuthigh>acqui || cutlow<replay) {
fprintf(stderr,"wrong band-pass filter frequencies. Default value are replay-2500Hz\n");
cuthigh=2500.0;
cutlow=replay;
}
if (cutlow<workingfreq/4095.0*1.3) cutlow=workingfreq/4095.0*1.3;
ws=acqui/replay; // 312 samples pour 15000Hz avec replay a 50Hz
if (ws<200) {
fprintf(stderr,"window size is small, results may be very innacurate\n");
}
if (ws<100) {
fprintf(stderr,"wont compute with such a small window, raise input frequency\n");
return;
}
// compléter le nombre de sample pour arriver pile sur un multiple de ws (window size) | default:312
nbwin=n/ws;
while (nbwin*ws<n) {
nbwin++;
}
if (nbwin*ws!=n) {
data=(double *)realloc(data,sizeof(double)*nbwin*ws);
for (i=n;i<nbwin*ws;i++) {
data[i]=data[i-1]*0.95; // histoire de terminer salement mais en evitant le poc => normalement le sample est ok de base!
}
n=nbwin*ws;
}
newdata=(double *)malloc(sizeof(double)*n);
// calculer la valeur du nettoyage a faire sur la transformee pour eviter les basses frequences mal filtrees
resolution=acqui/(double)ws;
clean=1+floor(replay/resolution+0.5);
// precalc des fouriers de l'AY sur la fenetre utilisee
ay=(struct s_ay_period *)malloc(4096*sizeof(struct s_ay_period));
memset(ay,0,4096*sizeof(struct s_ay_period));
// par defaut il faut filtrer en dessous de la frequence de replay et au dessus de 2500Hz a cause de la precision de restitution
fprintf(stderr,"band-pass %.1lf|%.1lf window size=%d preamp=%.1lf AY frequency=%.1lfMHz\n",cutlow,cuthigh,ws,preamp,workingfreq*16.0);
passe_bande(acqui,cutlow,cuthigh,data,newdata,n);
memcpy(data,newdata,n*sizeof(double));
// init temporal buffer
fourier=(double *)malloc(sizeof(double)*ws);
oldfourier=(double *)malloc(sizeof(double)*ws);
calcule_fourier(data,ws,oldfourier,clean);
fprintf(stderr,"%d windows for a %.1lfs sample\n",nbwin,n/acqui);
for (channel=0;channel<nbchannel;channel++) {
AYprevperiod[channel]=0xFFFF;
AYprevvolume[channel]=0;
}
if (!dmalist && !cpclist) {
printf("idx");
for (channel=0;channel<nbchannel;channel++) {
printf(";reg;value");
printf(";reg;value");
printf(";reg;value");
}
printf("\n");
}
if (wavout_filename) {
wavout=(short int *)malloc(nbwin*(sizeof(short int)*workingfreq*2.0/replay+1));
wavout_n=0;
}
if (preamp!=1.0) for (j=0;j<n;j++) data[j]*=preamp;
if (akifilename) {
akifile=fopen(akifilename,"wb");
if (!akifile) {
fprintf(stderr,"FATAL: cannot open %s for writing\n",akifilename);
exit(-66);
}
#ifdef OS_WIN
sr=_setmode(_fileno(akifile), _O_BINARY );
if (sr==-1) {
fprintf(stderr,"aki output binary mode got problem!\n");
exit(1);
}
#endif
fprintf(akifile,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\
"<aks:instrument xmlns:aks=\"http://www.julien-nevo.com/ArkosTrackerInstrument\" "\
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.julien-nevo.com/ArkosTrackerInstrument.xsd\">\n"\
" <aks:fmInstrument version=\"1.0\">\n"\
" <aks:number>1</aks:number>\n"\
" <aks:title>%s</aks:title>\n"\
" <aks:speed>%d</aks:speed>\n"\
" <aks:isLooping>false</aks:isLooping>\n"\
" <aks:loopStartIndex>%d</aks:loopStartIndex>\n"\
" <aks:endIndex>%d</aks:endIndex>\n"\
" <aks:isRetrig>false</aks:isRetrig>\n"\
" <aks:colorArgb>4281370811</aks:colorArgb>\n",akifilename,replay==50?0:replay==25?1:2,nbwin-1,nbwin-1);
}
for (i=0;i<nbwin;i++) {
// calcul de fourier sur le segment
calcule_fourier(&data[ws*i],ws,fourier,clean);
for (channel=0;channel<nbchannel;channel++) {
/*
// mix avec le segment precedent si besoin
if (pw>0.0) {
for (j=0;j<ws/2;j++) {
fourier[j]=oldfourier[j]*pw+fourier[j]*(1.0-pw);
}
}
*/
// trouver le pic
imax=getpeak(fourier,ws);
// energie suffisante pour s'en soucier?
if (fourier[imax]>treshold) {
int volume;
int plow,phigh;
if (info) fprintf(stderr,"segment %3d pic en %.0lfHz (resolution=%.1lf) norme=%.1lf\n",i,imax*resolution,resolution,fourier[imax]);
AYvolume[channel]=getvolume(fourier[imax]);
k=imax;
//export_csv("fourier_ref.csv",fourier,ws/2+1);
// chercher la precision sur le pic
picfreq=calcule_fourier_precision(&data[ws*i],ws,imax,cutlow,imax*resolution)*resolution;
if (info) fprintf(stderr," => recherche de precision donne %.1lfHz\n",picfreq);
if (picfreq>2500 && !noiseblocker) makenoise++;
// appliquer la soustraction AY sur la transformee de Fourier pour la suite
imax=workingfreq/picfreq; // periode AY
if (imax>4095) imax=4095;
if (!ay[imax].fourier) ay[imax].fourier=compute_AY(ws,replay,clean,cuthigh,cutlow,imax,workingfreq);
subcoef=fourier[k]/ay[imax].fourier[k];
for (j=0;j<=ws/2;j++) fourier[j]-=ay[imax].fourier[j]*subcoef;
/* simple psycho acoustic model ???
double *tmpbuffer=NULL;
passe_bande(acqui,peak*0.8,peak*1.5,datain,tmpbuffer,nb);
// substract
*/
// produire les registres AY (imax=periode)
AYperiod[channel]=imax;
} else {
AYvolume[channel]=0;
/* volume a zero on ne touchera pas la periode! */
AYperiod[channel]=AYprevperiod[channel];
}
}
/*********************************
Channel optimisation
*********************************/
// d'abord un tri pour conserver une harmonie éventuelle sur le volume!
if (nbchannel>1) {
if (AYperiod[0]<AYperiod[1]) {
imax=AYperiod[1];
AYperiod[1]=AYperiod[0];
AYperiod[0]=imax;
imax=AYvolume[1];
AYvolume[1]=AYvolume[0];
AYvolume[0]=imax;
}
if (nbchannel>2) {
if (AYperiod[0]<AYperiod[2]) {
imax=AYperiod[2];
AYperiod[2]=AYperiod[0];
AYperiod[0]=imax;
imax=AYvolume[2];
AYvolume[2]=AYvolume[0];
AYvolume[0]=imax;
}
if (AYperiod[1]<AYperiod[2]) {
imax=AYperiod[2];
AYperiod[2]=AYperiod[1];
AYperiod[1]=imax;
imax=AYvolume[2];
AYvolume[2]=AYvolume[1];
AYvolume[1]=imax;
}
}
}
/* switch channel if period are exactly the same */
if (nbchannel>1) {
if (AYperiod[0]==AYprevperiod[1]) {
imax=AYperiod[1];
AYperiod[1]=AYperiod[0];
AYperiod[0]=imax;
imax=AYvolume[1];
AYvolume[1]=AYvolume[0];
AYvolume[0]=imax;
}
if (nbchannel>2) {
if (AYperiod[0]==AYprevperiod[2]) {
imax=AYperiod[2];
AYperiod[2]=AYperiod[0];
AYperiod[0]=imax;
imax=AYvolume[2];
AYvolume[2]=AYvolume[0];
AYvolume[0]=imax;
} else if (AYperiod[1]==AYprevperiod[2]) {
imax=AYperiod[2];
AYperiod[2]=AYperiod[1];
AYperiod[1]=imax;
imax=AYvolume[2];
AYvolume[2]=AYvolume[1];
AYvolume[1]=imax;
}
}
}
if (wavout_filename) {
int ref[16]={0,231,695,1158,2084,2779,4168,6716,8105,13200,18294,24315,32189,40757,52799,65535};
int pulse,maxp;
int acc;
int tic[MAXCHANNEL]={0};
int vchan[MAXCHANNEL];
maxp=workingfreq*2.0/replay;
for (channel=0;channel<nbchannel;channel++) {
vchan[channel]=ref[AYvolume[channel]]/2;
tic[channel]=AYperiod[channel];
}
for (pulse=0;pulse<maxp;pulse++) {
acc=0;
for (channel=0;channel<nbchannel;channel++) {
acc+=vchan[channel];
tic[channel]--;
if (!tic[channel]) {
tic[channel]=AYperiod[channel];
vchan[channel]=-vchan[channel];
}
}
wavout[wavout_n++]=acc/nbchannel;
}
}
/*********************************
Amstrad CPC list output
*********************************/
if (cpclist) {
int packed_exec=0;
int minilist[10];
int ilist=0;
int zebit=128;
if (!i) {
printf("defb %d ; nombre d'iterations\n",nbwin);
}
for (channel=0;channel<3;channel++) {
if (AYvolume[channel]!=AYprevvolume[channel]) {
packed_exec|=zebit;
minilist[ilist++]=AYvolume[channel];
}
zebit>>=1;
if ((AYperiod[channel]>>8)!=(AYprevperiod[channel]>>8)) {
packed_exec|=zebit;
minilist[ilist++]=AYperiod[channel]>>8;
}
zebit>>=1;
minilist[ilist++]=AYperiod[channel]&0xFF; // au minimum on enverra la frequence "basse"
}
if (hadnoise && !makenoise) {
packed_exec|=2;
minilist[ilist++]=56;
}
if (!hadnoise && makenoise) {
packed_exec|=2;
minilist[ilist++]=0; // bruit partout sinon on n'entend rien...
}
printf("defb #%02X",packed_exec);
if (packed_exec) {
for (channel=0;channel<ilist;channel++) printf(",#%02X",minilist[channel]);
printf(" ; %5d-%02d %5d-%02d %5d-%02d %s\n",AYperiod[0],AYvolume[0],AYperiod[1],AYvolume[1],AYperiod[2],AYvolume[2],packed_exec&2?"noise mod":"");
} else {
printf("\n");
}
hadnoise=makenoise;
makenoise=0;
for (channel=0;channel<nbchannel;channel++) {
AYprevperiod[channel]=AYperiod[channel];
AYprevvolume[channel]=AYvolume[channel];
}
} else
/*********************************
Amstrad Plus DMA output
*********************************/
if (dmalist) {
psgperiod=0;
psgvolume=8;
nbchanges=1;
for (channel=0;channel<nbchannel;channel++) {
if (AYvolume[channel]) {
if (AYperiod[channel]!=AYprevperiod[channel]) {
if ((AYperiod[channel]>>8)!=(AYprevperiod[channel]>>8)) {
printf("defb %d,%d : ",AYperiod[channel]>>8,psgperiod+1);
nbchanges++;
}
if ((AYperiod[channel]&0xFF)!=(AYprevperiod[channel]&0xFF)) {
printf("defb %d,%d : ",AYperiod[channel]&0xFF,psgperiod);
nbchanges++;
}
}
}
if (AYvolume[channel]!=AYprevvolume[channel]) {
printf("defb %d,%d : ",AYvolume[channel],psgvolume);
nbchanges++;
}
psgperiod+=2;
psgvolume+=1;
}
nbpausedma=312.0/replay*50.0-nbchanges;
// no control on this value
printf("defb %d,#30+%d\n",nbpausedma&0xFF,(nbpausedma>>8)&0xF); // pause 50Hz
for (channel=0;channel<nbchannel;channel++) {
AYprevperiod[channel]=AYperiod[channel];
AYprevvolume[channel]=AYvolume[channel];
}
} else {
/*********************************
CSV Export
*********************************/
if (!i) {
}
printf(";%d",i);
for (channel=0;channel<nbchannel;channel++) {
printf(";%d;%d",channel_list[channel] ,AYvolume[channel]);
printf(";%d;%d",0+channel*2,AYperiod[channel]&0xFF);
printf(";%d;%d",1+channel*2,AYperiod[channel]>>8);
}
if (hadnoise && !makenoise) {
printf(";7;0");
}
if (!hadnoise && makenoise) {
printf(";7;40");
}
if (makenoise) printf(" // noise");
hadnoise=makenoise;
makenoise=0;
printf("\n");
}
if (akifilename) {
if (AYperiod[0]>4095) AYperiod[0]=AYprevperiod[0]; // try to get previous value
if (AYperiod[0]>4095) AYperiod[0]=4095; // still out of bounds?
fprintf(akifile," <aks:fmInstrumentCell>\n"\
" <aks:link>softOnly</aks:link>\n"\
" <aks:volume>%d</aks:volume>\n"\
" <aks:noise>%d</aks:noise>\n"\
" <aks:softwarePeriod>%d</aks:softwarePeriod>\n"\
" <aks:softwareArpeggio>0</aks:softwareArpeggio>\n"\
" <aks:softwarePitch>0</aks:softwarePitch>\n"\
" <aks:ratio>1</aks:ratio>\n"\
" <aks:hardwareCurve>8</aks:hardwareCurve>\n"\
" <aks:hardwarePeriod>0</aks:hardwarePeriod>\n"\
" <aks:hardwareArpeggio>0</aks:hardwareArpeggio>\n"\
" <aks:hardwarePitch>0</aks:hardwarePitch>\n"\
" <aks:isRetrig>false</aks:isRetrig>\n"\
" </aks:fmInstrumentCell>\n",AYvolume[0],makenoise,AYperiod[0]);
}
}
if (akifilename) {
fprintf(akifile," </aks:fmInstrument>\n</aks:instrument>\n");
fclose(akifile);
if (nbwin>63) {
fprintf(stderr,"****************************************\n");
fprintf(stderr," Warning AKI instrument has more than\n");
fprintf(stderr," 64 pattern, Arkos Tracker 2 will crash\n");
fprintf(stderr,"\n");
fprintf(stderr," the file is saved anyway because it's\n");
fprintf(stderr," better to let YOU cut yourself data ;)\n");
fprintf(stderr,"****************************************\n");
}
}
if (wavout_filename) {
double acc,pos,step,aron,frac,integer_part;
int iidx,oidx=0;
step=2.0*workingfreq/44100.0;
for (pos=0;pos<wavout_n-step;pos+=step) {
frac=modf(pos,&integer_part);
iidx=integer_part;
acc=(1.0-frac)*wavout[iidx];
while (integer_part+1.0<pos+step) {
acc+=wavout[++iidx];
integer_part+=1.0;
}
frac=modf(pos,&integer_part);
acc+=frac*wavout[++iidx];
acc/=step;
wavout[oidx++]=acc;
}
push_wav(wavout_filename,wavout,oidx);
free(wavout);
}
}
void usage() {
printf("=========== conversion from WAV to AY registers ===========\n");
printf("usage: wav2ay.exe <wavfile> <options>\n");
printf("options:\n");
printf("-preamp <value> amplification | default 1.0\n");
printf("-tresh <value> minimal energy| default 0.25 is minimum (max usable approx 15)\n");
printf("-replay <value> frequency play| default 10\n");
printf("-high <value> highcut filter| default 4000\n");
printf("-low <value> lowcut filter | default is replay (min: 20Hz)\n");
// printf("-pw <value> sound inertia | default 0.0 max 0.75\n");