master
ブランチ master
Tetris / src / sys / sndman.cc
5.3 KB、174行 生 Blame 履歴
1/**
2Copyright (c) テクニカル諏訪子
3
4Permission is hereby granted to any person obtaining a copy of the software
5Tetris (the "Software") to use, modify, merge, copy, publish, distribute,
6sublicense, and/or sell copies of the Software, subject to the following conditions:
7
8 1. **Origin Attribution**:
9 - You must not misrepresent the origin of the Software; you must not claim
10 you created the original Software.
11 - If the Software is used in a product, you must either:
12 a. Provide clear attribution in the product's documentation, user interface,
13 or other visible areas, **OR**
14 b. Pay the original developers a fee they specify in writing.
15 2. **Usage Restriction**:
16 - The Software, or any derivative works, dependencies, or libraries
17 incorporating it, must not be used for censorship or to suppress freedom of
18 speech, expression, or creativity. Prohibited uses include, but are not
19 limited to:
20 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
21 ideas, or objective reality.
22 - Tools or systems designed to restrict access to information or
23 artistic works.
24 3. **Notice Preservation**:
25 - This license and the above copyright notice must remain intact in all copies
26 of the source code.
27
28THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
29INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
33OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34*/
35#include <tetris/sys/sndman.hh>
36#include <algorithm>
37
38namespace 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} // namespace tetris::sys