-
Notifications
You must be signed in to change notification settings - Fork 14
/
mfcwave.h
397 lines (339 loc) · 16.3 KB
/
mfcwave.h
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
/*****************************************************************************/
/* MFCWAVE.H : class definition for wave base classes */
/*****************************************************************************/
/******************************************************************************
Copyright (C) 2006 Hermann Seib
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#if !defined(_MFCWAVE_H__INCLUDED_)
#define _MFCWAVE_H__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __cplusplus
#error mfcwave.h is for use with C++
#endif
/*****************************************************************************/
/* Necessary includes */
/*****************************************************************************/
/*****************************************************************************/
/* Wave definitions */
/*****************************************************************************/
#define WAVEOPENDEFAULT ((DWORD)-1)
/* default values: */
#define WAVEINHDRS 10 /* # Wave Input Buffer Headers */
#define WAVEINBUFSIZE 4410L /* length of Wave input buffer */
#define WMAP_NAME "Wave Mapper"
/*****************************************************************************/
/* CWaveBuffer : a wave buffer encapsulated */
/*****************************************************************************/
class CWaveBuffer
{
public:
CWaveBuffer(DWORD dwLen = 0); /* constructors */
CWaveBuffer(LPVOID lpBuf, DWORD dwLen);
CWaveBuffer(LPWAVEHDR lpHdr);
CWaveBuffer (CWaveBuffer const & org)
{ Init(); DoCopy(org); }
virtual ~CWaveBuffer() /* destructor */
{ Empty(); }
operator LPWAVEHDR(); /* allow use in base calls */
DWORD Length(); /* get data length */
/* assignment operator */
CWaveBuffer & operator=(CWaveBuffer const &org)
{ return DoCopy(org); }
CWaveBuffer & operator=(LPWAVEHDR lphdr)
{ Set(lphdr); return *this; }
/* set to input data */
void Set(DWORD dwLen = 0);
void Set(LPVOID lpBuf, DWORD dwLen);
void Set(LPWAVEHDR lpHdr);
/* set / get timestamp */
void SetStamp(DWORD dwTS) { dwStamp = dwTS; }
DWORD GetStamp() { return dwStamp; }
protected:
BOOL bAlloc; /* flag whether self-allocated */
LPWAVEHDR lpHdr; /* wave message header */
HGLOBAL hGlPtr; /* global pointer */
DWORD dwStamp; /* timestamp */
protected:
void Init(); /* initialize memory */
void Empty(); /* empty memory */
/* copy another wave buffer */
CWaveBuffer & DoCopy(CWaveBuffer const &org)
{
Empty(); /* remove any previous contents */
if (org.lpHdr) /* if original has a buffer */
/* copy original's buffer */
Set(org.lpHdr->lpData, org.lpHdr->dwBufferLength);
dwStamp = org.dwStamp;
return *this; /* pass back pointer to ourselves */
}
};
/*****************************************************************************/
/* CWaveDevice : wave in/output device virtual base class */
/*****************************************************************************/
class CWaveDevice : public CWnd
{
// Construction
public:
CWaveDevice(); /* constructor */
// Attributes
public:
BOOL IsOpen() /* return whether device is open */
{ return bIsOpen; }
LPCSTR DeviceName ( ) /* return opened device' name */
{ return bIsOpen ? szDevName : ""; }
// Operations
public:
virtual BOOL Open(LPCSTR szName, /* open with device name */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCallback = WAVEOPENDEFAULT,
DWORD dwCallbackInstance = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Open(int iID, /* open with device ID */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCallback = WAVEOPENDEFAULT,
DWORD dwCallbackInstance = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Reset() = 0; /* reset device */
/* prepare LPWAVEHDR */
virtual BOOL PrepareHeader(LPWAVEHDR lpHdr) = 0;
/* unprepare LPWAVEHDR */
virtual BOOL UnprepareHeader(LPWAVEHDR lpHdr) = 0;
virtual BOOL Close(); /* close device */
virtual BOOL GetPosition(LPMMTIME pmmt,
UINT cbmmt = sizeof(MMTIME)) = 0;
virtual DWORD Message(UINT msg, /* send special message */
DWORD dw1,
DWORD dw2) = 0;
virtual void OnEvent() = 0; /* event handler */
void SetupWaveformat(LPWAVEFORMATEX pwf, DWORD dwSamplesPerSec = 44100, WORD wBitsPerSample = 16, WORD nChannels = 2);
// Attributes
WORD FormatTag()
{ return (IsOpen()) ? wf.wFormatTag : 0; }
int Channels()
{ return (IsOpen()) ? (int)wf.nChannels : 0; }
int SamplesPerSecond()
{ return (IsOpen()) ? (int)wf.nSamplesPerSec : 0; }
int AvgBytesPerSecond()
{ return (IsOpen()) ? (int)wf.nAvgBytesPerSec : 0; }
int BlockAlignment()
{ return (IsOpen()) ? (int)wf.nBlockAlign : 0; }
int Bits()
{ return (IsOpen()) ? (int)wf.wBitsPerSample : 0; }
LPVOID AdditionalFormatData()
{ return ((IsOpen()) && (wf.cbSize)) ? (LPVOID)(&wf + 1) : 0; }
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWaveDevice)
//}}AFX_VIRTUAL
// Implementation
public:
BOOL BoostPriority();
void SetSample(CWaveBuffer &buf, int nSample, int nChannel, short sValue);
short GetSample(CWaveBuffer &buf, int nSample, int nChannel);
int GetSampleCount(CWaveBuffer &buf) { return BlockAlignment() ? buf.Length() / BlockAlignment() : 0; }
virtual ~CWaveDevice(); /* destructor */
// Generated message map functions
protected:
//{{AFX_MSG(CWaveDevice)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected :
BOOL bIsOpen; /* flag whether opened */
WAVEFORMATEX wf; /* format for open */
protected:
HANDLE hEvent; /* event if using event-based logic */
CWinThread * pEvtThread; /* event thread procedure */
BOOL volatile bEvtCancel; /* event thread cancel flag */
BOOL Create();
static UINT EvtThreadProc(LPVOID pParam);
BOOL StartEvt();
void KillEvt();
void AssureWnd()
{
if (!m_hWnd) /* if no notification window yet */
{
Create(); /* create it */
ASSERT(m_hWnd);
}
}
char szDevName[MAXPNAMELEN]; /* device name */
};
/*****************************************************************************/
/* CWaveInDeviceList : Wave Input device list */
/*****************************************************************************/
class CWaveInDeviceList : public CStringArray
{
public :
CWaveInDeviceList ( ); /* constructor */
};
/*****************************************************************************/
/* CWaveInDevice : Wave Input Device */
/*****************************************************************************/
class CWaveInDevice : public CWaveDevice
{
public:
CWaveInDevice(); /* constructor */
virtual ~CWaveInDevice(); /* destructor */
// operations
public:
virtual BOOL Open(LPCSTR szName, /* open with device name */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCallback = WAVEOPENDEFAULT,
DWORD dwCallbackInstance = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Open(int iID, /* open with device ID */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCallback = WAVEOPENDEFAULT,
DWORD dwCallbackInstance = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Start(); /* start recording */
virtual BOOL Stop(); /* stop recording */
/* prepare LPWAVEHDR */
virtual BOOL PrepareHeader(LPWAVEHDR lpHdr);
/* unprepare LPWAVEHDR */
virtual BOOL UnprepareHeader(LPWAVEHDR lpHdr);
virtual BOOL Reset(); /* reset device */
virtual BOOL Close(); /* close device */
virtual BOOL GetPosition(LPMMTIME pmmt,
UINT cbmmt = sizeof(MMTIME));
virtual DWORD Message(UINT msg, /* send special message */
DWORD dw1, DWORD dw2);
virtual void OnEvent(); /* incoming event */
/* process incoming data */
virtual void Data(CWaveBuffer &buf) { }
virtual void OnWimOpen() { }
virtual void OnWimClose() { }
virtual void OnWimData(LPWAVEHDR lphdr);
void SetupAllocSize(int nCount = WAVEINHDRS, int nSize = WAVEINBUFSIZE)
{ nAllocCount = nCount; nAllocSize = nSize; }
/* allocate buffer */
BOOL AllocBuf(int nCount = WAVEINHDRS, int nSize = WAVEINBUFSIZE);
BOOL FreeBuf(); /* free allocated buffer */
BOOL PrepareBuf(); /* prepare buffer for usage */
BOOL UnprepareBuf(); /* unprepare buffer from usage */
BOOL IsRecording()
{ return bRecording; }
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWaveInDevice)
//}}AFX_VIRTUAL
// Generated message map functions
protected:
//{{AFX_MSG(CWaveInDevice)
afx_msg LRESULT OnWimOpen(WPARAM w, LPARAM l);
afx_msg LRESULT OnWimClose(WPARAM w, LPARAM l);
afx_msg LRESULT OnWimData(WPARAM w, LPARAM l);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
WAVEINCAPS wCap; /* input device capabilities */
BOOL bClosing; /* flag whether currently closing */
HWAVEIN hDev; /* input device handle */
BOOL bRecording; /* flag whether currently recording */
DWORD dwStamp; /* internal start time stamp */
HGLOBAL hGlobal; /* global memory for SysEx buffers */
char * pGlobal; /* same as locked pointer */
LPWAVEHDR *lpGlHdr; /* Wave input buffers */
int nAllocated; /* # allocated input buffers */
int nAllocCount; /* # buffers to be alloc'd in open */
int nAllocSize; /* size of each of these */
protected:
int PreparedBuffers();
BOOL AddHeader(LPWAVEHDR lpHdr); /* add MIDI buffer to device */
BOOL CheckHeader(LPWAVEHDR lpHdr); /* check whether it's one of ours */
};
/*****************************************************************************/
/* CWaveOutDeviceList : Wave Output device list */
/*****************************************************************************/
class CWaveOutDeviceList : public CStringArray
{
public :
CWaveOutDeviceList ( ); /* constructor */
};
/*****************************************************************************/
/* CWaveOutDevice : Wave Output device */
/*****************************************************************************/
class CWaveOutDevice : public CWaveDevice
{
public:
CWaveOutDevice(); /* constructor */
virtual ~CWaveOutDevice(); /* destructor */
// operations
public:
virtual BOOL Open(LPCSTR szName, /* open with device name */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCb = WAVEOPENDEFAULT,
DWORD dwCbInst = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Open(int iID, /* open with device ID */
LPWAVEFORMATEX pwfx = NULL,
DWORD dwCb = WAVEOPENDEFAULT,
DWORD dwCbInst = WAVEOPENDEFAULT,
DWORD fdwOpen = CALLBACK_WINDOW);
virtual BOOL Reset();
virtual BOOL PrepareHeader(LPWAVEHDR lpHdr);
virtual BOOL UnprepareHeader(LPWAVEHDR lpHdr);
virtual BOOL Pause();
virtual BOOL Restart();
virtual BOOL BreakLoop();
virtual BOOL Close();
virtual BOOL Output(CWaveBuffer &Buf, BOOL bSync = TRUE);
virtual BOOL GetPosition(LPMMTIME pmmt,
UINT cbmmt = sizeof(MMTIME));
virtual BOOL GetPitch(LPDWORD pdwPitch);
virtual BOOL SetPitch(DWORD dwPitch);
virtual BOOL GetPlaybackRate(LPDWORD pdwPlaybackRate);
virtual BOOL SetPlaybackRate(DWORD dwPlaybackRate);
virtual BOOL GetVolume(LPDWORD pdwVolume);
virtual BOOL SetVolume(DWORD dwVolume);
virtual DWORD Message(UINT msg, DWORD dw1, DWORD dw2);
virtual void OnEvent() {} /* incoming event */
virtual void OnWomOpen() {}
virtual void OnWomClose() {}
virtual void OnWomDone(LPWAVEHDR lphdr)
{
// this MUST be overridden if asynchronous processing is done!
// in this case, a call to Unprepare() must be done here!
}
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWaveOutDevice)
//}}AFX_VIRTUAL
protected:
HWAVEOUT hDev; /* output device handle */
WAVEOUTCAPS wCap; /* output device capabilities */
//{{AFX_MSG(CWaveOutDevice)
afx_msg LRESULT OnWomOpen(WPARAM w, LPARAM l);
afx_msg LRESULT OnWomDone(WPARAM w, LPARAM l);
afx_msg LRESULT OnWomClose(WPARAM w, LPARAM l);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
WAVEOUTCAPS wc;
BOOL SendingDone(LPWAVEHDR lpHdr);
static void CALLBACK WaveOutProc
(
HMIDIOUT hWaveOut,
UINT wMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
);
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(_MFCWAVE_H__INCLUDED_)