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

Added methods to modify audio pitch #2381

Merged
merged 2 commits into from
Feb 11, 2025
Merged
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
15 changes: 15 additions & 0 deletions core/audio/AudioEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ void AudioEngine::setVolume(AUDIO_ID audioID, float volume)
}
}

void AudioEngine::setPitch(AUDIO_ID audioID, float pitch)
{
auto it = _audioIDInfoMap.find(audioID);
if (it != _audioIDInfoMap.end())
{
pitch = std::clamp(pitch, 0.5f, 2.0f);

if (it->second.pitch != pitch)
{
_audioEngineImpl->setPitch(audioID, pitch);
it->second.pitch = pitch;
}
}
}

void AudioEngine::pause(AUDIO_ID audioID)
{
auto it = _audioIDInfoMap.find(audioID);
Expand Down
17 changes: 17 additions & 0 deletions core/audio/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ class AX_DLL AudioEngine
*/
static float getVolume(AUDIO_ID audioID);

/**
* Sets pitch for an audio instance.
*
* @param audioID An audioID returned by the play2d function.
* @param pitch Volume value (range from 0.5 to 2.0).
*/
static void setPitch(AUDIO_ID audioID, float pitch);

/**
* Gets the volume value of an audio instance.
*
* @param audioID An audioID returned by the play2d function.
* @return pitch value (range from 0.5 to 2.0).
*/
static float getPitch(AUDIO_ID audioID);

/**
* Pause an audio instance.
*
Expand Down Expand Up @@ -354,6 +370,7 @@ class AX_DLL AudioEngine
ProfileHelper* profileHelper;

float volume;
float pitch;
bool loop;
float duration;
AudioState state;
Expand Down
25 changes: 25 additions & 0 deletions core/audio/AudioEngineImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ AUDIO_ID AudioEngineImpl::play2d(std::string_view filePath, bool loop, float vol
player->_alSource = alSource;
player->_loop = loop;
player->_volume = volume;
player->_pitch = 1.0f;
if (time > 0.0f)
{
player->_currTime = time;
Expand Down Expand Up @@ -644,6 +645,30 @@ void AudioEngineImpl::setVolume(AUDIO_ID audioID, float volume)
}
}

void AudioEngineImpl::setPitch(AUDIO_ID audioID, float pitch)
{
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
auto iter = _audioPlayers.find(audioID);
if (iter == _audioPlayers.end())
return;

auto player = iter->second;
lck.unlock();

player->_pitch = pitch;

if (player->_ready)
{
alSourcef(player->_alSource, AL_PITCH, pitch);

auto error = alGetError();
if (error != AL_NO_ERROR)
{
AXLOGE("{}: audio id = {}, error = {:#x}", __FUNCTION__, audioID, error);
}
}
}

void AudioEngineImpl::setLoop(AUDIO_ID audioID, bool loop)
{
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
Expand Down
1 change: 1 addition & 0 deletions core/audio/AudioEngineImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AX_DLL AudioEngineImpl : public ax::Object
bool init();
AUDIO_ID play2d(std::string_view fileFullPath, bool loop, float volume, float time);
void setVolume(AUDIO_ID audioID, float volume);
void setPitch(AUDIO_ID audioID, float pitch);
void setLoop(AUDIO_ID audioID, bool loop);
bool pause(AUDIO_ID audioID);
bool resume(AUDIO_ID audioID);
Expand Down
1 change: 1 addition & 0 deletions core/audio/AudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class AX_DLL AudioPlayer
AudioCache* _audioCache;

float _volume;
float _pitch;
bool _loop;
std::function<void(AUDIO_ID, std::string_view)> _finishCallbak;

Expand Down