SoundHandle AudioDevice::createSound(const std::string &name) { // Perform cleanup before creating new sounds audioCleanup(); auto itr = m_audioFiles.find(name); if(itr != m_audioFiles.end()) { // Check if data hasn't been loaded for this file previously if(!alIsBuffer(itr->second.buffer) || itr->second.buffer == 0) { // Open file and save initial data SF_INFO fileInfo; SNDFILE *file = sf_open(itr->second.fileName.c_str(), SFM_READ, &fileInfo); if(file == NULL) { JL_WARNING_LOG("Could not open audio file '%s'", itr->second.fileName.c_str()); return SoundHandle(); } // Create new buffer alGenBuffers(1, &itr->second.buffer); // Read whole file int sampleCount = fileInfo.frames * fileInfo.channels; std::vector<ALshort> fileData(sampleCount); sf_read_short(file, &fileData[0], sampleCount); alBufferData( itr->second.buffer, AudioDevice::getFormatFromChannels(fileInfo.channels), &fileData[0], sampleCount*sizeof(ALushort), fileInfo.samplerate); sf_close(file); } // Create new audio source m_sounds.push_back( SoundHandle(new AudioChunk(itr->second.buffer, itr->second.fileName))); return m_sounds.back(); } else { JL_WARNING_LOG("Couldn't find any sound by the name '%s'", name.c_str()); return SoundHandle(nullptr); } }
void Sound::LoadToMap(std::string name, double volume, int play_rate){ int tmphandle; std::string loadname = "Sounds/" + name; tmphandle = LoadSoundMem(loadname.c_str()); if (tmphandle != -1){ std::vector<std::string> extensionList = { ".wav", ".ogg", ".mp3" }; for (auto &i : extensionList){ Func::strReplace(name, i, ""); } handlemap.emplace(name, SoundHandle(tmphandle, volume, play_rate)); return; } OutputDebugString(("\n" + name + " is not exist\n").c_str()); }