Fix issues with timing stereo streams
This commit is contained in:
@@ -56,18 +56,20 @@ static const char* kTag = "audio_dec";
|
||||
|
||||
static constexpr std::size_t kCodecBufferLength = 240 * 4;
|
||||
|
||||
Timer::Timer(uint32_t sample_rate, uint32_t total_samples)
|
||||
: sample_rate_(sample_rate),
|
||||
Timer::Timer(const codecs::ICodec::OutputFormat& format)
|
||||
: format_(format),
|
||||
current_seconds_(0),
|
||||
current_sample_in_second_(0),
|
||||
total_duration_seconds_(total_samples / sample_rate) {}
|
||||
total_duration_seconds_(format.total_samples.value_or(0) /
|
||||
format.num_channels / format.sample_rate_hz) {}
|
||||
|
||||
auto Timer::AddSamples(std::size_t samples) -> void {
|
||||
bool incremented = false;
|
||||
current_sample_in_second_ += samples;
|
||||
while (current_sample_in_second_ >= sample_rate_) {
|
||||
while (current_sample_in_second_ >=
|
||||
format_.sample_rate_hz * format_.num_channels) {
|
||||
current_seconds_++;
|
||||
current_sample_in_second_ -= sample_rate_;
|
||||
current_sample_in_second_ -= format_.sample_rate_hz * format_.num_channels;
|
||||
incremented = true;
|
||||
}
|
||||
|
||||
@@ -138,8 +140,7 @@ auto AudioTask::BeginDecoding(std::shared_ptr<codecs::IStream> stream) -> bool {
|
||||
}
|
||||
|
||||
if (open_res->total_samples) {
|
||||
timer_.reset(
|
||||
new Timer(open_res->sample_rate_hz, open_res->total_samples.value()));
|
||||
timer_.reset(new Timer(open_res.value()));
|
||||
} else {
|
||||
timer_.reset();
|
||||
}
|
||||
|
||||
@@ -23,12 +23,12 @@ namespace audio {
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
Timer(uint32_t sample_rate, uint32_t total_samples);
|
||||
Timer(const codecs::ICodec::OutputFormat& format);
|
||||
|
||||
auto AddSamples(std::size_t) -> void;
|
||||
|
||||
private:
|
||||
uint32_t sample_rate_;
|
||||
codecs::ICodec::OutputFormat format_;
|
||||
|
||||
uint32_t current_seconds_;
|
||||
uint32_t current_sample_in_second_;
|
||||
|
||||
@@ -64,7 +64,7 @@ auto FoxenFlacDecoder::OpenStream(std::shared_ptr<IStream> input)
|
||||
|
||||
uint64_t num_samples = fx_flac_get_streaminfo(flac_, FLAC_KEY_N_SAMPLES);
|
||||
if (num_samples > 0) {
|
||||
format.total_samples = num_samples / fs;
|
||||
format.total_samples = num_samples;
|
||||
}
|
||||
|
||||
return format;
|
||||
|
||||
+3
-1
@@ -92,7 +92,9 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr<IStream> input)
|
||||
|
||||
auto vbr_length = GetVbrLength(header);
|
||||
if (vbr_length) {
|
||||
output.total_samples = vbr_length;
|
||||
output.total_samples = vbr_length.value() * channels;
|
||||
} else {
|
||||
// FIXME: calculate length using the filesize, assuming CBR.
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
+1
-9
@@ -125,15 +125,7 @@ auto XiphOpusDecoder::OpenStream(std::shared_ptr<IStream> input)
|
||||
auto l = op_pcm_total(opus_, -1);
|
||||
std::optional<uint32_t> length;
|
||||
if (l > 0) {
|
||||
// Output is always downmixed to two channels, but the pcm count does not
|
||||
// reflect this.
|
||||
int channels = op_channel_count(opus_, -1);
|
||||
if (channels == 1) {
|
||||
l *= 2;
|
||||
} else if (channels > 2) {
|
||||
l /= channels * 2;
|
||||
}
|
||||
length = l;
|
||||
length = l * 2;
|
||||
}
|
||||
|
||||
return OutputFormat{
|
||||
|
||||
Reference in New Issue
Block a user