summaryrefslogtreecommitdiff
path: root/src/SSVOpenHexagon/Core/Audio.cpp
blob: 23c972438f2f80aad37529a3936045fdb26a60d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#include "SSVOpenHexagon/Global/Audio.hpp"

#include "SSVOpenHexagon/Global/Assert.hpp"
#include "SSVOpenHexagon/Utils/Concat.hpp"

#include <SSVStart/SoundPlayer/SoundPlayer.hpp>

#include <SSVUtils/Core/Log/Log.hpp>

#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/Music.hpp>

#include <memory>
#include <optional>
#include <string>

namespace hg {

class Audio::AudioImpl
{
private:
    // TODO (P2): cleaner way of doing this
    SoundBufferGetter _soundBufferGetter;
    MusicPathGetter _musicPathGetter;

    // TODO (P2): remove these, roll own system
    ssvs::SoundPlayer _soundPlayer;

    std::optional<sf::Music> _music;
    float _musicVolume;
    std::string _lastLoadedMusicPath;


    void playSoundImpl(
        const std::string& assetId, const ssvs::SoundPlayer::Mode mode)
    {
        if(sf::SoundBuffer* soundBuffer = _soundBufferGetter(assetId);
            soundBuffer != nullptr)
        {
            _soundPlayer.play(*soundBuffer, mode);
        }
    }

public:
    explicit AudioImpl(const SoundBufferGetter& soundBufferGetter,
        const MusicPathGetter& musicPathGetter)
        : _soundBufferGetter{soundBufferGetter},
          _musicPathGetter{musicPathGetter},
          _soundPlayer{},
          _music{},
          _musicVolume{100.f},
          _lastLoadedMusicPath{}
    {
        SSVOH_ASSERT(static_cast<bool>(_soundBufferGetter));
    }

    void setSoundVolume(const float volume)
    {
        SSVOH_ASSERT(volume >= 0.f && volume <= 100.f);
        _soundPlayer.setVolume(volume);
    }

    void setMusicVolume(const float volume)
    {
        SSVOH_ASSERT(volume >= 0.f && volume <= 100.f);
        _musicVolume = volume;

        if(_music.has_value())
        {
            _music->setVolume(_musicVolume);
        }
    }

    void resumeMusic()
    {
        if(_music.has_value())
        {
            _music->setVolume(_musicVolume);
            _music->play();
        }
    }

    void pauseMusic()
    {
        if(_music.has_value())
        {
            _music->pause();
        }
    }

    void stopMusic()
    {
        if(_music.has_value())
        {
            _music->stop();
        }
    }

    void stopSounds()
    {
        _soundPlayer.stop();
    }

    void playSoundOverride(const std::string& id)
    {
        playSoundImpl(id, ssvs::SoundPlayer::Mode::Override);
    }

    void playPackSoundOverride(const std::string& packId, const std::string& id)
    {
        playSoundImpl(
            Utils::concat(packId, '_', id), ssvs::SoundPlayer::Mode::Override);
    }

    void playSoundAbort(const std::string& id)
    {
        playSoundImpl(id, ssvs::SoundPlayer::Mode::Abort);
    }

    void playPackSoundAbort(const std::string& packId, const std::string& id)
    {
        playSoundImpl(
            Utils::concat(packId, '_', id), ssvs::SoundPlayer::Mode::Abort);
    }

    [[nodiscard]] bool loadAndPlayMusic(const std::string& packId,
        const std::string& id, const float playingOffsetSeconds)
    {
        const std::string assetId = Utils::concat(packId, '_', id);
        const std::string* path = _musicPathGetter(assetId);

        if(path == nullptr)
        {
            ssvu::lo("hg::AudioImpl::playMusic")
                << "No path for music id '" << assetId << "'\n";

            return false;
        }

        if(!_music.has_value())
        {
            _music.emplace();
        }

        if(_lastLoadedMusicPath != *path)
        {
            if(!_music->openFromFile(*path))
            {
                ssvu::lo("hg::AudioImpl::playMusic")
                    << "Failed loading music file '" << path << "'\n";

                _music.reset();
                return false;
            }

            _lastLoadedMusicPath = *path;
        }

        _music->setLoop(true);
        _music->setPlayingOffset(sf::seconds(playingOffsetSeconds));
        resumeMusic();

        return true;
    }

    void setCurrentMusicPitch(const float pitch)
    {
        if(_music.has_value())
        {
            _music->setPitch(pitch);
        }
    }
};

[[nodiscard]] const Audio::AudioImpl& Audio::impl() const noexcept
{
    SSVOH_ASSERT(_impl != nullptr);
    return *_impl;
}

[[nodiscard]] Audio::AudioImpl& Audio::impl() noexcept
{
    SSVOH_ASSERT(_impl != nullptr);
    return *_impl;
}

Audio::Audio(const SoundBufferGetter& soundBufferGetter,
    const MusicPathGetter& musicPathGetter)
    : _impl{std::make_unique<AudioImpl>(soundBufferGetter, musicPathGetter)}
{}

Audio::~Audio() = default;

void Audio::setSoundVolume(const float volume)
{
    impl().setSoundVolume(volume);
}

void Audio::setMusicVolume(const float volume)
{
    impl().setMusicVolume(volume);
}

void Audio::resumeMusic()
{
    impl().resumeMusic();
}

void Audio::pauseMusic()
{
    impl().pauseMusic();
}

void Audio::stopMusic()
{
    impl().stopMusic();
}

void Audio::stopSounds()
{
    impl().stopSounds();
}

void Audio::playSoundOverride(const std::string& id)
{
    impl().playSoundOverride(id);
}

void Audio::playPackSoundOverride(
    const std::string& packId, const std::string& id)
{
    impl().playPackSoundOverride(packId, id);
}

void Audio::playSoundAbort(const std::string& id)
{
    impl().playSoundAbort(id);
}

void Audio::playPackSoundAbort(const std::string& packId, const std::string& id)
{
    impl().playPackSoundAbort(packId, id);
}

[[nodiscard]] bool Audio::loadAndPlayMusic(const std::string& packId,
    const std::string& id, const float playingOffsetSeconds)
{
    return impl().loadAndPlayMusic(packId, id, playingOffsetSeconds);
}

void Audio::setCurrentMusicPitch(const float pitch)
{
    impl().setCurrentMusicPitch(pitch);
}

} // namespace hg