Skip to content

Commit

Permalink
Merge branch 'dev' into pr/Plotja/1167
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Sep 16, 2023
2 parents 82e9076 + bd72c53 commit 383857b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/xrSound/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class XRSOUND_API XR_NOVTABLE CSound_emitter
virtual void set_range(float min, float max) = 0;
virtual void set_volume(float vol) = 0;
virtual void set_priority(float vol) = 0;
virtual void set_time(float t) = 0; //--#SM+#--
virtual void stop(bool isDeffered) = 0;
virtual const CSound_params* get_params() = 0;
virtual u32 play_time() = 0;
Expand Down Expand Up @@ -352,6 +353,7 @@ class ref_sound
void set_range(float min, float max) { VerSndUnlocked(); if (_feedback()) _feedback()->set_range(min, max); }
void set_volume(float vol) { VerSndUnlocked(); if (_feedback()) _feedback()->set_volume(vol); }
void set_priority(float p) { VerSndUnlocked(); if (_feedback()) _feedback()->set_priority(p); }
void set_time(float t) { VerSndUnlocked(); if (_feedback()) _feedback()->set_time(t); }; //--#SM+#--

const CSound_params* get_params()
{
Expand Down
1 change: 1 addition & 0 deletions src/xrSound/SoundRender_Core_Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void CSoundRender_Core::update(const Fvector& P, const Fvector& D, const Fvector
return;
Stats.Update.Begin();
isLocked = true;
Timer.time_factor(psSoundTimeFactor); //--#SM+#--
float new_tm = Timer.GetElapsed_sec();
fTimer_Delta = new_tm - fTimer_Value;
//float dt = float(Timer_Delta)/1000.f;
Expand Down
9 changes: 9 additions & 0 deletions src/xrSound/SoundRender_Emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ void CSoundRender_Emitter::set_frequency(float scale)
fTimeToStop = SoundRender->fTimer_Value + ((get_length_sec() - (SoundRender->fTimer_Value - fTimeStarted)) / (scale * sndTimeFactorKoeff));
}

// Перемотка звука на заданную секунду [rewind snd to target time] --#SM+#--
void CSoundRender_Emitter::set_time(float t)
{
VERIFY2(get_length_sec() >= t, "set_time: time is bigger than length of sound");
clamp(t, 0.0f, get_length_sec());
fTimeToRewind = t;
}

CSoundRender_Emitter::CSoundRender_Emitter()
{
#ifdef DEBUG
Expand All @@ -53,6 +61,7 @@ CSoundRender_Emitter::CSoundRender_Emitter()
fTimeStarted = 0.0f;
fTimeToStop = 0.0f;
fTimeToPropagade = 0.0f;
fTimeToRewind = 0.0f; //--#SM+#--
marker = 0xabababab;
starting_delay = 0.f;
priority_scale = 1.f;
Expand Down
2 changes: 2 additions & 0 deletions src/xrSound/SoundRender_Emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class CSoundRender_Emitter final : public CSound_emitter
float fTimeStarted; // time of "Start"
float fTimeToStop; // time to "Stop"
float fTimeToPropagade;
float fTimeToRewind; // --#SM+#--

u32 marker;
void i_stop();
Expand Down Expand Up @@ -100,6 +101,7 @@ class CSoundRender_Emitter final : public CSound_emitter
}

void set_priority(float p) override { priority_scale = p; }
void set_time(float t) override; //--#SM+#--
const CSound_params* get_params() override { return &p_source; }
void fill_block(void* ptr, u32 size);
void fill_data(u8* ptr, u32 offset, u32 size);
Expand Down
64 changes: 57 additions & 7 deletions src/xrSound/SoundRender_Emitter_FSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

XRSOUND_API extern float psSoundCull;

inline u32 calc_cursor(const float& fTimeStarted, float& fTime, const float& fTimeTotal, const WAVEFORMATEX& wfx)
inline u32 calc_cursor(const float& fTimeStarted, float& fTime, const float& fTimeTotal, const float& fFreq, const WAVEFORMATEX& wfx) //--#SM+#--
{
if (fTime < fTimeStarted)
fTime = fTimeStarted; // Андрюха посоветовал, ассерт что ниже вылетел из за паузы как то хитро
R_ASSERT((fTime - fTimeStarted) >= 0.0f);
while ((fTime - fTimeStarted) > fTimeTotal) // looped
while ((fTime - fTimeStarted) > fTimeTotal / fFreq) // looped
{
fTime -= fTimeTotal;
fTime -= fTimeTotal / fFreq;
}
u32 curr_sample_num = iFloor((fTime - fTimeStarted) * wfx.nSamplesPerSec);
u32 curr_sample_num = iFloor((fTime - fTimeStarted) * fFreq * wfx.nSamplesPerSec);
return curr_sample_num * (wfx.wBitsPerSample / 8) * wfx.nChannels;
}

Expand Down Expand Up @@ -49,7 +49,7 @@ void CSoundRender_Emitter::update(float dt)
if (iPaused)
break;
fTimeStarted = fTime;
fTimeToStop = fTime + (get_length_sec() / psSoundTimeFactor);
fTimeToStop = fTime + (get_length_sec() / p_source.freq); //--#SM+#--
fTimeToPropagade = fTime;
fade_volume = 1.f;
occluder_volume = SoundRender->get_occlusion(p_source.position, .2f, occluder);
Expand Down Expand Up @@ -143,7 +143,7 @@ void CSoundRender_Emitter::update(float dt)
}
else
{
u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), source()->m_wformat);
u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_wformat); //--#SM+#--
set_cursor(ptr);

