Reduced memory utilization of Siren audio engine by dyanmically opening and closing the CoreAudio virtual file.

--HG--
branch : nfb
This commit is contained in:
2014-05-05 23:02:13 -07:00
parent 16953ba932
commit 8d7ac095c9
4 changed files with 58 additions and 6 deletions

View File

@@ -414,6 +414,13 @@ void KRAudioManager::renderBlock()
// ----====---- Advance audio sources ----====----
m_audio_frame += KRENGINE_AUDIO_BLOCK_LENGTH;
std::set<KRAudioSample *> open_samples = m_openAudioSamples;
for(auto itr=open_samples.begin(); itr != open_samples.end(); itr++) {
KRAudioSample *sample = *itr;
sample->_endFrame();
}
m_anticlick_block = false;
m_mutex.unlock();
}
@@ -1429,6 +1436,16 @@ void KRAudioManager::deactivateAudioSource(KRAudioSource *audioSource)
m_activeAudioSources.erase(audioSource);
}
void KRAudioManager::_registerOpenAudioSample(KRAudioSample *audioSample)
{
m_openAudioSamples.insert(audioSample);
}
void KRAudioManager::_registerCloseAudioSample(KRAudioSample *audioSample)
{
m_openAudioSamples.erase(audioSample);
}
__int64_t KRAudioManager::getAudioFrame()
{
return m_audio_frame;

View File

@@ -160,6 +160,9 @@ public:
float getReverbMaxLength();
void setReverbMaxLength(float max_length);
void _registerOpenAudioSample(KRAudioSample *audioSample);
void _registerCloseAudioSample(KRAudioSample *audioSample);
private:
bool m_enable_audio;
bool m_enable_hrtf;
@@ -184,6 +187,8 @@ private:
std::set<KRAudioSource *> m_activeAudioSources;
std::set<KRAudioSample *> m_openAudioSamples;
void initAudio();
void initOpenAL();
void initSiren();

View File

@@ -49,7 +49,7 @@ KRAudioSample::KRAudioSample(KRContext &context, std::string name, std::string e
m_frameRate = 0;
m_bufferCount = 0;
openFile();
m_last_frame_used = 0;
}
KRAudioSample::KRAudioSample(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name)
@@ -64,8 +64,7 @@ KRAudioSample::KRAudioSample(KRContext &context, std::string name, std::string e
m_frameRate = 0;
m_bufferCount = 0;
openFile();
m_last_frame_used = 0;
}
KRAudioSample::~KRAudioSample()
@@ -76,18 +75,21 @@ KRAudioSample::~KRAudioSample()
int KRAudioSample::getChannelCount()
{
openFile();
loadInfo();
return m_channelsPerFrame;
}
int KRAudioSample::getFrameCount()
{
loadInfo();
//return (int)((__int64_t)m_totalFrames * (__int64_t)frame_rate / (__int64_t)m_frameRate);
return m_totalFrames;
}
float KRAudioSample::sample(int frame_offset, int frame_rate, int channel)
{
loadInfo();
int c = KRMIN(channel, m_channelsPerFrame - 1);
if(frame_offset < 0) {
@@ -123,6 +125,10 @@ float KRAudioSample::sample(int frame_offset, int frame_rate, int channel)
void KRAudioSample::sample(__int64_t frame_offset, int frame_count, int channel, float *buffer, float amplitude, bool loop)
{
loadInfo();
m_last_frame_used = getContext().getAudioManager()->getAudioFrame();
if(loop) {
int buffer_offset = 0;
int frames_left = frame_count;
@@ -275,6 +281,8 @@ void KRAudioSample::openFile()
m_dataFormat = (outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
m_channelsPerFrame = outputFormat.mChannelsPerFrame;
getContext().getAudioManager()->_registerOpenAudioSample(this);
}
}
@@ -289,6 +297,16 @@ void KRAudioSample::closeFile()
AudioFileClose(m_audio_file_id);
m_audio_file_id = 0;
}
getContext().getAudioManager()->_registerCloseAudioSample(this);
}
void KRAudioSample::loadInfo()
{
if(m_frameRate == 0) {
openFile();
closeFile();
}
}
std::string KRAudioSample::getExtension()
@@ -304,13 +322,13 @@ bool KRAudioSample::save(KRDataBlock &data)
float KRAudioSample::getDuration()
{
openFile();
loadInfo();
return (float)m_totalFrames / (float)m_frameRate;
}
int KRAudioSample::getBufferCount()
{
openFile();
loadInfo();
return m_bufferCount;
}
@@ -348,3 +366,11 @@ KRAudioBuffer *KRAudioSample::getBuffer(int index)
return buffer;
}
void KRAudioSample::_endFrame()
{
const __int64_t AUDIO_SAMPLE_EXPIRY_FRAMES = 500;
long current_frame = getContext().getAudioManager()->getAudioFrame();
if(current_frame > m_last_frame_used + AUDIO_SAMPLE_EXPIRY_FRAMES) {
closeFile();
}
}

View File

@@ -60,8 +60,11 @@ public:
float sample(int frame_offset, int frame_rate, int channel);
void sample(__int64_t frame_offset, int frame_count, int channel, float *buffer, float amplitude, bool loop);
void _endFrame();
private:
long m_last_frame_used;
std::string m_extension;
KRDataBlock *m_pData;
@@ -78,6 +81,7 @@ private:
void openFile();
void closeFile();
void loadInfo();
static OSStatus ReadProc( // AudioFile_ReadProc
void * inClientData,