Debian Codegraph @

docs/linux_games_programming.txt

raw
1 # Here are some some emails I exchanged with a guy trying to use
2 # libsndfile version 1 with code from the book "Linux Games Programming"
3 # by John Hall. The email addresses have been changed to foil the spam
4 # bots.
5
6 Date: Tue, 20 Jul 2004 22:49:21 +0100
7 From: Paul <[email protected]>
8 To: [email protected]
9 Subject: Can you help with a problem?
10 Date: Tue, 20 Jul 2004 22:49:21 +0100
11
12 Hi,
13
14 I'm trying to get the source examples in the "Programming Linux Games"
15 (NoStarch, Loki Software + John R. Hall) which use sndfile.h/libsndfile.
16
17 While I can guess some of the newer versions of function calls and
18 enumerations, there are some which I cannot guess.
19
20 Would you be able to translate them to the current version of
21 enumeration and function calls so that I can update the source?
22
23 These are the three currently failing me:
24
25 sf_open_read(filename, SF_INFO *sfinfo) (guess: sf_open(filename,SFM_READ, &sfinfo))
26 SF_FORMAT_PCM (guess: either SF_FORMAT_PCM_U8 or _RAW)
27 SF_INFO.pcmbitwidth (guess: no idea!)
28
29 There are probably more. I'm happy to send you the source files for
30 sound calls, scan the pages or anything else. Failing that, is there
31 somewhere with the changes listed so I can try and fix the code for myself?
32
33 Thanks
34
35 TTFN
36
37 Paul
38
39 ================================================================================
40
41 Date: Wed, 21 Jul 2004 17:38:08 +1000
42 From: Erik de Castro Lopo <[email protected]>
43 To: Paul <[email protected]>
44 Subject: Re: Can you help with a problem?
45
46 On Tue, 20 Jul 2004 22:49:21 +0100
47 Paul <[email protected]> wrote:
48
49 > Hi,
50 >
51 > I'm trying to get the source examples in the "Programming Linux Games"
52 > (NoStarch, Loki Software + John R. Hall) which use sndfile.h/libsndfile.
53 >
54 > While I can guess some of the newer versions of function calls and
55 > enumerations, there are some which I cannot guess.
56 >
57 > Would you be able to translate them to the current version of
58 > enumeration and function calls so that I can update the source?
59 >
60 > These are the three currently failing me:
61 >
62 > sf_open_read(filename, SF_INFO *sfinfo) (guess: sf_open(filename,
63 > SFM_READ, &sfinfo))
64
65 yes.
66
67 > SF_FORMAT_PCM (guess: either SF_FORMAT_PCM_U8 or _RAW)
68
69 Actually this list:
70
71 SF_FORMAT_PCM_U8
72 SF_FORMAT_PCM_S8
73 SF_FORMAT_PCM_16
74 SF_FORMAT_PCM_24
75 SF_FORMAT_PCM_32
76
77 > SF_INFO.pcmbitwidth (guess: no idea!)
78
79 WIth the above change, pcmbitwidth becomes redundant.
80
81 > There are probably more. I'm happy to send you the source files for
82 > sound calls, scan the pages or anything else. Failing that, is there
83 > somewhere with the changes listed so I can try and fix the code for
84 > myself?
85
86 Version 1.0.0 came out some time ago, but I think this:
87
88 http://www.mega-nerd.com/libsndfile/version-1.html
89
90 lists most of the changes. You should also look at the API docs:
91
92 http://www.mega-nerd.com/libsndfile/api.html
93
94 HTH,
95 Erik
96 --
97 +-----------------------------------------------------------+
98 Erik de Castro Lopo [email protected]
99 +-----------------------------------------------------------+
100 "There is no reason why anyone would want a computer in their home"
101 Ken Olson, DEC, 1977
102
103 ================================================================================
104
105 From: PFJ <[email protected]>
106 To: Erik de Castro Lopo <[email protected]>
107 Subject: Re: Can you help with a problem?
108 Date: Wed, 21 Jul 2004 09:07:39 +0100
109
110
111 Hi Erik,
112
113 Thanks for getting back to me.
114
115 > > sf_open_read(filename, SF_INFO *sfinfo) (guess: sf_open(filename, SFM_READ, &sfinfo))
116 >
117 > yes.
118
119 Yay!
120
121 > > SF_FORMAT_PCM (guess: either SF_FORMAT_PCM_U8 or _RAW)
122 >
123 > Actually this list:
124 >
125 > SF_FORMAT_PCM_U8
126 > SF_FORMAT_PCM_S8
127 > SF_FORMAT_PCM_16
128 > SF_FORMAT_PCM_24
129 > SF_FORMAT_PCM_32
130
131 I know, but the source code explicitly has SF_FORMAT_PCM which given the
132 code afterwards would equate to one of the above, but given that PCM
133 files can have a varied bitwidth the author probably wanted to cover all
134 bases.
135
136 > Version 1.0.0 came out some time ago, but I think this:
137 >
138 > http://www.mega-nerd.com/libsndfile/version-1.html
139 >
140 > lists most of the changes. You should also look at the API docs:
141 >
142 > http://www.mega-nerd.com/libsndfile/api.html
143
144 I'll download them and see what I can gleen.
145
146 Thanks again for getting back to me
147
148 TTFN
149
150 Paul
151
152 ================================================================================
153
154 Date: Wed, 21 Jul 2004 18:20:29 +1000
155 From: Erik de Castro Lopo <[email protected]>
156 To: PFJ <[email protected]>
157 Subject: Re: Can you help with a problem?
158
159 On Wed, 21 Jul 2004 09:07:39 +0100
160 PFJ <[email protected]> wrote:
161
162 > I know, but the source code explicitly has SF_FORMAT_PCM which given the
163 > code afterwards would equate to one of the above, but given that PCM
164 > files can have a varied bitwidth the author probably wanted to cover all
165 > bases.
166
167 But surely the existing code does something like:
168
169 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM;
170 sfinfo.pcmbitwidth = 16;
171
172 which can be directly translated to:
173
174 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
175
176 and the same for pcmbitwitdhs of 24 and 32. For pcmbitwidth of 8
177 you need to know that WAV files use SF_FORMAT_PCM_U8 and AIFF
178 files use SF_FORMAT_PCM_S8. That's all there is to it.
179
180 Erik
181 --
182 +-----------------------------------------------------------+
183 Erik de Castro Lopo [email protected]
184 +-----------------------------------------------------------+
185 "Python addresses true pseudocode's two major failings: that it
186 isn't standardized, and it isn't executable."
187 - Grant R. Griffin in comp.dsp
188
189 ================================================================================
190
191 Subject: Re: Can you help with a problem?
192 From: PFJ <[email protected]>
193 To: Erik de Castro Lopo <[email protected]>
194 Date: Wed, 21 Jul 2004 09:50:55 +0100
195
196 Hi Erik,
197
198 > > I know, but the source code explicitly has SF_FORMAT_PCM which given the
199 > > code afterwards would equate to one of the above, but given that PCM
200 > > files can have a varied bitwidth the author probably wanted to cover all
201 > > bases.
202 >
203 > But surely the existing code does something like:
204 >
205 > sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM;
206 > sfinfo.pcmbitwidth = 16;
207
208 If only!
209
210 The actual code is this
211
212 int LoadSoundFile(char *filename, sound_p sound)
213 {
214 SNDFILE *file;
215 SF_INFO file_info;
216 short *buffer_short = NULL;
217 u_int8_t *buffer_8 = NULL;
218 int16_t *buffer_16 = NULL;
219 unsigned int i;
220
221 /* Open the file and retrieve sample information. */
222 file = sf_open_read(filename, &file_info);
223 // I've sorted this one already - PFJ
224
225 /* Make sure the format is acceptable. */
226 if ((file_info.format & 0x0F) != SF_FORMAT_PCM) {
227 printf("'%s' is not a PCM-based audio file.\n", filename);
228 sf_close(file);
229 return -1;
230 }
231
232 if ((file_info.pcmbitwidth == 8) && (file_info.channels == 1)) {
233 sound->format = AL_FORMAT_MONO8;
234 } else if ((file_info.pcmbitwidth == 8) && (file_info.channels == 2)) {
235 sound->format = AL_FORMAT_STEREO8;
236 } else if ((file_info.pcmbitwidth == 16) && (file_info.channels == 1)) {
237 sound->format = AL_FORMAT_MONO16;
238 } else if ((file_info.pcmbitwidth == 16) && (file_info.channels == 2)) {
239 sound->format = AL_FORMAT_STEREO16;
240 } else {
241 printf("Unknown sample format in %s.\n", filename);
242 sf_close(file);
243 return -1;
244 }
245
246 /* Allocate buffers. */
247 buffer_short = (short *)malloc(file_info.samples * file_info.channels * sizeof (short));
248
249 buffer_8 = (u_int8_t *)malloc(file_info.samples * file_info.channels * file_info.pcmbitwidth / 8);
250
251 buffer_16 = (int16_t *)buffer_8;
252
253 if (buffer_short == NULL || buffer_8 == NULL) {
254 printf("Unable to allocate enough memory for '%s'.\n", filename);
255 goto error_cleanup;
256 }
257
258 /* Read the entire sound file. */
259 if (sf_readf_short(file,buffer_short,file_info.samples) == (size_t)-1) {
260 printf("Error while reading samples from '%s'.\n", filename);
261 goto error_cleanup;
262 }
263
264 <minor snip>
265
266 /* Fill in the sound data structure. */
267 sound->freq = file_info.samplerate;
268 sound->size = file_info.samples * file_info.channels * file_info.pcmbitwidth / 8;
269
270 /* Give our sound data to OpenAL. */
271 alGenBuffers(1, &sound->name);
272 if (alGetError() != AL_NO_ERROR) {
273 printf("Error creating an AL buffer name for %s.\n", filename);
274 goto error_cleanup;
275 }
276
277 alBufferData(sound->name, sound->format, buffer_8, sound->size,sound->freq);
278 if (alGetError() != AL_NO_ERROR) {
279 printf("Error sending buffer data to OpenAL for %s.\n", filename);
280 goto error_cleanup;
281 }
282
283 /* Close the file and return success. */
284 sf_close(file);
285 free(buffer_short);
286 free(buffer_8);
287
288 return 0;
289
290 error_cleanup:
291 if (file != NULL) fclose(file);
292 free(buffer_short);
293 free(buffer_8);
294 return -1;
295 }
296
297 As you can see, the PCM material in the listing will not currently
298 compile and for the other sndfile material, it probably won't either.
299
300 Any help would be appreciated.
301
302 TTFN
303
304 Paul
305
306 ================================================================================
307
308 From: Erik de Castro Lopo <[email protected]>
309 To: PFJ <[email protected]>
310 Subject: Re: Can you help with a problem?
311 Date: Wed, 21 Jul 2004 19:36:46 +1000
312
313 On Wed, 21 Jul 2004 09:50:55 +0100
314 PFJ <[email protected]> wrote:
315
316 > Hi Erik,
317 >
318 > > > I know, but the source code explicitly has SF_FORMAT_PCM which given the
319 > > > code afterwards would equate to one of the above, but given that PCM
320 > > > files can have a varied bitwidth the author probably wanted to cover all
321 > > > bases.
322 > >
323 > > But surely the existing code does something like:
324 > >
325 > > sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM;
326 > > sfinfo.pcmbitwidth = 16;
327 >
328 > If only!
329
330 No, really.
331
332 Drop this completely:
333
334 > /* Make sure the format is acceptable. */
335 > if ((file_info.format & 0x0F) != SF_FORMAT_PCM) {
336 > printf("'%s' is not a PCM-based audio file.\n", filename);
337 > sf_close(file);
338 > return -1;
339 > }
340
341 Replace this block:
342
343 > if ((file_info.pcmbitwidth == 8) && (file_info.channels == 1)) {
344 > sound->format = AL_FORMAT_MONO8;
345 > } else if ((file_info.pcmbitwidth == 8) && (file_info.channels == 2)) {
346 > sound->format = AL_FORMAT_STEREO8;
347 > } else if ((file_info.pcmbitwidth == 16) && (file_info.channels == 1)) {
348 > sound->format = AL_FORMAT_MONO16;
349 > } else if ((file_info.pcmbitwidth == 16) && (file_info.channels == 2)) {
350 > sound->format = AL_FORMAT_STEREO16;
351 > } else {
352 > printf("Unknown sample format in %s.\n", filename);
353 > sf_close(file);
354 > return -1;
355 > }
356
357 with:
358
359 int pcmbitwidth = 0;
360
361 if (file_info.format & SF_FORMAT_SUBMASK != SF_FORMAT_PCM_16)
362 { printf("'%s' is not a PCM-based audio file.\n", filename);
363 sf_close(file);
364 return -1;
365 }
366
367 if (file_info.channels < 1 || file_info.channels > 2)
368 { printf("'%s' bad channel count.\n", filename);
369 sf_close(file);
370 return -1;
371 }
372
373 switch (file_info.format & SF_FORMAT_SUBMASK + file_info.channels << 16)
374 { case (SF_FORMAT_PCM_U8 + 1 << 16):
375 sound->format = AL_FORMAT_MONO8;
376 pcmbitwidth = 8;
377 break;
378 case (SF_FORMAT_PCM_U8 + 2 << 16):
379 sound->format = AL_FORMAT_STEREO8;
380 pcmbitwidth = 8;
381 break;
382 case (SF_FORMAT_PCM_16 + 1 << 16):
383 sound->format = AL_FORMAT_MONO16;
384 pcmbitwidth = 16;
385 break;
386 case (SF_FORMAT_PCM_16 + 2 << 16):
387 sound->format = AL_FORMAT_STEREO16;
388 pcmbitwidth = 16;
389 break;
390 default:
391 printf("Unknown sample format in %s.\n", filename);
392 sf_close(file);
393 return -1;
394 }
395
396 > /* Allocate buffers. */
397 > buffer_short = (short *)malloc(file_info.samples *
398 > file_info.channels *
399 > sizeof (short));
400 >
401 > buffer_8 = (u_int8_t *)malloc(file_info.samples *
402 > file_info.channels *
403 > file_info.pcmbitwidth / 8);
404
405 Use pcmbitwidth as calculated above.
406
407 > buffer_16 = (int16_t *)buffer_8;
408 >
409 > if (buffer_short == NULL || buffer_8 == NULL) {
410 > printf("Unable to allocate enough memory for '%s'.\n", filename);
411 > goto error_cleanup;
412 > }
413 >
414 > /* Read the entire sound file. */
415 > if (sf_readf_short(file,buffer_short,file_info.samples) == (size_t)- 1) {
416
417 Replace "(size_t) - 1" with " < 0".
418
419 > As you can see, the PCM material in the listing will not currently
420 > compile and for the other sndfile material, it probably won't either.
421
422 None of the changes above should have been very difficult to figure
423 out.
424
425 Erik
426 --
427 +-----------------------------------------------------------+
428 Erik de Castro Lopo [email protected]
429 +-----------------------------------------------------------+
430 Microsoft is finally bringing all of its Windows operating system families
431 under one roof. It will combine all of the features of CE, stability and
432 support of ME and the speed of NT.
433 It will be called Windows CEMENT...
434