| 291 | } |
| 292 | |
| 293 | void GenericAudio::Decode(uint8_t* output_buffer, int buffer_length) { |
| 294 | bool channel_active = false; |
| 295 | float total_volume = 0; |
| 296 | int samples_per_frame = buffer_length / output_format.channels / 2; |
| 297 | |
| 298 | assert(buffer_length > 0); |
| 299 | |
| 300 | if (sample_buffer.size() != (size_t)buffer_length) { |
| 301 | sample_buffer.resize(buffer_length); |
| 302 | } |
| 303 | if (mixer_buffer.size() != (size_t)buffer_length) { |
| 304 | mixer_buffer.resize(buffer_length); |
| 305 | } |
| 306 | scrap_buffer_size = samples_per_frame * output_format.channels * sizeof(uint32_t); |
| 307 | if (scrap_buffer.size() != scrap_buffer_size) { |
| 308 | scrap_buffer.resize(scrap_buffer_size); |
| 309 | } |
| 310 | std::fill(mixer_buffer.begin(), mixer_buffer.end(), '\0'); |
| 311 | |
| 312 | for (unsigned i = 0; i < nr_of_bgm_channels + nr_of_se_channels; i++) { |
| 313 | int read_bytes = 0; |
| 314 | int channels = 0; |
| 315 | int samplesize = 0; |
| 316 | int frequency = 0; |
| 317 | AudioDecoder::Format sampleformat; |
| 318 | float volume; |
| 319 | |
| 320 | // Mix BGM and SE together; |
| 321 | bool is_bgm_channel = i < nr_of_bgm_channels; |
| 322 | bool channel_used = false; |
| 323 | |
| 324 | if (is_bgm_channel) { |
| 325 | BgmChannel& currently_mixed_channel = BGM_Channels[i]; |
| 326 | float current_master_volume = cfg.music_volume.Get() / 100.0f; |
| 327 | |
| 328 | if (currently_mixed_channel.decoder && !currently_mixed_channel.paused) { |
| 329 | if (currently_mixed_channel.stopped) { |
| 330 | currently_mixed_channel.decoder.reset(); |
| 331 | } else { |
| 332 | currently_mixed_channel.decoder->Update(std::chrono::microseconds(1000 * 1000 / 60)); |
| 333 | volume = current_master_volume * (currently_mixed_channel.decoder->GetVolume() / 100.0); |
| 334 | currently_mixed_channel.decoder->GetFormat(frequency, sampleformat, channels); |
| 335 | samplesize = AudioDecoder::GetSamplesizeForFormat(sampleformat); |
| 336 | |
| 337 | total_volume += volume; |
| 338 | |
| 339 | // determine how much data has to be read from this channel (but cap at the bounds of the scrap buffer) |
| 340 | unsigned bytes_to_read = (samplesize * channels * samples_per_frame); |
| 341 | bytes_to_read = (bytes_to_read < scrap_buffer_size) ? bytes_to_read : scrap_buffer_size; |
| 342 | |
| 343 | read_bytes = currently_mixed_channel.decoder->Decode(scrap_buffer.data(), bytes_to_read); |
| 344 | |
| 345 | if (read_bytes <= 0) { |
| 346 | // An error occured when reading - the channel is faulty - discard |
| 347 | currently_mixed_channel.decoder.reset(); |
| 348 | continue; // skip this loop run - there is nothing to mix |
| 349 | } |
| 350 |
no test coverage detected