diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b69f82..3cfe336 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ set(SOURCE_FILES src/platformTools.cpp src/platformTools.h src/tools.cpp src/tools.h src/audio/AudioStream.cpp src/audio/AudioStream.h + src/audio/AudioStreamSegment.cpp src/audio/AudioStreamSegment.h src/audio/DCOffset.cpp src/audio/DCOffset.h src/audio/SampleRateConverter.cpp src/audio/SampleRateConverter.h src/audio/UnboundedStream.cpp src/audio/UnboundedStream.h diff --git a/src/audio/AudioStreamSegment.cpp b/src/audio/AudioStreamSegment.cpp new file mode 100644 index 0000000..e9dc91f --- /dev/null +++ b/src/audio/AudioStreamSegment.cpp @@ -0,0 +1,50 @@ +#include "AudioStreamSegment.h" +#include + +AudioStreamSegment::AudioStreamSegment(std::unique_ptr audioStream, const TimeRange& range) : + audioStream(std::move(audioStream)) +{ + sampleOffset = range.getStart().count() * this->audioStream->getSampleRate() / 100; + sampleCount = range.getLength().count() * this->audioStream->getSampleRate() / 100; + seek(0); + + if (sampleOffset < 0 || sampleOffset + sampleCount > this->audioStream->getSampleCount()) { + throw std::invalid_argument("Segment extends beyond input stream."); + } +} + +AudioStreamSegment::AudioStreamSegment(const AudioStreamSegment& rhs, bool reset) : + audioStream(rhs.audioStream->clone(false)), + sampleOffset(rhs.sampleOffset), + sampleCount(rhs.sampleCount) +{ + if (reset) seek(0); +} + +std::unique_ptr AudioStreamSegment::clone(bool reset) const { + return std::make_unique(*this, reset); +} + +int AudioStreamSegment::getSampleRate() const { + return audioStream->getSampleRate(); +} + +int AudioStreamSegment::getSampleCount() const { + return sampleCount; +} + +int AudioStreamSegment::getSampleIndex() const { + return audioStream->getSampleIndex() - sampleOffset; +} + +void AudioStreamSegment::seek(int sampleIndex) { + audioStream->seek(sampleIndex + sampleOffset); +} + +float AudioStreamSegment::readSample() { + return audioStream->readSample(); +} + +std::unique_ptr createSegment(std::unique_ptr audioStream, const TimeRange& range) { + return std::make_unique(std::move(audioStream), range); +} diff --git a/src/audio/AudioStreamSegment.h b/src/audio/AudioStreamSegment.h new file mode 100644 index 0000000..c2df40f --- /dev/null +++ b/src/audio/AudioStreamSegment.h @@ -0,0 +1,21 @@ +#pragma once +#include