| 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 | #include <tetris/sys/sndman.hh> |
| 36 | #include <algorithm> |
| 37 | |
| 38 | namespace tetris::sys { |
| 39 | SndMan &SndMan::Get() { |
| 40 | static SndMan instance; |
| 41 | return instance; |
| 42 | } |
| 43 | |
| 44 | bool SndMan::Init() { |
| 45 | InitAudioDevice(); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | void SndMan::Shutdown() { |
| 50 | for (auto &pair : m_Sfx) { |
| 51 | UnloadSound(pair.second.sound); |
| 52 | } |
| 53 | m_Sfx.clear(); |
| 54 | |
| 55 | for (auto &pair : m_Bgm) { |
| 56 | UnloadMusicStream(pair.second.intro); |
| 57 | UnloadMusicStream(pair.second.loop); |
| 58 | } |
| 59 | m_Bgm.clear(); |
| 60 | m_Current = nullptr; |
| 61 | CloseAudioDevice(); |
| 62 | } |
| 63 | |
| 64 | void SndMan::Update(f32 dt) { |
| 65 | if (!m_Current) return; |
| 66 | |
| 67 | if (!m_Current->introDone) { |
| 68 | SetMusicVolume(m_Current->intro, 1.f); |
| 69 | UpdateMusicStream(m_Current->intro); |
| 70 | f32 time = GetMusicTimePlayed(m_Current->intro); |
| 71 | |
| 72 | if (time+1 < m_LastIntroTime) { |
| 73 | StopMusicStream(m_Current->intro); |
| 74 | |
| 75 | m_Current->introDone = true; |
| 76 | PlayMusicStream(m_Current->loop); |
| 77 | } |
| 78 | |
| 79 | m_LastIntroTime = time; |
| 80 | } else { |
| 81 | SetMusicVolume(m_Current->loop, 1.f); |
| 82 | UpdateMusicStream(m_Current->loop); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool SndMan::LoadSfx(std::string_view id, const u8 *data, i32 dataSize) { |
| 87 | if (!data || dataSize <= 0) return false; |
| 88 | |
| 89 | Wave wave = LoadWaveFromMemory(".ogg", data, dataSize); |
| 90 | if (wave.data == nullptr) return false; |
| 91 | |
| 92 | Sound snd = LoadSoundFromWave(wave); |
| 93 | UnloadWave(wave); |
| 94 | |
| 95 | if (snd.stream.buffer == nullptr) return false; |
| 96 | m_Sfx[string(id)] = { snd }; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | void SndMan::PlaySfx(std::string_view id, f32 volume) { |
| 101 | auto it = m_Sfx.find(string(id)); |
| 102 | if (it == m_Sfx.end()) return; |
| 103 | |
| 104 | SetSoundVolume(it->second.sound, volume * m_SfxVolume); |
| 105 | PlaySound(it->second.sound); |
| 106 | } |
| 107 | |
| 108 | bool SndMan::LoadBgm(std::string_view id, const u8 *intro, i32 introSize, const u8 *loop, i32 loopSize) { |
| 109 | if (!intro || !loop || introSize <= 0 || loopSize <= 0) return false; |
| 110 | |
| 111 | Bgm bgm; |
| 112 | bgm.intro = LoadMusicStreamFromMemory(".ogg", intro, introSize); |
| 113 | bgm.loop = LoadMusicStreamFromMemory(".ogg", loop, loopSize); |
| 114 | |
| 115 | bgm.intro.looping = false; |
| 116 | bgm.loop.looping = true; |
| 117 | |
| 118 | if (!IsMusicValid(bgm.intro) || !IsMusicValid(bgm.loop)) { |
| 119 | if (IsMusicValid(bgm.intro)) UnloadMusicStream(bgm.intro); |
| 120 | if (IsMusicValid(bgm.loop)) UnloadMusicStream(bgm.loop); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | m_Bgm.emplace(string(id), std::move(bgm)); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void SndMan::PlayBgm(std::string_view id, f32 fadeTime) { |
| 129 | auto it = m_Bgm.find(string(id)); |
| 130 | if (it == m_Bgm.end()) return; |
| 131 | |
| 132 | if (m_Current) { |
| 133 | StopMusicStream(m_Current->intro); |
| 134 | StopMusicStream(m_Current->loop); |
| 135 | } |
| 136 | |
| 137 | m_Current = &it->second; |
| 138 | m_Current->introDone = false; |
| 139 | PlayMusicStream(m_Current->intro); |
| 140 | } |
| 141 | |
| 142 | void SndMan::StopBgm(f32 fadeTime) { |
| 143 | if (m_Current) { |
| 144 | StopMusicStream(m_Current->intro); |
| 145 | StopMusicStream(m_Current->loop); |
| 146 | m_Current = nullptr; |
| 147 | m_SfxVolume = 1.f; |
| 148 | m_BgmVolume = 1.f; |
| 149 | m_LastIntroTime = 0.f; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void SndMan::PauseBgm() { |
| 154 | if (m_Current) { |
| 155 | PauseMusicStream(m_Current->intro); |
| 156 | PauseMusicStream(m_Current->loop); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void SndMan::ResumeBgm() { |
| 161 | if (m_Current) { |
| 162 | ResumeMusicStream(m_Current->intro); |
| 163 | ResumeMusicStream(m_Current->loop); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void SndMan::SetBgmPitch(f32 pitch) { |
| 168 | pitch = std::clamp(pitch, .5f, 2.f); |
| 169 | if (!m_Current) return; |
| 170 | |
| 171 | if (!m_Current->introDone) SetMusicPitch(m_Current->intro, pitch); |
| 172 | else SetMusicPitch(m_Current->loop, pitch); |
| 173 | } |
| 174 | } |