// Decode bool Sound::decode(const RString& path, SoundInfo& info, float** data) { bool decoded = false; AudioSource* source = 0; if (path.find(OGG_FILE_EXT) != RString::npos) { #if MOAI_WITH_VORBIS OggAudioSource* as = new OggAudioSource(); source = as; if(as->init(path, true)) decoded = true; #endif } else { #if defined(__APPLE__) ExtAudioFileAudioSource *as = new ExtAudioFileAudioSource(); source = as; if(as->init(path, true)) decoded = true; #elif defined(__ANDROID__) | defined(__linux__) | defined(__OPENAL__) | defined ( __QNX__ ) WaveFileAudioSource *as = new WaveFileAudioSource(); source = as; if(as->init(path, true)) decoded = true; #else DShowAudioSource* as = new DShowAudioSource(); source = as; if(as->init(path, true)) decoded = true; #endif } // Couldn't decode the file if(!decoded) { if(source) delete source; return false; } RAudioBuffer* buffer = source->getBuffer(); UInt32 size = buffer->getDataSize(); // Allocate space and copy the buffer *data = (float*)new char[size]; buffer->copyInto(*data); info.mChannels = source->getNumChannels(); info.mBitsPerSample = 32; info.mSampleRate = source->getSampleRate(); info.mTotalFrames = size / 4 / source->getNumChannels(); info.mLength = (double)size / 4.0 / source->getNumChannels() / source->getSampleRate(); if(source) delete source; return true; }
Sound* Sound::create(const RString& path, bool loadIntoMemory) { Sound* prevSound = UNTZ::System::get()->getData()->getInMemorySound(path); Sound* newSound = new Sound(); if (path.find(OGG_FILE_EXT) != RString::npos) { OggAudioSource* source; if(prevSound && loadIntoMemory && prevSound->getData()->getSource()->isLoadedInMemory()) // Use the existing AudioSource source = (OggAudioSource*)prevSound->getData()->getSource().get(); else // Create a new AudioSource source = new OggAudioSource(); if(source->init(path, loadIntoMemory)) { newSound->mpData = new UNTZ::SoundData(); newSound->mpData->mPath = path; if(prevSound) // Share the audio source newSound->mpData->mpSource = prevSound->getData()->getSource(); else // This is the first use of the audio soruce...set it explicitly newSound->mpData->mpSource = AudioSourcePtr(source); System::get()->getData()->mMixer.addSound(newSound); } else { delete source; delete newSound; return 0; } } else { #if defined(WIN32) DShowAudioSource* source; if(prevSound && loadIntoMemory && prevSound->getData()->getSource()->isLoadedInMemory()) source = (DShowAudioSource*)prevSound->getData()->getSource().get(); else source = new DShowAudioSource(); #elif defined(__APPLE__) ExtAudioFileAudioSource *source = 0; if(prevSound && loadIntoMemory && prevSound->getData()->getSource()->isLoadedInMemory()) source = (ExtAudioFileAudioSource*)prevSound->getData()->getSource().get(); else source = new ExtAudioFileAudioSource(); #else WaveFileAudioSource *source = 0; if(prevSound && loadIntoMemory && prevSound->getData()->getSource()->isLoadedInMemory()) source = (WaveFileAudioSource*)prevSound->getData()->getSource().get(); else source = new WaveFileAudioSource(); #endif if(source->init(path, loadIntoMemory)) { newSound->mpData = new UNTZ::SoundData(); newSound->mpData->mPath = path; if(prevSound) // Share the audio source newSound->mpData->mpSource = prevSound->getData()->getSource(); else // This is the first use of the audio soruce...set it explicitly newSound->mpData->mpSource = AudioSourcePtr(source); System::get()->getData()->mMixer.addSound(newSound); } else { delete source; delete newSound; return 0; } } return newSound; }