Skip to content

Commit 92a59bf

Browse files
authored
Added methods to modify audio pitch (#2381)
* Added methods to modify audio pitch * Replaced if statments with clamp
1 parent e7ee137 commit 92a59bf

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

core/audio/AudioEngine.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ void AudioEngine::setVolume(AUDIO_ID audioID, float volume)
216216
}
217217
}
218218

219+
void AudioEngine::setPitch(AUDIO_ID audioID, float pitch)
220+
{
221+
auto it = _audioIDInfoMap.find(audioID);
222+
if (it != _audioIDInfoMap.end())
223+
{
224+
pitch = std::clamp(pitch, 0.5f, 2.0f);
225+
226+
if (it->second.pitch != pitch)
227+
{
228+
_audioEngineImpl->setPitch(audioID, pitch);
229+
it->second.pitch = pitch;
230+
}
231+
}
232+
}
233+
219234
void AudioEngine::pause(AUDIO_ID audioID)
220235
{
221236
auto it = _audioIDInfoMap.find(audioID);

core/audio/AudioEngine.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ class AX_DLL AudioEngine
189189
*/
190190
static float getVolume(AUDIO_ID audioID);
191191

192+
/**
193+
* Sets pitch for an audio instance.
194+
*
195+
* @param audioID An audioID returned by the play2d function.
196+
* @param pitch Volume value (range from 0.5 to 2.0).
197+
*/
198+
static void setPitch(AUDIO_ID audioID, float pitch);
199+
200+
/**
201+
* Gets the volume value of an audio instance.
202+
*
203+
* @param audioID An audioID returned by the play2d function.
204+
* @return pitch value (range from 0.5 to 2.0).
205+
*/
206+
static float getPitch(AUDIO_ID audioID);
207+
192208
/**
193209
* Pause an audio instance.
194210
*
@@ -354,6 +370,7 @@ class AX_DLL AudioEngine
354370
ProfileHelper* profileHelper;
355371

356372
float volume;
373+
float pitch;
357374
bool loop;
358375
float duration;
359376
AudioState state;

core/audio/AudioEngineImpl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ AUDIO_ID AudioEngineImpl::play2d(std::string_view filePath, bool loop, float vol
551551
player->_alSource = alSource;
552552
player->_loop = loop;
553553
player->_volume = volume;
554+
player->_pitch = 1.0f;
554555
if (time > 0.0f)
555556
{
556557
player->_currTime = time;
@@ -644,6 +645,30 @@ void AudioEngineImpl::setVolume(AUDIO_ID audioID, float volume)
644645
}
645646
}
646647

648+
void AudioEngineImpl::setPitch(AUDIO_ID audioID, float pitch)
649+
{
650+
std::unique_lock<std::recursive_mutex> lck(_threadMutex);
651+
auto iter = _audioPlayers.find(audioID);
652+
if (iter == _audioPlayers.end())
653+
return;
654+
655+
auto player = iter->second;
656+
lck.unlock();
657+
658+
player->_pitch = pitch;
659+
660+
if (player->_ready)
661+
{
662+
alSourcef(player->_alSource, AL_PITCH, pitch);
663+
664+
auto error = alGetError();
665+
if (error != AL_NO_ERROR)
666+
{
667+
AXLOGE("{}: audio id = {}, error = {:#x}", __FUNCTION__, audioID, error);
668+
}
669+
}
670+
}
671+
647672
void AudioEngineImpl::setLoop(AUDIO_ID audioID, bool loop)
648673
{
649674
std::unique_lock<std::recursive_mutex> lck(_threadMutex);

core/audio/AudioEngineImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class AX_DLL AudioEngineImpl : public ax::Object
5151
bool init();
5252
AUDIO_ID play2d(std::string_view fileFullPath, bool loop, float volume, float time);
5353
void setVolume(AUDIO_ID audioID, float volume);
54+
void setPitch(AUDIO_ID audioID, float pitch);
5455
void setLoop(AUDIO_ID audioID, bool loop);
5556
bool pause(AUDIO_ID audioID);
5657
bool resume(AUDIO_ID audioID);

core/audio/AudioPlayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class AX_DLL AudioPlayer
7171
AudioCache* _audioCache;
7272

7373
float _volume;
74+
float _pitch;
7475
bool _loop;
7576
std::function<void(AUDIO_ID, std::string_view)> _finishCallbak;
7677

0 commit comments

Comments
 (0)