From 4d15ebb7de60eaa1fb9201522325779e6553a37a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 28 Feb 2024 23:14:28 +0300 Subject: [PATCH] audio::PCM.totalSamples --- src/audio/audio.h | 13 ++++++++----- src/coders/ogg.cpp | 3 ++- src/coders/ogg.h | 1 + src/coders/wav.cpp | 6 ++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/audio/audio.h b/src/audio/audio.h index cbeca812..48857e3b 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -31,26 +31,29 @@ namespace audio { struct PCM { /// @brief May contain 8 bit and 16 bit PCM data std::vector data; + size_t totalSamples; uint8_t channels; uint8_t bitsPerSample; uint sampleRate; - PCM( + PCM( std::vector data, + size_t totalSamples, uint8_t channels, uint8_t bitsPerSample, uint sampleRate - ) : data(std::move(data)), + ) : data(std::move(data)), + totalSamples(totalSamples), channels(channels), bitsPerSample(bitsPerSample), sampleRate(sampleRate) {} - inline size_t countSamples() const { - return data.size() / channels / (bitsPerSample / 8); + inline size_t countSamplesMono() const { + return totalSamples / channels; } inline duration_t getDuration() const { - return static_cast(countSamples()) / + return static_cast(countSamplesMono()) / static_cast(sampleRate); } }; diff --git a/src/coders/ogg.cpp b/src/coders/ogg.cpp index 6afc129c..d25a9bec 100644 --- a/src/coders/ogg.cpp +++ b/src/coders/ogg.cpp @@ -31,6 +31,7 @@ audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) { vorbis_info* info = ov_info(&vf, -1); uint channels = info->channels; uint sampleRate = info->rate; + size_t totalSamples = ov_pcm_total(&vf, -1); if (!headerOnly) { const int bufferSize = 4096; @@ -51,5 +52,5 @@ audio::PCM* ogg::load_pcm(const std::filesystem::path& file, bool headerOnly) { } ov_clear(&vf); - return new audio::PCM(std::move(data), channels, 16, sampleRate); + return new audio::PCM(std::move(data), totalSamples, channels, 16, sampleRate); } diff --git a/src/coders/ogg.h b/src/coders/ogg.h index c1203be2..dc9cfe33 100644 --- a/src/coders/ogg.h +++ b/src/coders/ogg.h @@ -5,6 +5,7 @@ namespace audio { struct PCM; + class Stream; } namespace ogg { diff --git a/src/coders/wav.cpp b/src/coders/wav.cpp index bb104f0b..f08804d9 100644 --- a/src/coders/wav.cpp +++ b/src/coders/wav.cpp @@ -27,8 +27,6 @@ std::int32_t convert_to_int(char* buffer, std::size_t len){ return a; } -#include - audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) { std::ifstream in(file, std::ios::binary); if(!in.is_open()){ @@ -85,7 +83,6 @@ audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) { throw std::runtime_error("could not read bits per sample"); } int bitsPerSample = convert_to_int(buffer, 2); - std::cout << "CHANN " << channels << std::endl; // data chunk header "data" if(!in.read(buffer, 4)){ @@ -110,11 +107,12 @@ audio::PCM* wav::load_pcm(const std::filesystem::path& file, bool headerOnly) { } std::vector data; + size_t totalSamples = size / (bitsPerSample / 8); if (!headerOnly) { data.resize(size); if (!in.read(data.data(), size)) { throw std::runtime_error("could not load wav data of '"+file.u8string()+"'"); } } - return new audio::PCM(std::move(data), channels, bitsPerSample, sampleRate); + return new audio::PCM(std::move(data), totalSamples, channels, bitsPerSample, sampleRate); }