forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_render.cpp
199 lines (176 loc) · 5.62 KB
/
wave_render.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
#include "internal.h"
#include "wave_render.h"
#include <math.h>
#define SAMPLESIZE 1024
#define BUFFER_SIZE 4096
#define BUFFER_COUNT 8
wave_render::wave_render()
: m_hwaveout(NULL)
, m_buffersize(0)
, m_wave_blocks(NULL)
, m_buf_write(0)
, m_buf_read(0)
{
m_volume.left = m_volume.right = m_volume.mute = 0;
}
wave_render::~wave_render()
{
destory_audio();
}
void __stdcall wave_render::waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2)
{
wave_render* this_ptr = (wave_render*)dwInstance;
if(uMsg != WOM_DONE)
return;
this_ptr->m_buf_read = (this_ptr->m_buf_read + 1) % BUFFER_COUNT;
}
bool wave_render::init_audio(void* ctx, int channels, int bits_per_sample, int sample_rate, int format)
{
WAVEFORMATEXTENSIBLE wformat = { 0 };
MMRESULT result;
unsigned char* buffer;
int i;
// FIXME multichannel mode is buggy.
if(channels > 2)
channels = 2;
// fill waveformatex.
ZeroMemory(&wformat, sizeof(WAVEFORMATEXTENSIBLE));
wformat.Format.cbSize = (channels > 2) ? sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) : 0;
wformat.Format.nChannels = channels;
wformat.Format.nSamplesPerSec = sample_rate;
if(AF_FORMAT_IS_AC3(format))
{
wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
wformat.Format.wBitsPerSample = 16;
wformat.Format.nBlockAlign = 4;
}
else
{
wformat.Format.wFormatTag = (channels > 2) ? WAVE_FORMAT_EXTENSIBLE : WAVE_FORMAT_PCM;
wformat.Format.wBitsPerSample = bits_per_sample; // af_fmt2bits(format);
wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
}
wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
// open sound device
// WAVE_MAPPER always points to the default wave device on the system
result = waveOutOpen(&m_hwaveout, WAVE_MAPPER, (WAVEFORMATEX*)&wformat,
(DWORD_PTR)wave_render::waveOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
if(result == WAVERR_BADFORMAT)
{
printf("format not supported switching to default.\n");
wformat.Format.wBitsPerSample = 16;
wformat.Format.wFormatTag = WAVE_FORMAT_PCM;
wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
m_buffersize = (wformat.Format.wBitsPerSample >> 3) * wformat.Format.nChannels * SAMPLESIZE;
result = waveOutOpen(&m_hwaveout, WAVE_MAPPER, (WAVEFORMATEX*)&wformat,
(DWORD_PTR)wave_render::waveOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
}
if(result != MMSYSERR_NOERROR)
{
printf("unable to open wave mapper device (result=%i)\n", result);
return false;
}
// allocate buffer memory as one big block.
buffer = (unsigned char*)calloc(BUFFER_COUNT, BUFFER_SIZE + sizeof(WAVEHDR));
// and setup pointers to each buffer.
m_wave_blocks = (WAVEHDR*)buffer;
buffer += sizeof(WAVEHDR) * BUFFER_COUNT;
for(i = 0; i < BUFFER_COUNT; i++) {
m_wave_blocks[i].lpData = (LPSTR)buffer;
buffer += BUFFER_SIZE;
}
m_buf_write = 0;
m_buf_read = 0;
return true;
}
int wave_render::play_audio(uint8_t* data, uint32_t size)
{
WAVEHDR* current;
int len2 = 0;
int x = 0;
while(size > 0) {
int buf_next = (m_buf_write + 1) % BUFFER_COUNT;
current = &m_wave_blocks[m_buf_write];
if(buf_next == m_buf_read)
break;
// unprepare the header if it is prepared.
if(current->dwFlags & WHDR_PREPARED)
waveOutUnprepareHeader(m_hwaveout, current, sizeof(WAVEHDR));
x = BUFFER_SIZE;
if(x > size)
x = size;
memcpy(current->lpData, data + len2, x);
len2 += x;
size -= x;
// prepare header and write data to device.
current->dwBufferLength = x;
waveOutPrepareHeader(m_hwaveout, current, sizeof(WAVEHDR));
waveOutWrite(m_hwaveout, current, sizeof(WAVEHDR));
m_buf_write = buf_next;
}
return len2;
}
void wave_render::audio_control(int cmd, void* arg)
{
DWORD volume;
switch (cmd)
{
case CONTROL_GET_VOLUME:
{
control_vol_t* vol = (control_vol_t*)arg;
waveOutGetVolume(m_hwaveout, &volume);
vol->left = (float)(LOWORD(volume) / 655.35);
vol->right = (float)(HIWORD(volume) / 655.35);
printf("volume left:%f volume right:%f\n", vol->left, vol->right);
}
break;
case CONTROL_SET_VOLUME:
{
control_vol_t* vol = (control_vol_t*)arg;
volume = MAKELONG(vol->left * 655.35, vol->right * 655.35);
waveOutSetVolume(m_hwaveout, volume);
}
break;
case CONTROL_MUTE_SET:
{
control_vol_t* v = (control_vol_t*)arg;
if (v->mute && !m_volume.mute)
{
control_vol_t* vol = &m_volume;
waveOutGetVolume(m_hwaveout, &volume);
vol->left = (float)(LOWORD(volume) / 655.35);
vol->right = (float)(HIWORD(volume) / 655.35);
vol->mute = true;
waveOutSetVolume(m_hwaveout, 0);
}
if (!v->mute && m_volume.mute)
{
control_vol_t* vol = &m_volume;
volume = MAKELONG(vol->left * 655.35, vol->right * 655.35);
vol->mute = false;
waveOutSetVolume(m_hwaveout, volume);
}
}
break;
}
}
void wave_render::destory_audio()
{
if (m_hwaveout)
{
// 修正在win7上退出后重启程序依然保持静音状态.
DWORD volume = MAKELONG(100.0f * 655.35, 100.0f * 655.35);
waveOutSetVolume(m_hwaveout, volume);
waveOutReset(m_hwaveout);
while (waveOutClose(m_hwaveout) == WAVERR_STILLPLAYING)
Sleep(0);
m_hwaveout = NULL;
}
if (m_wave_blocks)
{
free(m_wave_blocks);
m_wave_blocks = NULL;
}
}