Skip to content

Commit 022a1ca

Browse files
committed
audio small fix
1 parent eb0f0df commit 022a1ca

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

src/PedestrianStates.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ bool PedestrianStatesManager::TryProcessDamage(const DamageInfo& damageInfo)
332332
float killSpeed = 6.0f; // todo: magic numbers
333333
if (speedInDirection > killSpeed)
334334
{
335+
mPedestrian->StartGameObjectSound(ePedSfxChannelIndex_Voice, eSfxType_Level, SfxLevel_Squashed, SfxFlags_RandomPitch);
335336
mPedestrian->DieFromDamage(eDamageCause_CarCrash);
336337
}
337338
else if (speedInDirection > 1.0f)// todo: magic numbers
@@ -834,4 +835,5 @@ void PedestrianStatesManager::StateElectrocuted_ProcessEnter(const PedestrianSta
834835
mPedestrian->SetAnimation(ePedestrianAnim_FallShort, eSpriteAnimLoop_None);
835836
mPedestrian->mPhysicsBody->ClearForces();
836837
mPedestrian->mPhysicsBody->SetLinearVelocity(-mPedestrian->mPhysicsBody->GetSignVector() * impulse);
838+
mPedestrian->StartGameObjectSound(ePedSfxChannelIndex_Voice, eSfxType_Level, SfxLevel_DieScream4, SfxFlags_RandomPitch);
837839
}

src/SfxEmitter.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,37 @@ bool SfxEmitter::StartSound(int ichannel, SfxSample* sfxSample, SfxFlags sfxFlag
133133
channel.mSfxFlags = sfxFlags;
134134
channel.mSfxSample = sfxSample;
135135

136+
channel.mHardwareSource->Stop();
136137
if (!channel.mHardwareSource->SetSampleBuffer(sfxSample->mSampleBuffer))
137138
{
138139
debug_assert(false);
139140
}
141+
140142
float pitchValue = 1.0f;
141143
if ((sfxFlags & SfxFlags_RandomPitch) > 0)
142144
{
143145
pitchValue = gAudioManager.NextRandomPitch();
144146
}
145-
if (!channel.mHardwareSource->SetPitch(pitchValue))
147+
else
148+
{
149+
pitchValue = channel.mPitchValue;
150+
}
151+
if (!channel.mHardwareSource->SetPitch(pitchValue) ||
152+
!channel.mHardwareSource->SetGain(channel.mGainValue))
146153
{
147154
debug_assert(false);
148155
}
156+
149157
if (!channel.mHardwareSource->SetPosition3D(mEmitterPosition.x, mEmitterPosition.y, mEmitterPosition.y))
150158
{
151159
debug_assert(false);
152160
}
161+
153162
if (!channel.mHardwareSource->Start((sfxFlags & SfxFlags_Loop) > 0))
154163
{
155164
debug_assert(false);
156165
}
166+
157167
if ((sfxFlags & SfxFlags_StartPaused) > 0)
158168
{
159169
if (!channel.mHardwareSource->Pause())
@@ -170,10 +180,11 @@ bool SfxEmitter::StopSound(int ichannel)
170180
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
171181
return false;
172182

173-
if (mAudioChannels[ichannel].mHardwareSource)
183+
SfxChannel& channel = mAudioChannels[ichannel];
184+
if (channel.mHardwareSource)
174185
{
175-
mAudioChannels[ichannel].mHardwareSource->Stop();
176-
mAudioChannels[ichannel].mHardwareSource = nullptr;
186+
channel.mHardwareSource->Stop();
187+
channel.mHardwareSource = nullptr;
177188
}
178189
return true;
179190
}
@@ -183,9 +194,10 @@ bool SfxEmitter::IsPlaying(int ichannel) const
183194
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
184195
return false;
185196

186-
if (mAudioChannels[ichannel].mHardwareSource)
197+
const SfxChannel& channel = mAudioChannels[ichannel];
198+
if (channel.mHardwareSource)
187199
{
188-
return mAudioChannels[ichannel].mHardwareSource->IsPlaying();
200+
return channel.mHardwareSource->IsPlaying();
189201
}
190202

191203
return false;
@@ -206,9 +218,10 @@ bool SfxEmitter::IsPaused(int ichannel) const
206218
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
207219
return false;
208220

209-
if (mAudioChannels[ichannel].mHardwareSource)
221+
const SfxChannel& channel = mAudioChannels[ichannel];
222+
if (channel.mHardwareSource)
210223
{
211-
return mAudioChannels[ichannel].mHardwareSource->IsPaused();
224+
return channel.mHardwareSource->IsPaused();
212225
}
213226

214227
return false;
@@ -219,9 +232,10 @@ bool SfxEmitter::PauseSound(int ichannel)
219232
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
220233
return false;
221234

222-
if (mAudioChannels[ichannel].mHardwareSource)
235+
SfxChannel& channel = mAudioChannels[ichannel];
236+
if (channel.mHardwareSource)
223237
{
224-
return mAudioChannels[ichannel].mHardwareSource->Pause();
238+
return channel.mHardwareSource->Pause();
225239
}
226240

227241
return false;
@@ -232,9 +246,10 @@ bool SfxEmitter::ResumeSound(int ichannel)
232246
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
233247
return false;
234248

235-
if (mAudioChannels[ichannel].mHardwareSource)
249+
SfxChannel& channel = mAudioChannels[ichannel];
250+
if (channel.mHardwareSource)
236251
{
237-
return mAudioChannels[ichannel].mHardwareSource->Resume();
252+
return channel.mHardwareSource->Resume();
238253
}
239254

240255
return false;
@@ -245,9 +260,14 @@ bool SfxEmitter::SetPitch(int ichannel, float pitchValue)
245260
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
246261
return false;
247262

248-
if (mAudioChannels[ichannel].mHardwareSource)
263+
SfxChannel& channel = mAudioChannels[ichannel];
264+
if (channel.mHardwareSource)
249265
{
250-
return mAudioChannels[ichannel].mHardwareSource->SetPitch(pitchValue);
266+
if (channel.mPitchValue == pitchValue)
267+
return true;
268+
269+
channel.mPitchValue = pitchValue;
270+
return channel.mHardwareSource->SetPitch(pitchValue);
251271
}
252272

253273
return false;
@@ -258,9 +278,14 @@ bool SfxEmitter::SetGain(int ichannel, float gainValue)
258278
if ((ichannel < 0) || (ichannel >= (int) mAudioChannels.size()))
259279
return false;
260280

261-
if (mAudioChannels[ichannel].mHardwareSource)
281+
SfxChannel& channel = mAudioChannels[ichannel];
282+
if (channel.mHardwareSource)
262283
{
263-
return mAudioChannels[ichannel].mHardwareSource->SetGain(gainValue);
284+
if (channel.mGainValue == gainValue)
285+
return true;
286+
287+
channel.mGainValue = gainValue;
288+
return channel.mHardwareSource->SetGain(gainValue);
264289
}
265290

266291
return false;

src/SfxEmitter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class SfxEmitter final: public cxx::noncopyable
4141
AudioSource* mHardwareSource = nullptr; // channel is only active when source set
4242
SfxSample* mSfxSample = nullptr;
4343
SfxFlags mSfxFlags = SfxFlags_None;
44+
// audio params
45+
float mPitchValue = 1.0f;
46+
float mGainValue = 1.0f;
4447
};
4548

4649
//////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)