gosu 0.15.2 → 1.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  2. data/dependencies/SDL/include/SDL.h +138 -0
  3. data/dependencies/SDL/include/SDL_assert.h +293 -0
  4. data/dependencies/SDL/include/SDL_atomic.h +295 -0
  5. data/dependencies/SDL/include/SDL_audio.h +859 -0
  6. data/dependencies/SDL/include/SDL_bits.h +121 -0
  7. data/dependencies/SDL/include/SDL_blendmode.h +123 -0
  8. data/dependencies/SDL/include/SDL_clipboard.h +71 -0
  9. data/dependencies/SDL/include/SDL_config.h +55 -0
  10. data/dependencies/SDL/include/SDL_config_android.h +182 -0
  11. data/dependencies/SDL/include/SDL_config_iphoneos.h +207 -0
  12. data/dependencies/SDL/include/SDL_config_macosx.h +266 -0
  13. data/dependencies/SDL/include/SDL_config_minimal.h +85 -0
  14. data/dependencies/SDL/include/SDL_config_os2.h +188 -0
  15. data/dependencies/SDL/include/SDL_config_pandora.h +135 -0
  16. data/dependencies/SDL/include/SDL_config_psp.h +165 -0
  17. data/dependencies/SDL/include/SDL_config_windows.h +288 -0
  18. data/dependencies/SDL/include/SDL_config_winrt.h +243 -0
  19. data/dependencies/SDL/include/SDL_config_wiz.h +149 -0
  20. data/dependencies/SDL/include/SDL_copying.h +20 -0
  21. data/dependencies/SDL/include/SDL_cpuinfo.h +299 -0
  22. data/dependencies/SDL/include/SDL_egl.h +1676 -0
  23. data/dependencies/SDL/include/SDL_endian.h +263 -0
  24. data/dependencies/SDL/include/SDL_error.h +112 -0
  25. data/dependencies/SDL/include/SDL_events.h +827 -0
  26. data/dependencies/SDL/include/SDL_filesystem.h +136 -0
  27. data/dependencies/SDL/include/SDL_gamecontroller.h +541 -0
  28. data/dependencies/SDL/include/SDL_gesture.h +87 -0
  29. data/dependencies/SDL/include/SDL_haptic.h +1247 -0
  30. data/dependencies/SDL/include/SDL_hints.h +1578 -0
  31. data/dependencies/SDL/include/SDL_joystick.h +499 -0
  32. data/dependencies/SDL/include/SDL_keyboard.h +217 -0
  33. data/dependencies/SDL/include/SDL_keycode.h +351 -0
  34. data/dependencies/SDL/include/SDL_loadso.h +81 -0
  35. data/dependencies/SDL/include/SDL_locale.h +101 -0
  36. data/dependencies/SDL/include/SDL_log.h +211 -0
  37. data/dependencies/SDL/include/SDL_main.h +180 -0
  38. data/dependencies/SDL/include/SDL_messagebox.h +146 -0
  39. data/dependencies/SDL/include/SDL_metal.h +117 -0
  40. data/dependencies/SDL/include/SDL_misc.h +75 -0
  41. data/dependencies/SDL/include/SDL_mouse.h +302 -0
  42. data/dependencies/SDL/include/SDL_mutex.h +251 -0
  43. data/dependencies/SDL/include/SDL_name.h +33 -0
  44. data/dependencies/SDL/include/SDL_opengl.h +2183 -0
  45. data/dependencies/SDL/include/SDL_opengl_glext.h +11180 -0
  46. data/dependencies/SDL/include/SDL_opengles.h +39 -0
  47. data/dependencies/SDL/include/SDL_opengles2.h +52 -0
  48. data/dependencies/SDL/include/SDL_opengles2_gl2.h +621 -0
  49. data/dependencies/SDL/include/SDL_opengles2_gl2ext.h +2050 -0
  50. data/dependencies/SDL/include/SDL_opengles2_gl2platform.h +30 -0
  51. data/dependencies/SDL/include/SDL_opengles2_khrplatform.h +282 -0
  52. data/dependencies/SDL/include/SDL_pixels.h +479 -0
  53. data/dependencies/SDL/include/SDL_platform.h +198 -0
  54. data/dependencies/SDL/include/SDL_power.h +75 -0
  55. data/dependencies/SDL/include/SDL_quit.h +58 -0
  56. data/dependencies/SDL/include/SDL_rect.h +174 -0
  57. data/dependencies/SDL/include/SDL_render.h +1158 -0
  58. data/dependencies/SDL/include/SDL_revision.h +2 -0
  59. data/dependencies/SDL/include/SDL_rwops.h +283 -0
  60. data/dependencies/SDL/include/SDL_scancode.h +413 -0
  61. data/dependencies/SDL/include/SDL_sensor.h +267 -0
  62. data/dependencies/SDL/include/SDL_shape.h +144 -0
  63. data/dependencies/SDL/include/SDL_stdinc.h +647 -0
  64. data/dependencies/SDL/include/SDL_surface.h +563 -0
  65. data/dependencies/SDL/include/SDL_system.h +325 -0
  66. data/dependencies/SDL/include/SDL_syswm.h +354 -0
  67. data/dependencies/SDL/include/SDL_test.h +69 -0
  68. data/dependencies/SDL/include/SDL_test_assert.h +105 -0
  69. data/dependencies/SDL/include/SDL_test_common.h +218 -0
  70. data/dependencies/SDL/include/SDL_test_compare.h +69 -0
  71. data/dependencies/SDL/include/SDL_test_crc32.h +124 -0
  72. data/dependencies/SDL/include/SDL_test_font.h +81 -0
  73. data/dependencies/SDL/include/SDL_test_fuzzer.h +384 -0
  74. data/dependencies/SDL/include/SDL_test_harness.h +134 -0
  75. data/dependencies/SDL/include/SDL_test_images.h +78 -0
  76. data/dependencies/SDL/include/SDL_test_log.h +67 -0
  77. data/dependencies/SDL/include/SDL_test_md5.h +129 -0
  78. data/dependencies/SDL/include/SDL_test_memory.h +63 -0
  79. data/dependencies/SDL/include/SDL_test_random.h +115 -0
  80. data/dependencies/SDL/include/SDL_thread.h +366 -0
  81. data/dependencies/SDL/include/SDL_timer.h +115 -0
  82. data/dependencies/SDL/include/SDL_touch.h +102 -0
  83. data/dependencies/SDL/include/SDL_types.h +29 -0
  84. data/dependencies/SDL/include/SDL_version.h +162 -0
  85. data/dependencies/SDL/include/SDL_video.h +1282 -0
  86. data/dependencies/SDL/include/SDL_vulkan.h +276 -0
  87. data/dependencies/SDL/include/begin_code.h +166 -0
  88. data/dependencies/SDL/include/close_code.h +40 -0
  89. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  90. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  91. data/dependencies/SDL_sound/SDL_sound.c +795 -0
  92. data/dependencies/SDL_sound/SDL_sound.h +725 -0
  93. data/dependencies/SDL_sound/SDL_sound_aiff.c +537 -0
  94. data/dependencies/SDL_sound/SDL_sound_au.c +352 -0
  95. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +747 -0
  96. data/dependencies/SDL_sound/SDL_sound_flac.c +182 -0
  97. data/dependencies/SDL_sound/SDL_sound_internal.h +304 -0
  98. data/dependencies/SDL_sound/SDL_sound_modplug.c +228 -0
  99. data/dependencies/SDL_sound/SDL_sound_mp3.c +184 -0
  100. data/dependencies/SDL_sound/SDL_sound_raw.c +164 -0
  101. data/dependencies/SDL_sound/SDL_sound_shn.c +1309 -0
  102. data/dependencies/SDL_sound/SDL_sound_voc.c +550 -0
  103. data/dependencies/SDL_sound/SDL_sound_vorbis.c +223 -0
  104. data/dependencies/SDL_sound/SDL_sound_wav.c +783 -0
  105. data/dependencies/SDL_sound/dr_flac.h +5906 -0
  106. data/dependencies/SDL_sound/dr_mp3.h +2832 -0
  107. data/dependencies/SDL_sound/libmodplug/fastmix.c +1748 -0
  108. data/dependencies/SDL_sound/libmodplug/libmodplug.h +1001 -0
  109. data/dependencies/SDL_sound/libmodplug/load_669.c +188 -0
  110. data/dependencies/SDL_sound/libmodplug/load_abc.c +4725 -0
  111. data/dependencies/SDL_sound/libmodplug/load_amf.c +403 -0
  112. data/dependencies/SDL_sound/libmodplug/load_ams.c +587 -0
  113. data/dependencies/SDL_sound/libmodplug/load_dbm.c +357 -0
  114. data/dependencies/SDL_sound/libmodplug/load_dmf.c +531 -0
  115. data/dependencies/SDL_sound/libmodplug/load_dsm.c +232 -0
  116. data/dependencies/SDL_sound/libmodplug/load_far.c +253 -0
  117. data/dependencies/SDL_sound/libmodplug/load_it.c +796 -0
  118. data/dependencies/SDL_sound/libmodplug/load_mdl.c +488 -0
  119. data/dependencies/SDL_sound/libmodplug/load_med.c +757 -0
  120. data/dependencies/SDL_sound/libmodplug/load_mid.c +1405 -0
  121. data/dependencies/SDL_sound/libmodplug/load_mod.c +269 -0
  122. data/dependencies/SDL_sound/libmodplug/load_mt2.c +546 -0
  123. data/dependencies/SDL_sound/libmodplug/load_mtm.c +142 -0
  124. data/dependencies/SDL_sound/libmodplug/load_okt.c +192 -0
  125. data/dependencies/SDL_sound/libmodplug/load_pat.c +1143 -0
  126. data/dependencies/SDL_sound/libmodplug/load_pat.h +25 -0
  127. data/dependencies/SDL_sound/libmodplug/load_psm.c +350 -0
  128. data/dependencies/SDL_sound/libmodplug/load_ptm.c +204 -0
  129. data/dependencies/SDL_sound/libmodplug/load_s3m.c +325 -0
  130. data/dependencies/SDL_sound/libmodplug/load_stm.c +180 -0
  131. data/dependencies/SDL_sound/libmodplug/load_ult.c +206 -0
  132. data/dependencies/SDL_sound/libmodplug/load_umx.c +51 -0
  133. data/dependencies/SDL_sound/libmodplug/load_xm.c +554 -0
  134. data/dependencies/SDL_sound/libmodplug/mmcmp.c +382 -0
  135. data/dependencies/SDL_sound/libmodplug/modplug.c +170 -0
  136. data/dependencies/SDL_sound/libmodplug/modplug.h +90 -0
  137. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +301 -0
  138. data/dependencies/SDL_sound/libmodplug/snd_flt.c +63 -0
  139. data/dependencies/SDL_sound/libmodplug/snd_fx.c +2350 -0
  140. data/dependencies/SDL_sound/libmodplug/sndfile.c +1169 -0
  141. data/dependencies/SDL_sound/libmodplug/sndmix.c +1034 -0
  142. data/dependencies/SDL_sound/libmodplug/tables.h +371 -0
  143. data/{src/stb_vorbis.c → dependencies/SDL_sound/stb_vorbis.h} +143 -78
  144. data/dependencies/al_soft/AL/al.h +655 -0
  145. data/dependencies/al_soft/AL/alc.h +270 -0
  146. data/dependencies/al_soft/AL/alext.h +585 -0
  147. data/dependencies/al_soft/AL/efx-creative.h +3 -0
  148. data/dependencies/al_soft/AL/efx-presets.h +402 -0
  149. data/dependencies/al_soft/AL/efx.h +762 -0
  150. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  151. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  152. data/{src → dependencies/stb}/stb_image.h +330 -127
  153. data/{src → dependencies/stb}/stb_image_write.h +156 -85
  154. data/{src → dependencies/stb}/stb_truetype.h +192 -69
  155. data/{src → dependencies/utf8proc}/utf8proc.c +0 -0
  156. data/{src → dependencies/utf8proc}/utf8proc.h +0 -0
  157. data/{src → dependencies/utf8proc}/utf8proc_data.h +0 -0
  158. data/ext/gosu/extconf.rb +56 -22
  159. data/{Gosu → include/Gosu}/Audio.hpp +6 -8
  160. data/{Gosu → include/Gosu}/AutoLink.hpp +0 -0
  161. data/include/Gosu/Bitmap.hpp +100 -0
  162. data/{Gosu → include/Gosu}/Buttons.hpp +94 -35
  163. data/{Gosu → include/Gosu}/Channel.h +0 -0
  164. data/{Gosu → include/Gosu}/Color.h +0 -0
  165. data/{Gosu → include/Gosu}/Color.hpp +0 -0
  166. data/{Gosu → include/Gosu}/Directories.hpp +0 -0
  167. data/{Gosu → include/Gosu}/Font.h +0 -0
  168. data/{Gosu → include/Gosu}/Font.hpp +0 -0
  169. data/{Gosu → include/Gosu}/Fwd.hpp +0 -0
  170. data/{Gosu → include/Gosu}/Gosu.h +3 -0
  171. data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
  172. data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
  173. data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
  174. data/{Gosu → include/Gosu}/IO.hpp +0 -0
  175. data/{Gosu → include/Gosu}/Image.h +0 -0
  176. data/{Gosu → include/Gosu}/Image.hpp +7 -6
  177. data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
  178. data/{Gosu → include/Gosu}/Input.hpp +30 -15
  179. data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
  180. data/{Gosu → include/Gosu}/Math.hpp +0 -0
  181. data/{Gosu → include/Gosu}/Platform.hpp +0 -0
  182. data/{Gosu → include/Gosu}/Sample.h +0 -0
  183. data/{Gosu → include/Gosu}/Song.h +0 -0
  184. data/{Gosu → include/Gosu}/Text.hpp +0 -0
  185. data/{Gosu → include/Gosu}/TextInput.h +0 -0
  186. data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
  187. data/{Gosu → include/Gosu}/Timing.hpp +0 -0
  188. data/{Gosu → include/Gosu}/Utility.hpp +1 -1
  189. data/{Gosu → include/Gosu}/Version.hpp +0 -0
  190. data/{Gosu → include/Gosu}/Window.h +2 -0
  191. data/{Gosu → include/Gosu}/Window.hpp +21 -13
  192. data/lib/OpenAL32.dll +0 -0
  193. data/lib/SDL2.dll +0 -0
  194. data/lib/gosu.rb +0 -3
  195. data/lib/gosu/patches.rb +0 -9
  196. data/lib/gosu/swig_patches.rb +3 -2
  197. data/lib/libmpg123.dll +0 -0
  198. data/lib/libsndfile.dll +0 -0
  199. data/lib64/OpenAL32.dll +0 -0
  200. data/lib64/SDL2.dll +0 -0
  201. data/lib64/libmpg123.dll +0 -0
  202. data/lib64/libsndfile.dll +0 -0
  203. data/rdoc/gosu.rb +95 -20
  204. data/src/Audio.cpp +50 -224
  205. data/src/AudioFile.hpp +17 -37
  206. data/src/AudioFileAudioToolbox.cpp +237 -0
  207. data/src/AudioFileSDLSound.cpp +147 -0
  208. data/src/AudioImpl.cpp +3 -12
  209. data/src/AudioImpl.hpp +3 -1
  210. data/src/Bitmap.cpp +85 -83
  211. data/src/BitmapIO.cpp +52 -58
  212. data/src/Constants.cpp +80 -33
  213. data/src/Font.cpp +3 -1
  214. data/src/GosuWrapper.cpp +19 -0
  215. data/src/Graphics.cpp +7 -4
  216. data/src/Image.cpp +13 -16
  217. data/src/Input.cpp +408 -159
  218. data/src/LargeImageData.cpp +1 -1
  219. data/src/MarkupParser.cpp +2 -1
  220. data/src/RubyGosu.cxx +349 -83
  221. data/src/RubyGosu.h +4 -2
  222. data/src/TexChunk.cpp +1 -1
  223. data/src/TextBuilder.cpp +3 -1
  224. data/src/Texture.cpp +1 -1
  225. data/src/TrueTypeFont.cpp +1 -1
  226. data/src/Utility.cpp +11 -7
  227. data/src/Window.cpp +30 -39
  228. data/src/WindowWrapper.cpp +28 -0
  229. metadata +207 -52
  230. data/Gosu/Bitmap.hpp +0 -113
  231. data/src/AudioToolboxFile.hpp +0 -210
  232. data/src/OggFile.hpp +0 -92
  233. data/src/SndFile.hpp +0 -174
  234. data/src/WinMain.cpp +0 -64
