fix. sound fx
This commit is contained in:
@ -20,6 +20,9 @@
|
|||||||
#pragma comment(lib, "mfuuid.lib")
|
#pragma comment(lib, "mfuuid.lib")
|
||||||
#pragma comment(lib, "ole32.lib")
|
#pragma comment(lib, "ole32.lib")
|
||||||
using Microsoft::WRL::ComPtr;
|
using Microsoft::WRL::ComPtr;
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
#include <AudioToolbox/AudioToolbox.h>
|
||||||
|
#include <CoreFoundation/CoreFoundation.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// SoundEffect implementation
|
// SoundEffect implementation
|
||||||
@ -143,7 +146,7 @@ bool SoundEffect::loadWAV(const std::string& filePath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SoundEffect::loadMP3(const std::string& filePath) {
|
bool SoundEffect::loadMP3(const std::string& filePath) {
|
||||||
#ifdef _WIN32
|
#if defined(_WIN32)
|
||||||
static bool mfInitialized = false;
|
static bool mfInitialized = false;
|
||||||
if (!mfInitialized) {
|
if (!mfInitialized) {
|
||||||
if (FAILED(MFStartup(MF_VERSION))) {
|
if (FAILED(MFStartup(MF_VERSION))) {
|
||||||
@ -222,6 +225,67 @@ bool SoundEffect::loadMP3(const std::string& filePath) {
|
|||||||
channels = 2;
|
channels = 2;
|
||||||
sampleRate = 44100;
|
sampleRate = 44100;
|
||||||
return true;
|
return true;
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
CFURLRef url = CFURLCreateFromFileSystemRepresentation(nullptr, reinterpret_cast<const UInt8*>(filePath.c_str()), filePath.size(), false);
|
||||||
|
if (!url) {
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] Failed to create URL for %s", filePath.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExtAudioFileRef audioFile = nullptr;
|
||||||
|
OSStatus status = ExtAudioFileOpenURL(url, &audioFile);
|
||||||
|
CFRelease(url);
|
||||||
|
if (status != noErr || !audioFile) {
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] ExtAudioFileOpenURL failed (%d) for %s", static_cast<int>(status), filePath.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AudioStreamBasicDescription clientFormat{};
|
||||||
|
clientFormat.mSampleRate = 44100.0;
|
||||||
|
clientFormat.mFormatID = kAudioFormatLinearPCM;
|
||||||
|
clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
|
||||||
|
clientFormat.mBitsPerChannel = 16;
|
||||||
|
clientFormat.mChannelsPerFrame = 2;
|
||||||
|
clientFormat.mFramesPerPacket = 1;
|
||||||
|
clientFormat.mBytesPerFrame = (clientFormat.mBitsPerChannel / 8) * clientFormat.mChannelsPerFrame;
|
||||||
|
clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame * clientFormat.mFramesPerPacket;
|
||||||
|
|
||||||
|
status = ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);
|
||||||
|
if (status != noErr) {
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] Failed to set client format (%d) for %s", static_cast<int>(status), filePath.c_str());
|
||||||
|
ExtAudioFileDispose(audioFile);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const UInt32 framesPerBuffer = 2048;
|
||||||
|
std::vector<int16_t> buffer(framesPerBuffer * clientFormat.mChannelsPerFrame);
|
||||||
|
while (true) {
|
||||||
|
AudioBufferList abl{};
|
||||||
|
abl.mNumberBuffers = 1;
|
||||||
|
abl.mBuffers[0].mNumberChannels = clientFormat.mChannelsPerFrame;
|
||||||
|
abl.mBuffers[0].mDataByteSize = framesPerBuffer * clientFormat.mBytesPerFrame;
|
||||||
|
abl.mBuffers[0].mData = buffer.data();
|
||||||
|
|
||||||
|
UInt32 framesToRead = framesPerBuffer;
|
||||||
|
status = ExtAudioFileRead(audioFile, &framesToRead, &abl);
|
||||||
|
if (status != noErr) {
|
||||||
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] ExtAudioFileRead failed (%d) for %s", static_cast<int>(status), filePath.c_str());
|
||||||
|
ExtAudioFileDispose(audioFile);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (framesToRead == 0) {
|
||||||
|
break; // EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t samplesRead = static_cast<size_t>(framesToRead) * clientFormat.mChannelsPerFrame;
|
||||||
|
pcmData.insert(pcmData.end(), buffer.data(), buffer.data() + samplesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
ExtAudioFileDispose(audioFile);
|
||||||
|
channels = static_cast<int>(clientFormat.mChannelsPerFrame);
|
||||||
|
sampleRate = static_cast<int>(clientFormat.mSampleRate);
|
||||||
|
return !pcmData.empty();
|
||||||
#else
|
#else
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] MP3 support not available on this platform");
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[SoundEffect] MP3 support not available on this platform");
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user