gosu 0.15.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) 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} +128 -23
  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 +476 -176
  155. data/{src → dependencies/stb}/stb_image_write.h +253 -131
  156. data/{src → dependencies/stb}/stb_truetype.h +262 -104
  157. data/{src → dependencies/utf8proc}/utf8proc.c +47 -29
  158. data/{src → dependencies/utf8proc}/utf8proc.h +46 -24
  159. data/{src → dependencies/utf8proc}/utf8proc_data.h +10043 -9609
  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/include/Gosu/Channel.h +25 -0
  165. data/include/Gosu/Color.h +38 -0
  166. data/{Gosu → include/Gosu}/Color.hpp +0 -0
  167. data/{Gosu → include/Gosu}/Directories.hpp +0 -0
  168. data/include/Gosu/Font.h +36 -0
  169. data/{Gosu → include/Gosu}/Font.hpp +1 -1
  170. data/{Gosu → include/Gosu}/Fwd.hpp +0 -5
  171. data/include/Gosu/Gosu.h +82 -0
  172. data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
  173. data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
  174. data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
  175. data/{Gosu → include/Gosu}/IO.hpp +0 -0
  176. data/include/Gosu/Image.h +54 -0
  177. data/{Gosu → include/Gosu}/Image.hpp +7 -6
  178. data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
  179. data/{Gosu → include/Gosu}/Input.hpp +39 -51
  180. data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
  181. data/{Gosu → include/Gosu}/Math.hpp +0 -0
  182. data/{Gosu → include/Gosu}/Platform.hpp +0 -0
  183. data/include/Gosu/Sample.h +19 -0
  184. data/include/Gosu/Song.h +24 -0
  185. data/{Gosu → include/Gosu}/Text.hpp +0 -0
  186. data/include/Gosu/TextInput.h +30 -0
  187. data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
  188. data/{Gosu → include/Gosu}/Timing.hpp +0 -0
  189. data/{Gosu → include/Gosu}/Utility.hpp +15 -4
  190. data/{Gosu → include/Gosu}/Version.hpp +2 -2
  191. data/include/Gosu/Window.h +63 -0
  192. data/{Gosu → include/Gosu}/Window.hpp +23 -25
  193. data/lib/OpenAL32.dll +0 -0
  194. data/lib/SDL2.dll +0 -0
  195. data/lib/gosu.rb +0 -3
  196. data/lib/gosu/patches.rb +0 -23
  197. data/lib/gosu/preview.rb +1 -3
  198. data/lib/gosu/swig_patches.rb +3 -2
  199. data/lib64/OpenAL32.dll +0 -0
  200. data/lib64/SDL2.dll +0 -0
  201. data/rdoc/gosu.rb +98 -22
  202. data/src/Audio.cpp +50 -224
  203. data/src/AudioFile.hpp +20 -37
  204. data/src/AudioFileAudioToolbox.cpp +237 -0
  205. data/src/AudioFileSDLSound.cpp +147 -0
  206. data/src/AudioImpl.cpp +3 -12
  207. data/src/AudioImpl.hpp +3 -1
  208. data/src/Bitmap.cpp +85 -83
  209. data/src/BitmapIO.cpp +52 -58
  210. data/src/ChannelWrapper.cpp +50 -0
  211. data/src/ColorWrapper.cpp +126 -0
  212. data/src/Constants.cpp +338 -0
  213. data/src/Font.cpp +4 -1
  214. data/src/FontWrapper.cpp +74 -0
  215. data/src/GosuWrapper.cpp +251 -0
  216. data/src/Graphics.cpp +7 -4
  217. data/src/Image.cpp +13 -16
  218. data/src/ImageWrapper.cpp +168 -0
  219. data/src/Input.cpp +412 -164
  220. data/src/LargeImageData.cpp +2 -1
  221. data/src/MarkupParser.cpp +2 -1
  222. data/src/RubyGosu.cxx +912 -172
  223. data/src/RubyGosu.h +4 -2
  224. data/src/SampleWrapper.cpp +30 -0
  225. data/src/SongWrapper.cpp +52 -0
  226. data/src/TexChunk.cpp +1 -1
  227. data/src/Text.cpp +1 -0
  228. data/src/TextBuilder.cpp +3 -1
  229. data/src/TextInputWrapper.cpp +101 -0
  230. data/src/Texture.cpp +1 -1
  231. data/src/TrueTypeFont.cpp +2 -1
  232. data/src/Utility.cpp +11 -7
  233. data/src/Window.cpp +30 -39
  234. data/src/WindowWrapper.cpp +317 -0
  235. metadata +212 -43
  236. data/Gosu/AutoLink.hpp +0 -14
  237. data/Gosu/Bitmap.hpp +0 -113
  238. data/lib/gosu/zen.rb +0 -89
  239. data/src/AudioToolboxFile.hpp +0 -210
  240. data/src/OggFile.hpp +0 -92
  241. data/src/SndFile.hpp +0 -174
  242. data/src/WinMain.cpp +0 -64
