Skip to content

Commit

Permalink
Merge pull request #37362 from reduz/audioserver-memory-cleanup
Browse files Browse the repository at this point in the history
Remove the audio memory allocator, use regular one instead.
  • Loading branch information
akien-mga authored Mar 28, 2020
2 parents d2d359d + 16245f2 commit 3f7fa93
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 65 deletions.
11 changes: 6 additions & 5 deletions modules/stb_vorbis/audio_stream_ogg_vorbis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void AudioStreamPlaybackOGGVorbis::seek(float p_time) {
AudioStreamPlaybackOGGVorbis::~AudioStreamPlaybackOGGVorbis() {
if (ogg_alloc.alloc_buffer) {
stb_vorbis_close(ogg_stream);
AudioServer::get_singleton()->audio_data_free(ogg_alloc.alloc_buffer);
memfree(ogg_alloc.alloc_buffer);
}
}

Expand All @@ -134,7 +134,7 @@ Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {

ovs.instance();
ovs->vorbis_stream = Ref<AudioStreamOGGVorbis>(this);
ovs->ogg_alloc.alloc_buffer = (char *)AudioServer::get_singleton()->audio_data_alloc(decode_mem_size);
ovs->ogg_alloc.alloc_buffer = (char *)memalloc(decode_mem_size);
ovs->ogg_alloc.alloc_buffer_length_in_bytes = decode_mem_size;
ovs->frames_mixed = 0;
ovs->active = false;
Expand All @@ -143,7 +143,7 @@ Ref<AudioStreamPlayback> AudioStreamOGGVorbis::instance_playback() {
ovs->ogg_stream = stb_vorbis_open_memory((const unsigned char *)data, data_len, &error, &ovs->ogg_alloc);
if (!ovs->ogg_stream) {

AudioServer::get_singleton()->audio_data_free(ovs->ogg_alloc.alloc_buffer);
memfree(ovs->ogg_alloc.alloc_buffer);
ovs->ogg_alloc.alloc_buffer = NULL;
ERR_FAIL_COND_V(!ovs->ogg_stream, Ref<AudioStreamPlaybackOGGVorbis>());
}
Expand All @@ -158,7 +158,7 @@ String AudioStreamOGGVorbis::get_stream_name() const {

void AudioStreamOGGVorbis::clear_data() {
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_len = 0;
}
Expand Down Expand Up @@ -210,7 +210,8 @@ void AudioStreamOGGVorbis::set_data(const Vector<uint8_t> &p_data) {
// free any existing data
clear_data();

data = AudioServer::get_singleton()->audio_data_alloc(src_data_len, src_datar);
data = memalloc(src_data_len);
copymem(data, src_datar, src_data_len);
data_len = src_data_len;

break;
Expand Down
6 changes: 3 additions & 3 deletions scene/resources/audio_stream_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {

AudioServer::get_singleton()->lock();
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_bytes = 0;
}
Expand All @@ -491,7 +491,7 @@ void AudioStreamSample::set_data(const Vector<uint8_t> &p_data) {

const uint8_t *r = p_data.ptr();
int alloc_len = datalen + DATA_PAD * 2;
data = AudioServer::get_singleton()->audio_data_alloc(alloc_len); //alloc with some padding for interpolation
data = memalloc(alloc_len); //alloc with some padding for interpolation
zeromem(data, alloc_len);
uint8_t *dataptr = (uint8_t *)data;
copymem(dataptr + DATA_PAD, r, datalen);
Expand Down Expand Up @@ -660,7 +660,7 @@ AudioStreamSample::AudioStreamSample() {

AudioStreamSample::~AudioStreamSample() {
if (data) {
AudioServer::get_singleton()->audio_data_free(data);
memfree(data);
data = NULL;
data_bytes = 0;
}
Expand Down
43 changes: 0 additions & 43 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,47 +1131,6 @@ double AudioServer::get_time_since_last_mix() const {

AudioServer *AudioServer::singleton = NULL;

void *AudioServer::audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data) {

void *ad = memalloc(p_data_len);
ERR_FAIL_COND_V(!ad, NULL);
if (p_from_data) {
copymem(ad, p_from_data, p_data_len);
}

{
MutexLock lock(audio_data_lock);

audio_data[ad] = p_data_len;
audio_data_total_mem += p_data_len;
audio_data_max_mem = MAX(audio_data_total_mem, audio_data_max_mem);
}

return ad;
}

void AudioServer::audio_data_free(void *p_data) {

MutexLock lock(audio_data_lock);

if (!audio_data.has(p_data)) {
ERR_FAIL();
}

audio_data_total_mem -= audio_data[p_data];
audio_data.erase(p_data);
memfree(p_data);
}

size_t AudioServer::audio_data_get_total_memory_usage() const {

return audio_data_total_mem;
}
size_t AudioServer::audio_data_get_max_memory_usage() const {

return audio_data_max_mem;
}

void AudioServer::add_callback(AudioCallback p_callback, void *p_userdata) {
lock();
CallbackItem ci;
Expand Down Expand Up @@ -1400,8 +1359,6 @@ void AudioServer::_bind_methods() {
AudioServer::AudioServer() {

singleton = this;
audio_data_total_mem = 0;
audio_data_max_mem = 0;
mix_frames = 0;
channel_count = 0;
to_mix = 0;
Expand Down
14 changes: 0 additions & 14 deletions servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,6 @@ class AudioServer : public Object {

static AudioServer *singleton;

// TODO create an audiodata pool to optimize memory

Map<void *, uint32_t> audio_data;
size_t audio_data_total_mem;
size_t audio_data_max_mem;

Mutex audio_data_lock;

void init_channels_and_buffers();

void _mix_step();
Expand Down Expand Up @@ -350,12 +342,6 @@ class AudioServer : public Object {
virtual double get_time_to_next_mix() const;
virtual double get_time_since_last_mix() const;

void *audio_data_alloc(uint32_t p_data_len, const uint8_t *p_from_data = NULL);
void audio_data_free(void *p_data);

size_t audio_data_get_total_memory_usage() const;
size_t audio_data_get_max_memory_usage() const;

void add_callback(AudioCallback p_callback, void *p_userdata);
void remove_callback(AudioCallback p_callback, void *p_userdata);

Expand Down

0 comments on commit 3f7fa93

Please sign in to comment.