Skip to content

Commit

Permalink
Add sound effects
Browse files Browse the repository at this point in the history
  • Loading branch information
justindriggers committed Jan 18, 2024
1 parent a13c5c7 commit 8c07ff9
Show file tree
Hide file tree
Showing 43 changed files with 378 additions and 114 deletions.
44 changes: 40 additions & 4 deletions alfredo/src/platform/MacAudioEngineFileLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,47 @@
NSString* path;

switch (effectType) {
case EffectType::Pop:
path = @"Balloon Pop 1.wav";
case EffectType::ButtonDown:
path = @"select-16bit-44_1khz_1.wav";
break;
case EffectType::Select:
path = @"Select 1.wav";
case EffectType::ButtonUp:
path = @"select-16bit-44_1khz_2.wav";
break;
case EffectType::Collect1:
path = @"collect1-16bit-44_1khz.wav";
break;
case EffectType::Collect2:
path = @"collect2-16bit-44_1khz.wav";
break;
case EffectType::Collect3:
path = @"collect3-16bit-44_1khz.wav";
break;
case EffectType::Collect4:
path = @"collect4-16bit-44_1khz.wav";
break;
case EffectType::Collect5:
path = @"collect5-16bit-44_1khz.wav";
break;
case EffectType::Detonate:
path = @"detonation-16bit-44_1khz.wav";
break;
case EffectType::Explosion:
path = @"explosion-16bit-44_1khz.wav";
break;
case EffectType::Heal:
path = @"heal-16bit-44_1khz.wav";
break;
case EffectType::Level:
path = @"level-16bit-44_1khz.wav";
break;
case EffectType::PowerUp:
path = @"powerup-16bit-44_1khz.wav";
break;
case EffectType::Swoop:
path = @"swoop-16bit-44_1khz.wav";
break;
case EffectType::Xp:
path = @"xp-16bit-44_1khz.wav";
break;
}

Expand Down
Binary file removed assets/audio/Balloon Pop 1.wav
Binary file not shown.
Binary file removed assets/audio/Select 1.wav
Binary file not shown.
Binary file added assets/audio/collect1-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/collect2-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/collect3-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/collect4-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/collect5-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/detonation-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/explosion-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/heal-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/level-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/powerup-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/select-16bit-44_1khz_1.wav
Binary file not shown.
Binary file added assets/audio/select-16bit-44_1khz_2.wav
Binary file not shown.
Binary file added assets/audio/swoop-16bit-44_1khz.wav
Binary file not shown.
Binary file added assets/audio/xp-16bit-44_1khz.wav
Binary file not shown.
7 changes: 3 additions & 4 deletions audio/audioengine/include/AudioEngineAudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "audio/AudioManager.h"

#include <mutex>
#include <queue>

#import <AVFoundation/AVAudioEngine.h>
Expand All @@ -25,6 +24,8 @@ class AudioEngineAudioManager : public AudioManager {

void play(SongType songType, Mode mode) override;

void stopSongs() override;

void pause() override;

void resume() override;
Expand All @@ -35,7 +36,6 @@ class AudioEngineAudioManager : public AudioManager {
std::unique_ptr<AudioEngineFileLoader> _fileLoader;
AVAudioEngine* _audioEngine;
NSMutableArray<AVAudioPlayerNode*>* _playerNodes;
AVAudioFormat* _inputFormat;

std::array<AVAudioPlayerNode*, 2> _songNodes;
int _currentSongNode = 0;
Expand All @@ -45,8 +45,7 @@ class AudioEngineAudioManager : public AudioManager {
std::unordered_map<EffectType, AVAudioPCMBuffer*> _effectBuffers;
std::unordered_map<SongType, AVAudioPCMBuffer*> _songBuffers;

std::queue<AVAudioPlayerNode*> _nodePool;
std::mutex _poolMutex;
std::queue<AVAudioPlayerNode*> _effectLruPool;

void initSongNodes();

Expand Down
64 changes: 35 additions & 29 deletions audio/audioengine/src/AudioEngineAudioManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
_audioEngine([[AVAudioEngine alloc] init]),
_playerNodes([[NSMutableArray alloc] init]) {
auto outputFormat = [_audioEngine.outputNode inputFormatForBus:0];
_inputFormat = [[AVAudioFormat alloc] initWithCommonFormat:outputFormat.commonFormat
sampleRate:outputFormat.sampleRate
channels:2
interleaved:outputFormat.isInterleaved];

[_audioEngine connect:_audioEngine.mainMixerNode
to:_audioEngine.outputNode
Expand All @@ -27,8 +23,20 @@
return;
}

loadBuffer(EffectType::Pop);
loadBuffer(EffectType::Select);
loadBuffer(EffectType::ButtonDown);
loadBuffer(EffectType::ButtonUp);
loadBuffer(EffectType::Collect1);
loadBuffer(EffectType::Collect2);
loadBuffer(EffectType::Collect3);
loadBuffer(EffectType::Collect4);
loadBuffer(EffectType::Collect5);
loadBuffer(EffectType::Detonate);
loadBuffer(EffectType::Explosion);
loadBuffer(EffectType::Heal);
loadBuffer(EffectType::Level);
loadBuffer(EffectType::PowerUp);
loadBuffer(EffectType::Swoop);
loadBuffer(EffectType::Xp);
loadBuffer(SongType::Theme);
loadBuffer(SongType::Title);
loadBuffer(SongType::GameOver);
Expand All @@ -54,23 +62,14 @@
atTime:nil
options:AVAudioPlayerNodeBufferInterrupts
completionCallbackType:AVAudioPlayerNodeCompletionDataPlayedBack
completionHandler:^(AVAudioPlayerNodeCompletionCallbackType callbackType) {
std::unique_lock<std::mutex> lock(_poolMutex);
_nodePool.push(playerNode);
}];
completionHandler:nil];
}
}

