-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.cpp
642 lines (538 loc) · 11.7 KB
/
process.cpp
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
#include "stdafx.h"
#include "process.h"
#include "delay.h"
#include "dsp_util.h"
#include "pitshift.h"
#include "vcremove.h"
#include "vibrato.h"
#include "pdetect.h"
#include "reverb.h"
#include <math.h>
#include <mmsystem.h>
#include <stdio.h>
#define PORT_ON 1
#define PORT_OFF 0
//Audio Format//
#define DEF_SAMRATE 44100
#define DEF_BITS 16
#define DEF_CHS 2
#define DEF_TH 1
#define DEF_VOCAL_LO 39
#define DEF_VOCAL_HI 80
#define DEF_VOCAL_NO 100
#define DEF_PITCH_FRQ 10
#define DEF_LOW_VOL -40
#define DEF_ECHO_DRY 1.0
#define DEF_ECHO_WET 0.3
#define DEF_REV_TIME 2.0
#define DEF_REV_DRY 0.7
#define DEF_REV_WET 0.3
CWaveIn *pWaveIn;
CWaveOut *pWaveOut;
CWaveFile *pWaveFile;
CWaveFile *pSaveFile;
PITCHSHIFT Shift;
VIBRATO Vib;
ACPITCH Pitch;
HPSPITCH Hps;
PRCREVERB PRC;
JCREVERB JC;
DELAYDATA Echo;
WAVEFORMATEX WaveFmt;
float depth = 5,speed = 5;
float LoVol = DEF_LOW_VOL;
float echodry = DEF_ECHO_DRY;
float echowet = DEF_ECHO_WET;
int HOLDS = 5;
int HoldNote = 100;
int HoldSeg = HOLDS;
int DEV_BUFFERSIZE = 256;
int DEV_BUFFERCOUNT = 100;
int DEV_SAMS = 441;
float shiftamt = 0.0;
float vcamt = 0.0;
HWND UIWnd;
bool MsgProc = false;
bool ReverbReady = true;
bool EchoReady = false;
float RoomSize = DEF_REV_TIME;
int VocalVibratoTrigger()
{
float tmpnote;
float power;
if (Pitch.Count != 0)
return TRUE;
tmpnote = PitchToMIDINote(Pitch.CurrentPitch);
power = Pitch.power;
if ((tmpnote <= DEF_VOCAL_LO)||(tmpnote >= DEF_VOCAL_HI))
{
HoldSeg = HOLDS;
HoldNote = DEF_VOCAL_NO;
Vib.lfosw = false;
}else if ((fabs(tmpnote-HoldNote) < DEF_TH) && (power > LoVol))
{
if (HoldSeg != 0)
HoldSeg--;
else
HoldSeg=0;
}else{
HoldSeg = HOLDS;
HoldNote = tmpnote;
Vib.lfosw = false;
}
if (HoldSeg == 0)
Vib.lfosw = true;
else
Vib.lfosw = false;
return TRUE;
}
int CalcSegLen(int SamRate, int nSam, int Freq)
{
int nSamPermilisec;
int ChunkDuration;
int T;
T = 1000*1/(float)Freq;
nSamPermilisec = SamRate/1000;
ChunkDuration = nSam/nSamPermilisec;
if (ChunkDuration != 0)
return T / ChunkDuration;
else
return T;
}
void OnWaveDataDone(WAVEBUFFER *pBuffer, int Len, CWnd *pParent)
{
}
void OnWaveData(WAVEBUFFER *pBuffer, int Len, CWnd *pParent)
{
MMRESULT CHK;
int nSam;
MsgProc = true;
nSam = GetnSam(Len,WaveFmt.wBitsPerSample,WaveFmt.nChannels);
ACDetectPitch(&Pitch,pBuffer,nSam);
VocalVibratoTrigger();
ProcVibrato(&Vib,pBuffer,nSam);
ProcPRCRev(&PRC,pBuffer,nSam);
pWaveOut->SetHDR(pBuffer,Len,&CHK);
MsgProc = false;
}
void SetVCamount(float amt)
{
vcamt = amt;
}
void OpenInPort(int ID)
{
if (pWaveIn->GetDeviceStatus()==DEV_CLOSE)
{
pWaveIn->SetDeviceID(ID);
pWaveIn->SetDeviceStatus(DEV_OPEN);
}else{
while(pWaveIn->GetDeviceStatus()==DEV_OPEN)
{
pWaveIn->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
pWaveIn->SetDeviceID(ID);
pWaveIn->SetDeviceStatus(DEV_OPEN);
}
}
void SetFullFmt()
{
WaveFmt = pWaveIn->GetWaveFormat();
}
void OpenOutPort(int ID)
{
WaveFmt = pWaveIn->GetWaveFormat();
pWaveOut->SetWaveFormat(WaveFmt);
if (pWaveOut->GetDeviceStatus()==DEV_CLOSE)
{
pWaveOut->SetDeviceID(ID);
pWaveOut->SetDeviceStatus(DEV_OPEN);
}else{
while(pWaveOut->GetDeviceStatus()==DEV_OPEN)
{
pWaveOut->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
pWaveOut->SetDeviceID(ID);
pWaveOut->SetDeviceStatus(DEV_OPEN);
}
}
bool CloseInPort()
{
pWaveIn->SetDeviceStatus(DEV_CLOSE);
while(pWaveIn->GetDeviceStatus() == DEV_OPEN)
{
DoMsgProc();
}
return true;
}
bool CloseOutPort()
{
pWaveOut->SetDeviceStatus(DEV_CLOSE);
while(pWaveOut->GetDeviceStatus() == DEV_OPEN)
{
DoMsgProc();
}
return true;
}
void InitAudioDev(CWnd *pParent)
{
pWaveIn = new CWaveIn;
pWaveIn->Create(pParent);
pWaveOut = new CWaveOut;
pWaveOut->Create(pParent);
pWaveFile = new CWaveFile(pParent);
pSaveFile = new CWaveFile(pParent);
pWaveOut->OnWaveDataDone = OnWaveDataDone;
pWaveIn->OnWaveData = OnWaveData;
WaveFmt.cbSize = 0;
WaveFmt.nChannels = DEF_CHS;
WaveFmt.nSamplesPerSec = DEF_SAMRATE;
WaveFmt.wBitsPerSample = DEF_BITS;
WaveFmt.nAvgBytesPerSec = DEF_SAMRATE*DEF_BITS*DEF_CHS / 8;
WaveFmt.nBlockAlign = DEF_CHS*DEF_BITS/8;
WaveFmt.wFormatTag = WAVE_FORMAT_PCM;
pWaveIn->SetWaveFormat(WaveFmt);
pWaveIn->SetProcPriority(NORMAL);
pWaveIn->SetHandleType(HANDLE_WINDOW);
pWaveOut->SetWaveFormat(WaveFmt);
pWaveOut->SetProcPriority(HIGH);
pWaveOut->SetHandleType(HANDLE_WINDOW);
}
void ClearAudioDev()
{
while(pWaveIn->GetDeviceStatus()==DEV_OPEN)
{
pWaveIn->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
delete pWaveIn;
DoMsgProc();
InitPitchShift(&Shift);
DoMsgProc();
InitVibrato(&Vib);
DoMsgProc();
InitACPitchDetect(&Pitch);
DoMsgProc();
InitHPSPitchDetect(&Hps);
DoMsgProc();
InitPRCRev(&PRC);
DoMsgProc();
InitJCRev(&JC);
DoMsgProc();
InitDelay(&Echo);
DoMsgProc();
while(pWaveOut->GetDeviceStatus()==DEV_OPEN)
{
pWaveOut->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
delete pWaveOut;
DoMsgProc();
delete pWaveFile;
delete pSaveFile;
DoMsgProc();
}
int GetDevInNum()
{
return pWaveIn->GetDeviceCount();
}
int GetDevOutNum()
{
return pWaveOut->GetDeviceCount();
}
void GetDevInName(int ID, char *pBuff)
{
strcpy(pBuff,pWaveIn->GetDeviceName(ID));
}
void GetDevOutName(int ID, char *pBuff)
{
strcpy(pBuff,pWaveOut->GetDeviceName(ID));
}
int GetInPortStatus()
{
if (pWaveIn->GetDeviceStatus()==DEV_OPEN)
return PORT_ON;
else
return PORT_OFF;
}
int GetOutPortStatus()
{
if (pWaveOut->GetDeviceStatus()==DEV_OPEN)
return PORT_ON;
else
return PORT_OFF;
}
bool SetInPort(int ID, int nSams, int nBuffs)
{
CloseInPort();
while(pWaveIn->GetDeviceStatus()==DEV_OPEN)
{
pWaveIn->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
DEV_SAMS = nSams;
DEV_BUFFERCOUNT = nBuffs;
DEV_BUFFERSIZE = DEF_BITS/8*DEF_CHS*DEV_SAMS;
pWaveIn->SetBufferSize(DEV_BUFFERSIZE);
pWaveIn->SetInitBufferNum(DEV_BUFFERCOUNT);
InitPitchShift(&Shift);
Shift.nCh = DEF_CHS;
Shift.nBit = DEF_BITS;
Shift.nSamplePerSec = DEF_SAMRATE;
MakePitchShift(&Shift);
InitVibrato(&Vib);
Vib.nCh = DEF_CHS;
Vib.nBit = DEF_BITS;
Vib.nSamplePerSec = DEF_SAMRATE;
SetVibratoParam(&Vib, 5, 2);
MakeVibrato(&Vib);
InitACPitchDetect(&Pitch);
Pitch.nCh = DEF_CHS;
Pitch.nBit = DEF_BITS;
Pitch.nSamplePerSec = DEF_SAMRATE;
Pitch.Seg = CalcSegLen(DEF_SAMRATE,DEV_SAMS,DEF_PITCH_FRQ);
MakeACPitchDetect(&Pitch);
InitHPSPitchDetect(&Hps);
Hps.nCh = DEF_CHS;
Hps.nBit = DEF_BITS;
Hps.nSamplePerSec = DEF_SAMRATE;
Hps.Seg = CalcSegLen(DEF_SAMRATE,DEV_SAMS,DEF_PITCH_FRQ);
MakeHPSPitchDetect(&Hps);
InitPRCRev(&PRC);
PRC.nCh = DEF_CHS;
PRC.nBit = DEF_BITS;
PRC.nSamplePerSec = DEF_SAMRATE;
PRC.WetGain = DEF_REV_WET;
PRC.DryGain = DEF_REV_DRY;
PRC.PreGain = 0.1;
PRC.Diffuse = 0.5;
MakePRCRev(&PRC,RoomSize);
InitJCRev(&JC);
JC.nCh = DEF_CHS;
JC.nBit = DEF_BITS;
JC.nSamplePerSec = DEF_SAMRATE;
JC.WetGain = 0.3;
JC.DryGain = 0.7;
JC.PreGain = 0.05;
JC.Diffuse = 0.5;
MakeJCRev(&JC,RoomSize);
InitDelay(&Echo);
Echo.nCh = DEF_CHS;
Echo.nBit = DEF_BITS;
Echo.nSamplePerSec = DEF_SAMRATE;
Echo.DryGain = DEF_ECHO_DRY;
Echo.WetGain = DEF_ECHO_WET;
Echo.MixDryGain = DEF_ECHO_DRY;
MakeDelay(&Echo,DEV_SAMS);
DoMsgProc();
OpenInPort(ID);
return true;
}
bool SetOutPort(int ID)
{
CloseOutPort();
while(pWaveOut->GetDeviceStatus()==DEV_OPEN)
{
pWaveOut->SetDeviceStatus(DEV_CLOSE);
DoMsgProc();
}
OpenOutPort(ID);
return true;
}
int GetnSamps()
{
return DEV_SAMS;
}
int GetnBuffs()
{
return DEV_BUFFERCOUNT;
}
void SetnSamps(int num)
{
DEV_SAMS = num;
}
void SetnBuffs(int num)
{
DEV_BUFFERCOUNT = num;
}
void SetPitch(float amt)
{
SetShiftAmount(&Shift,amt);
shiftamt = amt;
}
void SetPitchParam(float thr,float snr)
{
SetACPitchSNR(&Pitch,snr);
LoVol = thr;
}
void SetVib(int dep, float rate, int moment)
{
depth = dep;
speed = rate;
SetVibratoParam(&Vib, dep, rate);
HOLDS = moment;
}
void SetVibSw(bool vibsw)
{
Vib.lfosw = vibsw;
}
bool ProcNonRealTime(char *inName, char*outName)
{
int nBytes, readbytes,i, nSam, inID, outID,nbuf;
WAVEBUFFER *pBuffer;
WAVEFORMATEX fmt;
inID = pWaveIn->GetDeviceID();
nbuf = pWaveIn->GetBufferCount();
outID= pWaveOut->GetDeviceID();
while (CloseInPort() != true);
DoMsgProc();
while (CloseOutPort() != true);
DoMsgProc();
if (pWaveFile->SetFile(inName,FILE_READ) == true)
{
fmt = pWaveFile->GetWaveFormat();
pSaveFile->SetWaveFormat(fmt);
pSaveFile->SetFile(outName,FILE_WRITE);
InitACPitchDetect(&Pitch);
Pitch.nCh = fmt.nChannels;
Pitch.nBit = fmt.wBitsPerSample;
Pitch.nSamplePerSec = fmt.nSamplesPerSec;
Pitch.Seg = CalcSegLen(Pitch.nSamplePerSec,DEV_SAMS,DEF_PITCH_FRQ);
MakeACPitchDetect(&Pitch);
InitVibrato(&Vib);
Vib.nCh = fmt.nChannels;
Vib.nBit = fmt.wBitsPerSample;
Vib.nSamplePerSec = fmt.nSamplesPerSec;
SetVibratoParam(&Vib, depth, speed);
MakeVibrato(&Vib);
InitPRCRev(&PRC);
PRC.nCh = fmt.nChannels;
PRC.nBit = fmt.wBitsPerSample;
PRC.nSamplePerSec = fmt.nSamplesPerSec;
PRC.WetGain = DEF_REV_WET;
PRC.DryGain = DEF_REV_DRY;
PRC.PreGain = 0.1;
PRC.Diffuse = 0.5;
MakePRCRev(&PRC,RoomSize);
InitJCRev(&JC);
JC.nCh = fmt.nChannels;
JC.nBit = fmt.wBitsPerSample;
JC.nSamplePerSec = fmt.nSamplesPerSec;
JC.WetGain = 0.3;
JC.DryGain = 0.7;
JC.PreGain = 0.05;
JC.Diffuse = 0.5;
MakeJCRev(&JC,RoomSize);
nBytes = fmt.nBlockAlign * DEV_SAMS;
pBuffer = (char*)calloc(nBytes,sizeof(char));
readbytes = pWaveFile->Read(pBuffer,nBytes);
while(readbytes != 0)
{
if (readbytes != nBytes)
{
for (i = readbytes ; i < nBytes ; i++)
{
switch(fmt.wBitsPerSample)
{
case 8:
pBuffer[i] = 0x80;
break;
case 16:
pBuffer[i] = 0x00;
break;
}
}
}
nSam = GetnSam(readbytes,fmt.wBitsPerSample,fmt.nChannels);
ACDetectPitch(&Pitch,pBuffer,nSam);
VocalVibratoTrigger();
ProcVibrato(&Vib,pBuffer,nSam);
ProcPRCRev(&PRC,pBuffer,nSam);
pSaveFile->Write(pBuffer,nBytes);
readbytes = pWaveFile->Read(pBuffer,nBytes);
DoMsgProc();
}
free(pBuffer);
pSaveFile->SetFile(NULL,FILE_CLOSE);
pWaveFile->SetFile(NULL,FILE_CLOSE);
}
SetOutPort(outID);
SetInPort(inID,DEV_SAMS,nbuf);
return true;
}
float GetHPSPitch(char *str)
{
float out;
int note;
out = Pitch.CurrentPitch;
note = PitchToMIDINote(out);
VocalMIDINoteToString(note,str);
return out;
}
float GetHPSPower()
{
return Pitch.power;
}
int GetVocalInfo(float *power, float *pitch)
{
int tmpnote;
STRING tmp;
while(MsgProc == true)
DoMsgProc();
*pitch = Pitch.CurrentPitch;
*power = Pitch.power;
return TRUE;
}
void SetProcHWND(HWND hWnd)
{
UIWnd = hWnd;
}
void MakeReverb(float time, float dry, float wet)
{
ReverbReady = false;
EchoReady = false;
InitPRCRev(&PRC);
PRC.nCh = DEF_CHS;
PRC.nBit = DEF_BITS;
PRC.nSamplePerSec = DEF_SAMRATE;
PRC.WetGain = wet;
PRC.DryGain = dry;
PRC.PreGain = 0.1;
PRC.Diffuse = 0.5;
MakePRCRev(&PRC,time);
ReverbReady = true;
EchoReady = false;
}
void MakeEcho()
{
InitDelay(&Echo);
Echo.nCh = DEF_CHS;
Echo.nBit = DEF_BITS;
Echo.nSamplePerSec = DEF_SAMRATE;
Echo.DryGain = DEF_ECHO_DRY;
Echo.WetGain = DEF_ECHO_WET;
Echo.MixDryGain = DEF_ECHO_DRY;
MakeDelay(&Echo,DEV_SAMS);
}
void SetReverbGains(float d, float w)
{
if (d > 1.0)
d = 1.0;
else if (d < 0.0)
d = 0;
if (w > 1.0)
w = 1.0;
else if (w < 0.0)
w = 0;
SetPRCRevDryWetGain(&PRC,d,w);
SetJCRevDryWetGain(&JC,d,w);
}
void SetRoomSize(float rm)
{
if (rm > 5)
rm = 5.0;
else if (rm < 0.0)
rm = 0.0;
RoomSize = rm;
}