summaryrefslogtreecommitdiff
path: root/src/audio/chunk.cpp
blob: 01760a848e3831e63e92082dc20651d07aa21efe (plain)
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
#include "chunk.hpp"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <optional>

#include "cbor.h"
#include "esp_log.h"

#include "stream_buffer.hpp"
#include "stream_message.hpp"

namespace audio {

static const std::size_t kWorkingBufferMultiple = 2;

ChunkReader::ChunkReader(std::size_t chunk_size)
    : raw_working_buffer_(static_cast<std::byte*>(
          heap_caps_malloc(chunk_size * kWorkingBufferMultiple,
                           MALLOC_CAP_SPIRAM))),
      working_buffer_(raw_working_buffer_,
                      chunk_size * kWorkingBufferMultiple) {}

ChunkReader::~ChunkReader() {
  free(raw_working_buffer_);
}

auto ChunkReader::HandleNewData(cpp::span<std::byte> data)
    -> cpp::span<std::byte> {
  assert(leftover_bytes_ + data.size() <= working_buffer_.size());
  // Copy the new data onto the front for anything that was left over from the
  // last portion. Note: this could be optimised for the '0 leftover bytes'
  // case, which technically shouldn't need a copy.
  std::copy(data.begin(), data.end(),
            working_buffer_.begin() + leftover_bytes_);
  last_data_in_working_buffer_ =
      working_buffer_.first(leftover_bytes_ + data.size());
  leftover_bytes_ = 0;
  return last_data_in_working_buffer_;
}

auto ChunkReader::HandleBytesUsed(std::size_t bytes_used) -> void {
  HandleBytesLeftOver(last_data_in_working_buffer_.size() - bytes_used);
}
auto ChunkReader::HandleBytesLeftOver(std::size_t bytes_left) -> void {
  leftover_bytes_ = bytes_left;

  // Ensure that we don't have more than a chunk of leftever bytes. This is
  // bad, because we probably won't have enough data to store the next chunk.
  assert(leftover_bytes_ <= working_buffer_.size() / kWorkingBufferMultiple);

  if (leftover_bytes_ > 0) {
    auto data_to_keep = last_data_in_working_buffer_.last(leftover_bytes_);
    std::copy(data_to_keep.begin(), data_to_keep.end(),
              working_buffer_.begin());
  }
}

auto ChunkReader::GetLeftovers() -> cpp::span<std::byte> {
  return working_buffer_.first(leftover_bytes_);
}

}  // namespace audio