-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWaveBox.h
144 lines (117 loc) · 5.5 KB
/
WaveBox.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
// WaveBox.h: interface for the CWave class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_WAVEBOX_H__DE24CFE1_7501_4DA3_AF18_667A845AAE49__INCLUDED_)
#define AFX_WAVEBOX_H__DE24CFE1_7501_4DA3_AF18_667A845AAE49__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
////
//// by Zen '05
////
//// WaveBox class v0.95
//// ~~~~~~~~~~~~~~~~~~~~~~
//// ( PCM multiwave player )
//// play & joy
////
////
////
////
/// precompiler
#include <windows.h>
#include <mmsystem.h>
/// wave & PCM marks
#define WAVE_FILE_MARK "RIFF"
#define WAVE_HEAD_MARK "WAVEfmt "
#define WAVE_DATA_MARK "data"
#define WAVE_PCM_16 16
#define WAVE_PCM_1 1
/// wfx header offsets
#define OFFSET_FILE_LEFT 4
#define OFFSET_HEAD_MARK 8
#define OFFSET_WAVE_PCM1 16
#define OFFSET_WAVE_PCM2 20
#define OFFSET_CHANNELS 22
#define OFFSET_SAMPLESPERSEC 24
#define OFFSET_AVGBYTESPERSEC 28
#define OFFSET_BLOCKALIGN 32
#define OFFSET_BITSPERSAMPLE 34
#define OFFSET_DATA_MARK 36
#define OFFSET_DATA_SIZE 40
#define OFFSET_WAVEDATA 44
#define HEADER_SIZE OFFSET_WAVEDATA
#define EOF_EXTRA_INFO 60
/// messages
typedef unsigned int WMsg; // wave messages
typedef unsigned int TMsg; // thread messages
#define WMSG_WAIT 0 // wave wait
#define WMSG_START 1 // wave play
#define TMSG_ALIVE 1 // thread alive
#define TMSG_CLOSE 0 // thread close
#define INT_FREE 0 // interface free
#define INT_USED 1 // interface used
#define THREAD_EXIT 0xABECEDA // thread exit code
/// performance predefines
#define SUPPORT_WAVES 130 // predefined wave count
#define SUPPORT_INTERFACES 130 // predefined interface count
/// buffering
#define READ_BLOCK 8192 // read wave ( file ) block size
#define BLOCK_SIZE 8192 // queue block size
#define BLOCK_COUNT 20 // queue block count
#define BP_TURN 1 // blocks per turn
static CRITICAL_SECTION cs; // critical section
static unsigned int __stdcall PlayThread( LPVOID lp ); // main play thread
static void CALLBACK waveOutProc( HWAVEOUT hWaveOut, // waveOut prototype
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2 );
class CWaveBox
{
struct WAVE
{
char *data; /// wave
unsigned long size; /// size
WAVEFORMATEX wfx; /// wfx
WMsg WMSG; /// { 0,1 } wait / play
};
struct INTERFACE
{
/// interface
HWAVEOUT dev; /// device handle
unsigned int state; /// { 0,1 } free / used
/// wave
WAVE *wave; /// current wave
/// wave interface
unsigned long wpos; /// current play position
WAVEHDR* wblock; /// wave block
volatile int wfreeblock; /// free blocks left
int wcurrblock; /// current block
};
public:
/// members
INTERFACE I[SUPPORT_INTERFACES]; // interface(s)
WAVE W[SUPPORT_WAVES]; // wave(s)
unsigned int wload; // current wave(s) loaded
TMsg TMSG; // thread msg { 1,0 } alive / close
/// prototypes
int Load( TCHAR *file ); // load wave into WaveBox
int Play( unsigned int wave ); // play n wave from WaveBox ( starts play thread )
CWaveBox(); // play thread created suspended
virtual ~CWaveBox(); // play thread terminated
/// waveOut open & close
int AddInterface( HWAVEOUT *dev, /// push wave wfx to interface
WAVEFORMATEX *wfx,
volatile int *wfreeblock );
int RemoveInterface( HWAVEOUT dev ); /// pull wave wfx from interface
protected:
/// thread
HANDLE thread; /// play thread handle
unsigned int run; /// suspended / resumed
/// prototypes
WAVEHDR* allocateBlocks( int size, int count ); /// alloc heap for wave header blocks
void freeBlocks( WAVEHDR* blockArray ); /// free heap
};
#endif // !defined(AFX_WAVEBOX_H__DE24CFE1_7501_4DA3_AF18_667A845AAE49__INCLUDED_)