Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio: use double for seeking #130

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -123,7 +123,7 @@ void ALStream::stop()
state = Stopped;
}

void ALStream::play(float offset)
void ALStream::play(double offset)
{
if (!source)
return;
Expand Down Expand Up @@ -184,13 +184,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 @@ -290,7 +291,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 @@ -95,21 +95,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 @@ -222,7 +222,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 @@ -253,7 +253,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 @@ -891,7 +891,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 @@ -101,12 +101,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