Reduced memory utilization of Siren audio engine by dyanmically opening and closing the CoreAudio virtual file.
--HG-- branch : nfb
This commit is contained in:
@@ -414,6 +414,13 @@ void KRAudioManager::renderBlock()
|
|||||||
// ----====---- Advance audio sources ----====----
|
// ----====---- Advance audio sources ----====----
|
||||||
m_audio_frame += KRENGINE_AUDIO_BLOCK_LENGTH;
|
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_anticlick_block = false;
|
||||||
m_mutex.unlock();
|
m_mutex.unlock();
|
||||||
}
|
}
|
||||||
@@ -1429,6 +1436,16 @@ void KRAudioManager::deactivateAudioSource(KRAudioSource *audioSource)
|
|||||||
m_activeAudioSources.erase(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()
|
__int64_t KRAudioManager::getAudioFrame()
|
||||||
{
|
{
|
||||||
return m_audio_frame;
|
return m_audio_frame;
|
||||||
|
|||||||
@@ -160,6 +160,9 @@ public:
|
|||||||
float getReverbMaxLength();
|
float getReverbMaxLength();
|
||||||
void setReverbMaxLength(float max_length);
|
void setReverbMaxLength(float max_length);
|
||||||
|
|
||||||
|
void _registerOpenAudioSample(KRAudioSample *audioSample);
|
||||||
|
void _registerCloseAudioSample(KRAudioSample *audioSample);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_enable_audio;
|
bool m_enable_audio;
|
||||||
bool m_enable_hrtf;
|
bool m_enable_hrtf;
|
||||||
@@ -184,6 +187,8 @@ private:
|
|||||||
|
|
||||||
std::set<KRAudioSource *> m_activeAudioSources;
|
std::set<KRAudioSource *> m_activeAudioSources;
|
||||||
|
|
||||||
|
std::set<KRAudioSample *> m_openAudioSamples;
|
||||||
|
|
||||||
void initAudio();
|
void initAudio();
|
||||||
void initOpenAL();
|
void initOpenAL();
|
||||||
void initSiren();
|
void initSiren();
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ KRAudioSample::KRAudioSample(KRContext &context, std::string name, std::string e
|
|||||||
m_frameRate = 0;
|
m_frameRate = 0;
|
||||||
m_bufferCount = 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)
|
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_frameRate = 0;
|
||||||
m_bufferCount = 0;
|
m_bufferCount = 0;
|
||||||
|
|
||||||
openFile();
|
m_last_frame_used = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
KRAudioSample::~KRAudioSample()
|
KRAudioSample::~KRAudioSample()
|
||||||
@@ -76,18 +75,21 @@ KRAudioSample::~KRAudioSample()
|
|||||||
|
|
||||||
int KRAudioSample::getChannelCount()
|
int KRAudioSample::getChannelCount()
|
||||||
{
|
{
|
||||||
openFile();
|
loadInfo();
|
||||||
return m_channelsPerFrame;
|
return m_channelsPerFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
int KRAudioSample::getFrameCount()
|
int KRAudioSample::getFrameCount()
|
||||||
{
|
{
|
||||||
|
loadInfo();
|
||||||
//return (int)((__int64_t)m_totalFrames * (__int64_t)frame_rate / (__int64_t)m_frameRate);
|
//return (int)((__int64_t)m_totalFrames * (__int64_t)frame_rate / (__int64_t)m_frameRate);
|
||||||
return m_totalFrames;
|
return m_totalFrames;
|
||||||
}
|
}
|
||||||
|
|
||||||
float KRAudioSample::sample(int frame_offset, int frame_rate, int channel)
|
float KRAudioSample::sample(int frame_offset, int frame_rate, int channel)
|
||||||
{
|
{
|
||||||
|
loadInfo();
|
||||||
|
|
||||||
int c = KRMIN(channel, m_channelsPerFrame - 1);
|
int c = KRMIN(channel, m_channelsPerFrame - 1);
|
||||||
|
|
||||||
if(frame_offset < 0) {
|
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)
|
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) {
|
if(loop) {
|
||||||
int buffer_offset = 0;
|
int buffer_offset = 0;
|
||||||
int frames_left = frame_count;
|
int frames_left = frame_count;
|
||||||
@@ -275,6 +281,8 @@ void KRAudioSample::openFile()
|
|||||||
|
|
||||||
m_dataFormat = (outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
|
m_dataFormat = (outputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
|
||||||
m_channelsPerFrame = outputFormat.mChannelsPerFrame;
|
m_channelsPerFrame = outputFormat.mChannelsPerFrame;
|
||||||
|
|
||||||
|
getContext().getAudioManager()->_registerOpenAudioSample(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,6 +297,16 @@ void KRAudioSample::closeFile()
|
|||||||
AudioFileClose(m_audio_file_id);
|
AudioFileClose(m_audio_file_id);
|
||||||
m_audio_file_id = 0;
|
m_audio_file_id = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getContext().getAudioManager()->_registerCloseAudioSample(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void KRAudioSample::loadInfo()
|
||||||
|
{
|
||||||
|
if(m_frameRate == 0) {
|
||||||
|
openFile();
|
||||||
|
closeFile();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string KRAudioSample::getExtension()
|
std::string KRAudioSample::getExtension()
|
||||||
@@ -304,13 +322,13 @@ bool KRAudioSample::save(KRDataBlock &data)
|
|||||||
|
|
||||||
float KRAudioSample::getDuration()
|
float KRAudioSample::getDuration()
|
||||||
{
|
{
|
||||||
openFile();
|
loadInfo();
|
||||||
return (float)m_totalFrames / (float)m_frameRate;
|
return (float)m_totalFrames / (float)m_frameRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
int KRAudioSample::getBufferCount()
|
int KRAudioSample::getBufferCount()
|
||||||
{
|
{
|
||||||
openFile();
|
loadInfo();
|
||||||
return m_bufferCount;
|
return m_bufferCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,3 +366,11 @@ KRAudioBuffer *KRAudioSample::getBuffer(int index)
|
|||||||
return buffer;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -60,8 +60,11 @@ public:
|
|||||||
float sample(int frame_offset, int frame_rate, int channel);
|
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 sample(__int64_t frame_offset, int frame_count, int channel, float *buffer, float amplitude, bool loop);
|
||||||
|
|
||||||
|
void _endFrame();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
long m_last_frame_used;
|
||||||
|
|
||||||
std::string m_extension;
|
std::string m_extension;
|
||||||
KRDataBlock *m_pData;
|
KRDataBlock *m_pData;
|
||||||
|
|
||||||
@@ -78,6 +81,7 @@ private:
|
|||||||
|
|
||||||
void openFile();
|
void openFile();
|
||||||
void closeFile();
|
void closeFile();
|
||||||
|
void loadInfo();
|
||||||
|
|
||||||
static OSStatus ReadProc( // AudioFile_ReadProc
|
static OSStatus ReadProc( // AudioFile_ReadProc
|
||||||
void * inClientData,
|
void * inClientData,
|
||||||
|
|||||||
Reference in New Issue
Block a user