diff options
Diffstat (limited to 'src/codecs/wav.cpp')
| -rw-r--r-- | src/codecs/wav.cpp | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/src/codecs/wav.cpp b/src/codecs/wav.cpp index 714ec237..f5b9d789 100644 --- a/src/codecs/wav.cpp +++ b/src/codecs/wav.cpp @@ -20,24 +20,24 @@ namespace codecs { [[maybe_unused]] static const char kTag[] = "wav"; -static inline auto bytes_to_u16(cpp::span<std::byte const, 2> bytes) +static inline auto bytes_to_u16(std::span<std::byte const, 2> bytes) -> uint16_t { return (uint16_t)bytes[0] | (uint16_t)bytes[1] << 8; } -static inline auto bytes_to_u32(cpp::span<std::byte const, 4> bytes) +static inline auto bytes_to_u32(std::span<std::byte const, 4> bytes) -> uint32_t { return (uint32_t)bytes[0] | (uint32_t)bytes[1] << 8 | (uint32_t)bytes[2] << 16 | (uint32_t)bytes[3] << 24; } -static inline auto bytes_to_str(cpp::span<std::byte const> bytes) +static inline auto bytes_to_str(std::span<std::byte const> bytes) -> std::string { return std::string(reinterpret_cast<const char*>(bytes.data()), - bytes.size_bytes()); + bytes.size_bytes()); } -static int16_t convert_f32_to_16_bit(cpp::span<const std::byte> bytes) { +static int16_t convert_f32_to_16_bit(std::span<const std::byte> bytes) { uint64_t val = 0; val = (uint8_t)bytes[3]; val = (val << 8) | (uint8_t)bytes[2]; @@ -57,7 +57,7 @@ static int16_t convert_f32_to_16_bit(cpp::span<const std::byte> bytes) { return sample::FromDouble(*fval); } -static int16_t convert_f64_to_16_bit(cpp::span<const std::byte> bytes) { +static int16_t convert_f64_to_16_bit(std::span<const std::byte> bytes) { uint64_t val = 0; val = (uint8_t)bytes[7]; val = (val << 8) | (uint8_t)bytes[6]; @@ -71,7 +71,7 @@ static int16_t convert_f64_to_16_bit(cpp::span<const std::byte> bytes) { return sample::FromDouble(*fval); } -static int16_t convert_to_16_bit(cpp::span<const std::byte> bytes) { +static int16_t convert_to_16_bit(std::span<const std::byte> bytes) { int depth = bytes.size(); int32_t val = 0; // If 8-bit Assume Unsigned @@ -82,10 +82,13 @@ static int16_t convert_to_16_bit(cpp::span<const std::byte> bytes) { switch (depth) { case 4: val = (uint8_t)bytes[3]; + [[fallthrough]]; case 3: val = (val << 8) | (uint8_t)bytes[2]; + [[fallthrough]]; case 2: val = (val << 8) | (uint8_t)bytes[1]; + [[fallthrough]]; case 1: val = (val << 8) | (uint8_t)bytes[0]; } @@ -98,7 +101,7 @@ WavDecoder::WavDecoder() : input_(), buffer_() {} WavDecoder::~WavDecoder() {} -auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) +auto WavDecoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset) -> cpp::result<OutputFormat, Error> { input_ = input; @@ -123,7 +126,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) // - end of this part, next header we care about is 'data' // - and then the next 4 bytes = 32 bit int = size of data - auto buffer_span = cpp::span{buf}; + auto buffer_span = std::span{buf}; std::string riff = bytes_to_str(buffer_span.subspan(0, 4)); if (riff != "RIFF") { @@ -131,7 +134,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) return cpp::fail(Error::kMalformedData); } - uint32_t file_size = bytes_to_u32(buffer_span.subspan(4, 4)) + 8; + // uint32_t file_size = bytes_to_u32(buffer_span.subspan(4, 4)) + 8; std::string fmt_header = bytes_to_str(buffer_span.subspan(12, 4)); ESP_LOGI(kTag, "fmt header found? %s", @@ -142,9 +145,9 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) } // Size of the fmt header, should be 16, 18 or 40 - uint32_t fmt_header_size = bytes_to_u32(buffer_span.subspan(16, 4)); + // uint32_t fmt_header_size = bytes_to_u32(buffer_span.subspan(16, 4)); - wave_format_ = bytes_to_u16(buffer_span.subspan(20, 2)); + wave_format_ = bytes_to_u16(buffer_span.subspan<20, 2>()); if (wave_format_ == kWaveFormatPCM) { ESP_LOGD(kTag, "wave format: PCM"); } else if (wave_format_ == kWaveFormatExtensible) { @@ -156,17 +159,17 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) return cpp::fail(Error::kUnsupportedFormat); } - num_channels_ = bytes_to_u16(buffer_span.subspan(22, 2)); + num_channels_ = bytes_to_u16(buffer_span.subspan<22, 2>()); - uint32_t samples_per_second = bytes_to_u32(buffer_span.subspan(24, 4)); + uint32_t samples_per_second = bytes_to_u32(buffer_span.subspan<24, 4>()); - uint32_t avg_bytes_per_second = bytes_to_u32(buffer_span.subspan(28, 4)); + // uint32_t avg_bytes_per_second = bytes_to_u32(buffer_span.subspan(28, 4)); - uint16_t block_align = bytes_to_u16(buffer_span.subspan(32, 2)); + uint16_t block_align = bytes_to_u16(buffer_span.subspan<32, 2>()); bytes_per_sample_ = block_align / num_channels_; - uint16_t bits_per_sample = bytes_to_u16(buffer_span.subspan(34, 2)); + // uint16_t bits_per_sample = bytes_to_u16(buffer_span.subspan(34, 2)); // find the start of the data chunk std::array<std::byte, 4> data_tag = {std::byte{0x64}, std::byte{0x61}, @@ -180,7 +183,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) int data_chunk_index = std::distance(buffer_span.begin(), data_loc.begin()); uint32_t data_chunk_size = - bytes_to_u32(buffer_span.subspan(data_chunk_index + 4, 4)); + bytes_to_u32(buffer_span.subspan(data_chunk_index + 4, 4).first<4>()); // calculate number of samples int number_of_samples = data_chunk_size / bytes_per_sample_; @@ -188,20 +191,20 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) // extension to the fmt chunk size (0 or 22) uint16_t extension_size = 0; if (wave_format_ == kWaveFormatExtensible) { - extension_size = bytes_to_u16(buffer_span.subspan(36, 2)); + extension_size = bytes_to_u16(buffer_span.subspan<36, 2>()); } // Parse extension if applicable if (extension_size == 22) { // Valid bits per sample - uint16_t valid_bits_per_sample = bytes_to_u16(buffer_span.subspan(38, 2)); + // uint16_t valid_bits_per_sample = bytes_to_u16(buffer_span.subspan(38, + // 2)); - uint32_t speaker_mask = bytes_to_u32(buffer_span.subspan(40, 4)); + // uint32_t speaker_mask = bytes_to_u32(buffer_span.subspan(40, 4)); // Parse subformat - subformat_ = bytes_to_u16(buffer_span.subspan(44, 2)); - if (!(subformat_ == kWaveFormatPCM || - subformat_ == kWaveFormatIEEEFloat)) { + subformat_ = bytes_to_u16(buffer_span.subspan<44, 2>()); + if (!(subformat_ == kWaveFormatPCM || subformat_ == kWaveFormatIEEEFloat)) { ESP_LOGW(kTag, "WAVE extensible subformat_ not supported"); return cpp::fail(Error::kUnsupportedFormat); } @@ -210,7 +213,8 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) int64_t data_offset = offset * samples_per_second * bytes_per_sample_; // Seek track to start of data - input->SeekTo(data_chunk_index + 8 + data_offset, IStream::SeekFrom::kStartOfStream); + input->SeekTo(data_chunk_index + 8 + data_offset, + IStream::SeekFrom::kStartOfStream); output_format_ = {.num_channels = (uint8_t)num_channels_, .sample_rate_hz = samples_per_second, @@ -219,12 +223,12 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset) return output_format_; } -auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output) +auto WavDecoder::DecodeTo(std::span<sample::Sample> output) -> cpp::result<OutputInfo, Error> { bool is_eof = buffer_.Refill(input_.get()); size_t samples_written = 0; - buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t { + buffer_.ConsumeBytes([&](std::span<std::byte> buf) -> size_t { size_t bytes_read = buf.size_bytes(); size_t frames_read = bytes_read / bytes_per_sample_ / output_format_.num_channels; @@ -254,7 +258,6 @@ auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output) return samples_written * bytes_per_sample_; }); - return OutputInfo{.samples_written = samples_written, .is_stream_finished = samples_written == 0 && is_eof}; } |
