Skip to content

Commit

Permalink
Unload streaming data when stopping audio
Browse files Browse the repository at this point in the history
Forgot some memory freeing, sorry.
  • Loading branch information
Reuh committed Mar 23, 2016
1 parent 04eb578 commit 4c9bdf7
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions source/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ typedef struct {
double prevStartTime; // audio time when last chunk started playing
bool eof; // if reached end of file
bool done; // if streaming ended and the stream will be skipped on the next update
// (the struct should be keept in memory until replaced or it will break audio:time())
// (the struct should be keept in memory until replaced or audio stopped or it will break audio:time())

char* nextData; // the next data to play
ndspWaveBuf* nextWaveBuf;
Expand All @@ -107,6 +107,34 @@ audio_userdata* channels[24];
// Array of the audio_instance that needs to be updated when calling audio.update (indexed per channel).
audio_stream* streaming[24];

// Stop playing audio on a channel, and stop streaming/free memory.
void stopAudio(int channel) {
ndspChnWaveBufClear(channel);

// Stop streaming and free data
if (streaming[channel] != NULL) {
audio_stream* stream = streaming[channel];
if (stream->nextWaveBuf != NULL) {
free(stream->nextWaveBuf);
stream->nextWaveBuf = NULL;
}
if (stream->nextData != NULL) {
linearFree(stream->nextData);
stream->nextData = NULL;
}
if (stream->prevWaveBuf != NULL) {
free(stream->prevWaveBuf);
stream->prevWaveBuf = NULL;
}
if (stream->prevData != NULL) {
linearFree(stream->prevData);
stream->prevData = NULL;
}
free(stream);
streaming[channel] = NULL;
}
}

/***
Load an audio file.
OGG Vorbis and PCM WAV file format are currently supported.
Expand Down Expand Up @@ -542,15 +570,15 @@ static int audio_stop(lua_State *L) {
if (channel == -1) {
for (int i = 0; i <= 23; i++) {
if (ndspChnIsPlaying(i)) {
ndspChnWaveBufClear(i);
stopAudio(i);
n++;
}
}
} else if (channel < 0 || channel > 23) {
luaL_error(L, "channel number must be between 0 and 23");
} else {
if (ndspChnIsPlaying(channel)) {
ndspChnWaveBufClear(channel);
stopAudio(channel);
n++;
}
}
Expand Down Expand Up @@ -838,7 +866,7 @@ static int audio_object_play(lua_State *L) {
if (channel < 0 || channel > 23) luaL_error(L, "channel number must be between 0 and 23");

// Set channel parameters
ndspChnWaveBufClear(channel);
stopAudio(channel);
ndspChnReset(channel);
ndspChnInitParams(channel);
ndspChnSetMix(channel, audio->mix);
Expand Down Expand Up @@ -910,15 +938,15 @@ static int audio_object_stop(lua_State *L) {
if (channel == -1) {
for (int i = 0; i <= 23; i++) {
if (channels[i] == audio && ndspChnIsPlaying(i)) {
ndspChnWaveBufClear(i);
stopAudio(i);
n++;
}
}
} else if (channel < 0 || channel > 23) {
luaL_error(L, "channel number must be between 0 and 23");
} else {
if (channels[channel] == audio && ndspChnIsPlaying(channel)) {
ndspChnWaveBufClear(channel);
stopAudio(channel);
n++;
}
}
Expand Down Expand Up @@ -959,7 +987,7 @@ static int audio_object_unload(lua_State *L) {
if (isAudioInitialized) {
for (int i = 0; i <= 23; i++) {
if (channels[i] == audio) {
ndspChnWaveBufClear(i);
stopAudio(i);
}
}
}
Expand Down

0 comments on commit 4c9bdf7

Please sign in to comment.