calculate length for CBR mp3 files

custom
jacqueline 1 year ago
parent 200a43fad3
commit 2d7f9fc775
  1. 4
      src/audio/audio_source.cpp
  2. 6
      src/audio/fatfs_source.cpp
  3. 2
      src/audio/include/audio_source.hpp
  4. 2
      src/audio/include/fatfs_source.hpp
  5. 2
      src/audio/include/readahead_source.hpp
  6. 8
      src/audio/readahead_source.cpp
  7. 2
      src/codecs/include/codec.hpp
  8. 5
      src/codecs/mad.cpp

@ -34,6 +34,10 @@ auto TaggedStream::CurrentPosition() -> int64_t {
return wrapped_->CurrentPosition();
}
auto TaggedStream::Size() -> std::optional<int64_t> {
return wrapped_->Size();
}
auto TaggedStream::SetPreambleFinished() -> void {
wrapped_->SetPreambleFinished();
}

@ -5,6 +5,7 @@
*/
#include "fatfs_source.hpp"
#include <sys/_stdint.h>
#include <cstddef>
#include <cstdint>
@ -66,4 +67,9 @@ auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void {
auto FatfsSource::CurrentPosition() -> int64_t {
return f_tell(file_.get());
}
auto FatfsSource::Size() -> std::optional<int64_t> {
return f_size(file_.get());
}
} // namespace audio

@ -28,6 +28,8 @@ class TaggedStream : public codecs::IStream {
auto CurrentPosition() -> int64_t override;
auto Size() -> std::optional<int64_t> override;
auto SetPreambleFinished() -> void override;
private:

@ -34,6 +34,8 @@ class FatfsSource : public codecs::IStream {
auto CurrentPosition() -> int64_t override;
auto Size() -> std::optional<int64_t> override;
FatfsSource(const FatfsSource&) = delete;
FatfsSource& operator=(const FatfsSource&) = delete;

@ -38,6 +38,8 @@ class ReadaheadSource : public codecs::IStream {
auto CurrentPosition() -> int64_t override;
auto Size() -> std::optional<int64_t> override;
auto SetPreambleFinished() -> void override;
ReadaheadSource(const ReadaheadSource&) = delete;

@ -47,8 +47,8 @@ auto ReadaheadSource::Read(cpp::span<std::byte> dest) -> ssize_t {
// Fill the destination from our buffer, until either the buffer is drained
// or the destination is full.
while (!dest.empty() && (is_refilling_ || !xStreamBufferIsEmpty(buffer_))) {
size_t bytes_read = xStreamBufferReceive(buffer_, dest.data(),
dest.size_bytes(), 1);
size_t bytes_read =
xStreamBufferReceive(buffer_, dest.data(), dest.size_bytes(), 1);
tell_ += bytes_read;
bytes_written += bytes_read;
dest = dest.subspan(bytes_read);
@ -102,6 +102,10 @@ auto ReadaheadSource::CurrentPosition() -> int64_t {
return tell_;
}
auto ReadaheadSource::Size() -> std::optional<int64_t> {
return wrapped_->Size();
}
auto ReadaheadSource::SetPreambleFinished() -> void {
readahead_enabled_ = true;
BeginReadahead();

@ -49,6 +49,8 @@ class IStream {
virtual auto CurrentPosition() -> int64_t = 0;
virtual auto Size() -> std::optional<int64_t> = 0;
/*
* Called by codecs to indicate that they've finished parsing any header data
* within this stream, and are about to begin decoding.

@ -109,8 +109,9 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr<IStream> input)
auto vbr_length = GetVbrLength(header);
if (vbr_length) {
output.total_samples = vbr_length.value() * channels;
} else {
// FIXME: calculate length using the filesize, assuming CBR.
} else if (input->Size() && header.bitrate > 0) {
auto cbr_length = input->Size().value() / (header.bitrate / 8);
output.total_samples = cbr_length * output.sample_rate_hz * channels;
}
return output;
}

Loading…
Cancel
Save