<commit_before>#ifdef PIXELBOOST_PLATFORM_IOS

#include <vector>

#import "ObjectAL.h"
#import "ALSoundSource.h"

#include "pixelboost/audio/soundManager.h"
#include "pixelboost/file/fileHelpers.h"

using namespace pixelboost;

Sound::Sound(const std::string& name, bool compressed, float volume, float pitch)
    : _Compressed(compressed)
    , _Name(name)
    , _Pitch(pitch)
    , _Volume(volume)
{
    _Id = SoundManager::Instance()->SfxGetId();
}

int Sound::GetId() const
{
    return _Id;
}

void Sound::Play()
{
    SoundManager::Instance()->SfxPlay(*this);
}

void Sound::Stop()
{
    SoundManager::Instance()->SfxStop(*this);
}

bool Sound::IsPlaying() const
{
    return false;
}

const std::string& Sound::GetName() const
{
    return _Name;
}

bool Sound::IsCompressed() const
{
    return _Compressed;
}

void Sound::SetCompressed(bool compressed)
{
    _Compressed = true;
}

bool Sound::IsLooping() const
{
    return _Looping;
}

void Sound::SetLooping(bool looping)
{
    _Looping = looping;
    SoundManager::Instance()->SfxUpdateLooping(*this);
}

float Sound::GetPitch() const
{
    return _Pitch;
}

void Sound::SetPitch(float pitch)
{
    _Pitch = pitch;
    SoundManager::Instance()->SfxUpdatePitch(*this);
}

float Sound::GetVolume() const
{
    return _Volume;
}

void Sound::SetVolume(float volume)
{
    _Volume = volume;
    SoundManager::Instance()->SfxUpdateVolume(*this);
}

SoundManager::SoundManager()
{
    _CurrentBgmName = "";
    _MuteBgm = false;
    _MuteSfx = false;
    
    [OALSimpleAudio sharedInstance].allowIpod = NO;
    [OALSimpleAudio sharedInstance].honorSilentSwitch = YES;
}

SoundManager::~SoundManager()
{
    
}

SoundManager* SoundManager::Instance()
{
    static SoundManager* instance = new SoundManager();
    return instance;
}

void SoundManager::Update(float time)
{
    std::vector<int> inactiveSounds;
    
    for (std::map<int, void*>::iterator it = _Sounds.begin(); it != _Sounds.end(); ++it)
    {
        id<ALSoundSource> src = (id<ALSoundSource>)it->second;
        
        if (src.playing == false)
            inactiveSounds.push_back(it->first);
    }
    
    for (std::vector<int>::iterator it = inactiveSounds.begin(); it != inactiveSounds.end(); ++it)
    {
        _Sounds.erase(*it);
    }
}
    
void SoundManager::MuteBgm(bool mute)
{
    _MuteBgm = mute;
    
    if (_MuteBgm)
    {
        std::string currentBgm = _CurrentBgmName;
        StopBgm();
        _CurrentBgmName = currentBgm;
    } else {
        PlayBgm(_CurrentBgmName, _CurrentBgmLoop, _CurrentBgmVolume);
    }
}

void SoundManager::MuteSfx(bool mute)
{
    _MuteSfx = mute;
}

void SoundManager::LoadBgm(const std::string& name)
{

}

void SoundManager::LoadSfx(const std::string& name, bool compressed)
{
    //if (compressed)
        return;
    
    std::string fileName = FileHelpers::GetRootPath() + "/data/audio/sfx/" + name + (compressed ? ".mp3" : ".wav");
    
    [[OALSimpleAudio sharedInstance] preloadEffect:[NSString stringWithUTF8String:fileName.c_str()]];
}

void SoundManager::PlayBgm(const std::string& name, bool loop, float volume)
{
    _CurrentBgmName = name;
    _CurrentBgmLoop = loop;
    _CurrentBgmVolume = volume;
    
    if (_MuteBgm || _CurrentBgmName == "")
        return;
    
     std::string fileName = FileHelpers::GetRootPath() + "/data/audio/bgm/" + name + ".mp3";
     
    [[OALSimpleAudio sharedInstance] playBg:[NSString stringWithUTF8String:fileName.c_str()] volume:volume pan:0.f loop:loop];
}
    
void SoundManager::StopBgm()
{
    _CurrentBgmName = "";
    
    [[OALSimpleAudio sharedInstance] stopBg];
}

Sound SoundManager::PlaySfx(const std::string& name, bool compressed, float volume, float pitch)
{
    if (_MuteSfx)
        return Sound(0);
    
    Sound sound(name, volume, pitch);
    
    sound.Play();
    
    return sound;
}

int SoundManager::SfxGetId()
{
    return _SoundId++;
}

void SoundManager::SfxPlay(const Sound& sound)
{
    std::string filename = FileHelpers::GetRootPath() + "/data/audio/sfx/" + sound.GetName() + (sound.IsCompressed() ? ".mp3" : ".wav");
    
    id<ALSoundSource> instance = [[OALSimpleAudio sharedInstance] playEffect:[NSString stringWithUTF8String:filename.c_str()] volume:sound.GetVolume() pitch:sound.GetPitch() pan:0.f loop:sound.IsLooping()];
    [instance retain];
    _Sounds[sound.GetId()] = instance;
}

