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.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/COPYING +1 -1
- data/dependencies/SDL/include/SDL.h +138 -0
- data/dependencies/SDL/include/SDL_assert.h +293 -0
- data/dependencies/SDL/include/SDL_atomic.h +295 -0
- data/dependencies/SDL/include/SDL_audio.h +859 -0
- data/dependencies/SDL/include/SDL_bits.h +121 -0
- data/dependencies/SDL/include/SDL_blendmode.h +123 -0
- data/dependencies/SDL/include/SDL_clipboard.h +71 -0
- data/dependencies/SDL/include/SDL_config.h +55 -0
- data/dependencies/SDL/include/SDL_config_android.h +182 -0
- data/dependencies/SDL/include/SDL_config_iphoneos.h +207 -0
- data/dependencies/SDL/include/SDL_config_macosx.h +266 -0
- data/dependencies/SDL/include/SDL_config_minimal.h +85 -0
- data/dependencies/SDL/include/SDL_config_os2.h +188 -0
- data/dependencies/SDL/include/SDL_config_pandora.h +135 -0
- data/dependencies/SDL/include/SDL_config_psp.h +165 -0
- data/dependencies/SDL/include/SDL_config_windows.h +288 -0
- data/dependencies/SDL/include/SDL_config_winrt.h +243 -0
- data/dependencies/SDL/include/SDL_config_wiz.h +149 -0
- data/dependencies/SDL/include/SDL_copying.h +20 -0
- data/dependencies/SDL/include/SDL_cpuinfo.h +299 -0
- data/dependencies/SDL/include/SDL_egl.h +1676 -0
- data/dependencies/SDL/include/SDL_endian.h +263 -0
- data/dependencies/SDL/include/SDL_error.h +112 -0
- data/dependencies/SDL/include/SDL_events.h +827 -0
- data/dependencies/SDL/include/SDL_filesystem.h +136 -0
- data/dependencies/SDL/include/SDL_gamecontroller.h +541 -0
- data/dependencies/SDL/include/SDL_gesture.h +87 -0
- data/dependencies/SDL/include/SDL_haptic.h +1247 -0
- data/dependencies/SDL/include/SDL_hints.h +1578 -0
- data/dependencies/SDL/include/SDL_joystick.h +499 -0
- data/dependencies/SDL/include/SDL_keyboard.h +217 -0
- data/dependencies/SDL/include/SDL_keycode.h +351 -0
- data/dependencies/SDL/include/SDL_loadso.h +81 -0
- data/dependencies/SDL/include/SDL_locale.h +101 -0
- data/dependencies/SDL/include/SDL_log.h +211 -0
- data/dependencies/SDL/include/SDL_main.h +180 -0
- data/dependencies/SDL/include/SDL_messagebox.h +146 -0
- data/dependencies/SDL/include/SDL_metal.h +117 -0
- data/dependencies/SDL/include/SDL_misc.h +75 -0
- data/dependencies/SDL/include/SDL_mouse.h +302 -0
- data/dependencies/SDL/include/SDL_mutex.h +251 -0
- data/dependencies/SDL/include/SDL_name.h +33 -0
- data/dependencies/SDL/include/SDL_opengl.h +2183 -0
- data/dependencies/SDL/include/SDL_opengl_glext.h +11180 -0
- data/dependencies/SDL/include/SDL_opengles.h +39 -0
- data/dependencies/SDL/include/SDL_opengles2.h +52 -0
- data/dependencies/SDL/include/SDL_opengles2_gl2.h +621 -0
- data/dependencies/SDL/include/SDL_opengles2_gl2ext.h +2050 -0
- data/dependencies/SDL/include/SDL_opengles2_gl2platform.h +30 -0
- data/dependencies/SDL/include/SDL_opengles2_khrplatform.h +282 -0
- data/dependencies/SDL/include/SDL_pixels.h +479 -0
- data/dependencies/SDL/include/SDL_platform.h +198 -0
- data/dependencies/SDL/include/SDL_power.h +75 -0
- data/dependencies/SDL/include/SDL_quit.h +58 -0
- data/dependencies/SDL/include/SDL_rect.h +174 -0
- data/dependencies/SDL/include/SDL_render.h +1158 -0
- data/dependencies/SDL/include/SDL_revision.h +2 -0
- data/dependencies/SDL/include/SDL_rwops.h +283 -0
- data/dependencies/SDL/include/SDL_scancode.h +413 -0
- data/dependencies/SDL/include/SDL_sensor.h +267 -0
- data/dependencies/SDL/include/SDL_shape.h +144 -0
- data/dependencies/SDL/include/SDL_stdinc.h +647 -0
- data/dependencies/SDL/include/SDL_surface.h +563 -0
- data/dependencies/SDL/include/SDL_system.h +325 -0
- data/dependencies/SDL/include/SDL_syswm.h +354 -0
- data/dependencies/SDL/include/SDL_test.h +69 -0
- data/dependencies/SDL/include/SDL_test_assert.h +105 -0
- data/dependencies/SDL/include/SDL_test_common.h +218 -0
- data/dependencies/SDL/include/SDL_test_compare.h +69 -0
- data/dependencies/SDL/include/SDL_test_crc32.h +124 -0
- data/dependencies/SDL/include/SDL_test_font.h +81 -0
- data/dependencies/SDL/include/SDL_test_fuzzer.h +384 -0
- data/dependencies/SDL/include/SDL_test_harness.h +134 -0
- data/dependencies/SDL/include/SDL_test_images.h +78 -0
- data/dependencies/SDL/include/SDL_test_log.h +67 -0
- data/dependencies/SDL/include/SDL_test_md5.h +129 -0
- data/dependencies/SDL/include/SDL_test_memory.h +63 -0
- data/dependencies/SDL/include/SDL_test_random.h +115 -0
- data/dependencies/SDL/include/SDL_thread.h +366 -0
- data/dependencies/SDL/include/SDL_timer.h +115 -0
- data/dependencies/SDL/include/SDL_touch.h +102 -0
- data/dependencies/SDL/include/SDL_types.h +29 -0
- data/dependencies/SDL/include/SDL_version.h +162 -0
- data/dependencies/SDL/include/SDL_video.h +1282 -0
- data/dependencies/SDL/include/SDL_vulkan.h +276 -0
- data/dependencies/SDL/include/begin_code.h +166 -0
- data/dependencies/SDL/include/close_code.h +40 -0
- data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
- data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
- data/dependencies/SDL_sound/SDL_sound.c +795 -0
- data/dependencies/SDL_sound/SDL_sound.h +725 -0
- data/dependencies/SDL_sound/SDL_sound_aiff.c +537 -0
- data/dependencies/SDL_sound/SDL_sound_au.c +352 -0
- data/dependencies/SDL_sound/SDL_sound_coreaudio.c +747 -0
- data/dependencies/SDL_sound/SDL_sound_flac.c +182 -0
- data/dependencies/SDL_sound/SDL_sound_internal.h +304 -0
- data/dependencies/SDL_sound/SDL_sound_modplug.c +228 -0
- data/dependencies/SDL_sound/SDL_sound_mp3.c +184 -0
- data/dependencies/SDL_sound/SDL_sound_raw.c +164 -0
- data/dependencies/SDL_sound/SDL_sound_shn.c +1309 -0
- data/dependencies/SDL_sound/SDL_sound_voc.c +550 -0
- data/dependencies/SDL_sound/SDL_sound_vorbis.c +223 -0
- data/dependencies/SDL_sound/SDL_sound_wav.c +783 -0
- data/dependencies/SDL_sound/dr_flac.h +5906 -0
- data/dependencies/SDL_sound/dr_mp3.h +2832 -0
- data/dependencies/SDL_sound/libmodplug/fastmix.c +1748 -0
- data/dependencies/SDL_sound/libmodplug/libmodplug.h +1001 -0
- data/dependencies/SDL_sound/libmodplug/load_669.c +188 -0
- data/dependencies/SDL_sound/libmodplug/load_abc.c +4725 -0
- data/dependencies/SDL_sound/libmodplug/load_amf.c +403 -0
- data/dependencies/SDL_sound/libmodplug/load_ams.c +587 -0
- data/dependencies/SDL_sound/libmodplug/load_dbm.c +357 -0
- data/dependencies/SDL_sound/libmodplug/load_dmf.c +531 -0
- data/dependencies/SDL_sound/libmodplug/load_dsm.c +232 -0
- data/dependencies/SDL_sound/libmodplug/load_far.c +253 -0
- data/dependencies/SDL_sound/libmodplug/load_it.c +796 -0
- data/dependencies/SDL_sound/libmodplug/load_mdl.c +488 -0
- data/dependencies/SDL_sound/libmodplug/load_med.c +757 -0
- data/dependencies/SDL_sound/libmodplug/load_mid.c +1405 -0
- data/dependencies/SDL_sound/libmodplug/load_mod.c +269 -0
- data/dependencies/SDL_sound/libmodplug/load_mt2.c +546 -0
- data/dependencies/SDL_sound/libmodplug/load_mtm.c +142 -0
- data/dependencies/SDL_sound/libmodplug/load_okt.c +192 -0
- data/dependencies/SDL_sound/libmodplug/load_pat.c +1143 -0
- data/dependencies/SDL_sound/libmodplug/load_pat.h +25 -0
- data/dependencies/SDL_sound/libmodplug/load_psm.c +350 -0
- data/dependencies/SDL_sound/libmodplug/load_ptm.c +204 -0
- data/dependencies/SDL_sound/libmodplug/load_s3m.c +325 -0
- data/dependencies/SDL_sound/libmodplug/load_stm.c +180 -0
- data/dependencies/SDL_sound/libmodplug/load_ult.c +206 -0
- data/dependencies/SDL_sound/libmodplug/load_umx.c +51 -0
- data/dependencies/SDL_sound/libmodplug/load_xm.c +554 -0
- data/dependencies/SDL_sound/libmodplug/mmcmp.c +382 -0
- data/dependencies/SDL_sound/libmodplug/modplug.c +170 -0
- data/dependencies/SDL_sound/libmodplug/modplug.h +90 -0
- data/dependencies/SDL_sound/libmodplug/snd_dsp.c +301 -0
- data/dependencies/SDL_sound/libmodplug/snd_flt.c +63 -0
- data/dependencies/SDL_sound/libmodplug/snd_fx.c +2350 -0
- data/dependencies/SDL_sound/libmodplug/sndfile.c +1169 -0
- data/dependencies/SDL_sound/libmodplug/sndmix.c +1034 -0
- data/dependencies/SDL_sound/libmodplug/tables.h +371 -0
- data/{src/stb_vorbis.c → dependencies/SDL_sound/stb_vorbis.h} +143 -78
- data/dependencies/al_soft/AL/al.h +655 -0
- data/dependencies/al_soft/AL/alc.h +270 -0
- data/dependencies/al_soft/AL/alext.h +585 -0
- data/dependencies/al_soft/AL/efx-creative.h +3 -0
- data/dependencies/al_soft/AL/efx-presets.h +402 -0
- data/dependencies/al_soft/AL/efx.h +762 -0
- data/dependencies/al_soft/x64/libOpenAL32.dll.a +0 -0
- data/dependencies/al_soft/x86/libOpenAL32.dll.a +0 -0
- data/{src → dependencies/stb}/stb_image.h +330 -127
- data/{src → dependencies/stb}/stb_image_write.h +156 -85
- data/{src → dependencies/stb}/stb_truetype.h +192 -69
- data/{src → dependencies/utf8proc}/utf8proc.c +0 -0
- data/{src → dependencies/utf8proc}/utf8proc.h +0 -0
- data/{src → dependencies/utf8proc}/utf8proc_data.h +0 -0
- data/ext/gosu/extconf.rb +53 -39
- data/{Gosu → include/Gosu}/Audio.hpp +6 -8
- data/include/Gosu/Bitmap.hpp +100 -0
- data/{Gosu → include/Gosu}/Buttons.hpp +104 -44
- data/{Gosu → include/Gosu}/Color.hpp +0 -0
- data/{Gosu → include/Gosu}/Directories.hpp +0 -0
- data/{Gosu → include/Gosu}/Font.hpp +1 -1
- data/{Gosu → include/Gosu}/Fwd.hpp +0 -5
- data/{Gosu → include/Gosu}/Gosu.hpp +0 -0
- data/{Gosu → include/Gosu}/Graphics.hpp +0 -0
- data/{Gosu → include/Gosu}/GraphicsBase.hpp +0 -0
- data/{Gosu → include/Gosu}/IO.hpp +0 -0
- data/{Gosu → include/Gosu}/Image.hpp +7 -6
- data/{Gosu → include/Gosu}/ImageData.hpp +0 -0
- data/{Gosu → include/Gosu}/Input.hpp +39 -51
- data/{Gosu → include/Gosu}/Inspection.hpp +0 -0
- data/{Gosu → include/Gosu}/Math.hpp +0 -0
- data/{Gosu → include/Gosu}/Platform.hpp +0 -0
- data/{Gosu → include/Gosu}/Text.hpp +0 -0
- data/{Gosu → include/Gosu}/TextInput.hpp +0 -0
- data/{Gosu → include/Gosu}/Timing.hpp +0 -0
- data/{Gosu → include/Gosu}/Utility.hpp +15 -4
- data/{Gosu → include/Gosu}/Version.hpp +3 -3
- data/{Gosu → include/Gosu}/Window.hpp +46 -34
- data/lib/OpenAL32.dll +0 -0
- data/lib/SDL2.dll +0 -0
- data/lib/gosu.rb +0 -3
- data/lib/gosu/patches.rb +0 -23
- data/lib/gosu/preview.rb +1 -3
- data/lib/gosu/swig_patches.rb +6 -8
- data/lib64/OpenAL32.dll +0 -0
- data/lib64/SDL2.dll +0 -0
- data/rdoc/gosu.rb +112 -23
- data/src/Audio.cpp +50 -224
- data/src/AudioFile.hpp +20 -37
- data/src/AudioFileAudioToolbox.cpp +237 -0
- data/src/AudioFileSDLSound.cpp +147 -0
- data/src/AudioImpl.cpp +3 -12
- data/src/AudioImpl.hpp +3 -1
- data/src/Bitmap.cpp +85 -83
- data/src/BitmapIO.cpp +52 -58
- data/src/Font.cpp +4 -1
- data/src/Graphics.cpp +7 -4
- data/src/Image.cpp +13 -16
- data/src/Input.cpp +412 -164
- data/src/LargeImageData.cpp +2 -1
- data/src/MarkupParser.cpp +2 -1
- data/src/Resolution.cpp +8 -8
- data/src/RubyGosu.cxx +1184 -352
- data/src/RubyGosu.h +3 -2
- data/src/TexChunk.cpp +1 -1
- data/src/Text.cpp +1 -0
- data/src/TextBuilder.cpp +3 -1
- data/src/Texture.cpp +1 -1
- data/src/TrueTypeFont.cpp +2 -1
- data/src/TrueTypeFontWin.cpp +3 -3
- data/src/Utility.cpp +11 -7
- data/src/Window.cpp +90 -62
- data/src/WindowUIKit.cpp +21 -9
- metadata +195 -46
- data/Gosu/AutoLink.hpp +0 -14
- data/Gosu/Bitmap.hpp +0 -113
- data/lib/gosu/zen.rb +0 -89
- data/src/AudioToolboxFile.hpp +0 -210
- data/src/MPEGFile.hpp +0 -90
- data/src/OggFile.hpp +0 -92
- data/src/SndFile.hpp +0 -174
- data/src/WinMain.cpp +0 -64
data/src/AudioFile.hpp
CHANGED
@@ -1,57 +1,40 @@
|
|
1
1
|
#pragma once
|
2
2
|
|
3
|
-
#include <Gosu/
|
4
|
-
|
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
|
-
|
16
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
26
|
+
explicit AudioFile(const std::string& filename);
|
27
|
+
explicit AudioFile(Reader reader);
|
28
|
+
~AudioFile();
|
29
|
+
|
30
|
+
ALenum format() const;
|
28
31
|
|
29
|
-
|
32
|
+
ALuint sample_rate() const;
|
30
33
|
|
31
|
-
|
34
|
+
std::size_t read_data(void* dest, std::size_t length);
|
32
35
|
|
33
|
-
|
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
|