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
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
Abstract sound class
*/
#include "MemDebug.h"
#include "Sound.h"
#include "SoundCard.h"
#include "Wave.h"
#include "vorbis/codec.h"
#include "vorbis/vorbisfile.h"
// +--------------------------------------------------------------------+
SoundCard* Sound::creator = 0;
Sound*
Sound::CreateStream(const char* filename)
{
Sound* sound = 0;
if (!filename || !filename[0] || !creator)
return sound;
int namelen = strlen(filename);
if (namelen < 5)
return sound;
if ((filename[namelen-3] == 'o' || filename[namelen-3] == 'O') &&
(filename[namelen-2] == 'g' || filename[namelen-2] == 'G') &&
(filename[namelen-1] == 'g' || filename[namelen-1] == 'G')) {
return CreateOggStream(filename);
}
WAVE_HEADER head;
WAVE_FMT fmt;
WAVE_FACT fact;
WAVE_DATA data;
WAVEFORMATEX wfex;
ZeroMemory(&head, sizeof(head));
ZeroMemory(&fmt, sizeof(fmt));
ZeroMemory(&fact, sizeof(fact));
ZeroMemory(&data, sizeof(data));
LPBYTE buf = 0;
LPBYTE p = 0;
int len = 0;
FILE* f;
::fopen_s(&f, filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
if (len > 4096) {
len = 4096;
}
buf = new(__FILE__,__LINE__) BYTE[len];
if (buf && len)
fread(buf, len, 1, f);
fclose(f);
}
if (len > sizeof(head)) {
CopyMemory(&head, buf, sizeof(head));
if (head.RIFF == MAKEFOURCC('R', 'I', 'F', 'F') &&
head.WAVE == MAKEFOURCC('W', 'A', 'V', 'E')) {
p = buf + sizeof(WAVE_HEADER);
do {
DWORD chunk_id = *((LPDWORD) p);
switch (chunk_id) {
case MAKEFOURCC('f', 'm', 't', ' '):
CopyMemory(&fmt, p, sizeof(fmt));
p += fmt.chunk_size + 8;
break;
case MAKEFOURCC('f', 'a', 'c', 't'):
CopyMemory(&fact, p, sizeof(fact));
p += fact.chunk_size + 8;
break;
case MAKEFOURCC('s', 'm', 'p', 'l'):
CopyMemory(&fact, p, sizeof(fact));
p += fact.chunk_size + 8;
break;
case MAKEFOURCC('d', 'a', 't', 'a'):
CopyMemory(&data, p, sizeof(data));
p += 8;
break;
default:
delete[] buf;
return sound;
}
}
while (data.chunk_size == 0);
wfex.wFormatTag = fmt.wFormatTag;
wfex.nChannels = fmt.nChannels;
wfex.nSamplesPerSec = fmt.nSamplesPerSec;
wfex.nAvgBytesPerSec = fmt.nAvgBytesPerSec;
wfex.nBlockAlign = fmt.nBlockAlign;
wfex.wBitsPerSample = fmt.wBitsPerSample;
wfex.cbSize = 0;
sound = Create(Sound::STREAMED, &wfex);
if (sound) {
sound->SetFilename(filename);
sound->StreamFile(filename, p - buf);
}
}
}
delete[] buf;
return sound;
}
// +--------------------------------------------------------------------+
Sound*
Sound::CreateOggStream(const char* filename)
{
Sound* sound = 0;
if (!filename || !filename[0] || !creator)
return sound;
int namelen = strlen(filename);
if (namelen < 5)
return sound;
WAVEFORMATEX wfex;
ZeroMemory(&wfex, sizeof(wfex));
FILE* f;
::fopen_s(&f, filename, "rb");
if (f) {
OggVorbis_File* povf = new(__FILE__,__LINE__) OggVorbis_File;
if (!povf) {
Print("Sound::CreateOggStream(%s) - out of memory!\n", filename);
return sound;
}
ZeroMemory(povf, sizeof(OggVorbis_File));
if (ov_open(f, povf, NULL, 0) < 0) {
Print("Sound::CreateOggStream(%s) - not an Ogg bitstream\n", filename);
delete povf;
return sound;
}
Print("\nOpened Ogg Bitstream '%s'\n", filename);
char **ptr=ov_comment(povf,-1)->user_comments;
vorbis_info *vi=ov_info(povf,-1);
while(*ptr){
Print("%s\n", *ptr);
++ptr;
}
Print("Bitstream is %d channel, %ldHz\n", vi->channels, vi->rate);
Print("Decoded length: %ld samples\n",
(long)ov_pcm_total(povf,-1));
Print("Encoded by: %s\n\n", ov_comment(povf,-1)->vendor);
wfex.wFormatTag = WAVE_FORMAT_PCM;
wfex.nChannels = vi->channels;
wfex.nSamplesPerSec = vi->rate;
wfex.nAvgBytesPerSec = vi->channels * vi->rate * 2;
wfex.nBlockAlign = vi->channels * 2;
wfex.wBitsPerSample = 16;
wfex.cbSize = 0;
sound = Create(Sound::STREAMED | Sound::OGGVORBIS,
&wfex,
sizeof(OggVorbis_File),
(LPBYTE) povf);
sound->SetFilename(filename);
}
return sound;
}
// +--------------------------------------------------------------------+
Sound*
Sound::Create(DWORD flags, LPWAVEFORMATEX format)
{
if (creator) return creator->CreateSound(flags, format);
else return 0;
}
Sound*
Sound::Create(DWORD flags, LPWAVEFORMATEX format, DWORD len, LPBYTE data)
{
if (creator) return creator->CreateSound(flags, format, len, data);
else return 0;
}
void
Sound::SetListener(const Camera& cam, const Vec3& vel)
{
if (creator)
creator->SetListener(cam, vel);
}
// +--------------------------------------------------------------------+
Sound::Sound()
: status(UNINITIALIZED), volume(0), flags(0), looped(0),
velocity(0,0,0), location(0,0,0), sound_check(0)
{
strcpy_s(filename, "Sound()");
}
// +--------------------------------------------------------------------+
Sound::~Sound()
{ }
// +--------------------------------------------------------------------+
void
Sound::Release()
{
flags &= ~LOCKED;
}
// +--------------------------------------------------------------------+
void
Sound::AddToSoundCard()
{
if (creator)
creator->AddSound(this);
}
// +--------------------------------------------------------------------+
void
Sound::SetFilename(const char* s)
{
if (s) {
int n = strlen(s);
if (n >= 60) {
ZeroMemory(filename, sizeof(filename));
strcpy_s(filename, "...");
strcat_s(filename, s + n - 59);
filename[63] = 0;
}
else {
strcpy_s(filename, s);
}
}
}
|