-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsp_util.cpp
155 lines (124 loc) · 2.51 KB
/
dsp_util.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
#include "stdafx.h"
#include "dsp_def.h"
#include "dsp_util.h"
#include <math.h>
#include <stdio.h>
#define DEF_VOCAL_LO 39
#define DEF_VOCAL_HI 80
#define DEF_LOG10_2 log10(2)
const char *NOTENAME[] = {"C","C#","D","D#",
"E","F","F#","G",
"G#","A","A#","B"};
inline float log2(float val)
{
int * const exp_ptr = reinterpret_cast <int *> (&val);
int x = *exp_ptr;
const int log_2 = ((x >> 23) & 255) - 128;
x &= ~(255 << 23);
x += 127 << 23;
*exp_ptr = x;
return (val + log_2);
}
void DoMsgProc()
{
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
INT8 Set8Bit(INT8 data)
{
//Turn (0-255) INT8 to (-128 - 127) INT8
INT8 ans;
if (data&0x80)
ans = -(DEF_8BIT_MINUS + data);
else
ans = DEF_8BIT_PLUS - data;
return ans;
}
INT8 Return8Bit(float data)
{
//Turn float to (0-255) INT8
INT8 ans;
if (data > DEF_8BIT_MAX)
data = DEF_8BIT_MAX;
if (data < DEF_8BIT_MIN)
data = DEF_8BIT_MIN;
if ((INT8)data&0x80)
ans = -(DEF_8BIT_MINUS + (INT8)data);
else
ans = DEF_8BIT_PLUS - (INT8)data;
return ans;
}
INT16 Return16Bit(float data)
{
//Check Upper/Lower Bound for INT16
INT16 ans;
if (data > DEF_16BIT_MAX)
data = DEF_16BIT_MAX;
if (data < DEF_16BIT_MIN)
data = DEF_16BIT_MIN;
ans = (INT16)data;
return ans;
}
int GetnSam(int nByte,int nBit,int nCh)
{
//nByte -> Buffer Length in Bytes
return (8*nByte)/(nBit*nCh);
}
int GetnByte(int nSam, int nBit, int nCh)
{
return (nSam*nBit*nCh)/8;
}
int MakeWindow(int nSam, int win, float *pWin)
{
int i;
float phase = 0, delta;
delta = 2 * PI / (float)nSam;
switch(win){
case DEF_WIN_HAN:
for (i = 0 ; i < nSam ; i++)
{
pWin[i] = (0.5 - 0.5*cos(phase));
phase += delta;
}
break;
case DEF_WIN_HAM:
for (i = 0 ; i < nSam ; i++)
{
pWin[i] = (0.54 - 0.46*cos(phase));
phase += delta;
}
break;
case DEF_WIN_BLACK:
for (i = 0 ; i < nSam ; i++)
{
pWin[i] = (0.42 - 0.5*cos(phase) + 0.08*cos(2*phase));
phase += delta;
}
break;
}
return TRUE;
}
float PitchToMIDINote(float Pitch)
{
float out = 0;
if (Pitch < 0)
Pitch = 0;
out = fabs( 12 * (log10(Pitch/DEF_TUNE_A4)/DEF_LOG10_2) + 69 );
if (out > 127)
out = -1;
return out;
}
int VocalMIDINoteToString(int note,char *str)
{
if ((note > DEF_VOCAL_LO) && (note < DEF_VOCAL_HI))
{
sprintf(str,"%s%d",NOTENAME[note%12],(note/12 - 1));
}else{
sprintf(str,"---");
}
return 1;
}