Implemented AudioStreamSegment

This commit is contained in:
Daniel Wolf 2016-04-19 22:04:43 +02:00
parent ce204c68de
commit 895b942df3
3 changed files with 72 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,50 @@
#include "AudioStreamSegment.h"
#include <stdexcept>
AudioStreamSegment::AudioStreamSegment(std::unique_ptr<AudioStream> 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<AudioStream> AudioStreamSegment::clone(bool reset) const {
return std::make_unique<AudioStreamSegment>(*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<AudioStream> createSegment(std::unique_ptr<AudioStream> audioStream, const TimeRange& range) {
return std::make_unique<AudioStreamSegment>(std::move(audioStream), range);
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <audio/AudioStream.h>
#include <TimeRange.h>
class AudioStreamSegment : public AudioStream {
public:
AudioStreamSegment(std::unique_ptr<AudioStream> audioStream, const TimeRange& range);
AudioStreamSegment(const AudioStreamSegment& rhs, bool reset);
std::unique_ptr<AudioStream> clone(bool reset) const override;
int getSampleRate() const override;
int getSampleCount() const override;
int getSampleIndex() const override;
void seek(int sampleIndex) override;
float readSample() override;
private:
std::unique_ptr<AudioStream> audioStream;
int sampleOffset, sampleCount;
};
std::unique_ptr<AudioStream> createSegment(std::unique_ptr<AudioStream> audioStream, const TimeRange& range);