@@ -1,57 +1,40 @@
1
1
  #pragma once
2
2
 
3
- #include <Gosu/Platform.hpp>
4
- #include <vector>
3
+ #include <Gosu/IO.hpp>
4
+
5
5
  #ifdef GOSU_IS_MAC
6
+ // Ignore OpenAL deprecation warnings. If macOS stops shipping OpenAL, it's more likely that we bundle our own version
7
+ // of it than that we switch to another audio API.
8
+ #define OPENAL_DEPRECATED
6
9
  #include <OpenAL/al.h>
7
10
  #else
8
11
  #include <AL/al.h>
9
12
  #endif
10
13
 
14
+ #include <cstddef>
15
+ #include <string>
16
+ #include <vector>
17
+
11
18
  namespace Gosu
12
19
  {
13
20
  class AudioFile
14
21
  {
15
- AudioFile(const AudioFile&) = delete;
16
- AudioFile& operator=(const AudioFile&) = delete;
17
- AudioFile(AudioFile&&) = delete;
18
- AudioFile& operator=(AudioFile&&) = delete;
19
-
20
- std::vector<char> data_;
22
+ struct Impl;
23
+ std::unique_ptr<Impl> pimpl;
21
24
 
22
25
  public:
23
- AudioFile() {}
24
-
25
- virtual ~AudioFile() {}
26
-
27
- virtual ALenum format() const = 0;
26
+ explicit AudioFile(const std::string& filename);
27
+ explicit AudioFile(Reader reader);
28
+ ~AudioFile();
29
+
30
+ ALenum format() const;
28
31
 
29
- virtual ALuint sample_rate() const = 0;
32
+ ALuint sample_rate() const;
30
33
 
31
- virtual std::size_t read_data(void* dest, std::size_t length) = 0;
34
+ std::size_t read_data(void* dest, std::size_t length);
32
35
 
33
- virtual void rewind() = 0;
36
+ void rewind();
34
37
 
35
- const std::vector<char>& decoded_data()
36
- {
37
- static const std::size_t INCREMENT = 512 * 1024;
38
-
39
- if (!data_.empty()) {
40
- return data_;
41
- }
42
-
43
- for (;;) {
44
- data_.resize(data_.size() + INCREMENT);
45
-
46
- auto read_bytes = read_data(&data_[data_.size() - INCREMENT], INCREMENT);
47
-
48
- if (read_bytes < INCREMENT) {
49
- data_.resize(data_.size() - INCREMENT + read_bytes);
50
- break;
51
- }
52
- }
53
-
54
- return data_;
55
- }
38
+ const std::vector<char>& decoded_data();
56
39
  };
57
40
  }
