Skip to content

Commit c2b8b6b

Browse files
committed
stash
1 parent 076479c commit c2b8b6b

File tree

8 files changed

+66
-7
lines changed

8 files changed

+66
-7
lines changed

src/audio.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ struct AudioInterface {
119119
*/
120120
virtual void BGM_Pitch(int pitch) = 0;
121121

122+
/**
123+
* Adjusts the balance of the currently playing background music.
124+
*/
125+
virtual void BGM_Balance(int balance) = 0;
126+
122127
/**
123128
* @return Type of music being played.
124129
*/
@@ -174,6 +179,7 @@ struct EmptyAudio : public AudioInterface {
174179
void BGM_Fade(int) override {}
175180
void BGM_Volume(int) override {}
176181
void BGM_Pitch(int) override {};
182+
void BGM_Balance(int) override {}
177183
std::string BGM_GetType() const override { return {}; };
178184
void SE_Play(std::unique_ptr<AudioSeCache>, int, int, int) override {}
179185
void SE_Stop() override {}

src/audio_decoder.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
// Headers
1919
#include <cassert>
20+
#include <cmath>
2021
#include <cstdint>
2122
#include <cstring>
2223
#include "audio_decoder.h"
@@ -226,13 +227,20 @@ int AudioDecoder::GetSamplesizeForFormat(AudioDecoderBase::Format format) {
226227
}
227228

228229
void AudioDecoder::SetLogVolume() {
230+
float base_gain = AdjustVolume(volume);
229231
int balance = GetBalance();
230-
float left_gain = 1.f, right_gain = 1.f;
232+
float left_gain = 1.f, right_gain = 1.f, atten_db;
231233
if (balance <= 50) {
232-
right_gain = balance / 50.f;
234+
atten_db = -6.0f * ((balance - 50) / 10.f);
235+
// right_gain = balance / 50.f;
236+
right_gain = powf(10.0f, atten_db / 20.0f);
233237
} else {
234-
left_gain = (100 - balance) / 50.f;
238+
atten_db = -6.0f * ((50 - balance) / 10.f);
239+
// left_gain = (100 - balance) / 50.f;
240+
left_gain = std::powf(10.0f, atten_db / 20.0f);
235241
}
236-
log_volume.left_volume = AdjustVolume(volume * left_gain);
237-
log_volume.right_volume = AdjustVolume(volume * right_gain);
238-
}
242+
log_volume.left_volume = base_gain * left_gain;
243+
log_volume.right_volume = base_gain * right_gain;
244+
// log_volume.left_volume = AdjustVolume(volume * left_gain);
245+
// log_volume.right_volume = AdjustVolume(volume * right_gain);
246+
}

src/audio_decoder_base.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
float AudioDecoderBase::AdjustVolume(float volume) {
2727
// Adjust to RPG_RT (Direct Sound) volume scale
2828
if (volume > 0) {
29-
return 100.0f * std::pow(10.0f, (-100 + volume) / 60.0f);
29+
// return 100.0f * std::pow(10.0f, (-100 + volume) / 60.0f);
30+
return 100.0f * std::pow(10.0f, -(35.0f / 20.0f) * (1.0f - volume / 100.0f));
3031
}
3132
return 0.0f;
3233
}

src/audio_generic.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ void GenericAudio::BGM_Pitch(int pitch) {
151151
UnlockMutex();
152152
}
153153

154+
void GenericAudio::BGM_Balance(int balance) {
155+
LockMutex();
156+
for (auto& BGM_Channel : BGM_Channels) {
157+
BGM_Channel.SetBalance(balance);
158+
}
159+
UnlockMutex();
160+
}
161+
154162
std::string GenericAudio::BGM_GetType() const {
155163
std::string type;
156164

@@ -545,6 +553,14 @@ void GenericAudio::BgmChannel::SetPitch(int pitch) {
545553
}
546554
}
547555

556+
void GenericAudio::BgmChannel::SetBalance(int balance) {
557+
if (midi_out_used) {
558+
instance->midi_thread->GetMidiOut().SetBalance(balance);
559+
} else if (decoder) {
560+
decoder->SetBalance(balance);
561+
}
562+
}
563+
548564
bool GenericAudio::BgmChannel::IsUsed() const {
549565
return decoder || midi_out_used;
550566
}

src/audio_generic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class GenericAudio : public AudioInterface {
5353
void BGM_Fade(int fade) override;
5454
void BGM_Volume(int volume) override;
5555
void BGM_Pitch(int pitch) override;
56+
void BGM_Balance(int balance) override;
5657
std::string BGM_GetType() const override;
5758

5859
void SE_Play(std::unique_ptr<AudioSeCache> se, int volume, int pitch, int balance) override;
@@ -84,6 +85,7 @@ class GenericAudio : public AudioInterface {
8485
void SetFade(int fade);
8586
void SetVolume(int volume);
8687
void SetPitch(int pitch);
88+
void SetBalance(int balance);
8789
bool IsUsed() const;
8890
};
8991
struct SeChannel {

src/game_system.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ void Game_System::BgmPlay(lcf::rpg::Music const& bgm) {
9393
Output::Debug("BGM {} has invalid tempo {}", bgm.name, bgm.tempo);
9494
}
9595

96+
if (bgm.balance < 0 || bgm.balance > 100) {
97+
data.current_music.balance = Utils::Clamp<int32_t>(bgm.balance, 0, 100);
98+
99+
Output::Debug("BGM {} has invalid balance {}", bgm.name, bgm.balance);
100+
}
101+
96102
// (OFF) means play nothing
97103
if (!bgm.name.empty() && bgm.name != "(OFF)") {
98104
// Same music: Only adjust volume and speed
@@ -107,6 +113,11 @@ void Game_System::BgmPlay(lcf::rpg::Music const& bgm) {
107113
Audio().BGM_Pitch(data.current_music.tempo);
108114
}
109115
}
116+
if (previous_music.balance != data.current_music.balance) {
117+
if (!bgm_pending) {
118+
Audio().BGM_Balance(data.current_music.balance);
119+
}
120+
}
110121
} else {
111122
Audio().BGM_Stop();
112123
bgm_pending = true;
@@ -164,6 +175,7 @@ void Game_System::SePlay(const lcf::rpg::Sound& se, bool stop_sounds) {
164175

165176
int32_t volume = se.volume;
166177
int32_t tempo = se.tempo;
178+
int32_t balance = se.balance;
167179

168180
// Validate
169181
if (volume < 0 || volume > 100) {
@@ -177,10 +189,16 @@ void Game_System::SePlay(const lcf::rpg::Sound& se, bool stop_sounds) {
177189
tempo = Utils::Clamp<int32_t>(se.tempo, 10, 400);
178190
}
179191

192+
if (balance < 0 || balance > 100) {
193+
Output::Debug("SE {} has invalid balance {}", se.name, balance);
194+
balance = Utils::Clamp<int32_t>(se.balance, 0, 100);
195+
}
196+
180197
FileRequestAsync* request = AsyncHandler::RequestFile("Sound", se.name);
181198
lcf::rpg::Sound se_adj = se;
182199
se_adj.volume = volume;
183200
se_adj.tempo = tempo;
201+
se_adj.balance = balance;
184202
se_request_ids[se.name] = request->Bind(&Game_System::OnSeReady, this, se_adj, stop_sounds);
185203
if (EndsWith(se.name, ".script")) {
186204
// Is a Ineluki Script File

src/platform/3ds/audio.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ void CtrAudio::BGM_Pitch(int pitch) {
207207
}
208208
}
209209

210+
void CtrAudio::BGM_Balance(int balance) {
211+
if (!bgm.decoder)
212+
return;
213+
214+
bgm.decoder->SetBalance(balance);
215+
}
216+
210217
std::string CtrAudio::BGM_GetType() const {
211218
if (bgm.decoder) {
212219
return bgm.decoder->GetType();

src/platform/3ds/audio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CtrAudio final : public AudioInterface {
4343
void BGM_Fade(int fade) override;
4444
void BGM_Volume(int volume) override;
4545
void BGM_Pitch(int pitch) override;
46+
void BGM_Balance(int balance) override;
4647
std::string BGM_GetType() const override;
4748
void SE_Play(std::unique_ptr<AudioSeCache> se, int volume, int pitch, int balance) override;
4849
void SE_Stop() override;

0 commit comments

Comments
 (0)