Skip to content

Commit

Permalink
Merge pull request Ancurio#130 from Splendide-Imaginarius/seek-double
Browse files Browse the repository at this point in the history
Audio: use double for seeking
  • Loading branch information
Splendide-Imaginarius authored Sep 27, 2024
2 parents a5d5749 + 567ee60 commit c58d708
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/audio/aldatasource.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct ALDataSource

/* If the source doesn't support seeking, it will
* reset back to the beginning */
virtual void seekToOffset(float seconds) = 0;
virtual void seekToOffset(double seconds) = 0;

/* The frame count right after wrap around */
virtual uint32_t loopStartFrames() = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/audio/alstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void ALStream::stop()
state = Stopped;
}

void ALStream::play(float offset)
void ALStream::play(double offset)
{
if (!source)
return;
Expand Down Expand Up @@ -173,13 +173,14 @@ ALStream::State ALStream::queryState()
return state;
}

float ALStream::queryOffset()
double ALStream::queryOffset()
{
if (state == Closed || !source)
return 0;

float procOffset = static_cast<float>(procFrames) / source->sampleRate();
double procOffset = static_cast<double>(procFrames) / source->sampleRate();

// TODO: getSecOffset returns a float, we should improve precision to double.
return procOffset + AL::Source::getSecOffset(alSrc);
}

Expand Down Expand Up @@ -289,7 +290,7 @@ void ALStream::stopStream()
procFrames = 0;
}

void ALStream::startStream(float offset)
void ALStream::startStream(double offset)
{
AL::Source::clearQueue(alSrc);

Expand Down
8 changes: 4 additions & 4 deletions src/audio/alstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct ALStream
AtomicFlag threadTermReq;

AtomicFlag needsRewind;
float startOffset;
double startOffset;

float pitch;

Expand Down Expand Up @@ -93,21 +93,21 @@ struct ALStream
void close();
void open(const std::string &filename);
void stop();
void play(float offset = 0);
void play(double offset = 0);
void pause();

void setVolume(float value);
void setPitch(float value);
State queryState();
float queryOffset();
double queryOffset();
bool queryNativePitch();

private:
void closeSource();
void openSource(const std::string &filename);

void stopStream();
void startStream(float offset);
void startStream(double offset);
void pauseStream();
void resumeStream();

Expand Down
8 changes: 4 additions & 4 deletions src/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Audio::Audio(RGSSThreadData &rtData)
void Audio::bgmPlay(const char *filename,
int volume,
int pitch,
float pos,
double pos,
int track)
{
if (track == -127) {
Expand Down Expand Up @@ -356,7 +356,7 @@ void Audio::bgmSetVolume(int volume, int track)
void Audio::bgsPlay(const char *filename,
int volume,
int pitch,
float pos)
double pos)
{
p->bgs.play(filename, volume, pitch, pos);
}
Expand Down Expand Up @@ -407,12 +407,12 @@ void Audio::setupMidi()
shState->midiState().initIfNeeded(shState->config());
}

float Audio::bgmPos(int track)
double Audio::bgmPos(int track)
{
return p->getTrackByIndex(track)->playingOffset();
}

float Audio::bgsPos()
double Audio::bgsPos()
{
return p->bgs.playingOffset();
}
Expand Down
8 changes: 4 additions & 4 deletions src/audio/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Audio
void bgmPlay(const char *filename,
int volume = 100,
int pitch = 100,
float pos = 0,
double pos = 0,
int track = -127);
void bgmStop(int track = -127);
void bgmFade(int time, int track = -127);
Expand All @@ -51,7 +51,7 @@ class Audio
void bgsPlay(const char *filename,
int volume = 100,
int pitch = 100,
float pos = 0);
double pos = 0);
void bgsStop();
void bgsFade(int time);

Expand All @@ -67,8 +67,8 @@ class Audio
void seStop();

void setupMidi();
float bgmPos(int track = 0);
float bgsPos();
double bgmPos(int track = 0);
double bgsPos();

void reset();

Expand Down
6 changes: 3 additions & 3 deletions src/audio/audiostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ AudioStream::~AudioStream()
void AudioStream::play(const std::string &filename,
int volume,
int pitch,
float offset)
double offset)
{
finiFadeOutInt();

Expand Down Expand Up @@ -217,7 +217,7 @@ void AudioStream::fadeOut(int duration)
unlockStream();
}

void AudioStream::seek(float offset)
void AudioStream::seek(double offset)
{
lockStream();
stream.play(offset);
Expand Down Expand Up @@ -248,7 +248,7 @@ float AudioStream::getVolume(VolumeType type)
return volumes[type];
}

float AudioStream::playingOffset()
double AudioStream::playingOffset()
{
return stream.queryOffset();
}
Expand Down
6 changes: 3 additions & 3 deletions src/audio/audiostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ struct AudioStream
void play(const std::string &filename,
int volume,
int pitch,
float offset = 0);
double offset = 0);
void stop();
void fadeOut(int duration);
void seek(float offset);
void seek(double offset);

/* Any access to this classes 'stream' member,
* whether state query or modification, must be
Expand All @@ -141,7 +141,7 @@ struct AudioStream
void setVolume(VolumeType type, float value);
float getVolume(VolumeType type);

float playingOffset();
double playingOffset();

private:
float volumes[VolumeTypeCount];
Expand Down
2 changes: 1 addition & 1 deletion src/audio/midisource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ struct MidiSource : ALDataSource, MidiReadHandler
}

/* Midi sources cannot seek, and so always reset to beginning */
void seekToOffset(float)
void seekToOffset(double)
{
/* Reset synth */
fluid.synth_system_reset(synth);
Expand Down
9 changes: 8 additions & 1 deletion src/audio/sdlsoundsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ struct SDLSoundSource : ALDataSource
return sample->actual.rate;
}

void seekToOffset(float seconds)
void seekToOffset(double seconds)
{
if (seconds <= 0)
{
Sound_Rewind(sample);
}
else
{
// Unfortunately there is no easy API in SDL_sound for seeking with better precision than 1ms.
// TODO: Work around this by manually consuming the remaining samples.
// TODO: Also we're flooring here when we probably should be rounding.
Sound_Seek(sample, static_cast<uint32_t>(seconds * 1000));
}
}

uint32_t loopStartFrames()
Expand Down
3 changes: 2 additions & 1 deletion src/audio/vorbissource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@ struct VorbisSource : ALDataSource
return info.rate;
}

void seekToOffset(float seconds)
void seekToOffset(double seconds)
{
if (seconds <= 0)
{
ov_raw_seek(&vf, 0);
currentFrame = 0;
}

// TODO: We're flooring here when we probably should be rounding.
currentFrame = seconds * info.rate;

if (loop.valid && currentFrame > loop.end)
Expand Down

0 comments on commit c58d708

Please sign in to comment.