-
Notifications
You must be signed in to change notification settings - Fork 9
/
Wave.cpp
318 lines (275 loc) · 6.76 KB
/
Wave.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
#include "Wave.h"
#include "math.h"
CWave::CWave(void)
{
// Init members
memset(&m_Descriptor, 0, sizeof(_WAVEDESCR));
memset(&m_Format, 0, sizeof(_WAVEFORMAT));
m_lpData = NULL;
m_dwSize = 0;
m_hWaveout = NULL;
memset(&m_WaveHeader, 0, sizeof(WAVEHDR));
m_bPaused = FALSE;
m_bStopped = TRUE;
}
CWave::~CWave(void)
{
// Close output device
Close();
}
BOOL CWave::Open(SHORT channels, DWORD sampleRate, SHORT bitsPerSample)
{
BOOL bResult = TRUE;
// Open output device
SHORT format = WAVE_FORMAT_PCM;
SHORT blockAlign = channels << 1;
WAVEFORMATEX wfex;
wfex.wFormatTag = format;
wfex.nChannels = channels;
wfex.nSamplesPerSec = sampleRate;
wfex.nAvgBytesPerSec = blockAlign * sampleRate;
wfex.nBlockAlign = blockAlign;
wfex.wBitsPerSample = bitsPerSample;
wfex.cbSize = 0;
if (waveOutOpen(&m_hWaveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&CWave::WaveOut_Proc, (DWORD_PTR)this, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
{
m_hWaveout = NULL;
bResult = FALSE;
}
m_bPaused = FALSE;
m_bStopped = TRUE;
return bResult;
}
BOOL CWave::Close()
{
BOOL bResult = TRUE;
// Stop playback
if (Stop())
{
// Close output device
if (waveOutClose(m_hWaveout) != MMSYSERR_NOERROR)
{
m_hWaveout = NULL;
bResult = FALSE;
}
}
// Check for valid sound data
if (IsValid())
{
// Clear sound data buffer
free(m_lpData);
m_lpData = NULL;
m_dwSize = 0;
}
return bResult;
}
BOOL CWave::Load(const char* lpszFilePath)
{
BOOL bResult = FALSE;
// Close output device
Close();
// Load .WAV file
FILE* file = fopen(lpszFilePath, "rb");
//FILE* file = _tfopen(lpszFilePath, _T("rb"));
if (file != NULL)
{
// Read .WAV descriptor
fread(&m_Descriptor, sizeof(_WAVEDESCR), 1, file);
// Check for valid .WAV file
if (strncmp((LPCSTR)m_Descriptor.wave, "WAVE", 4) == 0)
{
// Read .WAV format
fread(&m_Format, sizeof(_WAVEFORMAT), 1, file);
// Check for valid .WAV file
if ((strncmp((LPCSTR)m_Format.id, "fmt", 3) == 0) && (m_Format.format == 1))
{
// Read next chunk
BYTE id[4];
DWORD size;
fread(id, sizeof(BYTE), 4, file);
fread(&size, sizeof(DWORD), 1, file);
DWORD offset = ftell(file);
// Read .WAV data
LPBYTE lpTemp = (LPBYTE)malloc(m_Descriptor.size*sizeof(BYTE));
while (offset < m_Descriptor.size)
{
// Check for .WAV data chunk
if (strncmp((LPCSTR)id, "data", 4) == 0)
{
if (m_lpData == NULL)
m_lpData = (LPBYTE)malloc(size*sizeof(BYTE));
else
m_lpData = (LPBYTE)realloc(m_lpData, (m_dwSize+size)*sizeof(BYTE));
fread(m_lpData+m_dwSize, sizeof(BYTE), size, file);
m_dwSize += size;
}
else
fread(lpTemp, sizeof(BYTE), size, file);
// Read next chunk
fread(id, sizeof(BYTE), 4, file);
fread(&size, sizeof(DWORD), 1, file);
offset = ftell(file);
}
free(lpTemp);
// Open output device
if (!Open(m_Format.channels, m_Format.sampleRate, m_Format.bitsPerSample))
{
m_hWaveout = NULL;
bResult = FALSE;
}
else
bResult = TRUE;
}
}
// Close .WAV file
fclose(file);
}
return bResult;
}
BOOL CWave::Save(const char* lpszFilePath)
{
BOOL bResult = FALSE;
// Save .WAV file
FILE* file = fopen(lpszFilePath, "wb");
if (file != NULL)
{
// Save .WAV descriptor
m_Descriptor.size = m_dwSize;
fwrite(&m_Descriptor, sizeof(_WAVEDESCR), 1, file);
// Save .WAV format
fwrite(&m_Format, sizeof(_WAVEFORMAT), 1, file);
// Write .WAV data
BYTE id[4] = {'d', 'a', 't', 'a'};
fwrite(id, sizeof(BYTE), 4, file);
fwrite(&m_dwSize, sizeof(DWORD), 1, file);
fwrite(m_lpData, sizeof(BYTE), m_dwSize, file);
bResult = TRUE;
// Close .WAV file
fclose(file);
}
return bResult;
}
BOOL CWave::Play()
{
BOOL bResult = TRUE;
// Check for valid sound data
if (IsValid() && (m_bStopped || m_bPaused))
{
if (m_bPaused)
{
// Continue playback
if (waveOutRestart(m_hWaveout) != MMSYSERR_NOERROR)
bResult = FALSE;
}
else
{
// Start playback
m_WaveHeader.lpData = (LPSTR)m_lpData;
m_WaveHeader.dwBufferLength = m_dwSize;
if (waveOutPrepareHeader(m_hWaveout, &m_WaveHeader, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
bResult = FALSE;
else
{
if (waveOutWrite(m_hWaveout, &m_WaveHeader, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
bResult = FALSE;
}
}
m_bPaused = FALSE;
m_bStopped = FALSE;
}
return bResult;
}
BOOL CWave::Stop()
{
BOOL bResult = TRUE;
// Check for valid sound data
if (IsValid() && (!m_bStopped))
{
// Stop playback
m_bStopped = TRUE;
if (waveOutReset(m_hWaveout) != MMSYSERR_NOERROR)
bResult = FALSE;
else
{
if (waveOutUnprepareHeader(m_hWaveout, &m_WaveHeader, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
bResult = FALSE;
}
}
return bResult;
}
BOOL CWave::Pause()
{
BOOL bResult = TRUE;
// Check for valid sound data
if (IsValid() && (!m_bPaused) && (!m_bStopped))
{
// Pause playback
m_bPaused = TRUE;
if (waveOutPause(m_hWaveout) != MMSYSERR_NOERROR)
bResult = FALSE;
}
return bResult;
}
BOOL CWave::Mix(CWave& wave)
{
BOOL bResult = FALSE;
// Check for valid sound data
if ((IsValid() && m_bStopped) && (wave.IsValid() && wave.IsStopped()))
{
// Check for valid sound format
if ((m_Format.channels == wave.GetChannels()) && (m_Format.sampleRate == wave.GetSampleRate()) && (m_Format.bitsPerSample == wave.GetBitsPerSample()))
{
// Mix .WAVs
long sampleSize = min(m_dwSize, wave.GetSize()) / (m_Format.bitsPerSample >> 3);
switch (m_Format.bitsPerSample)
{
case 8:
{
LPBYTE lpSrcData = wave.GetData();
LPBYTE lpDstData = m_lpData;
float gain = log10(20.0f);
for (long i=0; i<sampleSize; i++)
{
*lpDstData = (BYTE)(((*lpSrcData+*lpDstData)>>1)*gain);
lpSrcData++;
lpDstData++;
}
}
break;
case 16:
{
LPWORD lpSrcData = (LPWORD)wave.GetData();
LPWORD lpDstData = (LPWORD)m_lpData;
for (long i=0; i<sampleSize; i++)
{
float sample1 = (*lpSrcData - 32768) / 32768.0f;
float sample2 = (*lpDstData - 32768) / 32768.0f;
if (fabs(sample1*sample2) > 0.25f)
*lpDstData = (WORD)(*lpSrcData + *lpDstData);
else
*lpDstData = fabs(sample1) < fabs(sample2) ? *lpSrcData : *lpDstData;
lpSrcData++;
lpDstData++;
}
}
break;
}
bResult = TRUE;
}
}
return bResult;
}
BOOL CWave::WaveOut_Proc(HWAVEOUT hwi, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
BOOL bResult = TRUE;
// Get current object
CWave* pWave = (CWave*)((DWORD_PTR)&dwInstance);
// Check for playback finished
if ((uMsg == WOM_DONE) && ((!pWave->m_bStopped) || (!pWave->m_bPaused)))
{
// Relase sound info
if (waveOutUnprepareHeader(pWave->m_hWaveout, &pWave->m_WaveHeader, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
bResult = FALSE;
}
return bResult;
}