if (update_culling(dt))
Expand Down Expand Up @@ -196,14 +196,64 @@ void CSoundRender_Emitter::update(float dt)
{
// switch to: PLAY
m_current_state = stPlayingLooped; // switch state
u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), source()->m_wformat);
u32 ptr = calc_cursor(fTimeStarted, fTime, get_length_sec(), p_source.freq, source()->m_wformat); //--#SM+#--
set_cursor(ptr);

SoundRender->i_start(this);
}
break;
}

//--#SM+# Begin--
// hard rewind
switch (m_current_state)
{
case stStarting:
case stStartingLooped:
case stPlaying:
case stSimulating:
case stPlayingLooped:
case stSimulatingLooped:
if (fTimeToRewind > 0.0f)
{
float fLength = get_length_sec();
bool bLooped = (fTimeToStop == 0xffffffff);

R_ASSERT2(fLength >= fTimeToRewind, "set_time: target time is bigger than length of sound");

float fRemainingTime = (fLength - fTimeToRewind) / p_source.freq;
float fPastTime = fTimeToRewind / p_source.freq;

fTimeStarted = SoundRender->fTimer_Value - fPastTime;
fTimeToPropagade = fTimeStarted; //--> For AI events

if (fTimeStarted < 0.0f)
{
Log("fTimer_Value = ", SoundRender->fTimer_Value);
Log("fTimeStarted = ", fTimeStarted);
Log("fRemainingTime = ", fRemainingTime);
Log("fPastTime = ", fPastTime);
R_ASSERT2(fTimeStarted >= 0.0f, "Possible error in sound rewind logic! See log.");

fTimeStarted = SoundRender->fTimer_Value;
fTimeToPropagade = fTimeStarted;
}

if (!bLooped)
{
//--> Пересчитываем время, когда звук должен остановиться [recalculate stop time]
fTimeToStop = SoundRender->fTimer_Value + fRemainingTime;
}

u32 ptr = calc_cursor(fTimeStarted, fTime, fLength, p_source.freq, source()->m_wformat);
set_cursor(ptr);

fTimeToRewind = 0.0f;
}
default: break;
}
//--#SM+# End--

// if deffered stop active and volume==0 -> physically stop sound
if (bStopping && fis_zero(fade_volume))
i_stop();
Expand Down
7 changes: 4 additions & 3 deletions src/xrSound/SoundRender_TargetA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,26 @@ void CSoundRender_TargetA::fill_parameters()
}

VERIFY2(m_pEmitter, SE->source()->file_name());

float _pitch = m_pEmitter->p_source.freq;
clamp(_pitch, EPS_L, 2.f);
clamp(_pitch, EPS_L, 100.f); //--#SM+#-- Increase sound frequency (speed) limit
if (m_pEmitter->bIsIgnoreTimeFactor && !fsimilar(cache_pitch, _pitch))
{
cache_pitch = 1.f;
cache_pitch = _pitch;
A_CHK(alSourcef(pSource, AL_PITCH, cache_pitch));
}
else if (!m_pEmitter->bIsIgnoreTimeFactor && !fsimilar(cache_pitch, _pitch * psSoundTimeFactor))
{
time_played = time_played + (SoundRender->fTimer_Value - last_pitch_change_time) * cache_pitch;
cache_pitch = _pitch * psSoundTimeFactor;

// Only update time to stop for non-looped sounds
if (!m_pEmitter->iPaused && (m_pEmitter->m_current_state == CSoundRender_Emitter::stStarting || m_pEmitter->m_current_state == CSoundRender_Emitter::stPlaying || m_pEmitter->m_current_state == CSoundRender_Emitter::stSimulating))
m_pEmitter->fTimeToStop = SoundRender->fTimer_Value + ((m_pEmitter->get_length_sec() - time_played) / cache_pitch);
A_CHK(alSourcef(pSource, AL_PITCH, cache_pitch));

last_pitch_change_time = SoundRender->fTimer_Value;
}

VERIFY2(m_pEmitter, SE->source()->file_name());
}

Expand Down

0 comments on commit 383857b

Please sign in to comment.