SoundHandle AudioSystem::LoadSound(const char * const filename) { SDL_AudioSpec spec; Uint32 len; Uint8 *wav; if (SDL_LoadWAV(filename, &spec, &wav, &len) == nullptr) { log::Error("Could not load sound ", filename, ": ", SDL_GetError()); return InvalidSound; } ALenum format = GetALFormat(spec); if (format == 0) { log::Error("Could not load sound ", filename, ": Unsupported data format"); SDL_FreeWAV(wav); return InvalidSound; } Sound *sound = m_sounds.Obtain(); alBufferData(sound->buffer, format, wav, len, spec.freq); AL_CHECK; SDL_FreeWAV(wav); return m_sounds.IndexOf(sound); }
OSStatus LoadFileData(const char *inFilePath, void* &outData, UInt32 &outDataSize, ALuint &outBufferID) { AudioFileID theAFID = 0; OSStatus result = noErr; UInt64 theFileSize = 0; AudioStreamBasicDescription theFileFormat; result = LoadFileDataInfo(inFilePath, theAFID, theFileFormat, theFileSize); outDataSize = (UInt32)theFileSize; AssertNoError("Error loading file info", fail) outData = malloc(outDataSize); result = AudioFileReadBytes(theAFID, false, 0, &outDataSize, outData); AssertNoError("Error reading file data", fail) if (!TestAudioFormatNativeEndian(theFileFormat) && (theFileFormat.mBitsPerChannel > 8)) return kSoundEngineErrInvalidFileFormat; alGenBuffers(1, &outBufferID); AssertNoOALError("Error generating buffer\n", fail); alBufferDataStaticProc(outBufferID, GetALFormat(theFileFormat), outData, outDataSize, theFileFormat.mSampleRate); AssertNoOALError("Error attaching data to buffer\n", fail); AudioFileClose(theAFID); return result; fail: if (theAFID) AudioFileClose(theAFID); if (outData) { free(outData); outData = NULL; } return result; }