Looping audio clips now functioning correctly in Siren.

KRAudioSource::IsPlaying() now returning false when a non-looping audio source ends.
This commit is contained in:
2013-02-27 13:11:24 -08:00
parent 52c9b90667
commit 5d1daa22dc
3 changed files with 92 additions and 65 deletions

View File

@@ -216,13 +216,10 @@ void KRAudioManager::renderReverb()
KRAudioSource *source = *itr; KRAudioSource *source = *itr;
float reverb_send_level = m_global_reverb_send_level * m_global_gain * source->getReverb(); float reverb_send_level = m_global_reverb_send_level * m_global_gain * source->getReverb();
if(reverb_send_level > 0.0f) { if(reverb_send_level > 0.0f) {
KRAudioSample *sample = source->getAudioSample(); source->sample(KRENGINE_AUDIO_BLOCK_LENGTH, 0, reverb_data, reverb_send_level);
if(sample) {
sample->sample((int)((__int64_t)m_audio_frame - source->getStartAudioFrame()), KRENGINE_AUDIO_BLOCK_LENGTH, 0, reverb_data, reverb_send_level);
vDSP_vadd(reverb_accum, 1, reverb_data, 1, reverb_accum, 1, KRENGINE_AUDIO_BLOCK_LENGTH); vDSP_vadd(reverb_accum, 1, reverb_data, 1, reverb_accum, 1, KRENGINE_AUDIO_BLOCK_LENGTH);
} }
} }
}
// Apply impulse response reverb // Apply impulse response reverb
// KRAudioSample *impulse_response = getContext().getAudioManager()->get("hrtf_kemar_H10e040a"); // KRAudioSample *impulse_response = getContext().getAudioManager()->get("hrtf_kemar_H10e040a");
@@ -1314,8 +1311,6 @@ void KRAudioManager::renderHRTF()
for(std::set<KRAudioSource *>::iterator itr=m_activeAudioSources.begin(); itr != m_activeAudioSources.end(); itr++) { for(std::set<KRAudioSource *>::iterator itr=m_activeAudioSources.begin(); itr != m_activeAudioSources.end(); itr++) {
KRAudioSource *source = *itr; KRAudioSource *source = *itr;
KRAudioSample *source_sample = source->getAudioSample();
if(source_sample) {
KRVector3 source_world_position = source->getWorldTranslation(); KRVector3 source_world_position = source->getWorldTranslation();
KRVector3 diff = source_world_position - m_listener_position; KRVector3 diff = source_world_position - m_listener_position;
float distance = diff.magnitude(); float distance = diff.magnitude();
@@ -1362,7 +1357,7 @@ void KRAudioManager::renderHRTF()
} }
} }
source_sample->sample((int)((__int64_t)m_audio_frame - source->getStartAudioFrame()), KRENGINE_AUDIO_BLOCK_LENGTH, 0, hrtf_sample->realp, gain); source->sample(KRENGINE_AUDIO_BLOCK_LENGTH, 0, hrtf_sample->realp, gain);
memset(hrtf_sample->realp + hrtf_frames, 0, sizeof(float) * hrtf_frames); memset(hrtf_sample->realp + hrtf_frames, 0, sizeof(float) * hrtf_frames);
memset(hrtf_sample->imagp, 0, sizeof(float) * fft_size); memset(hrtf_sample->imagp, 0, sizeof(float) * fft_size);
@@ -1384,7 +1379,6 @@ void KRAudioManager::renderHRTF()
} }
} }
} }
}
} }
void KRAudioManager::renderITD() void KRAudioManager::renderITD()

View File

@@ -504,6 +504,38 @@ __int64_t KRAudioSource::getStartAudioFrame()
return m_start_audio_frame; return m_start_audio_frame;
} }
void KRAudioSource::sample(int frame_count, int channel, float *buffer, float gain)
{
KRAudioSample *source_sample = getAudioSample();
if(source_sample && m_playing) {
if(m_looping) {
int buffer_offset = 0;
int frames_left = frame_count;
int sample_length = source_sample->getFrameCount();
while(frames_left) {
int next_frame = (int)((getContext().getAudioManager()->getAudioFrame() - getStartAudioFrame() + buffer_offset) % sample_length);
if(next_frame + frames_left >= sample_length) {
int frames_processed = sample_length - next_frame;
source_sample->sample(next_frame, frames_processed, channel, buffer + buffer_offset, gain);
frames_left -= frames_processed;
buffer_offset += frames_processed;
} else {
source_sample->sample(next_frame, frames_left, channel, buffer + buffer_offset, gain);
frames_left = 0;
}
}
} else {
int next_frame = (int)(getContext().getAudioManager()->getAudioFrame() - getStartAudioFrame());
source_sample->sample(next_frame, frame_count, channel, buffer, gain);
if(next_frame > source_sample->getFrameCount()) {
stop();
}
}
} else {
memset(buffer, 0, sizeof(float) * frame_count);
}
}
OSStatus alcASASetSourceProc(const ALuint property, ALuint source, ALvoid *data, ALuint dataSize) OSStatus alcASASetSourceProc(const ALuint property, ALuint source, ALvoid *data, ALuint dataSize)
{ {
OSStatus err = noErr; OSStatus err = noErr;

View File

@@ -96,6 +96,7 @@ public:
__int64_t getStartAudioFrame(); __int64_t getStartAudioFrame();
void sample(int frame_count, int channel, float *buffer, float gain);
private: private:
__int64_t m_start_audio_frame; // Global audio frame that matches the start of the audio sample playback __int64_t m_start_audio_frame; // Global audio frame that matches the start of the audio sample playback