1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* SoundBank.cpp
*
* Created on: 14 Feb 2016
* Author: jeremy
*/
#include "SoundBank.h"
#include "../Util/WavUtil.h"
#include <fstream>
#include <algorithm>
#include <stdexcept>
#include <dirent.h>
namespace Sound
{
SoundBank::SoundBank(const std::string& dataFolder) noexcept : soundBankFolder(dataFolder + "SoundBank/")
{
return;
}
SoundBank::~SoundBank()
{
return;
}
int SoundBank::AddSound(const std::vector<short>& data)
{
return AddSound(data, 1.0f);
}
int SoundBank::AddSound(const std::vector<short>& data, float volume)
{
// Add sound to collection
std::size_t sId = sounds.size();
sounds.push_back(Sound(sId, data, volume));
return sounds.back().GetId();
}
int SoundBank::AddSound(Sound&& sound, float volume)
{
// Add sound to collection
sound.id = sounds.size();
sound.volume.store(volume);
sounds.push_back(std::move(sound));
return sounds.back().GetId();
}
void SoundBank::DeleteSound(int id)
{
auto it = std::remove_if(sounds.begin(), sounds.end(), [&id](Sound& sound) { return id == sound.GetId(); });
if(it != end(sounds))
{
sounds.erase(it);
}
return;
}
void SoundBank::LoopSound(int id, bool s)
{
if(static_cast<size_t>(id) < sounds.size())
{
sounds[id].SetLoop(s);
}
}
int SoundBank::LoadSound(const std::string& filename)
{
return LoadSound(filename, 1.0f);
}
int SoundBank::LoadSound(const std::string& filename, float volume)
{
std::string fileLocation = this->soundBankFolder + filename;
// Open file
std::ifstream soundFile(fileLocation);
// Check file validity
if(!soundFile.good())
{
throw - 1;
}
soundFile.seekg(0, std::ios::end);
// Get file size in bytes
size_t fileSize = soundFile.tellg();
soundFile.seekg(0, std::ios::beg);
// HEADER
std::vector<uint8_t> header_data(44);
soundFile.read((char*)header_data.data(), header_data.size());
WavHeader header(header_data);
size_t chunkLength = header.get_subchunk2_size();
size_t dataLength = 0;
if(chunkLength + header_data.size() == fileSize)
{
dataLength = chunkLength;
}
else
{
std::string error{"Couldn't read sound file: " + fileLocation};
throw std::runtime_error(error);
}
uint32_t data_size_short = dataLength / sizeof(short);
std::vector<short> data(data_size_short);
soundFile.read((char*)data.data(), dataLength);
// Close file
soundFile.close();
// Add sound to collection
std::size_t sId = sounds.size();
sounds.push_back(Sound(sId, data, volume));
return sounds.back().GetId();
}
std::vector<std::string> SoundBank::GetSoundFiles(const std::string& dataFolder)
{
std::vector<std::string> paths;
std::string location = dataFolder + "SoundBank/";
struct dirent* ent;
DIR* directory = opendir(location.c_str());
// Scan directory
while((ent = readdir(directory)) != NULL)
{
// Check if entry is a directory
if(ent->d_type == DT_DIR)
{
// Get directory path
std::string soundsDirPath = location + std::string(ent->d_name) + "/";
std::string dirName = std::string(ent->d_name) + "/";
struct dirent* dir;
DIR* soundsDir = opendir(soundsDirPath.c_str());
while((dir = readdir(soundsDir)) != NULL)
{
// Get file name and extension
std::string fileName(dir->d_name);
std::string fileExtension = fileName.substr(fileName.find_last_of(".") + 1);
if(fileExtension == "raw")
{
paths.push_back(dirName + fileName);
}
}
closedir(soundsDir);
}
}
closedir(directory);
return paths;
}
} /* namespace Sound */
|