gosu 0.15.1 → 1.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/COPYING +1 -1
  4. data/dependencies/SDL/include/SDL.h +138 -0
  5. data/dependencies/SDL/include/SDL_assert.h +293 -0
  6. data/dependencies/SDL/include/SDL_atomic.h +295 -0
  7. data/dependencies/SDL/include/SDL_audio.h +859 -0
  8. data/dependencies/SDL/include/SDL_bits.h +121 -0
  9. data/dependencies/SDL/include/SDL_blendmode.h +123 -0
  10. data/dependencies/SDL/include/SDL_clipboard.h +71 -0
  11. data/dependencies/SDL/include/SDL_config.h +55 -0
  12. data/dependencies/SDL/include/SDL_config_android.h +182 -0
  13. data/dependencies/SDL/include/SDL_config_iphoneos.h +207 -0
  14. data/dependencies/SDL/include/SDL_config_macosx.h +266 -0
  15. data/dependencies/SDL/include/SDL_config_minimal.h +85 -0
  16. data/dependencies/SDL/include/SDL_config_os2.h +188 -0
  17. data/dependencies/SDL/include/SDL_config_pandora.h +135 -0
  18. data/dependencies/SDL/include/SDL_config_psp.h +165 -0
  19. data/dependencies/SDL/include/SDL_config_windows.h +288 -0
  20. data/dependencies/SDL/include/SDL_config_winrt.h +243 -0
  21. data/dependencies/SDL/include/SDL_config_wiz.h +149 -0
  22. data/dependencies/SDL/include/SDL_copying.h +20 -0
  23. data/dependencies/SDL/include/SDL_cpuinfo.h +299 -0
  24. data/dependencies/SDL/include/SDL_egl.h +1676 -0
  25. data/dependencies/SDL/include/SDL_endian.h +263 -0
  26. data/dependencies/SDL/include/SDL_error.h +112 -0
  27. data/dependencies/SDL/include/SDL_events.h +827 -0
  28. data/dependencies/SDL/include/SDL_filesystem.h +136 -0
  29. data/dependencies/SDL/include/SDL_gamecontroller.h +541 -0
  30. data/dependencies/SDL/include/SDL_gesture.h +87 -0
  31. data/dependencies/SDL/include/SDL_haptic.h +1247 -0
  32. data/dependencies/SDL/include/SDL_hints.h +1578 -0
  33. data/dependencies/SDL/include/SDL_joystick.h +499 -0
  34. data/dependencies/SDL/include/SDL_keyboard.h +217 -0
  35. data/dependencies/SDL/include/SDL_keycode.h +351 -0
  36. data/dependencies/SDL/include/SDL_loadso.h +81 -0
  37. data/dependencies/SDL/include/SDL_locale.h +101 -0
  38. data/dependencies/SDL/include/SDL_log.h +211 -0
  39. data/dependencies/SDL/include/SDL_main.h +180 -0
  40. data/dependencies/SDL/include/SDL_messagebox.h +146 -0
  41. data/dependencies/SDL/include/SDL_metal.h +117 -0
  42. data/dependencies/SDL/include/SDL_misc.h +75 -0
  43. data/dependencies/SDL/include/SDL_mouse.h +302 -0
  44. data/dependencies/SDL/include/SDL_mutex.h +251 -0
  45. data/dependencies/SDL/include/SDL_name.h +33 -0
  46. data/dependencies/SDL/include/SDL_opengl.h +2183 -0
  47. data/dependencies/SDL/include/SDL_opengl_glext.h +11180 -0
  48. data/dependencies/SDL/include/SDL_opengles.h +39 -0
  49. data/dependencies/SDL/include/SDL_opengles2.h +52 -0
  50. data/dependencies/SDL/include/SDL_opengles2_gl2.h +621 -0
  51. data/dependencies/SDL/include/SDL_opengles2_gl2ext.h +2050 -0
  52. data/dependencies/SDL/include/SDL_opengles2_gl2platform.h +30 -0
  53. data/dependencies/SDL/include/SDL_opengles2_khrplatform.h +282 -0
  54. data/dependencies/SDL/include/SDL_pixels.h +479 -0
  55. data/dependencies/SDL/include/SDL_platform.h +198 -0
  56. data/dependencies/SDL/include/SDL_power.h +75 -0
  57. data/dependencies/SDL/include/SDL_quit.h +58 -0
  58. data/dependencies/SDL/include/SDL_rect.h +174 -0
  59. data/dependencies/SDL/include/SDL_render.h +1158 -0
  60. data/dependencies/SDL/include/SDL_revision.h +2 -0
  61. data/dependencies/SDL/include/SDL_rwops.h +283 -0
  62. data/dependencies/SDL/include/SDL_scancode.h +413 -0
  63. data/dependencies/SDL/include/SDL_sensor.h +267 -0
  64. data/dependencies/SDL/include/SDL_shape.h +144 -0
  65. data/dependencies/SDL/include/SDL_stdinc.h +647 -0
  66. data/dependencies/SDL/include/SDL_surface.h +563 -0
  67. data/dependencies/SDL/include/SDL_system.h +325 -0
  68. data/dependencies/SDL/include/SDL_syswm.h +354 -0
  69. data/dependencies/SDL/include/SDL_test.h +69 -0
  70. data/dependencies/SDL/include/SDL_test_assert.h +105 -0
  71. data/dependencies/SDL/include/SDL_test_common.h +218 -0
  72. data/dependencies/SDL/include/SDL_test_compare.h +69 -0
  73. data/dependencies/SDL/include/SDL_test_crc32.h +124 -0
  74. data/dependencies/SDL/include/SDL_test_font.h +81 -0
  75. data/dependencies/SDL/include/SDL_test_fuzzer.h +384 -0
  76. data/dependencies/SDL/include/SDL_test_harness.h +134 -0
  77. data/dependencies/SDL/include/SDL_test_images.h +78 -0
  78. data/dependencies/SDL/include/SDL_test_log.h +67 -0
  79. data/dependencies/SDL/include/SDL_test_md5.h +129 -0
  80. data/dependencies/SDL/include/SDL_test_memory.h +63 -0
  81. data/dependencies/SDL/include/SDL_test_random.h +115 -0
  82. data/dependencies/SDL/include/SDL_thread.h +366 -0
  83. data/dependencies/SDL/include/SDL_timer.h +115 -0
  84. data/dependencies/SDL/include/SDL_touch.h +102 -0
  85. data/dependencies/SDL/include/SDL_types.h +29 -0
  86. data/dependencies/SDL/include/SDL_version.h +162 -0
  87. data/dependencies/SDL/include/SDL_video.h +1282 -0
  88. data/dependencies/SDL/include/SDL_vulkan.h +276 -0
  89. data/dependencies/SDL/include/begin_code.h +166 -0
  90. data/dependencies/SDL/include/close_code.h +40 -0
  91. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  92. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  93. data/dependencies/SDL_sound/SDL_sound.c +795 -0
  94. data/dependencies/SDL_sound/SDL_sound.h +725 -0
  95. data/dependencies/SDL_sound/SDL_sound_aiff.c +537 -0
  96. data/dependencies/SDL_sound/SDL_sound_au.c +352 -0
  97. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +747 -0
  98. data/dependencies/SDL_sound/SDL_sound_flac.c +182 -0
  99. data/dependencies/SDL_sound/SDL_sound_internal.h +304 -0
  100. data/dependencies/SDL_sound/SDL_sound_modplug.c +228 -0
  101. data/dependencies/SDL_sound/SDL_sound_mp3.c +184 -0
  102. data/dependencies/SDL_sound/SDL_sound_raw.c +164 -0
  103. data/dependencies/SDL_sound/SDL_sound_shn.c +1309 -0
  104. data/dependencies/SDL_sound/SDL_sound_voc.c +550 -0
  105. data/dependencies/SDL_sound/SDL_sound_vorbis.c +223 -0
  106. data/dependencies/SDL_sound/SDL_sound_wav.c +783 -0
  107. data/dependencies/SDL_sound/dr_flac.h +5906 -0
  108. data/dependencies/SDL_sound/dr_mp3.h +2832 -0
  109. data/dependencies/SDL_sound/libmodplug/fastmix.c +1748 -0
  110. data/dependencies/SDL_sound/libmodplug/libmodplug.h +1001 -0
  111. data/dependencies/SDL_sound/libmodplug/load_669.c +188 -0
  112. data/dependencies/SDL_sound/libmodplug/load_abc.c +4725 -0
  113. data/dependencies/SDL_sound/libmodplug/load_amf.c +403 -0
  114. data/dependencies/SDL_sound/libmodplug/load_ams.c +587 -0
  115. data/dependencies/SDL_sound/libmodplug/load_dbm.c +357 -0
  116. data/dependencies/SDL_sound/libmodplug/load_dmf.c +531 -0
  117. data/dependencies/SDL_sound/libmodplug/load_dsm.c +232 -0
  118. data/dependencies/SDL_sound/libmodplug/load_far.c +253 -0
  119. data/dependencies/SDL_sound/libmodplug/load_it.c +796 -0
  120. data/dependencies/SDL_sound/libmodplug/load_mdl.c +488 -0
  121. data/dependencies/SDL_sound/libmodplug/load_med.c +757 -0
  122. data/dependencies/SDL_sound/libmodplug/load_mid.c +1405 -0
  123. data/dependencies/SDL_sound/libmodplug/load_mod.c +269 -0
  124. data/dependencies/SDL_sound/libmodplug/load_mt2.c +546 -0
  125. data/dependencies/SDL_sound/libmodplug/load_mtm.c +142 -0
  126. data/dependencies/SDL_sound/libmodplug/load_okt.c +192 -0
  127. data/dependencies/SDL_sound/libmodplug/load_pat.c +1143 -0
  128. data/dependencies/SDL_sound/libmodplug/load_pat.h +25 -0
  129. data/dependencies/SDL_sound/libmodplug/load_psm.c +350 -0
  130. data/dependencies/SDL_sound/libmodplug/load_ptm.c +204 -0
  131. data/dependencies/SDL_sound/libmodplug/load_s3m.c +325 -0
  132. data/dependencies/SDL_sound/libmodplug/load_stm.c +180 -0
  133. data/dependencies/SDL_sound/libmodplug/load_ult.c +206 -0
  134. data/dependencies/SDL_sound/libmodplug/load_umx.c +51 -0
  135. data/dependencies/SDL_sound/libmodplug/load_xm.c +554 -0
  136. data/dependencies/SDL_sound/libmodplug/mmcmp.c +382 -0
  137. data/dependencies/SDL_sound/libmodplug/modplug.c +170 -0
  138. data/dependencies/SDL_sound/libmodplug/modplug.h +90 -0
  139. data/dependencies/SDL_sound/libmodplug/snd_dsp.c +301 -0
  140. data/dependencies/SDL_sound/libmodplug/snd_flt.c +63 -0
  141. data/dependencies/SDL_sound/libmodplug/snd_fx.c +2350 -0
  142. data/dependencies/SDL_sound/libmodplug/sndfile.c +1169 -0
  143. data/dependencies/SDL_sound/libmodplug/sndmix.c +1034 -0
  144. data/dependencies/SDL_sound/libmodplug/tables.h +371 -0
  145. data/{src/stb_vorbis.c → dependencies/SDL_sound/stb_vorbis.h} +143 -78
  146. data/dependencies/al_soft/AL/al.h +655 -0
  147. data/dependencies/al_soft/AL/alc.h +270 -0
  148. data/dependencies/al_soft/AL/alext.h +585 -0
  149. data/dependencies/al_soft/AL/efx-creative.h +3 -0
  150. data/dependencies/al_soft/AL/efx-presets.h +402 -0
  151. data/dependencies/al_soft/AL/efx.h +762 -0
  152. data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
  153. data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
  154. data/{src → dependencies/stb}/stb_image.h +330 -127
  155. data/{src → dependencies/stb}/stb_image_write.h +156 -85
  156. data/{src → dependencies/stb}/stb_truetype.h +192 -69
  157. data/{src → dependencies/utf8proc}/utf8proc.c +0 -0
  158. data/{src → dependencies/utf8proc}/utf8proc.h +0 -0
  159. data/{src → dependencies/utf8proc}/utf8proc_data.h +0 -0
  160. data/ext/gosu/extconf.rb +53 -39
  161. data/{Gosu → include/Gosu}/Audio.hpp +6 -8
  162. data/include/Gosu/Bitmap.hpp +100 -0
  163. data/{Gosu → include/Gosu}/Buttons.hpp +104 -44
  164. data/{Gosu → include/Gosu}/Color.hpp +0 -0
  165. data/{Gosu → include/Gosu}/Directories.hpp +0 -0
  166. data/{Gosu → include/Gosu}/Font.hpp +1 -1
  167. data/{Gosu → include/Gosu}/Fwd.hpp +0 -5
  168. data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
  169. data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
  170. data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
  171. data/{Gosu → include/Gosu}/IO.hpp +0 -0
  172. data/{Gosu → include/Gosu}/Image.hpp +7 -6
  173. data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
  174. data/{Gosu → include/Gosu}/Input.hpp +39 -51
  175. data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
  176. data/{Gosu → include/Gosu}/Math.hpp +0 -0
  177. data/{Gosu → include/Gosu}/Platform.hpp +0 -0
  178. data/{Gosu → include/Gosu}/Text.hpp +0 -0
  179. data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
  180. data/{Gosu → include/Gosu}/Timing.hpp +0 -0
  181. data/{Gosu → include/Gosu}/Utility.hpp +15 -4
  182. data/{Gosu → include/Gosu}/Version.hpp +3 -3
  183. data/{Gosu → include/Gosu}/Window.hpp +46 -34
  184. data/lib/OpenAL32.dll +0 -0
  185. data/lib/SDL2.dll +0 -0
  186. data/lib/gosu.rb +0 -3
  187. data/lib/gosu/patches.rb +0 -23
  188. data/lib/gosu/preview.rb +1 -3
  189. data/lib/gosu/swig_patches.rb +6 -8
  190. data/lib64/OpenAL32.dll +0 -0
  191. data/lib64/SDL2.dll +0 -0
  192. data/rdoc/gosu.rb +112 -23
  193. data/src/Audio.cpp +50 -224
  194. data/src/AudioFile.hpp +20 -37
  195. data/src/AudioFileAudioToolbox.cpp +237 -0
  196. data/src/AudioFileSDLSound.cpp +147 -0
  197. data/src/AudioImpl.cpp +3 -12
  198. data/src/AudioImpl.hpp +3 -1
  199. data/src/Bitmap.cpp +85 -83
  200. data/src/BitmapIO.cpp +52 -58
  201. data/src/Font.cpp +4 -1
  202. data/src/Graphics.cpp +7 -4
  203. data/src/Image.cpp +13 -16
  204. data/src/Input.cpp +412 -164
  205. data/src/LargeImageData.cpp +2 -1
  206. data/src/MarkupParser.cpp +2 -1
  207. data/src/Resolution.cpp +8 -8
  208. data/src/RubyGosu.cxx +1184 -352
  209. data/src/RubyGosu.h +3 -2
  210. data/src/TexChunk.cpp +1 -1
  211. data/src/Text.cpp +1 -0
  212. data/src/TextBuilder.cpp +3 -1
  213. data/src/Texture.cpp +1 -1
  214. data/src/TrueTypeFont.cpp +2 -1
  215. data/src/TrueTypeFontWin.cpp +3 -3
  216. data/src/Utility.cpp +11 -7
  217. data/src/Window.cpp +90 -62
  218. data/src/WindowUIKit.cpp +21 -9
  219. metadata +195 -46
  220. data/Gosu/AutoLink.hpp +0 -14
  221. data/Gosu/Bitmap.hpp +0 -113
  222. data/lib/gosu/zen.rb +0 -89
  223. data/src/AudioToolboxFile.hpp +0 -210
  224. data/src/MPEGFile.hpp +0 -90
  225. data/src/OggFile.hpp +0 -92
  226. data/src/SndFile.hpp +0 -174
  227. data/src/WinMain.cpp +0 -64