@@ -0,0 +1,237 @@
1
+ #include <Gosu/Platform.hpp>
2
+
3
+ // The reason for having this implementation in addition to SDL_sound is that we don't currently
4
+ // use SDL at all on iOS. The duplication between this file and SDL_sound_coreaudio.c is pretty
5
+ // painful to watch, though. TODO: See if we can use parts of SDL on iOS as well?
6
+
7
+ #ifdef GOSU_IS_IPHONE
8
+
9
+ #include "AudioFile.hpp"
10
+ #include "AudioImpl.hpp"
11
+ #include <Gosu/Utility.hpp>
12
+
13
+ #import <AudioToolbox/AudioToolbox.h>
14
+ #import <AudioToolbox/AudioConverter.h>
15
+ #import <AudioToolbox/ExtendedAudioFile.h>
16
+ #import <CoreFoundation/CoreFoundation.h>
17
+ #import <Foundation/Foundation.h>
18
+
19
+ #include <algorithm>
20
+ #include <arpa/inet.h>
21
+ #include <stdexcept>
22
+ #include <vector>
23
+
24
+ static void throw_os_error(OSStatus status, unsigned line)
25
+ {
26
+ std::string what;
27
+ @autoreleasepool {
28
+ NSError* error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
29
+ NSString* message = [NSString stringWithFormat:@"Error 0x%x on line %u: %@", line, status,
30
+ error.localizedDescription];
31
+ what = message.UTF8String;
32
+ }
33
+ throw std::runtime_error(what);
34
+ }
35
+
36
+ #define CHECK_OS(status) do { if (status) throw_os_error(status, __LINE__); } while (0)
37
+
38
+ struct Gosu::AudioFile::Impl
39
+ {
40
+ Buffer buffer;
41
+ AudioFileID file_id;
42
+ ExtAudioFileRef file;
43
+ SInt64 position;
44
+ SInt64 seek_offset;
45
+
46
+ ALenum format;
47
+ ALuint sample_rate;
48
+ UInt32 bytes_per_frame;
49
+ bool big_endian;
50
+
51
+ std::vector<char> data;
52
+
53
+ static OSStatus AudioFile_ReadProc(void* in_client_data, SInt64 in_position,
54
+ UInt32 request_count, void* buffer, UInt32* actual_count)
55
+ {
56
+ const Resource& res = *static_cast<Resource*>(in_client_data);
57
+ *actual_count = std::min<UInt32>(request_count,
58
+ static_cast<UInt32>(res.size() - in_position));
59
+ res.read(in_position, *actual_count, buffer);
60
+ return noErr;
61
+ }
62
+
63
+ static SInt64 AudioFile_GetSizeProc(void* in_client_data)
64
+ {
65
+ const Resource& res = *static_cast<Resource*>(in_client_data);
66
+ return res.size();
67
+ }
68
+
69
+ void init_seek_offset()
70
+ {
71
+ AudioConverterRef ac_ref;
72
+ UInt32 acr_size = sizeof ac_ref;
73
+ CHECK_OS(ExtAudioFileGetProperty(file, kExtAudioFileProperty_AudioConverter,
74
+ &acr_size, &ac_ref));
75
+
76
+ AudioConverterPrimeInfo prime_info;
77
+ UInt32 pi_size = sizeof prime_info;
78
+ OSStatus result = AudioConverterGetProperty(ac_ref, kAudioConverterPrimeInfo,
79
+ &pi_size, &prime_info);
80
+ if (result != kAudioConverterErr_PropertyNotSupported) {
81
+ CHECK_OS(result);
82
+ seek_offset = prime_info.leadingFrames;
83
+ }
84
+ }
85
+
86
+ void init_client_format_based_on(const AudioStreamBasicDescription& base)
87
+ {
88
+ AudioStreamBasicDescription client_data = { 0 };
89
+ sample_rate = client_data.mSampleRate = 22050;
90
+ client_data.mFormatID = kAudioFormatLinearPCM;
91
+ client_data.mFormatFlags = kAudioFormatFlagIsSignedInteger |
92
+ kAudioFormatFlagsNativeEndian |
93
+ kAudioFormatFlagIsPacked;
94
+ client_data.mBitsPerChannel = 16;
95
+ client_data.mChannelsPerFrame = base.mChannelsPerFrame;
96
+ client_data.mFramesPerPacket = 1;
97
+ client_data.mBytesPerPacket =
98
+ client_data.mBytesPerFrame =
99
+ client_data.mChannelsPerFrame * client_data.mBitsPerChannel / 8;
100
+ CHECK_OS(ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat,
101
+ sizeof client_data, &client_data));
102
+
103
+ init_seek_offset();
104
+
105
+ format = client_data.mChannelsPerFrame == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
106
+ }
107
+
108
+ void init()
109
+ {
110
+ // Streaming starts at beginning
111
+ position = 0;
112
+
113
+ // Unless overridden later, assume that the index into seek() is 0-based
114
+ seek_offset = 0;
115
+
116
+ AudioStreamBasicDescription desc;
117
+ UInt32 size_of_property = sizeof desc;
118
+ CHECK_OS(ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileDataFormat,
119
+ &size_of_property, &desc));
120
+
121
+ // Sample rate for OpenAL
122
+ sample_rate = desc.mSampleRate;
123
+
124
+ // Sanity checks
125
+ if (desc.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
126
+ throw std::runtime_error("Non-interleaved formats are unsupported");
127
+ }
128
+
129
+ // Easy formats
130
+ format = 0;
131
+ if (desc.mChannelsPerFrame == 1 && desc.mBitsPerChannel == 16) {
132
+ format = AL_FORMAT_MONO16;
133
+ }
134
+ else if (desc.mChannelsPerFrame == 2 && desc.mBitsPerChannel == 16) {
135
+ format = AL_FORMAT_STEREO16;
136
+ }
137
+
138
+ if (format == 0 ||
139
+ // If format not native for OpenAL, set client data format to enable conversion
140
+ desc.mFormatFlags & kAudioFormatFlagIsBigEndian ||
141
+ desc.mFormatFlags & kAudioFormatFlagIsFloat ||
142
+ !(desc.mFormatFlags & kAudioFormatFlagIsSignedInteger)) {
143
+ init_client_format_based_on(desc);
144
+ }
145
+ else {
146
+ // Just set the old format as the client format so
147
+ // ExtAudioFileSeek will work for us.
148
+ CHECK_OS(ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat,
149
+ sizeof desc, &desc));
150
+ }
151
+ }
152
+ };
153
+
154
+ Gosu::AudioFile::AudioFile(const std::string& filename)
155
+ : pimpl(new Impl)
156
+ {
157
+ NSURL* URL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:filename.c_str()]];
158
+ CHECK_OS(ExtAudioFileOpenURL((__bridge CFURLRef) URL, &pimpl->file));
159
+
160
+ pimpl->file_id = 0;
161
+
162
+ pimpl->init();
163
+ }
164
+
165
+ Gosu::AudioFile::AudioFile(Reader reader)
166
+ : pimpl(new Impl)
167
+ {
168
+ pimpl->buffer.resize(reader.resource().size() - reader.position());
169
+ reader.read(pimpl->buffer.data(), pimpl->buffer.size());
170
+
171
+ void* client_data = &pimpl->buffer;
172
+ CHECK_OS(AudioFileOpenWithCallbacks(client_data, Impl::AudioFile_ReadProc, nullptr,
173
+ Impl::AudioFile_GetSizeProc, nullptr, 0, &pimpl->file_id));
174
+ CHECK_OS(ExtAudioFileWrapAudioFileID(pimpl->file_id, false, &pimpl->file));
175
+
176
+ pimpl->init();
177
+ }
178
+
179
+ Gosu::AudioFile::~AudioFile()
180
+ {
181
+ ExtAudioFileDispose(pimpl->file);
182
+
183
+ if (pimpl->file_id) {
184
+ AudioFileClose(pimpl->file_id);
185
+ }
186
+ }
187
+
188
+ ALenum Gosu::AudioFile::format() const
189
+ {
190
+ return pimpl->format;
191
+ }
192
+
193
+ ALuint Gosu::AudioFile::sample_rate() const
194
+ {
195
+ return pimpl->sample_rate;
196
+ }
197
+
198
+ void Gosu::AudioFile::rewind()
199
+ {
200
+ CHECK_OS(ExtAudioFileSeek(pimpl->file, 0 + pimpl->seek_offset));
201
+ }
202
+
203
+ std::size_t Gosu::AudioFile::read_data(void* dest, size_t length)
204
+ {
205
+ AudioBufferList abl;
206
+ abl.mNumberBuffers = 1;
207
+ abl.mBuffers[0].mNumberChannels = 1;
208
+ abl.mBuffers[0].mDataByteSize = static_cast<UInt32>(length);
209
+ abl.mBuffers[0].mData = dest;
210
+ UInt32 numFrames = 0xffffffff; // give us as many frames as possible given our buffer
211
+ CHECK_OS(ExtAudioFileRead(pimpl->file, &numFrames, &abl));
212
+ return abl.mBuffers[0].mDataByteSize;
213
+ }
214
+
215
+ const std::vector<char>& Gosu::AudioFile::decoded_data()
216
+ {
217
+ if (!pimpl->data.empty()) {
218
+ return pimpl->data;
219
+ }
220
+
221
+ static const std::size_t INCREMENT = 512 * 1024;
222
+
223
+ for (;;) {
224
+ pimpl->data.resize(pimpl->data.size() + INCREMENT);
225
+
226
+ auto read_bytes = read_data(&pimpl->data[pimpl->data.size() - INCREMENT], INCREMENT);
227
+
228
+ if (read_bytes < INCREMENT) {
229
+ pimpl->data.resize(pimpl->data.size() - INCREMENT + read_bytes);
230
+ break;
231
+ }
232
+ }
233
+
234
+ return pimpl->data;
235
+ }
236
+
237
+ #endif
@@ -0,0 +1,147 @@
1
+ #include <Gosu/Platform.hpp>
2
+
3
+ #ifndef GOSU_IS_IPHONE
4
+
5
+ #include "AudioFile.hpp"
6
+ #include "AudioImpl.hpp"
7
+
8
+ #include <SDL_sound.h>
9
+
10
+ #include <cstdlib>
11
+ #include <cstring>
12
+ #include <algorithm>
13
+ #include <memory>
14
+ #include <mutex>
15
+ #include <vector>
16
+
17
+ struct Gosu::AudioFile::Impl
18
+ {
19
+ Buffer buffer;
20
+
21
+ std::shared_ptr<Sound_Sample> sample;
22
+
23
+ std::vector<char> data;
24
+
25
+ Impl()
26
+ {
27
+ static std::once_flag initialized;
28
+ std::call_once(initialized, Sound_Init);
29
+ // Don't bother calling Sound_Quit(); it's hard to know when to clean up within the scope
30
+ // of a Ruby C extension. Let the operating system clean up after us.
31
+ }
32
+ };
33
+
34
+ Gosu::AudioFile::AudioFile(const std::string& filename)
35
+ : pimpl(new Impl)
36
+ {
37
+ // We need to set a "desired" audio format, otherwise we may encounter data formats such as
38
+ // floating-point samples that Apple's OpenAL implementation does not support.
39
+ Sound_AudioInfo desired;
40
+ // Prefer 16-bit signed data which is supported by all versions of OpenAL.
41
+ desired.format = AUDIO_S16;
42
+ // 0 means "use whatever we have" (see SDL_sound.c, init_sample).
43
+ desired.channels = 0;
44
+ desired.rate = 0;
45
+
46
+ pimpl->sample.reset(Sound_NewSampleFromFile(filename.c_str(), &desired, 4096),
47
+ Sound_FreeSample);
48
+ if (!pimpl->sample) {
49
+ std::string message = "Could not parse audio file " + filename;
50
+ if (const char* error = Sound_GetError()) {
51
+ message += ": ";
52
+ message += error;
53
+ }
54
+ throw std::runtime_error(message);
55
+ }
56
+ }
57
+
58
+ Gosu::AudioFile::AudioFile(Reader reader)
59
+ : pimpl(new Impl)
60
+ {
61
+ pimpl->buffer.resize(reader.resource().size() - reader.position());
62
+ reader.read(pimpl->buffer.data(), pimpl->buffer.size());
63
+ pimpl->sample.reset(Sound_NewSampleFromMem(reinterpret_cast<Uint8*>(pimpl->buffer.data()),
64
+ static_cast<Uint32>(pimpl->buffer.size()),
65
+ "", nullptr, 4096),
66
+ Sound_FreeSample);
67
+ if (!pimpl->sample) {
68
+ std::string message = "Could not parse audio file";
69
+ if (const char* error = Sound_GetError()) {
70
+ message += ": ";
71
+ message += error;
72
+ }
73
+ throw std::runtime_error(message);
74
+ }
75
+ }
76
+
77
+ Gosu::AudioFile::~AudioFile()
78
+ {
79
+ }
80
+
81
+ ALenum Gosu::AudioFile::format() const
82
+ {
83
+ auto channels = pimpl->sample->desired.channels;
84
+ auto format = pimpl->sample->desired.format;
85
+
86
+ if (channels == 1 && SDL_AUDIO_ISINT(format)) {
87
+ if (SDL_AUDIO_BITSIZE(format) == 8) return AL_FORMAT_MONO8;
88
+ if (SDL_AUDIO_BITSIZE(format) == 16) return AL_FORMAT_MONO16;
89
+ }
90
+ if (channels == 2 && SDL_AUDIO_ISINT(format)) {
91
+ if (SDL_AUDIO_BITSIZE(format) == 8) return AL_FORMAT_STEREO8;
92
+ if (SDL_AUDIO_BITSIZE(format) == 16) return AL_FORMAT_STEREO16;
93
+ }
94
+
95
+ throw std::runtime_error("Unsupported number of channels: " + std::to_string(channels) +
96
+ " and format: " + std::to_string(format));
97
+ }
98
+
99
+ ALuint Gosu::AudioFile::sample_rate() const
100
+ {
101
+ return pimpl->sample->actual.rate;
102
+ }
103
+
104
+ void Gosu::AudioFile::rewind()
105
+ {
106
+ Sound_Rewind(pimpl->sample.get());
107
+ }
108
+
109
+ std::size_t Gosu::AudioFile::read_data(void* dest, size_t length)
110
+ {
111
+ std::size_t read = 0;
112
+ while (length > 0) {
113
+ // TODO offset magic
114
+ auto result = static_cast<std::size_t>(Sound_Decode(pimpl->sample.get()));
115
+ if (result == 0) break;
116
+ auto to_copy = std::min(length, result);
117
+ std::memcpy(dest, pimpl->sample->buffer, to_copy);
118
+ dest = static_cast<char*>(dest) + to_copy;
119
+ length -= to_copy;
120
+ read += to_copy;
121
+ }
122
+ return read;
123
+ }
124
+
125
+ const std::vector<char>& Gosu::AudioFile::decoded_data()
126
+ {
127
+ if (!pimpl->data.empty()) {
128
+ return pimpl->data;
129
+ }
130
+
131
+ static const std::size_t INCREMENT = 512 * 1024;
132
+
133
+ for (;;) {
134
+ pimpl->data.resize(pimpl->data.size() + INCREMENT);
135
+
136
+ auto read_bytes = read_data(&pimpl->data[pimpl->data.size() - INCREMENT], INCREMENT);
137
+
138
+ if (read_bytes < INCREMENT) {
139
+ pimpl->data.resize(pimpl->data.size() - INCREMENT + read_bytes);
140
+ break;
141
+ }
142
+ }
143
+
144
+ return pimpl->data;
145
+ }
146
+
147
+ #endif