void AudioEngineAudioManager::play(SongType songType, Mode mode) {
auto generation = ++_generation;
stopSongs();

for (auto songNode : _songNodes) {
if (songNode.isPlaying) {
[songNode stop];
}

[songNode play];
}
auto generation = _generation;

auto songNode = getNextSongNode();

Expand Down Expand Up @@ -103,6 +102,18 @@
}
}

void AudioEngineAudioManager::stopSongs() {
++_generation;

for (auto songNode : _songNodes) {
if (songNode.isPlaying) {
[songNode stop];
}

[songNode play];
}
}

void AudioEngineAudioManager::pause() {
for (AVAudioPlayerNode* playerNode in _playerNodes) {
[playerNode pause];
Expand Down Expand Up @@ -147,15 +158,15 @@
format:[[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100
channels:2]];

_nodePool.push(playerNode);
_effectLruPool.push(playerNode);
}
}

void AudioEngineAudioManager::loadBuffer(EffectType effectType) {
auto file = _fileLoader->getAudioFileForEffect(effectType);

auto buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:file.processingFormat
frameCapacity:file.length];
frameCapacity:static_cast<AVAudioFrameCount>(file.length)];

NSError* error;
if (![file readIntoBuffer:buffer error:&error]) {
Expand All @@ -170,7 +181,7 @@
auto file = _fileLoader->getAudioFileForSong(songType);

auto buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:file.processingFormat
frameCapacity:file.length];
frameCapacity:static_cast<AVAudioFrameCount>(file.length)];

NSError* error;
if (![file readIntoBuffer:buffer error:&error]) {
Expand All @@ -182,14 +193,9 @@
}

AVAudioPlayerNode* AudioEngineAudioManager::getPlayerNode() {
if (_nodePool.empty()) {
return nullptr;
}

std::unique_lock<std::mutex> lock(_poolMutex);

auto result = _nodePool.front();
_nodePool.pop();
auto result = _effectLruPool.front();
_effectLruPool.pop();
_effectLruPool.push(result);

return result;
}
Expand Down
12 changes: 7 additions & 5 deletions audio/openal/include/OpenALAudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class OpenALAudioManager : public AudioManager {

void play(SongType songType, Mode mode) override;

void stopSongs() override;

void pause() override;

void resume() override;

private:
struct SongSourceState {
struct SourceState {
ALint state;
ALuint source;
std::function<void(ALint)> callback;
Expand All @@ -55,10 +57,10 @@ class OpenALAudioManager : public AudioManager {
ALCdevice* _device;
ALCcontext* _context;

std::array<ALuint, 8> _effectSources{};
std::array<SongSourceState, 2> _songSources{};
std::array<SourceState, 8> _effectSources{};
std::array<SourceState, 2> _songSources{};

std::queue<ALuint> _effectLruPool{};
std::queue<SourceState*> _effectLruPool{};
int _currentSongSource = 0;
float _time = 0.0f;
float _lastSongStartTime = 0.0f;
Expand All @@ -73,7 +75,7 @@ class OpenALAudioManager : public AudioManager {

void loadBuffer(SongType songType);

SongSourceState& getNextSongSource();
SourceState& getNextSongSource();
};

} // namespace linguine::audio

0 comments on commit 8c07ff9

Please sign in to comment.