diff options
author | sanine <sanine.not@pm.me> | 2022-09-07 11:23:34 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-09-07 11:23:34 -0500 |
commit | a77b34a4ebdc994cce7a3d1f00121f421ca31b53 (patch) | |
tree | 19901965a6e2d0e0afe5f441306acb15cd38af95 | |
parent | 500dd00fb11efdd19aef924b0d8ff0b370c28d33 (diff) |
update README and fix memory leaks
-rw-r--r-- | README.md | 18 | ||||
-rw-r--r-- | examples/mp3.c | 7 | ||||
-rw-r--r-- | include/mossrose.h | 1 | ||||
-rw-r--r-- | src/load/load-mp3.c | 11 | ||||
-rw-r--r-- | src/mossrose.c | 7 | ||||
-rw-r--r-- | src/sound.c | 4 |
6 files changed, 30 insertions, 18 deletions
@@ -142,6 +142,24 @@ Callbacks will not be triggered on their own! You must periodically call Check all channels for playback-finished callbacks and run them if necessary. +### `struct mossrose_sound_t * load_mp3(const char *filename)` ### + +Load an MP3 file. + + * `filename` - the name of the file to load. + +Returns a pointer to a `mossrose_sound_t` struct containing the audio data, or NULL on an error. You must free the sound yourself using `mossrose_free_sound`. + + +### `void mossrose_free_sound(struct mossrose_sound_t *sound)` ### + +Free a mossrose-allocated sound struct. + + * `sound` - a pointer to a sound struct. + +Using this function with user-allocated sounds may or may not work correctly -- be sure you know what you're doing! + + todo ---- diff --git a/examples/mp3.c b/examples/mp3.c index 7560bf1..7ca545d 100644 --- a/examples/mp3.c +++ b/examples/mp3.c @@ -22,10 +22,7 @@ int main() Pa_Sleep(1000); mossrose_terminate(); - free(mono->left); - free(mono); - free(stereo->left); - free(stereo->right); - free(stereo); + mossrose_free_sound(mono); + mossrose_free_sound(stereo); return 0; } diff --git a/include/mossrose.h b/include/mossrose.h index 8003b45..04aef94 100644 --- a/include/mossrose.h +++ b/include/mossrose.h @@ -26,5 +26,6 @@ void mossrose_channel_set_callback(int channel, mossrose_channel_callback_t call void mossrose_poll_callbacks(); struct mossrose_sound_t * mossrose_load_mp3(const char *filename); +void mossrose_free_sound(struct mossrose_sound_t *sound); #endif diff --git a/src/load/load-mp3.c b/src/load/load-mp3.c index 09eef0c..d3e7598 100644 --- a/src/load/load-mp3.c +++ b/src/load/load-mp3.c @@ -22,17 +22,6 @@ struct mossrose_sound_t * load_mp3(const char *filename) return NULL; } - printf("samples: %lu\n" - "channels: %d\n" - "hz: %d\n" - "layer: %d\n" - "bitrate: %d\n", - mp3.samples, - mp3.channels, - mp3.hz, - mp3.layer, - mp3.avg_bitrate_kbps); - if (mp3.channels != 1 && mp3.channels != 2) { ABORT("files with %d audio tracks are not supported!\n", mp3.channels); } diff --git a/src/mossrose.c b/src/mossrose.c index f96d394..61ee27b 100644 --- a/src/mossrose.c +++ b/src/mossrose.c @@ -161,6 +161,13 @@ struct mossrose_sound_t * mossrose_load_mp3(const char *filename) } +void mossrose_free_sound(struct mossrose_sound_t *sound) +{ + sound_free_audio(sound); + free(sound); +} + + int mossrose_terminate() { for (int i=0; i<mossrose_global.n_channels; i++) { diff --git a/src/sound.c b/src/sound.c index 7296578..73bd6d4 100644 --- a/src/sound.c +++ b/src/sound.c @@ -34,11 +34,11 @@ int sound_copy(struct mossrose_sound_t *dest, struct mossrose_sound_t *src) void sound_free_audio(struct mossrose_sound_t *sound) { - if (sound->left == NULL) { + if (sound->left != NULL) { free(sound->left); sound->left = NULL; } - if (sound->right == NULL) { + if (sound->right != NULL) { free(sound->right); sound->right = NULL; } |