--------------------------------- AudioAsset::LoadOggFile Read a OGG Vorbis file from binary data using STB Vorbis
| 204 | // Read a OGG Vorbis file from binary data using STB Vorbis |
| 205 | // |
| 206 | bool AudioAsset::LoadOggFile(AudioBufferData &bufferData, std::vector<uint8> const& binaryContent) |
| 207 | { |
| 208 | int e = 0; |
| 209 | stb_vorbis* vorbis = stb_vorbis_open_memory(binaryContent.data(), (int)binaryContent.size(), &e, NULL); |
| 210 | if (!vorbis) return false; |
| 211 | if (e) |
| 212 | { |
| 213 | stb_vorbis_get_error(vorbis); |
| 214 | stb_vorbis_close(vorbis); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | stb_vorbis_info info = stb_vorbis_get_info(vorbis); |
| 219 | |
| 220 | switch (info.channels) |
| 221 | { |
| 222 | case 1:bufferData.format = AL_FORMAT_MONO16; break; |
| 223 | case 2:bufferData.format = AL_FORMAT_STEREO16; break; |
| 224 | default: |
| 225 | LOG(std::string("Only mono and stereo supported by openAL, numChannels: ") + std::to_string(info.channels), core::LogLevel::Warning); |
| 226 | stb_vorbis_close(vorbis); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | int32 samples = stb_vorbis_stream_length_in_samples(vorbis); |
| 231 | const int32 bufferSize = samples * info.channels; |
| 232 | |
| 233 | ALshort* pcm = new ALshort[bufferSize]; |
| 234 | int32 size = 0; |
| 235 | int32 result = 0; |
| 236 | |
| 237 | while (size < bufferSize) |
| 238 | { |
| 239 | result = stb_vorbis_get_samples_short_interleaved(vorbis, info.channels, pcm + size, bufferSize - size); |
| 240 | if (result > 0) size += result * info.channels; |
| 241 | else break; |
| 242 | } |
| 243 | if (size == 0) |
| 244 | { |
| 245 | stb_vorbis_close(vorbis); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | bufferData.data = pcm; |
| 250 | bufferData.size = size * sizeof(ALshort); |
| 251 | bufferData.frequency = info.sample_rate; |
| 252 | |
| 253 | stb_vorbis_close(vorbis); |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | //--------------------------------- |
| 259 | // AudioAsset::ConvertToMono |
nothing calls this directly
no outgoing calls
no test coverage detected