@@ -0,0 +1,1309 @@
1
+ /**
2
+ * SDL_sound; A sound processing toolkit.
3
+ *
4
+ * Please see the file LICENSE.txt in the source's root directory.
5
+ *
6
+ * This file written by Ryan C. Gordon.
7
+ */
8
+
9
+ /*
10
+ * Shorten decoder for SDL_sound.
11
+ *
12
+ * This driver handles Shorten-compressed waveforms. Despite the fact that
13
+ * SHNs tend to be much bigger than MP3s, they are still the de facto
14
+ * standard in online music trading communities. If an MP3 crunches the
15
+ * waveform to 10-20 percent of its original size, SHNs only go to about
16
+ * 50-60%. Why do the Phish fans of the world use this format then? Rabid
17
+ * music traders appreciate the sound quality; SHNs, unlike MP3s, do not
18
+ * throw away any part of the waveform. Yes, there are people that notice
19
+ * this, and further more, they demand it...and if they can't get a good
20
+ * transfer of those larger files over the 'net, they haven't underestimated
21
+ * the bandwidth of CDs travelling the world through the postal system.
22
+ *
23
+ * Shorten homepage: http://www.softsound.com/Shorten.html
24
+ *
25
+ * !!! FIXME: softsound.com is gone, I think.
26
+ *
27
+ * The Shorten format was gleaned from the shorten codebase, by Tony
28
+ * Robinson and SoftSound Limited.
29
+ */
30
+
31
+ #define __SDL_SOUND_INTERNAL__
32
+ #include "SDL_sound_internal.h"
33
+
34
+ #if SOUND_SUPPORTS_SHN
35
+
36
+ #define SHN_BUFSIZ 512
37
+
38
+ typedef struct
39
+ {
40
+ Sint32 version;
41
+ Sint32 datatype;
42
+ Sint32 nchan;
43
+ Sint32 blocksize;
44
+ Sint32 maxnlpc;
45
+ Sint32 nmean;
46
+ Sint32 nwrap;
47
+ Sint32 **buffer;
48
+ Sint32 **offset;
49
+ Sint32 *qlpc;
50
+ Sint32 lpcqoffset;
51
+ Sint32 bitshift;
52
+ int nbitget;
53
+ int nbyteget;
54
+ Uint8 *getbuf;
55
+ Uint8 *getbufp;
56
+ Uint32 gbuffer;
57
+ Uint8 *backBuffer;
58
+ Uint32 backBufferSize;
59
+ Uint32 backBufLeft;
60
+ Uint32 start_pos;
61
+ } shn_t;
62
+
63
+
64
+ static const Uint32 mask_table[] =
65
+ {
66
+ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F,
67
+ 0x0000003F, 0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
68
+ 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF,
69
+ 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
70
+ 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF,
71
+ 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
72
+ };
73
+
74
+
75
+ static const Uint8 ulaw_outward[13][256] = {
76
+ {127,0,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,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128},
77
+ {112,114,116,118,120,122,124,126,127,0,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,113,115,117,119,121,123,125,255,253,251,249,247,245,243,241,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,252,250,248,246,244,242,240},
78
+ {96,98,100,102,104,106,108,110,112,113,114,116,117,118,120,121,122,124,125,126,127,0,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,97,99,101,103,105,107,109,111,115,119,123,255,251,247,243,239,237,235,233,231,229,227,225,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,250,249,248,246,245,244,242,241,240,238,236,234,232,230,228,226,224},
79
+ {80,82,84,86,88,90,92,94,96,97,98,100,101,102,104,105,106,108,109,110,112,113,114,115,116,117,118,120,121,122,123,124,125,126,127,0,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,81,83,85,87,89,91,93,95,99,103,107,111,119,255,247,239,235,231,227,223,221,219,217,215,213,211,209,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,246,245,244,243,242,241,240,238,237,236,234,233,232,230,229,228,226,225,224,222,220,218,216,214,212,210,208},
80
+ {64,66,68,70,72,74,76,78,80,81,82,84,85,86,88,89,90,92,93,94,96,97,98,99,100,101,102,104,105,106,107,108,109,110,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,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,65,67,69,71,73,75,77,79,83,87,91,95,103,111,255,239,231,223,219,215,211,207,205,203,201,199,197,195,193,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,238,237,236,235,234,233,232,230,229,228,227,226,225,224,222,221,220,218,217,216,214,213,212,210,209,208,206,204,202,200,198,196,194,192},
81
+ {49,51,53,55,57,59,61,63,64,66,67,68,70,71,72,74,75,76,78,79,80,81,82,84,85,86,87,88,89,90,92,93,94,95,96,97,98,99,100,101,102,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,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,50,52,54,56,58,60,62,65,69,73,77,83,91,103,255,231,219,211,205,201,197,193,190,188,186,184,182,180,178,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,230,229,228,227,226,225,224,223,222,221,220,218,217,216,215,214,213,212,210,209,208,207,206,204,203,202,200,199,198,196,195,194,192,191,189,187,185,183,181,179,177},
82
+ {32,34,36,38,40,42,44,46,48,49,51,52,53,55,56,57,59,60,61,63,64,65,66,67,68,70,71,72,73,74,75,76,78,79,80,81,82,83,84,85,86,87,88,89,90,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,0,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,33,35,37,39,41,43,45,47,50,54,58,62,69,77,91,255,219,205,197,190,186,182,178,175,173,171,169,167,165,163,161,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,218,217,216,215,214,213,212,211,210,209,208,207,206,204,203,202,201,200,199,198,196,195,194,193,192,191,189,188,187,185,184,183,181,180,179,177,176,174,172,170,168,166,164,162,160},
83
+ {16,18,20,22,24,26,28,30,32,33,34,36,37,38,40,41,42,44,45,46,48,49,50,51,52,53,55,56,57,58,59,60,61,63,64,65,66,67,68,69,70,71,72,73,74,75,76,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,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,19,21,23,25,27,29,31,35,39,43,47,54,62,77,255,205,190,182,175,171,167,163,159,157,155,153,151,149,147,145,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,204,203,202,201,200,199,198,197,196,195,194,193,192,191,189,188,187,186,185,184,183,181,180,179,178,177,176,174,173,172,170,169,168,166,165,164,162,161,160,158,156,154,152,150,148,146,144},
84
+ {2,4,6,8,10,12,14,16,17,18,20,21,22,24,25,26,28,29,30,32,33,34,35,36,37,38,40,41,42,43,44,45,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,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,0,1,3,5,7,9,11,13,15,19,23,27,31,39,47,62,255,190,175,167,159,155,151,147,143,141,139,137,135,133,131,129,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,189,188,187,186,185,184,183,182,181,180,179,178,177,176,174,173,172,171,170,169,168,166,165,164,163,162,161,160,158,157,156,154,153,152,150,149,148,146,145,144,142,140,138,136,134,132,130,128},
85
+ {1,2,4,5,6,8,9,10,12,13,14,16,17,18,19,20,21,22,24,25,26,27,28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,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,0,3,7,11,15,23,31,47,255,175,159,151,143,139,135,131,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,158,157,156,155,154,153,152,150,149,148,147,146,145,144,142,141,140,138,137,136,134,133,132,130,129,128},
86
+ {1,2,3,4,5,6,8,9,10,11,12,13,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,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,0,7,15,31,255,159,143,135,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,142,141,140,139,138,137,136,134,133,132,131,130,129,128},
87
+ {1,2,3,4,5,6,7,8,9,10,11,12,13,14,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,0,15,255,143,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128},
88
+ {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,0,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128}
89
+ };
90
+
91
+
92
+ #ifndef MIN_MACRO
93
+ #define MIN_MACRO(a,b) (((a)<(b))?(a):(b))
94
+ #endif
95
+
96
+ #ifndef MAX_MACRO
97
+ #define MAX_MACRO(a,b) (((a)>(b))?(a):(b))
98
+ #endif
99
+
100
+ #define POSITIVE_ULAW_ZERO 0xff
101
+ #define NEGATIVE_ULAW_ZERO 0x7f
102
+
103
+ #define CAPMAXSCHAR(x) ((x > 127) ? 127 : x)
104
+ #define CAPMAXUCHAR(x) ((x > 255) ? 255 : x)
105
+ #define CAPMAXSHORT(x) ((x > 32767) ? 32767 : x)
106
+ #define CAPMAXUSHORT(x) ((x > 65535) ? 65535 : x)
107
+
108
+ #define UNDEFINED_UINT -1
109
+ #define DEFAULT_BLOCK_SIZE 256
110
+ #define DEFAULT_V0NMEAN 0
111
+ #define DEFAULT_V2NMEAN 4
112
+ #define DEFAULT_MAXNLPC 0
113
+ #define DEFAULT_NCHAN 1
114
+ #define DEFAULT_NSKIP 0
115
+ #define DEFAULT_NDISCARD 0
116
+ #define NBITPERLONG 32
117
+ #define DEFAULT_MINSNR 256
118
+ #define DEFAULT_QUANTERROR 0
119
+ #define MINBITRATE 2.5
120
+
121
+ #define MEAN_VERSION0 0
122
+ #define MEAN_VERSION2 4
123
+
124
+ #define SHN_FN_DIFF0 0
125
+ #define SHN_FN_DIFF1 1
126
+ #define SHN_FN_DIFF2 2
127
+ #define SHN_FN_DIFF3 3
128
+ #define SHN_FN_QUIT 4
129
+ #define SHN_FN_BLOCKSIZE 5
130
+ #define SHN_FN_BITSHIFT 6
131
+ #define SHN_FN_QLPC 7
132
+ #define SHN_FN_ZERO 8
133
+ #define SHN_FN_VERBATIM 9
134
+
135
+ #define SHN_TYPE_AU1 0
136
+ #define SHN_TYPE_S8 1
137
+ #define SHN_TYPE_U8 2
138
+ #define SHN_TYPE_S16HL 3
139
+ #define SHN_TYPE_U16HL 4
140
+ #define SHN_TYPE_S16LH 5
141
+ #define SHN_TYPE_U16LH 6
142
+ #define SHN_TYPE_ULAW 7
143
+ #define SHN_TYPE_AU2 8
144
+ #define SHN_TYPE_AU3 9
145
+ #define SHN_TYPE_ALAW 10
146
+ #define SHN_TYPE_RIFF_WAVE 11
147
+ #define SHN_TYPE_EOF 12
148
+ #define SHN_TYPE_GENERIC_ULAW 128
149
+ #define SHN_TYPE_GENERIC_ALAW 129
150
+
151
+ #define SHN_FNSIZE 2
152
+ #define SHN_CHANNELSIZE 0
153
+ #define SHN_TYPESIZE 4
154
+ #define SHN_ULONGSIZE 2
155
+ #define SHN_NSKIPSIZE 1
156
+ #define SHN_LPCQSIZE 2
157
+ #define SHN_LPCQUANT 5
158
+ #define SHN_XBYTESIZE 7
159
+ #define SHN_VERBATIM_CKSIZE_SIZE 5
160
+ #define SHN_VERBATIM_BYTE_SIZE 8
161
+ #define SHN_ENERGYSIZE 3
162
+ #define SHN_BITSHIFTSIZE 2
163
+
164
+ #define SHN_LPCQOFFSET_VER2 (1 << SHN_LPCQUANT)
165
+
166
+
167
+ #define SHN_MAGIC 0x676B6A61 /* looks like "ajkg" as chars. */
168
+
169
+ #ifndef M_LN2
170
+ #define M_LN2 0.69314718055994530942
171
+ #endif
172
+
173
+ #ifndef M_PI
174
+ #define M_PI 3.14159265358979323846
175
+ #endif
176
+
177
+
178
+ static int word_get(shn_t *shn, SDL_RWops *rw, Uint32 *word)
179
+ {
180
+ if (shn->nbyteget < 4)
181
+ {
182
+ shn->nbyteget += SDL_RWread(rw, shn->getbuf, 1, SHN_BUFSIZ);
183
+ BAIL_IF_MACRO(shn->nbyteget < 4, NULL, 0);
184
+ shn->getbufp = shn->getbuf;
185
+ } /* if */
186
+
187
+ if (word != NULL)
188
+ {
189
+ *word = (((Sint32) shn->getbufp[0]) << 24) |
190
+ (((Sint32) shn->getbufp[1]) << 16) |
191
+ (((Sint32) shn->getbufp[2]) << 8) |
192
+ (((Sint32) shn->getbufp[3]) );
193
+ } /* if */
194
+
195
+ shn->getbufp += 4;
196
+ shn->nbyteget -= 4;
197
+
198
+ return 1;
199
+ } /* word_get */
200
+
201
+
202
+ static int uvar_get(int nbin, shn_t *shn, SDL_RWops *rw, Sint32 *word)
203
+ {
204
+ Sint32 result;
205
+
206
+ if (shn->nbitget == 0)
207
+ {
208
+ BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
209
+ shn->nbitget = 32;
210
+ } /* if */
211
+
212
+ for (result = 0; !(shn->gbuffer & (1L << --shn->nbitget)); result++)
213
+ {
214
+ if (shn->nbitget == 0)
215
+ {
216
+ BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
217
+ shn->nbitget = 32;
218
+ } /* if */
219
+ } /* for */
220
+
221
+ while (nbin != 0)
222
+ {
223
+ if (shn->nbitget >= nbin)
224
+ {
225
+ result = ( (result << nbin) |
226
+ ((shn->gbuffer >> (shn->nbitget - nbin)) &
227
+ mask_table[nbin]) );
228
+ shn->nbitget -= nbin;
229
+ break;
230
+ } /* if */
231
+ else
232
+ {
233
+ result = (result << shn->nbitget) |
234
+ (shn->gbuffer & mask_table[shn->nbitget]);
235
+ BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
236
+ nbin -= shn->nbitget;
237
+ shn->nbitget = 32;
238
+ } /* else */
239
+ } /* while */
240
+
241
+ if (word != NULL)
242
+ *word = result;
243
+
244
+ return 1;
245
+ } /* uvar_get */
246
+
247
+
248
+ static int var_get(int nbin, shn_t *shn, SDL_RWops *rw, Sint32 *word)
249
+ {
250
+ BAIL_IF_MACRO(!uvar_get(nbin + 1, shn, rw, word), NULL, 0);
251
+
252
+ if ((*word) & 1)
253
+ *word = (Sint32) ~((*word) >> 1);
254
+ else
255
+ *word = (Sint32) ((*word) >> 1);
256
+
257
+ return 1;
258
+ } /* var_get */
259
+
260
+
261
+ static int ulong_get(shn_t *shn, SDL_RWops *rw, Sint32 *word)
262
+ {
263
+ Sint32 nbit;
264
+ Sint32 retval;
265
+ BAIL_IF_MACRO(!uvar_get(SHN_ULONGSIZE, shn, rw, &nbit), NULL, 0);
266
+ BAIL_IF_MACRO(!uvar_get(nbit, shn, rw, &retval), NULL, 0);
267
+
268
+ if (word != NULL)
269
+ *word = retval;
270
+
271
+ return 1;
272
+ } /* ulong_get */
273
+
274
+
275
+ static SDL_INLINE int uint_get(int nbit, shn_t *shn, SDL_RWops *rw, Sint32 *w)
276
+ {
277
+ return (shn->version == 0) ? uvar_get(nbit, shn, rw, w) : ulong_get(shn, rw, w);
278
+ } /* uint_get */
279
+
280
+
281
+ static int SHN_init(void)
282
+ {
283
+ return 1; /* initialization always successful. */
284
+ } /* SHN_init */
285
+
286
+
287
+ static void SHN_quit(void)
288
+ {
289
+ /* it's a no-op. */
290
+ } /* SHN_quit */
291
+
292
+
293
+ /*
294
+ * Look through the whole file for a SHN magic number. This is costly, so
295
+ * it should only be done if the user SWEARS they have a Shorten stream...
296
+ */
297
+ static SDL_INLINE int extended_shn_magic_search(Sound_Sample *sample)
298
+ {
299
+ SDL_RWops *rw = ((Sound_SampleInternal *) sample->opaque)->rw;
300
+ Uint32 word = 0;
301
+ Uint8 ch;
302
+
303
+ while (1)
304
+ {
305
+ BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
306
+ word = ((word << 8) & 0xFFFFFF00) | ch;
307
+ if (SDL_SwapBE32(word) == SHN_MAGIC)
308
+ {
309
+ BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
310
+ return (int) ch;
311
+ } /* if */
312
+ } /* while */
313
+
314
+ return (int) ch;
315
+ } /* extended_shn_magic_search */
316
+
317
+
318
+ /* look for the magic number in the RWops and see what kind of file this is. */
319
+ static SDL_INLINE int determine_shn_version(Sound_Sample *sample,
320
+ const char *ext)
321
+ {
322
+ SDL_RWops *rw = ((Sound_SampleInternal *) sample->opaque)->rw;
323
+ Uint32 magic;
324
+ Uint8 ch;
325
+
326
+ /*
327
+ * Apparently the magic number can start at any byte offset in the file,
328
+ * and we should just discard prior data, but I'm going to restrict it
329
+ * to offset zero for now, so we don't chug down every file that might
330
+ * happen to pass through here. If the extension is explicitly "SHN", we
331
+ * check the whole stream, though.
332
+ */
333
+
334
+ if (SDL_strcasecmp(ext, "shn") == 0)
335
+ return extended_shn_magic_search(sample);
336
+
337
+ BAIL_IF_MACRO(SDL_RWread(rw, &magic, sizeof (magic), 1) != 1, NULL, -1);
338
+ BAIL_IF_MACRO(SDL_SwapLE32(magic) != SHN_MAGIC, "SHN: Not a SHN file", -1);
339
+ BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
340
+ BAIL_IF_MACRO(ch > 3, "SHN: Unsupported file version", -1);
341
+
342
+ return (int) ch;
343
+ } /* determine_shn_version */
344
+
345
+
346
+ static void init_shn_offset(Sint32 **offset, int nchan, int nblock, int ftype)
347
+ {
348
+ Sint32 mean = 0;
349
+ int chan;
350
+
351
+ switch (ftype)
352
+ {
353
+ case SHN_TYPE_AU1:
354
+ case SHN_TYPE_S8:
355
+ case SHN_TYPE_S16HL:
356
+ case SHN_TYPE_S16LH:
357
+ case SHN_TYPE_ULAW:
358
+ case SHN_TYPE_AU2:
359
+ case SHN_TYPE_AU3:
360
+ case SHN_TYPE_ALAW:
361
+ mean = 0;
362
+ break;
363
+ case SHN_TYPE_U8:
364
+ mean = 0x80;
365
+ break;
366
+ case SHN_TYPE_U16HL:
367
+ case SHN_TYPE_U16LH:
368
+ mean = 0x8000;
369
+ break;
370
+ default:
371
+ __Sound_SetError("SHN: unknown file type");
372
+ return;
373
+ } /* switch */
374
+
375
+ for(chan = 0; chan < nchan; chan++)
376
+ {
377
+ int i;
378
+ for(i = 0; i < nblock; i++)
379
+ offset[chan][i] = mean;
380
+ } /* for */
381
+ } /* init_shn_offset */
382
+
383
+
384
+ static SDL_INLINE Uint16 cvt_shnftype_to_sdlfmt(Sint16 shntype)
385
+ {
386
+ switch (shntype)
387
+ {
388
+ case SHN_TYPE_S8:
389
+ return AUDIO_S8;
390
+
391
+ case SHN_TYPE_ALAW:
392
+ case SHN_TYPE_ULAW:
393
+ case SHN_TYPE_AU1:
394
+ case SHN_TYPE_AU2:
395
+ case SHN_TYPE_AU3:
396
+ case SHN_TYPE_U8:
397
+ return AUDIO_U8;
398
+
399
+ case SHN_TYPE_S16HL:
400
+ return AUDIO_S16MSB;
401
+
402
+ case SHN_TYPE_S16LH:
403
+ return AUDIO_S16LSB;
404
+
405
+ case SHN_TYPE_U16HL:
406
+ return AUDIO_U16MSB;
407
+
408
+ case SHN_TYPE_U16LH:
409
+ return AUDIO_U16LSB;
410
+ } /* switch */
411
+
412
+ return 0;
413
+ } /* cvt_shnftype_to_sdlfmt */
414
+
415
+
416
+ static SDL_INLINE int skip_bits(shn_t *shn, SDL_RWops *rw)
417
+ {
418
+ int i;
419
+ Sint32 skip;
420
+ Sint32 trash;
421
+
422
+ BAIL_IF_MACRO(!uint_get(SHN_NSKIPSIZE, shn, rw, &skip), NULL, 0);
423
+ for(i = 0; i < skip; i++)
424
+ {
425
+ BAIL_IF_MACRO(!uint_get(SHN_XBYTESIZE, shn, rw, &trash), NULL, 0);
426
+ } /* for */
427
+
428
+ return 1;
429
+ } /* skip_bits */
430
+
431
+
432
+ static Sint32 **shn_long2d(Uint32 n0, Uint32 n1)
433
+ {
434
+ Sint32 **array0;
435
+ Uint32 size = (n0 * sizeof (Sint32 *)) + (n0 * n1 * sizeof (Sint32));
436
+
437
+ array0 = (Sint32 **) SDL_malloc(size);
438
+ if (array0 != NULL)
439
+ {
440
+ int i;
441
+ Sint32 *array1 = (Sint32 *) (array0 + n0);
442
+ for(i = 0; i < n0; i++)
443
+ array0[i] = array1 + (i * n1);
444
+ } /* if */
445
+
446
+ return array0;
447
+ } /* shn_long2d */
448
+
449
+ #define riffID 0x46464952 /* "RIFF", in ascii. */
450
+ #define waveID 0x45564157 /* "WAVE", in ascii. */
451
+ #define fmtID 0x20746D66 /* "fmt ", in ascii. */
452
+ #define dataID 0x61746164 /* "data", in ascii. */
453
+
454
+ static int verb_ReadLE32(shn_t *shn, SDL_RWops *rw, Uint32 *word)
455
+ {
456
+ int i;
457
+ Uint8 chars[4];
458
+ Sint32 byte;
459
+
460
+ for (i = 0; i < 4; i++)
461
+ {
462
+ if (!uvar_get(SHN_VERBATIM_BYTE_SIZE, shn, rw, &byte))
463
+ return 0;
464
+ chars[i] = (Uint8) byte;
465
+ } /* for */
466
+
467
+ SDL_memcpy(word, chars, sizeof (*word));
468
+ *word = SDL_SwapLE32(*word);
469
+
470
+ return 1;
471
+ } /* verb_ReadLE32 */
472
+
473
+
474
+ static int verb_ReadLE16(shn_t *shn, SDL_RWops *rw, Uint16 *word)
475
+ {
476
+ int i;
477
+ Uint8 chars[2];
478
+ Sint32 byte;
479
+
480
+ for (i = 0; i < 2; i++)
481
+ {
482
+ if (!uvar_get(SHN_VERBATIM_BYTE_SIZE, shn, rw, &byte))
483
+ return 0;
484
+ chars[i] = (Uint8) byte;
485
+ } /* for */
486
+
487
+ SDL_memcpy(word, chars, sizeof (*word));
488
+ *word = SDL_SwapLE16(*word);
489
+
490
+ return 1;
491
+ } /* verb_ReadLE16 */
492
+
493
+
494
+ static SDL_INLINE int parse_riff_header(shn_t *shn, Sound_Sample *sample)
495
+ {
496
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
497
+ SDL_RWops *rw = internal->rw;
498
+ Uint16 u16;
499
+ Uint32 u32;
500
+ Sint32 cklen;
501
+ Uint32 bytes_per_second;
502
+
503
+ BAIL_IF_MACRO(!uvar_get(SHN_VERBATIM_CKSIZE_SIZE, shn, rw, &cklen), NULL, 0);
504
+
505
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* RIFF header */
506
+ BAIL_IF_MACRO(u32 != riffID, "SHN: No RIFF header.", 0);
507
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* length */
508
+
509
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* WAVE header */
510
+ BAIL_IF_MACRO(u32 != waveID, "SHN: No WAVE header.", 0);
511
+
512
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* 'fmt ' header */
513
+ BAIL_IF_MACRO(u32 != fmtID, "SHN: No 'fmt ' header.", 0);
514
+
515
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* chunksize */
516
+ BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* format */
517
+ BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* channels */
518
+ sample->actual.channels = u16;
519
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* sample rate */
520
+ sample->actual.rate = u32;
521
+
522
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* bytespersec */
523
+ bytes_per_second = u32;
524
+ BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* blockalign */
525
+ BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* bitspersample */
526
+
527
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* 'data' header */
528
+ BAIL_IF_MACRO(u32 != dataID, "SHN: No 'data' header.", 0);
529
+ BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* chunksize */
530
+ internal->total_time = u32 / bytes_per_second * 1000;
531
+ internal->total_time += (u32 % bytes_per_second) * 1000 / bytes_per_second;
532
+ return 1;
533
+ } /* parse_riff_header */
534
+
535
+
536
+ static int SHN_open(Sound_Sample *sample, const char *ext)
537
+ {
538
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
539
+ SDL_RWops *rw = internal->rw;
540
+ shn_t _shn;
541
+ shn_t *shn = &_shn; /* malloc and copy later. */
542
+ Sint32 cmd;
543
+ Sint32 chan;
544
+
545
+ SDL_memset(shn, '\0', sizeof (shn_t));
546
+ shn->getbufp = shn->getbuf = (Uint8 *) SDL_malloc(SHN_BUFSIZ);
547
+ shn->datatype = SHN_TYPE_EOF;
548
+ shn->nchan = DEFAULT_NCHAN;
549
+ shn->blocksize = DEFAULT_BLOCK_SIZE;
550
+ shn->maxnlpc = DEFAULT_MAXNLPC;
551
+ shn->nmean = UNDEFINED_UINT;
552
+ shn->version = determine_shn_version(sample, ext);
553
+
554
+ if (shn->version == -1) goto shn_open_puke;
555
+ if (!uint_get(SHN_TYPESIZE, shn, rw, &shn->datatype)) goto shn_open_puke;
556
+ if (!uint_get(SHN_CHANNELSIZE, shn, rw, &shn->nchan)) goto shn_open_puke;
557
+
558
+ sample->actual.format = cvt_shnftype_to_sdlfmt(shn->datatype);
559
+ if (sample->actual.format == 0)
560
+ {
561
+ SDL_SetError(ERR_UNSUPPORTED_FORMAT);
562
+ goto shn_open_puke;
563
+ } /* if */
564
+
565
+ if (shn->version > 0)
566
+ {
567
+ int rc = uint_get((int) (SDL_log((double) DEFAULT_BLOCK_SIZE) / M_LN2),
568
+ shn, rw, &shn->blocksize);
569
+ if (!rc) goto shn_open_puke;;
570
+ if (!uint_get(SHN_LPCQSIZE, shn, rw, &shn->maxnlpc)) goto shn_open_puke;
571
+ if (!uint_get(0, shn, rw, &shn->nmean)) goto shn_open_puke;
572
+ if (!skip_bits(shn, rw)) goto shn_open_puke;
573
+ } /* else */
574
+
575
+ shn->nwrap = (shn->maxnlpc > 3) ? shn->maxnlpc : 3;
576
+
577
+ /* grab some space for the input buffer */
578
+ shn->buffer = shn_long2d((Uint32) shn->nchan, shn->blocksize + shn->nwrap);
579
+ shn->offset = shn_long2d((Uint32) shn->nchan, MAX_MACRO(1, shn->nmean));
580
+
581
+ for (chan = 0; chan < shn->nchan; chan++)
582
+ {
583
+ int i;
584
+ for(i = 0; i < shn->nwrap; i++)
585
+ shn->buffer[chan][i] = 0;
586
+ shn->buffer[chan] += shn->nwrap;
587
+ } /* for */
588
+
589
+ if (shn->maxnlpc > 0)
590
+ {
591
+ shn->qlpc = (int *) SDL_malloc((Uint32) (shn->maxnlpc * sizeof (Sint32)));
592
+ if (shn->qlpc == NULL)
593
+ {
594
+ __Sound_SetError(ERR_OUT_OF_MEMORY);
595
+ goto shn_open_puke;
596
+ } /* if */
597
+ } /* if */
598
+
599
+ if (shn->version > 1)
600
+ shn->lpcqoffset = SHN_LPCQOFFSET_VER2;
601
+
602
+ init_shn_offset(shn->offset, shn->nchan,
603
+ MAX_MACRO(1, shn->nmean), shn->datatype);
604
+
605
+ if ( (!uvar_get(SHN_FNSIZE, shn, rw, &cmd)) ||
606
+ (cmd != SHN_FN_VERBATIM) ||
607
+ (!parse_riff_header(shn, sample)) )
608
+ {
609
+ if (cmd != SHN_FN_VERBATIM) /* the other conditions set error state */
610
+ __Sound_SetError("SHN: Expected VERBATIM function");
611
+
612
+ goto shn_open_puke;
613
+ return 0;
614
+ } /* if */
615
+
616
+ shn->start_pos = SDL_RWtell(rw);
617
+
618
+ shn = (shn_t *) SDL_malloc(sizeof (shn_t));
619
+ if (shn == NULL)
620
+ {
621
+ __Sound_SetError(ERR_OUT_OF_MEMORY);
622
+ goto shn_open_puke;
623
+ } /* if */
624
+
625
+ SDL_memcpy(shn, &_shn, sizeof (shn_t));
626
+ internal->decoder_private = shn;
627
+
628
+ SNDDBG(("SHN: Accepting data stream.\n"));
629
+ sample->flags = SOUND_SAMPLEFLAG_NONE;
630
+ return 1; /* we'll handle this data. */
631
+
632
+ shn_open_puke:
633
+ if (_shn.getbuf)
634
+ SDL_free(_shn.getbuf);
635
+ if (_shn.buffer != NULL)
636
+ SDL_free(_shn.buffer);
637
+ if (_shn.offset != NULL)
638
+ SDL_free(_shn.offset);
639
+ if (_shn.qlpc != NULL)
640
+ SDL_free(_shn.qlpc);
641
+
642
+ return 0;
643
+ } /* SHN_open */
644
+
645
+
646
+ static void fix_bitshift(Sint32 *buffer, int nitem, int bitshift, int ftype)
647
+ {
648
+ int i;
649
+
650
+ if (ftype == SHN_TYPE_AU1)
651
+ {
652
+ for (i = 0; i < nitem; i++)
653
+ buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
654
+ } /* if */
655
+ else if (ftype == SHN_TYPE_AU2)
656
+ {
657
+ for(i = 0; i < nitem; i++)
658
+ {
659
+ if (buffer[i] >= 0)
660
+ buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
661
+ else if (buffer[i] == -1)
662
+ buffer[i] = NEGATIVE_ULAW_ZERO;
663
+ else
664
+ buffer[i] = ulaw_outward[bitshift][buffer[i] + 129];
665
+ } /* for */
666
+ } /* else if */
667
+ else
668
+ {
669
+ if (bitshift != 0)
670
+ {
671
+ for(i = 0; i < nitem; i++)
672
+ buffer[i] <<= bitshift;
673
+ } /* if */
674
+ } /* else */
675
+ } /* fix_bitshift */
676
+
677
+
678
+ static void SHN_close(Sound_Sample *sample)
679
+ {
680
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
681
+ shn_t *shn = (shn_t *) internal->decoder_private;
682
+
683
+ if (shn->qlpc != NULL)
684
+ SDL_free(shn->qlpc);
685
+
686
+ if (shn->backBuffer != NULL)
687
+ SDL_free(shn->backBuffer);
688
+
689
+ if (shn->offset != NULL)
690
+ SDL_free(shn->offset);
691
+
692
+ if (shn->buffer != NULL)
693
+ SDL_free(shn->buffer);
694
+
695
+ if (shn->getbuf != NULL)
696
+ SDL_free(shn->getbuf);
697
+
698
+ SDL_free(shn);
699
+ } /* SHN_close */
700
+
701
+
702
+ /* xLaw conversions... */
703
+
704
+ /* adapted by ajr for int input */
705
+ static Uint8 Slinear2ulaw(int sample)
706
+ {
707
+ /*
708
+ ** This routine converts from linear to ulaw.
709
+ **
710
+ ** Craig Reese: IDA/Supercomputing Research Center
711
+ ** Joe Campbell: Department of Defense
712
+ ** 29 September 1989
713
+ **
714
+ ** References:
715
+ ** 1) CCITT Recommendation G.711 (very difficult to follow)
716
+ ** 2) "A New Digital Technique for Implementation of Any
717
+ ** Continuous PCM Companding Law," Villeret, Michel,
718
+ ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
719
+ ** 1973, pg. 11.12-11.17
720
+ ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
721
+ ** for Analog-to_Digital Conversion Techniques,"
722
+ ** 17 February 1987
723
+ **
724
+ ** Input: Signed 16 bit linear sample
725
+ ** Output: 8 bit ulaw sample
726
+ */
727
+
728
+ #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
729
+ #define CLIP 32635
730
+
731
+ int sign, exponent, mantissa;
732
+ Uint8 ulawbyte;
733
+ static const int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
734
+ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
735
+ 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
736
+ 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
737
+ 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
738
+ 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
739
+ 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
740
+ 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
741
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
742
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
743
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
744
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
745
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
746
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
747
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
748
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
749
+
750
+ /* Get the sample into sign-magnitude. */
751
+ if (sample >= 0)
752
+ sign = 0;
753
+ else
754
+ {
755
+ sign = 0x80;
756
+ sample = -sample;
757
+ } /* else */
758
+
759
+ /* clip the magnitude */
760
+ if (sample > CLIP)
761
+ sample = CLIP;
762
+
763
+ /* Convert from 16 bit linear to ulaw. */
764
+ sample = sample + BIAS;
765
+ exponent = exp_lut[( sample >> 7 ) & 0xFF];
766
+ mantissa = (sample >> (exponent + 3)) & 0x0F;
767
+ ulawbyte = ~(sign | (exponent << 4) | mantissa);
768
+
769
+ return ulawbyte;
770
+ } /* Slinear2ulaw */
771
+
772
+
773
+ /* this is derived from the Sun code - it is a bit simpler and has int input */
774
+ #define QUANT_MASK (0xf) /* Quantization field mask. */
775
+ #define NSEGS (8) /* Number of A-law segments. */
776
+ #define SEG_SHIFT (4) /* Left shift for segment number. */
777
+
778
+
779
+ static Uint8 Slinear2alaw(Sint32 linear)
780
+ {
781
+ int seg;
782
+ Uint8 aval, mask;
783
+ static const Sint32 seg_aend[NSEGS] =
784
+ {
785
+ 0x1f,0x3f,0x7f,0xff,0x1ff,0x3ff,0x7ff,0xfff
786
+ };
787
+
788
+ linear >>= 3;
789
+ if(linear >= 0)
790
+ mask = 0xd5; /* sign (7th) bit = 1 */
791
+ else
792
+ {
793
+ mask = 0x55; /* sign bit = 0 */
794
+ linear = -linear - 1;
795
+ } /* else */
796
+
797
+ /* Convert the scaled magnitude to segment number. */
798
+ for (seg = 0; (seg < NSEGS) && (linear > seg_aend[seg]); seg++);
799
+
800
+ /* Combine the sign, segment, and quantization bits. */
801
+ if (seg >= NSEGS) /* out of range, return maximum value. */
802
+ return (Uint8) (0x7F ^ mask);
803
+
804
+ aval = (Uint8) seg << SEG_SHIFT;
805
+ if (seg < 2)
806
+ aval |= (linear >> 1) & QUANT_MASK;
807
+ else
808
+ aval |= (linear >> seg) & QUANT_MASK;
809
+
810
+ return (aval ^ mask);
811
+ } /* Slinear2alaw */
812
+
813
+
814
+ /* convert from signed ints to a given type and write */
815
+ static Uint32 put_to_buffers(Sound_Sample *sample, Uint32 bw)
816
+ {
817
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
818
+ shn_t *shn = (shn_t *) internal->decoder_private;
819
+ int i, chan;
820
+ Sint32 *data0 = shn->buffer[0];
821
+ Sint32 nitem = shn->blocksize;
822
+ int datasize = ((sample->actual.format & 0xFF) / 8);
823
+ Uint32 bsiz = shn->nchan * nitem * datasize;
824
+
825
+ SDL_assert(shn->backBufLeft == 0);
826
+
827
+ if (shn->backBufferSize < bsiz)
828
+ {
829
+ void *rc = SDL_realloc(shn->backBuffer, bsiz);
830
+ if (rc == NULL)
831
+ {
832
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
833
+ BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
834
+ } /* if */
835
+ shn->backBuffer = (Uint8 *) rc;
836
+ shn->backBufferSize = bsiz;
837
+ } /* if */
838
+
839
+ switch (shn->datatype)
840
+ {
841
+ case SHN_TYPE_AU1: /* leave the conversion to fix_bitshift() */
842
+ case SHN_TYPE_AU2:
843
+ {
844
+ Uint8 *writebufp = shn->backBuffer;
845
+ if (shn->nchan == 1)
846
+ {
847
+ for (i = 0; i < nitem; i++)
848
+ *writebufp++ = data0[i];
849
+ } /* if */
850
+ else
851
+ {
852
+ for (i = 0; i < nitem; i++)
853
+ {
854
+ for (chan = 0; chan < shn->nchan; chan++)
855
+ *writebufp++ = shn->buffer[chan][i];
856
+ } /* for */
857
+ } /* else */
858
+ } /* case */
859
+ break;
860
+
861
+ case SHN_TYPE_U8:
862
+ {
863
+ Uint8 *writebufp = shn->backBuffer;
864
+ if (shn->nchan == 1)
865
+ {
866
+ for (i = 0; i < nitem; i++)
867
+ *writebufp++ = CAPMAXUCHAR(data0[i]);
868
+ } /* if */
869
+ else
870
+ {
871
+ for (i = 0; i < nitem; i++)
872
+ {
873
+ for (chan = 0; chan < shn->nchan; chan++)
874
+ *writebufp++ = CAPMAXUCHAR(shn->buffer[chan][i]);
875
+ } /* for */
876
+ } /* else */
877
+ } /* case */
878
+ break;
879
+
880
+ case SHN_TYPE_S8:
881
+ {
882
+ Sint8 *writebufp = (Sint8 *) shn->backBuffer;
883
+ if (shn->nchan == 1)
884
+ {
885
+ for(i = 0; i < nitem; i++)
886
+ *writebufp++ = CAPMAXSCHAR(data0[i]);
887
+ } /* if */
888
+ else
889
+ {
890
+ for(i = 0; i < nitem; i++)
891
+ {
892
+ for(chan = 0; chan < shn->nchan; chan++)
893
+ *writebufp++ = CAPMAXSCHAR(shn->buffer[chan][i]);
894
+ } /* for */
895
+ } /* else */
896
+ } /* case */
897
+ break;
898
+
899
+ case SHN_TYPE_S16HL:
900
+ case SHN_TYPE_S16LH:
901
+ {
902
+ Sint16 *writebufp = (Sint16 *) shn->backBuffer;
903
+ if (shn->nchan == 1)
904
+ {
905
+ for (i = 0; i < nitem; i++)
906
+ *writebufp++ = CAPMAXSHORT(data0[i]);
907
+ } /* if */
908
+ else
909
+ {
910
+ for (i = 0; i < nitem; i++)
911
+ {
912
+ for (chan = 0; chan < shn->nchan; chan++)
913
+ *writebufp++ = CAPMAXSHORT(shn->buffer[chan][i]);
914
+ } /* for */
915
+ } /* else */
916
+ } /* case */
917
+ break;
918
+
919
+ case SHN_TYPE_U16HL:
920
+ case SHN_TYPE_U16LH:
921
+ {
922
+ Uint16 *writebufp = (Uint16 *) shn->backBuffer;
923
+ if (shn->nchan == 1)
924
+ {
925
+ for (i = 0; i < nitem; i++)
926
+ *writebufp++ = CAPMAXUSHORT(data0[i]);
927
+ } /* if */
928
+ else
929
+ {
930
+ for (i = 0; i < nitem; i++)
931
+ {
932
+ for (chan = 0; chan < shn->nchan; chan++)
933
+ *writebufp++ = CAPMAXUSHORT(shn->buffer[chan][i]);
934
+ } /* for */
935
+ } /* else */
936
+ } /* case */
937
+ break;
938
+
939
+ case SHN_TYPE_ULAW:
940
+ {
941
+ Uint8 *writebufp = shn->backBuffer;
942
+ if (shn->nchan == 1)
943
+ {
944
+ for(i = 0; i < nitem; i++)
945
+ *writebufp++ = Slinear2ulaw(CAPMAXSHORT((data0[i] << 3)));
946
+ } /* if */
947
+ else
948
+ {
949
+ for(i = 0; i < nitem; i++)
950
+ {
951
+ for(chan = 0; chan < shn->nchan; chan++)
952
+ *writebufp++ = Slinear2ulaw(CAPMAXSHORT((shn->buffer[chan][i] << 3)));
953
+ } /* for */
954
+ } /* else */
955
+ } /* case */
956
+ break;
957
+
958
+ case SHN_TYPE_AU3:
959
+ {
960
+ Uint8 *writebufp = shn->backBuffer;
961
+ if (shn->nchan == 1)
962
+ {
963
+ for (i = 0; i < nitem; i++)
964
+ if(data0[i] < 0)
965
+ *writebufp++ = (127 - data0[i]) ^ 0xd5;
966
+ else
967
+ *writebufp++ = (data0[i] + 128) ^ 0x55;
968
+ } /* if */
969
+ else
970
+ {
971
+ for (i = 0; i < nitem; i++)
972
+ {
973
+ for (chan = 0; chan < shn->nchan; chan++)
974
+ {
975
+ if (shn->buffer[chan][i] < 0)
976
+ *writebufp++ = (127 - shn->buffer[chan][i]) ^ 0xd5;
977
+ else
978
+ *writebufp++ = (shn->buffer[chan][i] + 128) ^ 0x55;
979
+ } /* for */
980
+ } /* for */
981
+ } /* else */
982
+ } /* case */
983
+ break;
984
+
985
+ case SHN_TYPE_ALAW:
986
+ {
987
+ Uint8 *writebufp = shn->backBuffer;
988
+ if (shn->nchan == 1)
989
+ {
990
+ for (i = 0; i < nitem; i++)
991
+ *writebufp++ = Slinear2alaw(CAPMAXSHORT((data0[i] << 3)));
992
+ } /* if */
993
+ else
994
+ {
995
+ for (i = 0; i < nitem; i++)
996
+ {
997
+ for(chan = 0; chan < shn->nchan; chan++)
998
+ *writebufp++ = Slinear2alaw(CAPMAXSHORT((shn->buffer[chan][i] << 3)));
999
+ } /* for */
1000
+ }/* else */
1001
+ } /* case */
1002
+ break;
1003
+ } /* switch */
1004
+
1005
+ i = MIN_MACRO(internal->buffer_size - bw, bsiz);
1006
+ SDL_memcpy((char *)internal->buffer + bw, shn->backBuffer, i);
1007
+ shn->backBufLeft = bsiz - i;
1008
+ SDL_memcpy(shn->backBuffer, shn->backBuffer + i, shn->backBufLeft);
1009
+ return i;
1010
+ } /* put_to_buffers */
1011
+
1012
+
1013
+ #define ROUNDEDSHIFTDOWN(x, n) (((n) == 0) ? (x) : ((x) >> ((n) - 1)) >> 1)
1014
+
1015
+ static Uint32 SHN_read(Sound_Sample *sample)
1016
+ {
1017
+ Uint32 retval = 0;
1018
+ Sint32 chan = 0;
1019
+ Uint32 cpyBytes = 0;
1020
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
1021
+ SDL_RWops *rw = internal->rw;
1022
+ shn_t *shn = (shn_t *) internal->decoder_private;
1023
+ Sint32 cmd;
1024
+
1025
+ SDL_assert(shn->backBufLeft >= 0);
1026
+
1027
+ /* see if there are leftovers to copy... */
1028
+ if (shn->backBufLeft > 0)
1029
+ {
1030
+ retval = MIN_MACRO(shn->backBufLeft, internal->buffer_size);
1031
+ SDL_memcpy(internal->buffer, shn->backBuffer, retval);
1032
+ shn->backBufLeft -= retval;
1033
+ SDL_memcpy(shn->backBuffer, shn->backBuffer + retval, shn->backBufLeft);
1034
+ } /* if */
1035
+
1036
+ SDL_assert((shn->backBufLeft == 0) || (retval == internal->buffer_size));
1037
+
1038
+ /* get commands from file and execute them */
1039
+ while (retval < internal->buffer_size)
1040
+ {
1041
+ if (!uvar_get(SHN_FNSIZE, shn, rw, &cmd))
1042
+ {
1043
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1044
+ return retval;
1045
+ } /* if */
1046
+
1047
+ if (cmd == SHN_FN_QUIT)
1048
+ {
1049
+ sample->flags |= SOUND_SAMPLEFLAG_EOF;
1050
+ return retval;
1051
+ } /* if */
1052
+
1053
+ switch(cmd)
1054
+ {
1055
+ case SHN_FN_ZERO:
1056
+ case SHN_FN_DIFF0:
1057
+ case SHN_FN_DIFF1:
1058
+ case SHN_FN_DIFF2:
1059
+ case SHN_FN_DIFF3:
1060
+ case SHN_FN_QLPC:
1061
+ {
1062
+ Sint32 i;
1063
+ Sint32 coffset, *cbuffer = shn->buffer[chan];
1064
+ Sint32 resn = 0, nlpc, j;
1065
+
1066
+ if (cmd != SHN_FN_ZERO)
1067
+ {
1068
+ if (!uvar_get(SHN_ENERGYSIZE, shn, rw, &resn))
1069
+ {
1070
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1071
+ return retval;
1072
+ } /* if */
1073
+
1074
+ /* version 0 differed in definition of var_get */
1075
+ if (shn->version == 0)
1076
+ resn--;
1077
+ } /* if */
1078
+
1079
+ /* find mean offset : N.B. this code duplicated */
1080
+ if (shn->nmean == 0)
1081
+ coffset = shn->offset[chan][0];
1082
+ else
1083
+ {
1084
+ Sint32 sum = (shn->version < 2) ? 0 : shn->nmean / 2;
1085
+ for (i = 0; i < shn->nmean; i++)
1086
+ sum += shn->offset[chan][i];
1087
+
1088
+ if (shn->version < 2)
1089
+ coffset = sum / shn->nmean;
1090
+ else
1091
+ coffset = ROUNDEDSHIFTDOWN(sum / shn->nmean, shn->bitshift);
1092
+ } /* else */
1093
+
1094
+ switch (cmd)
1095
+ {
1096
+ case SHN_FN_ZERO:
1097
+ for (i = 0; i < shn->blocksize; i++)
1098
+ cbuffer[i] = 0;
1099
+ break;
1100
+
1101
+ case SHN_FN_DIFF0:
1102
+ for(i = 0; i < shn->blocksize; i++)
1103
+ {
1104
+ if (!var_get(resn, shn, rw, &cbuffer[i]))
1105
+ {
1106
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1107
+ return retval;
1108
+ } /* if */
1109
+ cbuffer[i] += coffset;
1110
+ } /* for */
1111
+ break;
1112
+
1113
+ case SHN_FN_DIFF1:
1114
+ for(i = 0; i < shn->blocksize; i++)
1115
+ {
1116
+ if (!var_get(resn, shn, rw, &cbuffer[i]))
1117
+ {
1118
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1119
+ return retval;
1120
+ } /* if */
1121
+ cbuffer[i] += cbuffer[i - 1];
1122
+ } /* for */
1123
+ break;
1124
+
1125
+ case SHN_FN_DIFF2:
1126
+ for (i = 0; i < shn->blocksize; i++)
1127
+ {
1128
+ if (!var_get(resn, shn, rw, &cbuffer[i]))
1129
+ {
1130
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1131
+ return retval;
1132
+ } /* if */
1133
+ cbuffer[i] += (2 * cbuffer[i-1] - cbuffer[i-2]);
1134
+ } /* for */
1135
+ break;
1136
+
1137
+ case SHN_FN_DIFF3:
1138
+ for (i = 0; i < shn->blocksize; i++)
1139
+ {
1140
+ if (!var_get(resn, shn, rw, &cbuffer[i]))
1141
+ {
1142
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1143
+ return retval;
1144
+ } /* if */
1145
+ cbuffer[i] += 3 * (cbuffer[i - 1] - cbuffer[i - 2]) + cbuffer[i - 3];
1146
+ } /* for */
1147
+ break;
1148
+
1149
+ case SHN_FN_QLPC:
1150
+ if (!uvar_get(SHN_LPCQSIZE, shn, rw, &nlpc))
1151
+ {
1152
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1153
+ return retval;
1154
+ } /* if */
1155
+
1156
+ for(i = 0; i < nlpc; i++)
1157
+ {
1158
+ if (!var_get(SHN_LPCQUANT, shn, rw, &shn->qlpc[i]))
1159
+ {
1160
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1161
+ return retval;
1162
+ } /* if */
1163
+ } /* for */
1164
+
1165
+ for(i = 0; i < nlpc; i++)
1166
+ cbuffer[i - nlpc] -= coffset;
1167
+
1168
+ for(i = 0; i < shn->blocksize; i++)
1169
+ {
1170
+ Sint32 sum = shn->lpcqoffset;
1171
+
1172
+ for(j = 0; j < nlpc; j++)
1173
+ sum += shn->qlpc[j] * cbuffer[i - j - 1];
1174
+
1175
+ if (!var_get(resn, shn, rw, &cbuffer[i]))
1176
+ {
1177
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1178
+ return retval;
1179
+ } /* if */
1180
+ cbuffer[i] += (sum >> SHN_LPCQUANT);
1181
+ } /* for */
1182
+
1183
+ if (coffset != 0)
1184
+ {
1185
+ for(i = 0; i < shn->blocksize; i++)
1186
+ cbuffer[i] += coffset;
1187
+ } /* if */
1188
+
1189
+ break;
1190
+ } /* switch */
1191
+
1192
+ /* store mean value if appropriate : N.B. Duplicated code */
1193
+ if (shn->nmean > 0)
1194
+ {
1195
+ Sint32 sum = (shn->version < 2) ? 0 : shn->blocksize / 2;
1196
+ for (i = 0; i < shn->blocksize; i++)
1197
+ sum += cbuffer[i];
1198
+
1199
+ for(i = 1; i < shn->nmean; i++)
1200
+ shn->offset[chan][i - 1] = shn->offset[chan][i];
1201
+
1202
+ if (shn->version < 2)
1203
+ shn->offset[chan][shn->nmean - 1] = sum / shn->blocksize;
1204
+ else
1205
+ shn->offset[chan][shn->nmean - 1] = (sum / shn->blocksize) << shn->bitshift;
1206
+ } /* if */
1207
+
1208
+ /* do the wrap */
1209
+ for(i = -shn->nwrap; i < 0; i++)
1210
+ cbuffer[i] = cbuffer[i + shn->blocksize];
1211
+
1212
+ fix_bitshift(cbuffer, shn->blocksize, shn->bitshift, shn->datatype);
1213
+
1214
+ if (chan == shn->nchan - 1)
1215
+ {
1216
+ retval += put_to_buffers(sample, retval);
1217
+ if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
1218
+ return retval;
1219
+ } /* if */
1220
+
1221
+ chan = (chan + 1) % shn->nchan;
1222
+ break;
1223
+ } /* case */
1224
+
1225
+ case SHN_FN_BLOCKSIZE:
1226
+ if (!uint_get((int) (SDL_log((double) shn->blocksize) / M_LN2),
1227
+ shn, rw, &shn->blocksize))
1228
+ {
1229
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1230
+ return retval;
1231
+ } /* if */
1232
+ break;
1233
+
1234
+ case SHN_FN_BITSHIFT:
1235
+ if (!uvar_get(SHN_BITSHIFTSIZE, shn, rw, &shn->bitshift))
1236
+ {
1237
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1238
+ return retval;
1239
+ } /* if */
1240
+ break;
1241
+
1242
+ case SHN_FN_VERBATIM:
1243
+ default:
1244
+ sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1245
+ BAIL_MACRO("SHN: Unhandled function.", retval);
1246
+ } /* switch */
1247
+ } /* while */
1248
+
1249
+ return retval;
1250
+ } /* SHN_read */
1251
+
1252
+
1253
+ static int SHN_rewind(Sound_Sample *sample)
1254
+ {
1255
+ Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
1256
+ shn_t *shn = (shn_t *) internal->decoder_private;
1257
+
1258
+ #if 0
1259
+ int rc = SDL_RWseek(internal->rw, shn->start_pos, SEEK_SET);
1260
+ BAIL_IF_MACRO(rc != shn->start_pos, ERR_IO_ERROR, 0);
1261
+ /* !!! FIXME: set state. */
1262
+ return 1;
1263
+ #else
1264
+ /*
1265
+ * !!! FIXME: This is really unacceptable; state should be reset and
1266
+ * !!! FIXME: the RWops should be pointed to the start of the data
1267
+ * !!! FIXME: to decode. The below kludge adds unneeded overhead and
1268
+ * !!! FIXME: risk of failure.
1269
+ */
1270
+ BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0);
1271
+ SHN_close(sample);
1272
+ return SHN_open(sample, "SHN");
1273
+ #endif
1274
+ } /* SHN_rewind */
1275
+
1276
+
1277
+ static int SHN_seek(Sound_Sample *sample, Uint32 ms)
1278
+ {
1279
+ /*
1280
+ * (This CAN be done for SHNs that have a seek table at the end of the
1281
+ * stream, btw.)
1282
+ */
1283
+ BAIL_MACRO("SHN: Seeking not implemented", 0);
1284
+ } /* SHN_seek */
1285
+
1286
+
1287
+ static const char *extensions_shn[] = { "SHN", NULL };
1288
+ const Sound_DecoderFunctions __Sound_DecoderFunctions_SHN =
1289
+ {
1290
+ {
1291
+ extensions_shn,
1292
+ "Shorten-compressed audio data",
1293
+ "Ryan C. Gordon <icculus@icculus.org>",
1294
+ "https://icculus.org/SDL_sound/"
1295
+ },
1296
+
1297
+ SHN_init, /* init() method */
1298
+ SHN_quit, /* quit() method */
1299
+ SHN_open, /* open() method */
1300
+ SHN_close, /* close() method */
1301
+ SHN_read, /* read() method */
1302
+ SHN_rewind, /* rewind() method */
1303
+ SHN_seek /* seek() method */
1304
+ };
1305
+
1306
+ #endif /* defined SOUND_SUPPORTS_SHN */
1307
+
1308
+ /* end of SDL_sound_shn.c ... */
1309
+