///////////////////////////////////////////////////////
| 359 | |
| 360 | //////////////////////////////////////////////////////////// |
| 361 | std::uint64_t SoundFileReaderFlac::read(std::int16_t* samples, std::uint64_t maxCount) |
| 362 | { |
| 363 | assert(m_decoder && "No decoder available. Call SoundFileReaderFlac::open() to create a new one."); |
| 364 | |
| 365 | // If there are leftovers from previous call, use it first |
| 366 | const std::size_t left = m_clientData.leftovers.size(); |
| 367 | if (left > 0) |
| 368 | { |
| 369 | if (left > maxCount) |
| 370 | { |
| 371 | // There are more leftovers than needed |
| 372 | const auto signedMaxCount = static_cast<std::vector<std::int16_t>::difference_type>(maxCount); |
| 373 | std::copy(m_clientData.leftovers.begin(), m_clientData.leftovers.begin() + signedMaxCount, samples); |
| 374 | m_clientData.leftovers = std::vector<std::int16_t>(m_clientData.leftovers.begin() + signedMaxCount, |
| 375 | m_clientData.leftovers.end()); |
| 376 | return maxCount; |
| 377 | } |
| 378 | |
| 379 | // We can use all the leftovers and decode new frames |
| 380 | std::copy(m_clientData.leftovers.begin(), m_clientData.leftovers.end(), samples); |
| 381 | } |
| 382 | |
| 383 | // Reset the data that will be used in the callback |
| 384 | m_clientData.buffer = samples + left; |
| 385 | m_clientData.remaining = maxCount - left; |
| 386 | m_clientData.leftovers.clear(); |
| 387 | |
| 388 | // Decode frames one by one until we reach the requested sample count, the end of file or an error |
| 389 | while (m_clientData.remaining > 0) |
| 390 | { |
| 391 | // Everything happens in the "write" callback |
| 392 | // This will break on any fatal error (does not include EOF) |
| 393 | if (!FLAC__stream_decoder_process_single(m_decoder.get())) |
| 394 | break; |
| 395 | |
| 396 | // Break on EOF |
| 397 | if (FLAC__stream_decoder_get_state(m_decoder.get()) == FLAC__STREAM_DECODER_END_OF_STREAM) |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | return maxCount - m_clientData.remaining; |
| 402 | } |
| 403 | |
| 404 | } // namespace sf::priv |
no test coverage detected