audio::Stream.setTime(time)

This commit is contained in:
MihailRis
2024-03-06 22:58:48 +03:00
parent 98352627ec
commit a5f0f0a780
3 changed files with 41 additions and 13 deletions
+38 -12
View File
@@ -92,17 +92,7 @@ speakerid_t ALStream::getSpeaker() const {
return speaker; return speaker;
} }
void ALStream::update(double delta) { void ALStream::unqueueBuffers(uint alsource) {
if (this->speaker == 0) {
return;
}
auto speaker = audio::get_speaker(this->speaker);
if (speaker == nullptr) {
speaker = 0;
return;
}
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(speaker);
uint alsource = alspeaker->source;
uint processed = AL::getSourcei(alsource, AL_BUFFERS_PROCESSED); uint processed = AL::getSourcei(alsource, AL_BUFFERS_PROCESSED);
while (processed--) { while (processed--) {
@@ -120,7 +110,9 @@ void ALStream::update(double delta) {
totalPlayedSamples %= source->getTotalSamples(); totalPlayedSamples %= source->getTotalSamples();
} }
} }
}
uint ALStream::enqueueBuffers(uint alsource) {
uint preloaded = 0; uint preloaded = 0;
if (!unusedBuffers.empty()) { if (!unusedBuffers.empty()) {
uint buffer = unusedBuffers.front(); uint buffer = unusedBuffers.front();
@@ -130,6 +122,24 @@ void ALStream::update(double delta) {
AL_CHECK(alSourceQueueBuffers(alsource, 1, &buffer)); AL_CHECK(alSourceQueueBuffers(alsource, 1, &buffer));
} }
} }
return preloaded;
}
void ALStream::update(double delta) {
if (this->speaker == 0) {
return;
}
auto speaker = audio::get_speaker(this->speaker);
if (speaker == nullptr) {
speaker = 0;
return;
}
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(speaker);
uint alsource = alspeaker->source;
unqueueBuffers(alsource);
uint preloaded = enqueueBuffers(alsource);
if (speaker->isStopped() && !alspeaker->stopped) { if (speaker->isStopped() && !alspeaker->stopped) {
if (preloaded) { if (preloaded) {
speaker->play(); speaker->play();
@@ -153,7 +163,20 @@ duration_t ALStream::getTime() const {
} }
void ALStream::setTime(duration_t time) { void ALStream::setTime(duration_t time) {
// TODO: implement if (!source->isSeekable())
return;
uint sample = time * source->getSampleRate();
source->seek(sample);
auto alspeaker = dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
if (alspeaker) {
AL_CHECK(alSourceStop(alspeaker->source));
unqueueBuffers(alspeaker->source);
totalPlayedSamples = sample;
enqueueBuffers(alspeaker->source);
AL_CHECK(alSourcePlay(alspeaker->source));
} else {
totalPlayedSamples = sample;
}
} }
ALSpeaker::ALSpeaker(ALAudio* al, uint source, int priority, int channel) ALSpeaker::ALSpeaker(ALAudio* al, uint source, int priority, int channel)
@@ -247,6 +270,9 @@ duration_t ALSpeaker::getDuration() const {
} }
void ALSpeaker::setTime(duration_t time) { void ALSpeaker::setTime(duration_t time) {
if (stream) {
return stream->setTime(time);
}
AL_CHECK(alSourcef(source, AL_SEC_OFFSET, static_cast<float>(time))); AL_CHECK(alSourcef(source, AL_SEC_OFFSET, static_cast<float>(time)));
} }
+2
View File
@@ -55,6 +55,8 @@ namespace audio {
bool loop = false; bool loop = false;
bool preloadBuffer(uint buffer, bool loop); bool preloadBuffer(uint buffer, bool loop);
void unqueueBuffers(uint alsource);
uint enqueueBuffers(uint alsource);
public: public:
size_t totalPlayedSamples = 0; size_t totalPlayedSamples = 0;
+1 -1
View File
@@ -134,7 +134,7 @@ public:
void seek(size_t position) override { void seek(size_t position) override {
if (!closed && seekable) { if (!closed && seekable) {
ov_raw_seek(&vf, position); ov_pcm_seek(&vf, position);
} }
} }
}; };