• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6Date: Tue, 20 Jul 2004 22:49:21 +0100
7From: Paul <paul@fake-domain-name.co.uk>
8To: erikd@fake-domain-name.com
9Subject: Can you help with a problem?
10Date: Tue, 20 Jul 2004 22:49:21 +0100
11
12Hi,
13
14I'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
17While I can guess some of the newer versions of function calls and
18enumerations, there are some which I cannot guess.
19
20Would you be able to translate them to the current version of
21enumeration and function calls so that I can update the source?
22
23These 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
29There are probably more. I'm happy to send you the source files for
30sound calls, scan the pages or anything else. Failing that, is there
31somewhere with the changes listed so I can try and fix the code for myself?
32
33Thanks
34
35TTFN
36
37Paul
38
39================================================================================
40
41Date: Wed, 21 Jul 2004 17:38:08 +1000
42From: Erik de Castro Lopo <erikd@fake-domain-name.com>
43To: Paul <paul@fake-domain-name.co.uk>
44Subject: Re: Can you help with a problem?
45
46On Tue, 20 Jul 2004 22:49:21 +0100
47Paul <paul@fake-domain-name.co.uk> 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
65yes.
66
67> SF_FORMAT_PCM (guess: either SF_FORMAT_PCM_U8 or _RAW)
68
69Actually 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
79WIth 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
86Version 1.0.0 came out some time ago, but I think this:
87
88    http://www.mega-nerd.com/libsndfile/version-1.html
89
90lists most of the changes. You should also look at the API docs:
91
92    http://www.mega-nerd.com/libsndfile/api.html
93
94HTH,
95Erik
96--
97+-----------------------------------------------------------+
98  Erik de Castro Lopo      nospam@fake-domain-name.com
99+-----------------------------------------------------------+
100"There is no reason why anyone would want a computer in their home"
101Ken Olson, DEC, 1977
102
103================================================================================
104
105From: PFJ <paul@fake-domain-name.co.uk>
106To: Erik de Castro Lopo <erikd@fake-domain-name.com>
107Subject: Re: Can you help with a problem?
108Date: Wed, 21 Jul 2004 09:07:39 +0100
109
110
111Hi Erik,
112
113Thanks 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
119Yay!
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
131I know, but the source code explicitly has SF_FORMAT_PCM which given the
132code afterwards would equate to one of the above, but given that PCM
133files can have a varied bitwidth the author probably wanted to cover all
134bases.
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
144I'll download them and see what I can gleen.
145
146Thanks again for getting back to me
147
148TTFN
149
150Paul
151
152================================================================================
153
154Date: Wed, 21 Jul 2004 18:20:29 +1000
155From: Erik de Castro Lopo <erikd@fake-domain-name.com>
156To: PFJ <paul@fake-domain-name.co.uk>
157Subject: Re: Can you help with a problem?
158
159On Wed, 21 Jul 2004 09:07:39 +0100
160PFJ <paul@fake-domain-name.co.uk> 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
167But surely the existing code does something like:
168
169    sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM;
170    sfinfo.pcmbitwidth = 16;
171
172which can be directly translated to:
173
174    sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
175
176and the same for pcmbitwitdhs of 24 and 32. For pcmbitwidth of 8
177you need to know that WAV files use SF_FORMAT_PCM_U8 and AIFF
178files use SF_FORMAT_PCM_S8. Thats all there is to it.
179
180Erik
181--
182+-----------------------------------------------------------+
183  Erik de Castro Lopo      nospam@fake-domain-name.com
184+-----------------------------------------------------------+
185"Python addresses true pseudocode's two major failings: that it
186isn't standardized, and it isn't executable."
187- Grant R. Griffin  in comp.dsp
188
189================================================================================
190
191Subject: Re: Can you help with a problem?
192From: PFJ <paul@fake-domain-name.co.uk>
193To: Erik de Castro Lopo <erikd@fake-domain-name.com>
194Date: Wed, 21 Jul 2004 09:50:55 +0100
195
196Hi 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
208If only!
209
210The actual code is this
211
212int 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
297As you can see, the PCM material in the listing will not currently
298compile and for the other sndfile material, it probably won't either.
299
300Any help would be appreciated.
301
302TTFN
303
304Paul
305
306================================================================================
307
308From: Erik de Castro Lopo <erikd@fake-domain-name.com>
309To: PFJ <paul@fake-domain-name.co.uk>
310Subject: Re: Can you help with a problem?
311Date: Wed, 21 Jul 2004 19:36:46 +1000
312
313On Wed, 21 Jul 2004 09:50:55 +0100
314PFJ <paul@fake-domain-name.co.uk> 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
330No, really.
331
332Drop 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
341Replace 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
357with:
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
405Use 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
417Replace "(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
422None of the changes above should have been very difficult to figure
423out.
424
425Erik
426--
427+-----------------------------------------------------------+
428  Erik de Castro Lopo      nospam@fake-domain-name.com
429+-----------------------------------------------------------+
430Microsoft is finally bringing all of its Windows operating system families
431under one roof. It will combine all of the features of CE, stability and
432support of ME and the speed of NT.
433It will be called Windows CEMENT...
434
435