28 #if AUDIO_TK == AUDIO_TK_OPENAL && defined(__APPLE__)
31 static OSStatus AudioToolBoxReadCallback(
void* context, SInt64 inPosition, UInt32 requestCount,
void* buffer, UInt32* actualCount)
33 CFDataRef audio = (CFDataRef)context;
34 CFIndex audio_length = CFDataGetLength(audio);
35 int requestOffset = inPosition + requestCount;
36 int availableBytes = audio_length - inPosition;
37 if (requestOffset > audio_length) {
38 *actualCount = availableBytes;
40 *actualCount = requestCount;
43 range.location = inPosition;
44 range.length = *actualCount;
45 CFDataGetBytes(audio, range, (UInt8*)buffer);
48 static SInt64 AudioToolBoxGetSizeProc(
void* context)
50 return CFDataGetLength((CFDataRef)context);
54 bool AppleSoundLoader::ReadInfo(
SoundInfo* result,
BYTE* data,
size_t data_length, uint32_t)
56 CFDataRef data_container = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, data, data_length, kCFAllocatorNull);
57 AudioFileID sound_file;
58 OSStatus err = AudioFileOpenWithCallbacks((
void*)data_container,
59 AudioToolBoxReadCallback,
61 AudioToolBoxGetSizeProc,
70 uint64_t sound_data_size = 0;
71 property_size =
sizeof(sound_data_size);
72 AudioFileGetProperty(sound_file, kAudioFilePropertyAudioDataByteCount, &property_size, &sound_data_size);
76 AudioFileGetProperty(sound_file, kAudioFilePropertyEstimatedDuration, &property_size, &result->
sample_length);
78 UInt32 sound_data_size_32 = sound_data_size;
80 AudioFileReadBytes(sound_file,
false, 0, &sound_data_size_32, &result->
sound_data[0]);
82 AudioStreamBasicDescription desc;
83 property_size =
sizeof(desc);
84 AudioFileGetProperty(sound_file, kAudioFilePropertyDataFormat, &property_size, &desc);
87 switch (desc.mChannelsPerFrame)
90 result->
format = desc.mBitsPerChannel == 16 ? AL_FORMAT_MONO16 : AL_FORMAT_MONO8;
93 result->
format = desc.mBitsPerChannel == 16 ? AL_FORMAT_STEREO16 : AL_FORMAT_STEREO8;
99 CFRelease(data_container);
103 AppleSoundLoader AppleSoundLoader::singleton;
106 #if AUDIO_TK == AUDIO_TK_OPENAL
110 size_t actualSizeToRead;
114 if (size_to_read*byte_size < spaceToEOF)
115 actualSizeToRead = size_to_read*byte_size;
117 actualSizeToRead = spaceToEOF;
119 if (actualSizeToRead)
121 memcpy(ptr, (
char*)data->
data + data->
data_pos, actualSizeToRead);
125 return actualSizeToRead;
159 C4MusicFileOgg *ogg =
static_cast<C4MusicFileOgg *
>(datasource);
160 size_t bytes_to_read = size_to_read*byte_size;
161 size_t bytes_read = 0u;
162 ogg->source_file.Read(ptr, bytes_to_read, &bytes_read);
163 return bytes_read / byte_size;
168 C4MusicFileOgg *ogg =
static_cast<C4MusicFileOgg *
>(datasource);
169 return ogg->source_file.Seek(
static_cast<long int>(offset), whence);
174 C4MusicFileOgg *ogg =
static_cast<C4MusicFileOgg *
>(datasource);
175 ogg->source_file.Close();
181 C4MusicFileOgg *ogg =
static_cast<C4MusicFileOgg *
>(datasource);
182 return ogg->source_file.Tell();
192 OggVorbis_File ogg_file;
193 memset(&ogg_file, 0,
sizeof(ogg_file));
194 ov_callbacks callbacks;
201 if (ov_open_callbacks(&compressed, &ogg_file,
nullptr, 0, callbacks) != 0)
208 info = ov_info(&ogg_file, -1);
209 if (info->channels == 1)
210 result->
format = AL_FORMAT_MONO16;
212 result->
format = AL_FORMAT_STEREO16;
217 const unsigned long total_size =
static_cast<unsigned int>(result->
sample_rate * result->
sample_length * 1000.0 * info->channels * 2 + 0.5);
218 const unsigned long extra_size = 1024 * 8;
222 unsigned long buffer_size = 0;
225 const int chunk_size = total_size + extra_size - std::min(total_size, buffer_size);
227 if (buffer_size+chunk_size > result->
sound_data.size())
228 result->
sound_data.resize(buffer_size+chunk_size);
229 bytes_read = ov_read(&ogg_file, (
char*)&result->
sound_data[buffer_size], chunk_size*
sizeof(
BYTE), endian, 2, 1, &bitStream);
230 buffer_size += bytes_read;
231 }
while (bytes_read > 0);
246 ALuint wav = alutCreateBufferFromFileImage((
const ALvoid *)data, data_length);
253 ALint freq, chans, bits, size;
254 alGetBufferi(wav, AL_FREQUENCY, &freq);
256 alGetBufferi(wav, AL_CHANNELS, &chans);
257 alGetBufferi(wav, AL_BITS, &bits);
258 alGetBufferi(wav, AL_SIZE, &size);
261 result->
format = AL_FORMAT_MONO8;
263 result->
format = AL_FORMAT_MONO16;
266 result->
format = AL_FORMAT_STEREO8;
268 result->
format = AL_FORMAT_STEREO16;
271 result->
sample_length = double(size) / double(bits*chans*freq/8);
280 #elif AUDIO_TK == AUDIO_TK_SDL_MIXER
282 #include <SDL_mixer.h>
284 bool SDLMixerSoundLoader::ReadInfo(
SoundInfo* result,
BYTE* data,
size_t data_length, uint32_t)
289 SDL_RWops * rwops = SDL_RWFromConstMem(data, data_length);
301 SDLMixerSoundLoader SDLMixerSoundLoader::singleton;
C4Application Application
C4MusicSystem MusicSystem
static SoundLoader * first_loader
bool ReadInfo(SoundInfo *result, BYTE *data, size_t data_length, uint32_t) override
static VorbisLoader singleton
static long file_tell_func(void *datasource)
static long mem_tell_func(void *datasource)
static int mem_close_func(void *datasource)
static int mem_seek_func(void *datasource, ogg_int64_t offset, int whence)
static int file_close_func(void *datasource)
static size_t file_read_func(void *ptr, size_t byte_size, size_t size_to_read, void *datasource)
static size_t mem_read_func(void *ptr, size_t byte_size, size_t size_to_read, void *datasource)
static int file_seek_func(void *datasource, ogg_int64_t offset, int whence)
static WavLoader singleton
bool ReadInfo(SoundInfo *result, BYTE *data, size_t data_length, uint32_t) override
std::vector< BYTE > sound_data
C4SoundHandle final_handle