void SoundManager::SfxStop(const Sound& sound)
{
    std::map<int, void*>::iterator it = _Sounds.find(sound.GetId());
    
    if (it == _Sounds.end())<commit_msg>Minor cleanup<commit_after>#ifdef PIXELBOOST_PLATFORM_IOS

#include <vector>

#import "ObjectAL.h"
#import "ALSoundSource.h"

#include "pixelboost/audio/soundManager.h"
#include "pixelboost/file/fileHelpers.h"

using namespace pixelboost;

Sound::Sound(const std::string& name, bool compressed, float volume, float pitch)
    : _Compressed(compressed)
    , _Name(name)
    , _Pitch(pitch)
    , _Volume(volume)
{
    _Id = SoundManager::Instance()->SfxGetId();
}

int Sound::GetId() const
{
    return _Id;
}

void Sound::Play()
{
    SoundManager::Instance()->SfxPlay(*this);
}

void Sound::Stop()
{
    SoundManager::Instance()->SfxStop(*this);
}

bool Sound::IsPlaying() const
{
    return false;
}

const std::string& Sound::GetName() const
{
    return _Name;
}

bool Sound::IsCompressed() const
{
    return _Compressed;
}

void Sound::SetCompressed(bool compressed)
{
    _Compressed = true;
}

bool Sound::IsLooping() const
{
    return _Looping;
}

void Sound::SetLooping(bool looping)
{
    _Looping = looping;
    SoundManager::Instance()->SfxUpdateLooping(*this);
}

float Sound::GetPitch() const
{
    return _Pitch;
}

void Sound::SetPitch(float pitch)
{
    _Pitch = pitch;
    SoundManager::Instance()->SfxUpdatePitch(*this);
}

float Sound::GetVolume() const
{
    return _Volume;
}

void Sound::SetVolume(float volume)
{
    _Volume = volume;
    SoundManager::Instance()->SfxUpdateVolume(*this);
}

SoundManager::SoundManager()
{
    _CurrentBgmName = "";
    _MuteBgm = false;
    _MuteSfx = false;
    
    [OALSimpleAudio sharedInstance].allowIpod = NO;
    [OALSimpleAudio sharedInstance].honorSilentSwitch = YES;
}

SoundManager::~SoundManager()
{
    
}

SoundManager* SoundManager::Instance()
{
    static SoundManager* instance = new SoundManager();
    return instance;
}

void SoundManager::Update(float time)
{
    std::vector<int> inactiveSounds;
    
    for (std::map<int, void*>::iterator it = _Sounds.begin(); it != _Sounds.end(); ++it)
    {
        id<ALSoundSource> src = (id<ALSoundSource>)it->second;
        
        if (src.playing == false)
            inactiveSounds.push_back(it->first);
    }
    
    for (std::vector<int>::iterator it = inactiveSounds.begin(); it != inactiveSounds.end(); ++it)
    {
        _Sounds.erase(*it);
    }
}
    
void SoundManager::MuteBgm(bool mute)
{
    _MuteBgm = mute;
    
    if (_MuteBgm)
    {
        std::string currentBgm = _CurrentBgmName;
        StopBgm();
        _CurrentBgmName = currentBgm;
    } else {
        PlayBgm(_CurrentBgmName, _CurrentBgmLoop, _CurrentBgmVolume);
    }
}

void SoundManager::MuteSfx(bool mute)
{
    _MuteSfx = mute;
}

void SoundManager::LoadBgm(const std::string& name)
{

}

void SoundManager::LoadSfx(const std::string& name, bool compressed)
{
    //if (compressed)
        return;
    
    std::string fileName = FileHelpers::GetRootPath() + "/data/audio/sfx/" + name + (compressed ? ".mp3" : ".wav");
    
    [[OALSimpleAudio sharedInstance] preloadEffect:[NSString stringWithUTF8String:fileName.c_str()]];
}

void SoundManager::PlayBgm(const std::string& name, bool loop, float volume)
{
    _CurrentBgmName = name;
    _CurrentBgmLoop = loop;
    _CurrentBgmVolume = volume;
    
    if (_MuteBgm || _CurrentBgmName == "")
        return;
    
     std::string fileName = FileHelpers::GetRootPath() + "/data/audio/bgm/" + name + ".mp3";
     
    [[OALSimpleAudio sharedInstance] playBg:[NSString stringWithUTF8String:fileName.c_str()] volume:volume pan:0.f loop:loop];
}
    
void SoundManager::StopBgm()
{
    _CurrentBgmName = "";
    
    [[OALSimpleAudio sharedInstance] stopBg];
}

Sound SoundManager::PlaySfx(const std::string& name, bool compressed, float volume, float pitch)
{
    if (_MuteSfx)
        return Sound();
    
    Sound sound(name, volume, pitch);
    
    sound.Play();
    
    return sound;
}

int SoundManager::SfxGetId()
{
    return _SoundId++;
}

void SoundManager::SfxPlay(const Sound& sound)
{
    std::string filename = FileHelpers::GetRootPath() + "/data/audio/sfx/" + sound.GetName() + (sound.IsCompressed() ? ".mp3" : ".wav");
    
    id<ALSoundSource> instance = [[OALSimpleAudio sharedInstance] playEffect:[NSString stringWithUTF8String:filename.c_str()] volume:sound.GetVolume() pitch:sound.GetPitch() pan:0.f loop:sound.IsLooping()];
    [instance retain];
    _Sounds[sound.GetId()] = instance;
}

void SoundManager::SfxStop(const Sound& sound)
{
    std::map<int, void*>::iterator it = _Sounds.find(sound.GetId());
    
    if (it == _Sounds.end())
{'fraction_non_alphanumeric': 0.14514525571633713, 'fraction_numerical': 0.00187783055340771, 'mean_word_length': 3.8006362672322376, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '367734', 'n_tokens_mistral': 3161, 'n_tokens_neox': 3072, 'n_words': 582}