linux_games_programming.txt 13 KB

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