AACContainer.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "AACContainer.h"
  2. #include "iostream"
  3. using namespace bell;
  4. #define SYNC_WORLD_LEN 4
  5. AACContainer::AACContainer(std::istream& istr) : bell::AudioContainer(istr) {}
  6. bool AACContainer::fillBuffer() {
  7. if (this->bytesInBuffer < AAC_MAX_FRAME_SIZE * 2) {
  8. this->istr.read((char*)buffer.data() + bytesInBuffer,
  9. buffer.size() - bytesInBuffer);
  10. this->bytesInBuffer += istr.gcount();
  11. }
  12. return this->bytesInBuffer >= AAC_MAX_FRAME_SIZE * 2;
  13. }
  14. std::byte* AACContainer::readSample(uint32_t& len) {
  15. if (!this->fillBuffer()) {
  16. len = 0;
  17. return nullptr;
  18. }
  19. // Align the data if previous read was offseted
  20. if (toConsume > 0 && toConsume <= bytesInBuffer) {
  21. memmove(buffer.data(), buffer.data() + toConsume,
  22. buffer.size() - toConsume);
  23. bytesInBuffer = bytesInBuffer - toConsume;
  24. toConsume = 0;
  25. }
  26. int startOffset =
  27. AACFindSyncWord((uint8_t*)this->buffer.data(), bytesInBuffer);
  28. if (startOffset < 0) {
  29. // Discard word
  30. toConsume = AAC_MAX_FRAME_SIZE;
  31. return nullptr;
  32. }
  33. len = bytesInBuffer - startOffset;
  34. return this->buffer.data() + startOffset;
  35. }
  36. void AACContainer::parseSetupData() {
  37. channels = 2;
  38. sampleRate = bell::SampleRate::SR_44100;
  39. bitWidth = bell::BitWidth::BW_16;
  40. }