-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsound.c
216 lines (170 loc) · 4.19 KB
/
dsound.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
#include <dsound.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
LPDIRECTSOUND8 lpDS;
LPDIRECTSOUNDBUFFER lpDSB;
DSBUFFERDESC dsbd;
HWND hWnd;
int SampleRate=44100;
short SineTable[256]; // Sine wave lookup table
unsigned short phase=0; // Current phase in sine wave
const float TwoPI=2.0f*3.1415926f;
float midi[127]; // MIDI note to frequency table
unsigned char Done=0;
int InitDSound(void)
{
WAVEFORMATEX wfx;
if(DirectSoundCreate8(NULL, &lpDS, NULL)!=DS_OK)
return 0;
if(IDirectSound8_SetCooperativeLevel(lpDS, hWnd, DSSCL_NORMAL)!=DS_OK)
return 0;
memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx.wFormatTag=WAVE_FORMAT_PCM;
wfx.nChannels=2;
wfx.wBitsPerSample=16;
wfx.nSamplesPerSec=SampleRate;
wfx.nBlockAlign=4;
wfx.nAvgBytesPerSec=wfx.nSamplesPerSec*wfx.nChannels*(wfx.wBitsPerSample>>3);
memset(&dsbd, 0, sizeof(DSBUFFERDESC));
dsbd.dwSize=sizeof(DSBUFFERDESC);
dsbd.dwFlags=DSBCAPS_CTRLPOSITIONNOTIFY|DSBCAPS_GETCURRENTPOSITION2;
dsbd.dwBufferBytes=wfx.nAvgBytesPerSec;
dsbd.lpwfxFormat=&wfx;
if(IDirectSound_CreateSoundBuffer(lpDS, &dsbd, &lpDSB, NULL)!=DS_OK)
return 0;
return 1;
}
void PlayNote(float freq)
{
LPVOID lpvWrite;
short *ptr, output;
DWORD dwLength;
IDirectSoundBuffer_Stop(lpDSB);
if(IDirectSoundBuffer_Lock(lpDSB, 0, dsbd.dwBufferBytes, &lpvWrite, &dwLength, NULL, 0, DSBLOCK_ENTIREBUFFER)==DS_OK)
{
ptr=(short *)lpvWrite;
float duration=0.38f*SampleRate;
float attackTime=0.0001f*SampleRate;
float decayTime=0.1f*SampleRate;
float decayStart=duration-decayTime;
float peakAmp=1.0f;
float startAmp=0.0f;
float endAmp=0.0f;
float volume=startAmp;
float envInc=(peakAmp-startAmp)/attackTime;
for(int i=0;i<SampleRate;i++)
{
if(i<(int)duration)
{
if(i<attackTime||i>decayStart)
volume+=envInc;
else if(i==decayStart)
{
envInc=endAmp-volume;
if(decayTime>0)
envInc/=decayTime;
}
else
volume=peakAmp;
output=((float)SineTable[phase>>8]*volume);
phase+=(65535.0f*freq/SampleRate);
ptr[0]=output;
ptr[1]=output;
ptr+=2;
}
else
{
ptr[0]=0;
ptr[1]=0;
ptr+=2;
}
}
IDirectSoundBuffer_Unlock(lpDSB, lpvWrite , dwLength , NULL , 0);
}
IDirectSoundBuffer_SetCurrentPosition(lpDSB, 0);
IDirectSoundBuffer_Play(lpDSB, 0 , 0 , 0);
}
int main(int argc, char *argv[])
{
SetConsoleTitle("Simple DirectSound Synth");
hWnd=FindWindow(NULL, "Simple DirectSound Synth");
if(!InitDSound())
exit(-1);
// Generate MIDI -> Frequency table
for(int i=0;i<127;i++)
midi[i]=440.0f*pow(2, ((float)i-69.0f)/12.0f);
// Generate sine wave table
for(int i=0;i<256;i++)
SineTable[i]=(short)(32767.0f*sin(TwoPI*((float)i/256.0f)));
printf("Simple synthesizer\n\nSharps: S D G H J K\nWhole notes: Z X C V B N M\n\nQ to quit\n\n");
while(!Done)
{
if(_kbhit())
{
switch(getch())
{
case 'q':
Done=1;
break;
case 'z':
PlayNote(midi[60]); // C
printf("C %3.3fHz\n", midi[60]);
break;
case 's':
PlayNote(midi[61]); // C#
printf("C# %3.3fHz\n", midi[61]);
break;
case 'x':
PlayNote(midi[62]); // D
printf("D %3.3fHz\n", midi[62]);
break;
case 'd':
PlayNote(midi[63]); // D#
printf("D# %3.3fHz\n", midi[63]);
break;
case 'c':
PlayNote(midi[64]); // E
printf("E %3.3fHz\n", midi[64]);
break;
case 'v':
PlayNote(midi[65]); // F
printf("F %3.3fHz\n", midi[65]);
break;
case 'g':
PlayNote(midi[66]); // F#
printf("F# %3.3fHz\n", midi[66]);
break;
case 'b':
PlayNote(midi[67]); // G
printf("G %3.3fHz\n", midi[67]);
break;
case 'h':
PlayNote(midi[68]); // G#
printf("G# %3.3fHz\n", midi[68]);
break;
case 'n':
PlayNote(midi[69]); // A
printf("A %3.3fHz\n", midi[69]);
break;
case 'j':
PlayNote(midi[70]); // A#
printf("A# %3.3fHz\n", midi[70]);
break;
case 'm':
PlayNote(midi[71]); // B
printf("B %3.3fHz\n", midi[71]);
break;
case 'k':
PlayNote(midi[72]); // B#
printf("B# %3.3fHz\n", midi[72]);
break;
default:
break;
}
}
}
IDirectSoundBuffer_Release(lpDSB);
IDirectSound_Release(lpDS);
return 0;
}