audio streams: getTime fix

This commit is contained in:
MihailRis
2024-03-06 20:01:24 +03:00
parent cea4867567
commit 6874e3c812
5 changed files with 56 additions and 4 deletions
+31 -4
View File
@@ -83,6 +83,7 @@ void ALStream::bindSpeaker(speakerid_t speaker) {
sp = audio::get_speaker(speaker); sp = audio::get_speaker(speaker);
if (sp) { if (sp) {
auto alspeaker = dynamic_cast<ALSpeaker*>(sp); auto alspeaker = dynamic_cast<ALSpeaker*>(sp);
alspeaker->stream = this;
alspeaker->duration = source->getTotalDuration(); alspeaker->duration = source->getTotalDuration();
} }
} }
@@ -101,13 +102,23 @@ void ALStream::update(double delta) {
return; return;
} }
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(speaker); ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(speaker);
uint source = alspeaker->source; uint alsource = alspeaker->source;
uint processed = AL::getSourcei(source, AL_BUFFERS_PROCESSED); uint processed = AL::getSourcei(alsource, AL_BUFFERS_PROCESSED);
while (processed--) { while (processed--) {
uint buffer; uint buffer;
AL_CHECK(alSourceUnqueueBuffers(source, 1, &buffer)); AL_CHECK(alSourceUnqueueBuffers(alsource, 1, &buffer));
unusedBuffers.push(buffer); unusedBuffers.push(buffer);
uint bps = source->getBitsPerSample()/8;
uint channels = source->getChannels();
ALint bufferSize;
alGetBufferi(buffer, AL_SIZE, &bufferSize);
totalPlayedSamples += bufferSize / bps / channels;
if (source->isSeekable()) {
totalPlayedSamples %= source->getTotalSamples();
}
} }
uint preloaded = 0; uint preloaded = 0;
@@ -116,7 +127,7 @@ void ALStream::update(double delta) {
if (preloadBuffer(buffer, loop)) { if (preloadBuffer(buffer, loop)) {
preloaded++; preloaded++;
unusedBuffers.pop(); unusedBuffers.pop();
AL_CHECK(alSourceQueueBuffers(source, 1, &buffer)); AL_CHECK(alSourceQueueBuffers(alsource, 1, &buffer));
} }
} }
if (speaker->isStopped() && !alspeaker->stopped) { if (speaker->isStopped() && !alspeaker->stopped) {
@@ -128,6 +139,19 @@ void ALStream::update(double delta) {
} }
} }
duration_t ALStream::getTime() const {
uint total = totalPlayedSamples;
auto alspeaker = dynamic_cast<ALSpeaker*>(audio::get_speaker(this->speaker));
if (alspeaker) {
uint alsource = alspeaker->source;
total += static_cast<duration_t>(AL::getSourcef(alsource, AL_SAMPLE_OFFSET));
if (source->isSeekable()) {
total %= source->getTotalSamples();
}
}
return total / static_cast<duration_t>(source->getSampleRate());
}
void ALStream::setTime(duration_t time) { void ALStream::setTime(duration_t time) {
// TODO: implement // TODO: implement
} }
@@ -212,6 +236,9 @@ void ALSpeaker::stop() {
} }
duration_t ALSpeaker::getTime() const { duration_t ALSpeaker::getTime() const {
if (stream) {
return stream->getTime();
}
return static_cast<duration_t>(AL::getSourcef(source, AL_SEC_OFFSET)); return static_cast<duration_t>(AL::getSourcef(source, AL_SEC_OFFSET));
} }
+5
View File
@@ -56,6 +56,8 @@ namespace audio {
bool preloadBuffer(uint buffer, bool loop); bool preloadBuffer(uint buffer, bool loop);
public: public:
size_t totalPlayedSamples = 0;
ALStream(ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource); ALStream(ALAudio* al, std::shared_ptr<PCMStream> source, bool keepSource);
~ALStream(); ~ALStream();
@@ -64,6 +66,8 @@ namespace audio {
Speaker* createSpeaker(bool loop, int channel) override; Speaker* createSpeaker(bool loop, int channel) override;
speakerid_t getSpeaker() const override; speakerid_t getSpeaker() const override;
void update(double delta) override; void update(double delta) override;
duration_t getTime() const override;
void setTime(duration_t time) override; void setTime(duration_t time) override;
static inline constexpr uint STREAM_BUFFERS = 3; static inline constexpr uint STREAM_BUFFERS = 3;
@@ -76,6 +80,7 @@ namespace audio {
int channel; int channel;
float volume = 0.0f; float volume = 0.0f;
public: public:
ALStream* stream = nullptr;
bool stopped = true; bool stopped = true;
bool paused = false; bool paused = false;
uint source; uint source;
+4
View File
@@ -53,6 +53,10 @@ namespace audio {
void update(double delta) override { void update(double delta) override {
} }
duration_t getTime() const override {
return 0.0;
}
void setTime(duration_t time) override { void setTime(duration_t time) override {
} }
}; };
+8
View File
@@ -347,6 +347,14 @@ Channel* audio::get_channel(int index) {
return channels.at(index).get(); return channels.at(index).get();
} }
std::shared_ptr<Stream> audio::get_associated_stream(speakerid_t id) {
auto found = streams.find(id);
if (found != streams.end()) {
return found->second;
}
return nullptr;
}
size_t audio::count_speakers() { size_t audio::count_speakers() {
return speakers.size(); return speakers.size();
} }
+8
View File
@@ -182,6 +182,9 @@ namespace audio {
/// @param delta time elapsed since the last update /// @param delta time elapsed since the last update
virtual void update(double delta) = 0; virtual void update(double delta) = 0;
/// @brief Get current stream time
virtual duration_t getTime() const = 0;
/// @brief Set playhead to the selected time /// @brief Set playhead to the selected time
/// @param time selected time /// @param time selected time
virtual void setTime(duration_t time) = 0; virtual void setTime(duration_t time) = 0;
@@ -473,6 +476,11 @@ namespace audio {
/// @return channel or nullptr /// @return channel or nullptr
extern Channel* get_channel(int index); extern Channel* get_channel(int index);
/// @brief Get stream associated with speaker
/// @param id speaker id
/// @return stream or nullptr
extern std::shared_ptr<Stream> get_associated_stream(speakerid_t id);
/// @brief Get alive speakers number (including paused) /// @brief Get alive speakers number (including paused)
extern size_t count_speakers(); extern size_t count_speakers();