@@ -0,0 +1,1169 @@
1
+ /*
2
+ * This source code is public domain.
3
+ *
4
+ * Authors: Olivier Lapicque <olivierl@jps.net>,
5
+ * Adam Goode <adam@evdebs.org> (endian and char fixes for PPC)
6
+ */
7
+
8
+ #include <math.h> //for GCCFIX
9
+ #include "modplug.h"
10
+ #include "libmodplug.h"
11
+
12
+ extern BOOL MMCMP_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength);
13
+
14
+ // External decompressors
15
+ extern void AMSUnpack(const char *psrc, UINT inputlen, char *pdest, UINT dmax, char packcharacter);
16
+ extern WORD MDLReadBits(DWORD *bitbuf, UINT *bitnum, LPBYTE *ibuf, CHAR n);
17
+ extern int DMFUnpack(LPBYTE psample, LPBYTE ibuf, LPBYTE ibufmax, UINT maxlen);
18
+ extern void ITUnpack8Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215);
19
+ extern void ITUnpack16Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215);
20
+
21
+
22
+ #define MAX_PACK_TABLES 3
23
+
24
+
25
+ // Compression table
26
+ static const signed char UnpackTable[MAX_PACK_TABLES][16] =
27
+ //--------------------------------------------
28
+ {
29
+ // CPU-generated dynamic table
30
+ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
31
+ // u-Law table
32
+ {0, 1, 2, 4, 8, 16, 32, 64,
33
+ -1, -2, -4, -8, -16, -32, -48, -64},
34
+ // Linear table
35
+ {0, 1, 2, 3, 5, 7, 12, 19,
36
+ -1, -2, -3, -5, -7, -12, -19, -31}
37
+ };
38
+
39
+
40
+ static void CSoundFile_UpdateSettings(CSoundFile *_this, const ModPlug_Settings *settings)
41
+ {
42
+ if(settings->mFlags & MODPLUG_ENABLE_REVERB)
43
+ CSoundFile_SetReverbParameters(_this, settings->mReverbDepth, settings->mReverbDelay);
44
+
45
+ if(settings->mFlags & MODPLUG_ENABLE_MEGABASS)
46
+ CSoundFile_SetXBassParameters(_this, settings->mBassAmount, settings->mBassRange);
47
+ else // modplug seems to ignore the SetWaveConfigEx() setting for bass boost
48
+ CSoundFile_SetXBassParameters(_this, 0, 0);
49
+
50
+ if(settings->mFlags & MODPLUG_ENABLE_SURROUND)
51
+ CSoundFile_SetSurroundParameters(_this, settings->mSurroundDepth, settings->mSurroundDelay);
52
+
53
+ CSoundFile_SetWaveConfig(_this, settings->mFrequency, settings->mBits, settings->mChannels);
54
+ CSoundFile_SetMixConfig(_this, settings->mStereoSeparation, settings->mMaxMixChannels);
55
+ _this->gSampleSize = settings->mBits / 8 * settings->mChannels;
56
+
57
+ CSoundFile_SetWaveConfigEx(_this, settings->mFlags & MODPLUG_ENABLE_SURROUND,
58
+ !(settings->mFlags & MODPLUG_ENABLE_OVERSAMPLING),
59
+ settings->mFlags & MODPLUG_ENABLE_REVERB,
60
+ TRUE,
61
+ settings->mFlags & MODPLUG_ENABLE_MEGABASS,
62
+ settings->mFlags & MODPLUG_ENABLE_NOISE_REDUCTION,
63
+ FALSE);
64
+ CSoundFile_SetResamplingMode(_this, settings->mResamplingMode);
65
+ }
66
+
67
+ CSoundFile *new_CSoundFile(LPCBYTE lpStream, DWORD dwMemLength, const ModPlug_Settings *settings)
68
+ //----------------------------------------------------------
69
+ {
70
+ CSoundFile *_this = (CSoundFile *) SDL_calloc(1, sizeof (CSoundFile));
71
+ if (!_this) return NULL;
72
+ int i;
73
+
74
+ _this->m_nXBassDepth = 6;
75
+ _this->m_nXBassRange = XBASS_DELAY;
76
+ _this->m_nReverbDepth = 1;
77
+ _this->m_nReverbDelay = 100;
78
+ _this->m_nProLogicDepth = 12;
79
+ _this->m_nProLogicDelay = 20;
80
+ _this->m_nStereoSeparation = 128;
81
+ _this->m_nMaxMixChannels = 32;
82
+ _this->gnChannels = 1;
83
+ _this->gdwSoundSetup = 0;
84
+ _this->gdwMixingFreq = 44100;
85
+ _this->gnBitsPerSample = 16;
86
+ _this->gnVolumeRampSamples = 64;
87
+
88
+ _this->m_nFreqFactor = _this->m_nTempoFactor = 128;
89
+ _this->m_nMasterVolume = 128;
90
+ _this->m_nDefaultGlobalVolume = 256;
91
+ _this->m_nGlobalVolume = 256;
92
+ _this->m_nDefaultSpeed = 6;
93
+ _this->m_nDefaultTempo = 125;
94
+ _this->m_nMinPeriod = 16;
95
+ _this->m_nMaxPeriod = 32767;
96
+ _this->m_nSongPreAmp = 0x30;
97
+ SDL_memset(_this->Order, 0xFF, sizeof(_this->Order));
98
+ CSoundFile_ResetMidiCfg(_this);
99
+ for (UINT npt=0; npt<MAX_PATTERNS; npt++) _this->PatternSize[npt] = 64;
100
+ for (UINT nch=0; nch<MAX_BASECHANNELS; nch++)
101
+ {
102
+ _this->ChnSettings[nch].nPan = 128;
103
+ _this->ChnSettings[nch].nVolume = 64;
104
+ }
105
+ if (lpStream)
106
+ {
107
+ BOOL bMMCmp = MMCMP_Unpack(&lpStream, &dwMemLength);
108
+ if ((!CSoundFile_ReadXM(_this, lpStream, dwMemLength))
109
+ && (!CSoundFile_ReadS3M(_this, lpStream, dwMemLength))
110
+ && (!CSoundFile_ReadIT(_this, lpStream, dwMemLength))
111
+ #ifndef MODPLUG_BASIC_SUPPORT
112
+ /* Sequencer File Format Support */
113
+ && (!CSoundFile_ReadABC(_this, lpStream, dwMemLength))
114
+ && (!CSoundFile_ReadMID(_this, lpStream, dwMemLength))
115
+ && (!CSoundFile_ReadPAT(_this, lpStream, dwMemLength))
116
+ && (!CSoundFile_ReadSTM(_this, lpStream, dwMemLength))
117
+ && (!CSoundFile_ReadMed(_this, lpStream, dwMemLength))
118
+ && (!CSoundFile_ReadMTM(_this, lpStream, dwMemLength))
119
+ && (!CSoundFile_ReadMDL(_this, lpStream, dwMemLength))
120
+ && (!CSoundFile_ReadDBM(_this, lpStream, dwMemLength))
121
+ && (!CSoundFile_Read669(_this, lpStream, dwMemLength))
122
+ && (!CSoundFile_ReadFAR(_this, lpStream, dwMemLength))
123
+ && (!CSoundFile_ReadAMS(_this, lpStream, dwMemLength))
124
+ && (!CSoundFile_ReadOKT(_this, lpStream, dwMemLength))
125
+ && (!CSoundFile_ReadPTM(_this, lpStream, dwMemLength))
126
+ && (!CSoundFile_ReadUlt(_this, lpStream, dwMemLength))
127
+ && (!CSoundFile_ReadDMF(_this, lpStream, dwMemLength))
128
+ && (!CSoundFile_ReadDSM(_this, lpStream, dwMemLength))
129
+ && (!CSoundFile_ReadUMX(_this, lpStream, dwMemLength))
130
+ && (!CSoundFile_ReadAMF(_this, lpStream, dwMemLength))
131
+ && (!CSoundFile_ReadPSM(_this, lpStream, dwMemLength))
132
+ && (!CSoundFile_ReadMT2(_this, lpStream, dwMemLength))
133
+ #endif // MODPLUG_BASIC_SUPPORT
134
+ && (!CSoundFile_ReadMod(_this, lpStream, dwMemLength))) _this->m_nType = MOD_TYPE_NONE;
135
+ if (bMMCmp)
136
+ {
137
+ GlobalFreePtr(lpStream);
138
+ lpStream = NULL;
139
+ }
140
+ }
141
+ // Adjust channels
142
+ for (i=0; i<MAX_BASECHANNELS; i++)
143
+ {
144
+ if (_this->ChnSettings[i].nVolume > 64) _this->ChnSettings[i].nVolume = 64;
145
+ if (_this->ChnSettings[i].nPan > 256) _this->ChnSettings[i].nPan = 128;
146
+ _this->Chn[i].nPan = _this->ChnSettings[i].nPan;
147
+ _this->Chn[i].nGlobalVol = _this->ChnSettings[i].nVolume;
148
+ _this->Chn[i].dwFlags = _this->ChnSettings[i].dwFlags;
149
+ _this->Chn[i].nVolume = 256;
150
+ _this->Chn[i].nCutOff = 0x7F;
151
+ }
152
+ // Checking instruments
153
+ MODINSTRUMENT *pins = _this->Ins;
154
+
155
+ for (i=0; i<MAX_INSTRUMENTS; i++, pins++)
156
+ {
157
+ if (pins->pSample)
158
+ {
159
+ if (pins->nLoopEnd > pins->nLength) pins->nLoopEnd = pins->nLength;
160
+ if (pins->nLoopStart + 3 >= pins->nLoopEnd)
161
+ {
162
+ pins->nLoopStart = 0;
163
+ pins->nLoopEnd = 0;
164
+ }
165
+ if (pins->nSustainEnd > pins->nLength) pins->nSustainEnd = pins->nLength;
166
+ if (pins->nSustainStart + 3 >= pins->nSustainEnd)
167
+ {
168
+ pins->nSustainStart = 0;
169
+ pins->nSustainEnd = 0;
170
+ }
171
+ } else
172
+ {
173
+ pins->nLength = 0;
174
+ pins->nLoopStart = 0;
175
+ pins->nLoopEnd = 0;
176
+ pins->nSustainStart = 0;
177
+ pins->nSustainEnd = 0;
178
+ }
179
+ if (!pins->nLoopEnd) pins->uFlags &= ~CHN_LOOP;
180
+ if (!pins->nSustainEnd) pins->uFlags &= ~CHN_SUSTAINLOOP;
181
+ if (pins->nGlobalVol > 64) pins->nGlobalVol = 64;
182
+ }
183
+ // Check invalid instruments
184
+ while ((_this->m_nInstruments > 0) && (!_this->Headers[_this->m_nInstruments]))
185
+ _this->m_nInstruments--;
186
+ // Set default values
187
+ if (_this->m_nSongPreAmp < 0x20) _this->m_nSongPreAmp = 0x20;
188
+ if (_this->m_nDefaultTempo < 32) _this->m_nDefaultTempo = 125;
189
+ if (!_this->m_nDefaultSpeed) _this->m_nDefaultSpeed = 6;
190
+ _this->m_nMusicSpeed = _this->m_nDefaultSpeed;
191
+ _this->m_nMusicTempo = _this->m_nDefaultTempo;
192
+ _this->m_nGlobalVolume = _this->m_nDefaultGlobalVolume;
193
+ _this->m_nNextPattern = 0;
194
+ _this->m_nCurrentPattern = 0;
195
+ _this->m_nPattern = 0;
196
+ _this->m_nBufferCount = 0;
197
+ _this->m_nTickCount = _this->m_nMusicSpeed;
198
+ _this->m_nNextRow = 0;
199
+ _this->m_nRow = 0;
200
+ if ((_this->m_nRestartPos >= MAX_ORDERS) || (_this->Order[_this->m_nRestartPos] >= MAX_PATTERNS)) _this->m_nRestartPos = 0;
201
+ if (_this->m_nType)
202
+ {
203
+ UINT maxpreamp = 0x10+(_this->m_nChannels*8);
204
+ if (maxpreamp > 100) maxpreamp = 100;
205
+ if (_this->m_nSongPreAmp > maxpreamp) _this->m_nSongPreAmp = maxpreamp;
206
+ CSoundFile_UpdateSettings(_this, settings);
207
+ return _this;
208
+ }
209
+ SDL_free(_this);
210
+ return NULL;
211
+ }
212
+
213
+
214
+ void delete_CSoundFile(CSoundFile *_this)
215
+ {
216
+ int i;
217
+ for (i=0; i<MAX_PATTERNS; i++) if (_this->Patterns[i])
218
+ {
219
+ CSoundFile_FreePattern(_this->Patterns[i]);
220
+ _this->Patterns[i] = NULL;
221
+ }
222
+ _this->m_nPatternNames = 0;
223
+ if (_this->m_lpszPatternNames)
224
+ {
225
+ SDL_free(_this->m_lpszPatternNames);
226
+ _this->m_lpszPatternNames = NULL;
227
+ }
228
+ for (i=1; i<MAX_SAMPLES; i++)
229
+ {
230
+ MODINSTRUMENT *pins = &_this->Ins[i];
231
+ if (pins->pSample)
232
+ {
233
+ CSoundFile_FreeSample(pins->pSample);
234
+ pins->pSample = NULL;
235
+ }
236
+ }
237
+ for (i=0; i<MAX_INSTRUMENTS; i++)
238
+ {
239
+ if (_this->Headers[i])
240
+ {
241
+ SDL_free(_this->Headers[i]);
242
+ _this->Headers[i] = NULL;
243
+ }
244
+ }
245
+ _this->m_nType = MOD_TYPE_NONE;
246
+ _this->m_nChannels = _this->m_nSamples = _this->m_nInstruments = 0;
247
+
248
+ SDL_free(_this);
249
+ }
250
+
251
+
252
+ //////////////////////////////////////////////////////////////////////////
253
+ // Memory Allocation
254
+
255
+ MODCOMMAND *CSoundFile_AllocatePattern(UINT rows, UINT nchns)
256
+ //------------------------------------------------------------
257
+ {
258
+ return (MODCOMMAND *) SDL_calloc(sizeof (MODCOMMAND), rows * nchns);
259
+ }
260
+
261
+
262
+ void CSoundFile_FreePattern(LPVOID pat)
263
+ //--------------------------------------
264
+ {
265
+ SDL_free(pat);
266
+ }
267
+
268
+
269
+ signed char* CSoundFile_AllocateSample(UINT nbytes)
270
+ //-------------------------------------------
271
+ {
272
+ signed char * p = (signed char *)GlobalAllocPtr(GHND, (nbytes+39) & ~7);
273
+ if (p) p += 16;
274
+ return p;
275
+ }
276
+
277
+
278
+ void CSoundFile_FreeSample(LPVOID p)
279
+ //-----------------------------------
280
+ {
281
+ if (p)
282
+ {
283
+ GlobalFreePtr(((LPSTR)p)-16);
284
+ }
285
+ }
286
+
287
+
288
+ //////////////////////////////////////////////////////////////////////////
289
+ // Misc functions
290
+
291
+ void CSoundFile_ResetMidiCfg(CSoundFile *_this)
292
+ //-----------------------------
293
+ {
294
+ SDL_memset(&_this->m_MidiCfg, 0, sizeof(_this->m_MidiCfg));
295
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiGlb[MIDIOUT_START*32], "FF", 32);
296
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiGlb[MIDIOUT_STOP*32], "FC", 32);
297
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiGlb[MIDIOUT_NOTEON*32], "9c n v", 32);
298
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiGlb[MIDIOUT_NOTEOFF*32], "9c n 0", 32);
299
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiGlb[MIDIOUT_PROGRAM*32], "Cc p", 32);
300
+ SDL_strlcpy(&_this->m_MidiCfg.szMidiSFXExt[0], "F0F000z", 32);
301
+ for (int iz=0; iz<16; iz++) SDL_snprintf(&_this->m_MidiCfg.szMidiZXXExt[iz*32], 32, "F0F001%02X", iz*8);
302
+ }
303
+
304
+
305
+
306
+
307
+ BOOL CSoundFile_SetWaveConfig(CSoundFile *_this, UINT nRate,UINT nBits,UINT nChannels)
308
+ //----------------------------------------------------------------------------
309
+ {
310
+ BOOL bReset = FALSE;
311
+ DWORD d = _this->gdwSoundSetup;
312
+ if ((_this->gdwMixingFreq != nRate) || (_this->gnBitsPerSample != nBits) || (_this->gnChannels != nChannels) || (d != _this->gdwSoundSetup)) bReset = TRUE;
313
+ _this->gnChannels = nChannels;
314
+ _this->gdwSoundSetup = d;
315
+ _this->gdwMixingFreq = nRate;
316
+ _this->gnBitsPerSample = nBits;
317
+ CSoundFile_InitPlayer(_this, bReset);
318
+ return TRUE;
319
+ }
320
+
321
+ BOOL CSoundFile_SetMixConfig(CSoundFile *_this, UINT nStereoSeparation, UINT nMaxMixChannels)
322
+ //-------------------------------------------------------------------------
323
+ {
324
+ if (nMaxMixChannels < 2) return FALSE;
325
+
326
+ _this->m_nMaxMixChannels = nMaxMixChannels;
327
+ _this->m_nStereoSeparation = nStereoSeparation;
328
+ return TRUE;
329
+ }
330
+
331
+
332
+ BOOL CSoundFile_SetResamplingMode(CSoundFile *_this, UINT nMode)
333
+ //--------------------------------------------
334
+ {
335
+ DWORD d = _this->gdwSoundSetup & ~(SNDMIX_NORESAMPLING|SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE);
336
+ switch(nMode)
337
+ {
338
+ case SRCMODE_NEAREST: d |= SNDMIX_NORESAMPLING; break;
339
+ case SRCMODE_LINEAR: break;
340
+ case SRCMODE_SPLINE: d |= SNDMIX_HQRESAMPLER; break;
341
+ case SRCMODE_POLYPHASE: d |= (SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE); break;
342
+ default:
343
+ return FALSE;
344
+ }
345
+ _this->gdwSoundSetup = d;
346
+ return TRUE;
347
+ }
348
+
349
+ UINT CSoundFile_GetMaxPosition(CSoundFile *_this)
350
+ //-------------------------------------
351
+ {
352
+ UINT max = 0;
353
+ UINT i = 0;
354
+
355
+ while ((i < MAX_ORDERS) && (_this->Order[i] != 0xFF))
356
+ {
357
+ if (_this->Order[i] < MAX_PATTERNS) max += _this->PatternSize[_this->Order[i]];
358
+ i++;
359
+ }
360
+ return max;
361
+ }
362
+
363
+ void CSoundFile_SetCurrentPos(CSoundFile *_this, UINT nPos)
364
+ //---------------------------------------
365
+ {
366
+ UINT i, nPattern;
367
+
368
+ for (i=0; i<MAX_CHANNELS; i++)
369
+ {
370
+ _this->Chn[i].nNote = _this->Chn[i].nNewNote = _this->Chn[i].nNewIns = 0;
371
+ _this->Chn[i].pInstrument = NULL;
372
+ _this->Chn[i].pHeader = NULL;
373
+ _this->Chn[i].nPortamentoDest = 0;
374
+ _this->Chn[i].nCommand = 0;
375
+ _this->Chn[i].nPatternLoopCount = 0;
376
+ _this->Chn[i].nPatternLoop = 0;
377
+ _this->Chn[i].nFadeOutVol = 0;
378
+ _this->Chn[i].dwFlags |= CHN_KEYOFF|CHN_NOTEFADE;
379
+ _this->Chn[i].nTremorCount = 0;
380
+ }
381
+ if (!nPos)
382
+ {
383
+ for (i=0; i<MAX_CHANNELS; i++)
384
+ {
385
+ _this->Chn[i].nPeriod = 0;
386
+ _this->Chn[i].nPos = _this->Chn[i].nLength = 0;
387
+ _this->Chn[i].nLoopStart = 0;
388
+ _this->Chn[i].nLoopEnd = 0;
389
+ _this->Chn[i].nROfs = _this->Chn[i].nLOfs = 0;
390
+ _this->Chn[i].pSample = NULL;
391
+ _this->Chn[i].pInstrument = NULL;
392
+ _this->Chn[i].pHeader = NULL;
393
+ _this->Chn[i].nCutOff = 0x7F;
394
+ _this->Chn[i].nResonance = 0;
395
+ _this->Chn[i].nLeftVol = _this->Chn[i].nRightVol = 0;
396
+ _this->Chn[i].nNewLeftVol = _this->Chn[i].nNewRightVol = 0;
397
+ _this->Chn[i].nLeftRamp = _this->Chn[i].nRightRamp = 0;
398
+ _this->Chn[i].nVolume = 256;
399
+ if (i < MAX_BASECHANNELS)
400
+ {
401
+ _this->Chn[i].dwFlags = _this->ChnSettings[i].dwFlags;
402
+ _this->Chn[i].nPan = _this->ChnSettings[i].nPan;
403
+ _this->Chn[i].nGlobalVol = _this->ChnSettings[i].nVolume;
404
+ } else
405
+ {
406
+ _this->Chn[i].dwFlags = 0;
407
+ _this->Chn[i].nPan = 128;
408
+ _this->Chn[i].nGlobalVol = 64;
409
+ }
410
+ }
411
+ _this->m_nGlobalVolume = _this->m_nDefaultGlobalVolume;
412
+ _this->m_nMusicSpeed = _this->m_nDefaultSpeed;
413
+ _this->m_nMusicTempo = _this->m_nDefaultTempo;
414
+ }
415
+ _this->m_dwSongFlags &= ~(SONG_PATTERNLOOP|SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE);
416
+ for (nPattern = 0; nPattern < MAX_ORDERS; nPattern++)
417
+ {
418
+ UINT ord = _this->Order[nPattern];
419
+ if (ord == 0xFE) continue;
420
+ if (ord == 0xFF) break;
421
+ if (ord < MAX_PATTERNS)
422
+ {
423
+ if (nPos < (UINT)_this->PatternSize[ord]) break;
424
+ nPos -= _this->PatternSize[ord];
425
+ }
426
+ }
427
+ // Buggy position ?
428
+ if ((nPattern >= MAX_ORDERS)
429
+ || (_this->Order[nPattern] >= MAX_PATTERNS)
430
+ || (nPos >= _this->PatternSize[_this->Order[nPattern]]))
431
+ {
432
+ nPos = 0;
433
+ nPattern = 0;
434
+ }
435
+ UINT nRow = nPos;
436
+ if ((nRow) && (_this->Order[nPattern] < MAX_PATTERNS))
437
+ {
438
+ MODCOMMAND *p = _this->Patterns[_this->Order[nPattern]];
439
+ if ((p) && (nRow < _this->PatternSize[_this->Order[nPattern]]))
440
+ {
441
+ BOOL bOk = FALSE;
442
+ while ((!bOk) && (nRow > 0))
443
+ {
444
+ UINT n = nRow * _this->m_nChannels;
445
+ for (UINT k=0; k<_this->m_nChannels; k++, n++)
446
+ {
447
+ if (p[n].note)
448
+ {
449
+ bOk = TRUE;
450
+ break;
451
+ }
452
+ }
453
+ if (!bOk) nRow--;
454
+ }
455
+ }
456
+ }
457
+ _this->m_nNextPattern = nPattern;
458
+ _this->m_nNextRow = nRow;
459
+ _this->m_nTickCount = _this->m_nMusicSpeed;
460
+ _this->m_nBufferCount = 0;
461
+ _this->m_nPatternDelay = 0;
462
+ _this->m_nFrameDelay = 0;
463
+ }
464
+
465
+
466
+ // Flags:
467
+ // 0 = signed 8-bit PCM data (default)
468
+ // 1 = unsigned 8-bit PCM data
469
+ // 2 = 8-bit ADPCM data with linear table
470
+ // 3 = 4-bit ADPCM data
471
+ // 4 = 16-bit ADPCM data with linear table
472
+ // 5 = signed 16-bit PCM data
473
+ // 6 = unsigned 16-bit PCM data
474
+
475
+
476
+ UINT CSoundFile_ReadSample(CSoundFile *_this, MODINSTRUMENT *pIns, UINT nFlags, LPCSTR lpMemFile, DWORD dwMemLength)
477
+ //------------------------------------------------------------------------------
478
+ {
479
+ UINT len = 0, mem = pIns->nLength+6;
480
+
481
+ // Disable >2Gb samples,(preventing buffer overflow in AllocateSample)
482
+ if ((!pIns) || ((int)pIns->nLength < 4) || (!lpMemFile)) return 0;
483
+ if (pIns->nLength > MAX_SAMPLE_LENGTH) pIns->nLength = MAX_SAMPLE_LENGTH;
484
+ pIns->uFlags &= ~(CHN_16BIT|CHN_STEREO);
485
+ if (nFlags & RSF_16BIT)
486
+ {
487
+ mem *= 2;
488
+ pIns->uFlags |= CHN_16BIT;
489
+ }
490
+ if (nFlags & RSF_STEREO)
491
+ {
492
+ mem *= 2;
493
+ pIns->uFlags |= CHN_STEREO;
494
+ }
495
+ if ((pIns->pSample = CSoundFile_AllocateSample(mem)) == NULL)
496
+ {
497
+ pIns->nLength = 0;
498
+ return 0;
499
+ }
500
+ switch(nFlags)
501
+ {
502
+ // 1: 8-bit unsigned PCM data
503
+ case RS_PCM8U:
504
+ {
505
+ len = pIns->nLength;
506
+ if (len > dwMemLength) len = pIns->nLength = dwMemLength;
507
+ signed char *pSample = pIns->pSample;
508
+ for (UINT j=0; j<len; j++) pSample[j] = (signed char)(lpMemFile[j] - 0x80);
509
+ }
510
+ break;
511
+
512
+ // 2: 8-bit ADPCM data with linear table
513
+ case RS_PCM8D:
514
+ {
515
+ len = pIns->nLength;
516
+ if (len > dwMemLength) break;
517
+ signed char *pSample = pIns->pSample;
518
+ const signed char *p = (const signed char *)lpMemFile;
519
+ int delta = 0;
520
+
521
+ for (UINT j=0; j<len; j++)
522
+ {
523
+ delta += p[j];
524
+ *pSample++ = (signed char)delta;
525
+ }
526
+ }
527
+ break;
528
+
529
+ // 3: 4-bit ADPCM data
530
+ case RS_ADPCM4:
531
+ {
532
+ len = (pIns->nLength + 1) / 2;
533
+ if (len > dwMemLength - 16) break;
534
+ SDL_memcpy(_this->CompressionTable, lpMemFile, 16);
535
+ lpMemFile += 16;
536
+ signed char *pSample = pIns->pSample;
537
+ signed char delta = 0;
538
+ for (UINT j=0; j<len; j++)
539
+ {
540
+ const BYTE b0 = (BYTE)lpMemFile[j];
541
+ const BYTE b1 = (BYTE)(lpMemFile[j] >> 4);
542
+ delta = (signed char)(delta + _this->CompressionTable[b0 & 0x0F]);
543
+ pSample[0] = delta;
544
+ delta = (signed char)(delta + _this->CompressionTable[b1 & 0x0F]);
545
+ pSample[1] = (signed char)(delta + _this->CompressionTable[b1 & 0x0F]);
546
+ pSample += 2;
547
+ }
548
+ len += 16;
549
+ }
550
+ break;
551
+
552
+ // 4: 16-bit ADPCM data with linear table
553
+ case RS_PCM16D:
554
+ {
555
+ len = pIns->nLength * 2;
556
+ if (len > dwMemLength) break;
557
+ int16_t *pSample = (int16_t *)pIns->pSample;
558
+ int16_t *p = (int16_t *)lpMemFile;
559
+ int delta16 = 0;
560
+ for (UINT j=0; j<len; j+=2)
561
+ {
562
+ delta16 += bswapLE16(*p++);
563
+ *pSample++ = (int16_t )delta16;
564
+ }
565
+ }
566
+ break;
567
+
568
+ // 5: 16-bit signed PCM data
569
+ case RS_PCM16S:
570
+ {
571
+ len = pIns->nLength * 2;
572
+ if (len <= dwMemLength) SDL_memcpy(pIns->pSample, lpMemFile, len);
573
+ int16_t *pSample = (int16_t *)pIns->pSample;
574
+ for (UINT j=0; j<len; j+=2)
575
+ {
576
+ int16_t rawSample = *pSample;
577
+ *pSample++ = bswapLE16(rawSample);
578
+ }
579
+ }
580
+ break;
581
+
582
+ // 16-bit signed mono PCM motorola byte order
583
+ case RS_PCM16M:
584
+ len = pIns->nLength * 2;
585
+ if (len > dwMemLength) len = dwMemLength & ~1;
586
+ if (len > 1)
587
+ {
588
+ signed char *pSample = (signed char *)pIns->pSample;
589
+ signed char *pSrc = (signed char *)lpMemFile;
590
+ for (UINT j=0; j<len; j+=2)
591
+ {
592
+ // pSample[j] = pSrc[j+1];
593
+ // pSample[j+1] = pSrc[j];
594
+ *((uint16_t *)(pSample+j)) = bswapBE16(*((uint16_t *)(pSrc+j)));
595
+ }
596
+ }
597
+ break;
598
+
599
+ // 6: 16-bit unsigned PCM data
600
+ case RS_PCM16U:
601
+ {
602
+ len = pIns->nLength * 2;
603
+ if (len > dwMemLength) break;
604
+ int16_t *pSample = (int16_t *)pIns->pSample;
605
+ int16_t *pSrc = (int16_t *)lpMemFile;
606
+ for (UINT j=0; j<len; j+=2) *pSample++ = bswapLE16(*(pSrc++)) - 0x8000;
607
+ }
608
+ break;
609
+
610
+ // 16-bit signed stereo big endian
611
+ case RS_STPCM16M:
612
+ len = pIns->nLength * 2;
613
+ if (len*2 <= dwMemLength)
614
+ {
615
+ signed char *pSample = (signed char *)pIns->pSample;
616
+ signed char *pSrc = (signed char *)lpMemFile;
617
+ for (UINT j=0; j<len; j+=2)
618
+ {
619
+ // pSample[j*2] = pSrc[j+1];
620
+ // pSample[j*2+1] = pSrc[j];
621
+ // pSample[j*2+2] = pSrc[j+1+len];
622
+ // pSample[j*2+3] = pSrc[j+len];
623
+ *((uint16_t *)(pSample+j*2)) = bswapBE16(*((uint16_t *)(pSrc+j)));
624
+ *((uint16_t *)(pSample+j*2+2)) = bswapBE16(*((uint16_t *)(pSrc+j+len)));
625
+ }
626
+ len *= 2;
627
+ }
628
+ break;
629
+
630
+ // 8-bit stereo samples
631
+ case RS_STPCM8S:
632
+ case RS_STPCM8U:
633
+ case RS_STPCM8D:
634
+ {
635
+ int iadd_l = 0, iadd_r = 0;
636
+ if (nFlags == RS_STPCM8U) { iadd_l = iadd_r = -128; }
637
+ len = pIns->nLength;
638
+ signed char *psrc = (signed char *)lpMemFile;
639
+ signed char *pSample = (signed char *)pIns->pSample;
640
+ if (len*2 > dwMemLength) break;
641
+ for (UINT j=0; j<len; j++)
642
+ {
643
+ pSample[j*2] = (signed char)(psrc[0] + iadd_l);
644
+ pSample[j*2+1] = (signed char)(psrc[len] + iadd_r);
645
+ psrc++;
646
+ if (nFlags == RS_STPCM8D)
647
+ {
648
+ iadd_l = pSample[j*2];
649
+ iadd_r = pSample[j*2+1];
650
+ }
651
+ }
652
+ len *= 2;
653
+ }
654
+ break;
655
+
656
+ // 16-bit stereo samples
657
+ case RS_STPCM16S:
658
+ case RS_STPCM16U:
659
+ case RS_STPCM16D:
660
+ {
661
+ int iadd_l = 0, iadd_r = 0;
662
+ if (nFlags == RS_STPCM16U) { iadd_l = iadd_r = -0x8000; }
663
+ len = pIns->nLength;
664
+ int16_t *psrc = (int16_t *)lpMemFile;
665
+ int16_t *pSample = (int16_t *)pIns->pSample;
666
+ if (len*4 > dwMemLength) break;
667
+ for (UINT j=0; j<len; j++)
668
+ {
669
+ pSample[j*2] = (int16_t) (bswapLE16(psrc[0]) + iadd_l);
670
+ pSample[j*2+1] = (int16_t) (bswapLE16(psrc[len]) + iadd_r);
671
+ psrc++;
672
+ if (nFlags == RS_STPCM16D)
673
+ {
674
+ iadd_l = pSample[j*2];
675
+ iadd_r = pSample[j*2+1];
676
+ }
677
+ }
678
+ len *= 4;
679
+ }
680
+ break;
681
+
682
+ // IT 2.14 compressed samples
683
+ case RS_IT2148:
684
+ case RS_IT21416:
685
+ case RS_IT2158:
686
+ case RS_IT21516:
687
+ len = dwMemLength;
688
+ if (len < 4) break;
689
+ if ((nFlags == RS_IT2148) || (nFlags == RS_IT2158))
690
+ ITUnpack8Bit(pIns->pSample, pIns->nLength, (LPBYTE)lpMemFile, dwMemLength, (nFlags == RS_IT2158));
691
+ else
692
+ ITUnpack16Bit(pIns->pSample, pIns->nLength, (LPBYTE)lpMemFile, dwMemLength, (nFlags == RS_IT21516));
693
+ break;
694
+
695
+ #ifndef MODPLUG_BASIC_SUPPORT
696
+ // 8-bit interleaved stereo samples
697
+ case RS_STIPCM8S:
698
+ case RS_STIPCM8U:
699
+ {
700
+ int iadd = 0;
701
+ if (nFlags == RS_STIPCM8U) { iadd = -0x80; }
702
+ len = pIns->nLength;
703
+ if (len*2 > dwMemLength) len = dwMemLength >> 1;
704
+ LPBYTE psrc = (LPBYTE)lpMemFile;
705
+ LPBYTE pSample = (LPBYTE)pIns->pSample;
706
+ for (UINT j=0; j<len; j++)
707
+ {
708
+ pSample[j*2] = (signed char)(psrc[0] + iadd);
709
+ pSample[j*2+1] = (signed char)(psrc[1] + iadd);
710
+ psrc+=2;
711
+ }
712
+ len *= 2;
713
+ }
714
+ break;
715
+
716
+ // 16-bit interleaved stereo samples
717
+ case RS_STIPCM16S:
718
+ case RS_STIPCM16U:
719
+ {
720
+ int iadd = 0;
721
+ if (nFlags == RS_STIPCM16U) iadd = -32768;
722
+ len = pIns->nLength;
723
+ if (len*4 > dwMemLength) len = dwMemLength >> 2;
724
+ int16_t *psrc = (int16_t *)lpMemFile;
725
+ int16_t *pSample = (int16_t *)pIns->pSample;
726
+ for (UINT j=0; j<len; j++)
727
+ {
728
+ pSample[j*2] = (int16_t)(bswapLE16(psrc[0]) + iadd);
729
+ pSample[j*2+1] = (int16_t)(bswapLE16(psrc[1]) + iadd);
730
+ psrc += 2;
731
+ }
732
+ len *= 4;
733
+ }
734
+ break;
735
+
736
+ // AMS compressed samples
737
+ case RS_AMS8:
738
+ case RS_AMS16:
739
+ len = 9;
740
+ if (dwMemLength > 9)
741
+ {
742
+ const char *psrc = lpMemFile;
743
+ char packcharacter = lpMemFile[8], *pdest = (char *)pIns->pSample;
744
+ len += bswapLE32(*((LPDWORD)(lpMemFile+4)));
745
+ if (len > dwMemLength) len = dwMemLength;
746
+ UINT dmax = pIns->nLength;
747
+ if (pIns->uFlags & CHN_16BIT) dmax <<= 1;
748
+ AMSUnpack(psrc+9, len-9, pdest, dmax, packcharacter);
749
+ }
750
+ break;
751
+
752
+ // PTM 8bit delta to 16-bit sample
753
+ case RS_PTM8DTO16:
754
+ {
755
+ len = pIns->nLength * 2;
756
+ if (len > dwMemLength) break;
757
+ int8_t *pSample = (int8_t *)pIns->pSample;
758
+ int8_t delta8 = 0;
759
+ for (UINT j=0; j<len; j++)
760
+ {
761
+ delta8 += lpMemFile[j];
762
+ *pSample++ = delta8;
763
+ }
764
+ uint16_t *pSampleW = (uint16_t *)pIns->pSample;
765
+ for (UINT j=0; j<len; j+=2) // swaparoni!
766
+ {
767
+ uint16_t rawSample = *pSampleW;
768
+ *pSampleW++ = bswapLE16(rawSample);
769
+ }
770
+ }
771
+ break;
772
+
773
+ // Huffman MDL compressed samples
774
+ case RS_MDL8:
775
+ case RS_MDL16:
776
+ len = dwMemLength;
777
+ if (len >= 4)
778
+ {
779
+ LPBYTE pSample = (LPBYTE)pIns->pSample;
780
+ LPBYTE ibuf = (LPBYTE)lpMemFile;
781
+ DWORD bitbuf = bswapLE32(*((DWORD *)ibuf));
782
+ UINT bitnum = 32;
783
+ BYTE dlt = 0, lowbyte = 0;
784
+ ibuf += 4;
785
+ for (UINT j=0; j<pIns->nLength; j++)
786
+ {
787
+ BYTE hibyte;
788
+ BYTE sign;
789
+ if (nFlags == RS_MDL16) lowbyte = (BYTE)MDLReadBits(&bitbuf, &bitnum, &ibuf, 8);
790
+ sign = (BYTE)MDLReadBits(&bitbuf, &bitnum, &ibuf, 1);
791
+ if (MDLReadBits(&bitbuf, &bitnum, &ibuf, 1))
792
+ {
793
+ hibyte = (BYTE)MDLReadBits(&bitbuf, &bitnum, &ibuf, 3);
794
+ } else
795
+ {
796
+ hibyte = 8;
797
+ while (!MDLReadBits(&bitbuf, &bitnum, &ibuf, 1)) hibyte += 0x10;
798
+ hibyte += MDLReadBits(&bitbuf, &bitnum, &ibuf, 4);
799
+ }
800
+ if (sign) hibyte = ~hibyte;
801
+ dlt += hibyte;
802
+ if (nFlags != RS_MDL16)
803
+ pSample[j] = dlt;
804
+ else
805
+ {
806
+ pSample[j<<1] = lowbyte;
807
+ pSample[(j<<1)+1] = dlt;
808
+ }
809
+ }
810
+ }
811
+ break;
812
+
813
+ case RS_DMF8:
814
+ case RS_DMF16:
815
+ len = dwMemLength;
816
+ if (len >= 4)
817
+ {
818
+ UINT maxlen = pIns->nLength;
819
+ if (pIns->uFlags & CHN_16BIT) maxlen <<= 1;
820
+ LPBYTE ibuf = (LPBYTE)lpMemFile, ibufmax = (LPBYTE)(lpMemFile+dwMemLength);
821
+ len = DMFUnpack((LPBYTE)pIns->pSample, ibuf, ibufmax, maxlen);
822
+ }
823
+ break;
824
+
825
+ #ifdef MODPLUG_TRACKER
826
+ // PCM 24-bit signed -> load sample, and normalize it to 16-bit
827
+ case RS_PCM24S:
828
+ case RS_PCM32S:
829
+ len = pIns->nLength * 3;
830
+ if (nFlags == RS_PCM32S) len += pIns->nLength;
831
+ if (len > dwMemLength) break;
832
+ if (len > 4*8)
833
+ {
834
+ UINT slsize = (nFlags == RS_PCM32S) ? 4 : 3;
835
+ LPBYTE pSrc = (LPBYTE)lpMemFile;
836
+ LONG max = 255;
837
+ if (nFlags == RS_PCM32S) pSrc++;
838
+ for (UINT j=0; j<len; j+=slsize)
839
+ {
840
+ LONG l = ((((pSrc[j+2] << 8) + pSrc[j+1]) << 8) + pSrc[j]) << 8;
841
+ l /= 256;
842
+ if (l > max) max = l;
843
+ if (-l > max) max = -l;
844
+ }
845
+ max = (max / 128) + 1;
846
+ int16_t *pDest = (int16_t *)pIns->pSample;
847
+ for (UINT k=0; k<len; k+=slsize)
848
+ {
849
+ LONG l = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
850
+ *pDest++ = (uint16_t)(l / max);
851
+ }
852
+ }
853
+ break;
854
+
855
+ // Stereo PCM 24-bit signed -> load sample, and normalize it to 16-bit
856
+ case RS_STIPCM24S:
857
+ case RS_STIPCM32S:
858
+ len = pIns->nLength * 6;
859
+ if (nFlags == RS_STIPCM32S) len += pIns->nLength * 2;
860
+ if (len > dwMemLength) break;
861
+ if (len > 8*8)
862
+ {
863
+ UINT slsize = (nFlags == RS_STIPCM32S) ? 4 : 3;
864
+ LPBYTE pSrc = (LPBYTE)lpMemFile;
865
+ LONG max = 255;
866
+ if (nFlags == RS_STIPCM32S) pSrc++;
867
+ for (UINT j=0; j<len; j+=slsize)
868
+ {
869
+ LONG l = ((((pSrc[j+2] << 8) + pSrc[j+1]) << 8) + pSrc[j]) << 8;
870
+ l /= 256;
871
+ if (l > max) max = l;
872
+ if (-l > max) max = -l;
873
+ }
874
+ max = (max / 128) + 1;
875
+ int16_t *pDest = (int16_t *)pIns->pSample;
876
+ for (UINT k=0; k<len; k+=slsize)
877
+ {
878
+ LONG lr = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
879
+ k += slsize;
880
+ LONG ll = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
881
+ pDest[0] = (int16_t)ll;
882
+ pDest[1] = (int16_t)lr;
883
+ pDest += 2;
884
+ }
885
+ }
886
+ break;
887
+
888
+ // 16-bit signed big endian interleaved stereo
889
+ case RS_STIPCM16M:
890
+ {
891
+ len = pIns->nLength;
892
+ if (len*4 > dwMemLength) len = dwMemLength >> 2;
893
+ LPCBYTE psrc = (LPCBYTE)lpMemFile;
894
+ int16_t *pSample = (int16_t *)pIns->pSample;
895
+ for (UINT j=0; j<len; j++)
896
+ {
897
+ pSample[j*2] = (int16_t)(((UINT)psrc[0] << 8) | (psrc[1]));
898
+ pSample[j*2+1] = (int16_t)(((UINT)psrc[2] << 8) | (psrc[3]));
899
+ psrc += 4;
900
+ }
901
+ len *= 4;
902
+ }
903
+ break;
904
+
905
+ #endif // MODPLUG_TRACKER
906
+ #endif // !MODPLUG_BASIC_SUPPORT
907
+
908
+ // Default: 8-bit signed PCM data
909
+ default:
910
+ len = pIns->nLength;
911
+ if (len > dwMemLength) len = pIns->nLength = dwMemLength;
912
+ SDL_memcpy(pIns->pSample, lpMemFile, len);
913
+ }
914
+ if (len > dwMemLength)
915
+ {
916
+ if (pIns->pSample)
917
+ {
918
+ pIns->nLength = 0;
919
+ CSoundFile_FreeSample(pIns->pSample);
920
+ pIns->pSample = NULL;
921
+ }
922
+ return 0;
923
+ }
924
+ CSoundFile_AdjustSampleLoop(_this, pIns);
925
+ return len;
926
+ }
927
+
928
+
929
+ void CSoundFile_AdjustSampleLoop(CSoundFile *_this, MODINSTRUMENT *pIns)
930
+ //----------------------------------------------------
931
+ {
932
+ if (!pIns->pSample) return;
933
+ if (pIns->nLength > MAX_SAMPLE_LENGTH) pIns->nLength = MAX_SAMPLE_LENGTH;
934
+ if (pIns->nLoopEnd > pIns->nLength) pIns->nLoopEnd = pIns->nLength;
935
+ if (pIns->nLoopStart > pIns->nLength+2) pIns->nLoopStart = pIns->nLength+2;
936
+ if (pIns->nLoopStart+2 >= pIns->nLoopEnd)
937
+ {
938
+ pIns->nLoopStart = pIns->nLoopEnd = 0;
939
+ pIns->uFlags &= ~CHN_LOOP;
940
+ }
941
+ UINT len = pIns->nLength;
942
+ if (pIns->uFlags & CHN_16BIT)
943
+ {
944
+ int16_t *pSample = (int16_t *)pIns->pSample;
945
+ // Adjust end of sample
946
+ if (pIns->uFlags & CHN_STEREO)
947
+ {
948
+ pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0;
949
+ pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0;
950
+ } else
951
+ {
952
+ pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0;
953
+ }
954
+ if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP)
955
+ {
956
+ // Fix bad loops
957
+ if ((pIns->nLoopEnd+3 >= pIns->nLength) || (_this->m_nType & MOD_TYPE_S3M))
958
+ {
959
+ pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart];
960
+ pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1];
961
+ pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2];
962
+ pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3];
963
+ pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4];
964
+ }
965
+ }
966
+ } else
967
+ {
968
+ signed char *pSample = pIns->pSample;
969
+ // Crappy samples (except chiptunes) ?
970
+ if ((pIns->nLength > 0x100) && (_this->m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M))
971
+ && (!(pIns->uFlags & CHN_STEREO)))
972
+ {
973
+ int smpend = pSample[pIns->nLength-1], smpfix = 0, kscan;
974
+ for (kscan=pIns->nLength-1; kscan>0; kscan--)
975
+ {
976
+ smpfix = pSample[kscan-1];
977
+ if (smpfix != smpend) break;
978
+ }
979
+ int delta = smpfix - smpend;
980
+ if (((!(pIns->uFlags & CHN_LOOP)) || (kscan > (int)pIns->nLoopEnd))
981
+ && ((delta < -8) || (delta > 8)))
982
+ {
983
+ while (kscan<(int)pIns->nLength)
984
+ {
985
+ if (!(kscan & 7))
986
+ {
987
+ if (smpfix > 0) smpfix--;
988
+ if (smpfix < 0) smpfix++;
989
+ }
990
+ pSample[kscan] = (signed char)smpfix;
991
+ kscan++;
992
+ }
993
+ }
994
+ }
995
+
996
+ // Adjust end of sample
997
+ if (pIns->uFlags & CHN_STEREO)
998
+ {
999
+ pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0;
1000
+ pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0;
1001
+
1002
+ } else
1003
+ {
1004
+ pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0;
1005
+ }
1006
+ if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP)
1007
+ {
1008
+ if ((pIns->nLoopEnd+3 >= pIns->nLength) || (_this->m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M)))
1009
+ {
1010
+ pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart];
1011
+ pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1];
1012
+ pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2];
1013
+ pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3];
1014
+ pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4];
1015
+ }
1016
+ }
1017
+ }
1018
+ }
1019
+
1020
+
1021
+ /////////////////////////////////////////////////////////////
1022
+ // Transpose <-> Frequency conversions
1023
+
1024
+ // returns 8363*2^((transp*128+ftune)/(12*128))
1025
+ DWORD CSoundFile_TransposeToFrequency(int transp, int ftune)
1026
+ //-----------------------------------------------------------
1027
+ {
1028
+ return (DWORD)(8363*SDL_pow(2, (double)(transp*128+ftune)/(1536)));
1029
+ }
1030
+
1031
+
1032
+ // returns 12*128*log2(freq/8363)
1033
+ int CSoundFile_FrequencyToTranspose(DWORD freq)
1034
+ //----------------------------------------------
1035
+ {
1036
+ return (int) (1536*(SDL_log(freq/8363.0)/SDL_log(2.0)));
1037
+ }
1038
+
1039
+
1040
+ void CSoundFile_FrequencyToTransposeInstrument(MODINSTRUMENT *psmp)
1041
+ //--------------------------------------------------------
1042
+ {
1043
+ int f2t = CSoundFile_FrequencyToTranspose(psmp->nC4Speed);
1044
+ int transp = f2t >> 7;
1045
+ int ftune = f2t & 0x7F;
1046
+ if (ftune > 80)
1047
+ {
1048
+ transp++;
1049
+ ftune -= 128;
1050
+ }
1051
+ if (transp > 127) transp = 127;
1052
+ if (transp < -127) transp = -127;
1053
+ psmp->RelativeTone = transp;
1054
+ psmp->nFineTune = ftune;
1055
+ }
1056
+
1057
+ void CSoundFile_SetRepeatCount(CSoundFile *_this, int n)
1058
+ {
1059
+ _this->m_nRepeatCount = n;
1060
+ _this->m_nInitialRepeatCount = n;
1061
+ }
1062
+
1063
+ BOOL CSoundFile_SetPatternName(CSoundFile *_this, UINT nPat, LPCSTR lpszName)
1064
+ //---------------------------------------------------------
1065
+ {
1066
+ char szName[MAX_PATTERNNAME];
1067
+ szName[0] = 0;
1068
+ // check input arguments
1069
+ if (nPat >= MAX_PATTERNS) return FALSE;
1070
+ if (lpszName == NULL) return(FALSE);
1071
+
1072
+ if (lpszName) SDL_strlcpy(szName, lpszName, MAX_PATTERNNAME);
1073
+ szName[MAX_PATTERNNAME-1] = 0;
1074
+ if (!_this->m_lpszPatternNames) _this->m_nPatternNames = 0;
1075
+ if (nPat >= _this->m_nPatternNames)
1076
+ {
1077
+ if (!lpszName[0]) return TRUE;
1078
+ UINT len = (nPat+1)*MAX_PATTERNNAME;
1079
+ char *p = (char *) SDL_malloc(len);
1080
+ if (!p) return FALSE;
1081
+ SDL_memset(p, 0, len);
1082
+ if (_this->m_lpszPatternNames)
1083
+ {
1084
+ SDL_memcpy(p, _this->m_lpszPatternNames, _this->m_nPatternNames * MAX_PATTERNNAME);
1085
+ SDL_free(_this->m_lpszPatternNames);
1086
+ _this->m_lpszPatternNames = NULL;
1087
+ }
1088
+ _this->m_lpszPatternNames = p;
1089
+ _this->m_nPatternNames = nPat + 1;
1090
+ }
1091
+ SDL_memcpy(_this->m_lpszPatternNames + nPat * MAX_PATTERNNAME, szName, MAX_PATTERNNAME);
1092
+ return TRUE;
1093
+ }
1094
+
1095
+
1096
+ UINT CSoundFile_DetectUnusedSamples(CSoundFile *_this, BOOL *pbIns)
1097
+ //-----------------------------------------------
1098
+ {
1099
+ UINT nExt = 0;
1100
+
1101
+ if (!pbIns) return 0;
1102
+ if (_this->m_nInstruments)
1103
+ {
1104
+ SDL_memset(pbIns, 0, MAX_SAMPLES * sizeof(BOOL));
1105
+ for (UINT ipat=0; ipat<MAX_PATTERNS; ipat++)
1106
+ {
1107
+ MODCOMMAND *p = _this->Patterns[ipat];
1108
+ if (p)
1109
+ {
1110
+ UINT jmax = _this->PatternSize[ipat] * _this->m_nChannels;
1111
+ for (UINT j=0; j<jmax; j++, p++)
1112
+ {
1113
+ if ((p->note) && (p->note <= NOTE_MAX))
1114
+ {
1115
+ if ((p->instr) && (p->instr < MAX_INSTRUMENTS))
1116
+ {
1117
+ INSTRUMENTHEADER *penv = _this->Headers[p->instr];
1118
+ if (penv)
1119
+ {
1120
+ UINT n = penv->Keyboard[p->note-1];
1121
+ if (n < MAX_SAMPLES) pbIns[n] = TRUE;
1122
+ }
1123
+ } else
1124
+ {
1125
+ for (UINT k=1; k<=_this->m_nInstruments; k++)
1126
+ {
1127
+ INSTRUMENTHEADER *penv = _this->Headers[k];
1128
+ if (penv)
1129
+ {
1130
+ UINT n = penv->Keyboard[p->note-1];
1131
+ if (n < MAX_SAMPLES) pbIns[n] = TRUE;
1132
+ }
1133
+ }
1134
+ }
1135
+ }
1136
+ }
1137
+ }
1138
+ }
1139
+ for (UINT ichk=1; ichk<=_this->m_nSamples; ichk++)
1140
+ {
1141
+ if ((!pbIns[ichk]) && (_this->Ins[ichk].pSample)) nExt++;
1142
+ }
1143
+ }
1144
+ return nExt;
1145
+ }
1146
+
1147
+
1148
+ BOOL CSoundFile_DestroySample(CSoundFile *_this, UINT nSample)
1149
+ //------------------------------------------
1150
+ {
1151
+ if ((!nSample) || (nSample >= MAX_SAMPLES)) return FALSE;
1152
+ if (!_this->Ins[nSample].pSample) return TRUE;
1153
+ MODINSTRUMENT *pins = &_this->Ins[nSample];
1154
+ signed char *pSample = pins->pSample;
1155
+ pins->pSample = NULL;
1156
+ pins->nLength = 0;
1157
+ pins->uFlags &= ~(CHN_16BIT);
1158
+ for (UINT i=0; i<MAX_CHANNELS; i++)
1159
+ {
1160
+ if (_this->Chn[i].pSample == pSample)
1161
+ {
1162
+ _this->Chn[i].nPos = _this->Chn[i].nLength = 0;
1163
+ _this->Chn[i].pSample = _this->Chn[i].pCurrentSample = NULL;
1164
+ }
1165
+ }
1166
+ CSoundFile_FreeSample(pSample);
1167
+ return TRUE;
1168
+ }
1169
+