mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
increase memory safety of audio module & small optimisations
This commit is contained in:
@@ -65,8 +65,8 @@ void game_init(game_data* const dat) {
|
|||||||
|
|
||||||
// initialize audio
|
// initialize audio
|
||||||
dat->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
dat->audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096);
|
||||||
dat->music = audio_load_wav(dat->audio_device, "korobeiniki.wav");
|
dat->music = audio_wav_load(dat->audio_device, "korobeiniki.wav");
|
||||||
audio_play(dat->audio_device, dat->music);
|
audio_play(dat->audio_device, &dat->music);
|
||||||
}
|
}
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
@@ -83,10 +83,13 @@ void game_update(game_data* const dat, uint8_t const* const keys) {
|
|||||||
place_update(dat, move);
|
place_update(dat, move);
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_free(game_data* const game_data) {
|
void game_free(game_data* const dat) {
|
||||||
|
audio_wav_unload(&dat->music);
|
||||||
|
audio_device_free(dat->audio_device);
|
||||||
|
|
||||||
// clear each row
|
// clear each row
|
||||||
for (int8_t i = 0; i < ROWS; i++) {
|
for (int8_t i = 0; i < ROWS; i++) {
|
||||||
free(game_data->rows[i]);
|
free(dat->rows[i]);
|
||||||
game_data->rows[i] = NULL;
|
dat->rows[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ typedef colour8* row;
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
row rows[ROWS];
|
row rows[ROWS];
|
||||||
audio_device const* audio_device;
|
audio_device* audio_device;
|
||||||
audio_data music;
|
audio_data music;
|
||||||
uint16_t score;
|
uint16_t score;
|
||||||
shape_id nxt[7]; // the order of the shape ids that they should appear in
|
shape_id nxt[7]; // the order of the shape ids that they should appear in
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ typedef struct {
|
|||||||
audio_device const audio_device;
|
audio_device const audio_device;
|
||||||
} AudioCallbackData;
|
} AudioCallbackData;
|
||||||
|
|
||||||
|
//
|
||||||
|
// audio mixing
|
||||||
|
//
|
||||||
// audio callback from SDL_AudioSpec; called when the audio device needs more data
|
// audio callback from SDL_AudioSpec; called when the audio device needs more data
|
||||||
static void audio_mixer(void* const userdata, uint8_t* const stream, int32_t const len) {
|
static void audio_mixer(void* const userdata, uint8_t* const stream, int32_t const len) {
|
||||||
memset(stream, 0, len); // clear the playing audio
|
memset(stream, 0, len); // clear the playing audio
|
||||||
@@ -39,41 +42,44 @@ static void audio_mixer(void* const userdata, uint8_t* const stream, int32_t con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts the audio to the format of the audio device
|
// converts the audio to the format of the audio device, reallocates wav_buf to a new size outputted to wav_len
|
||||||
static void convert_audio(audio_device const* const audio_device, SDL_AudioSpec const wav_spec, uint8_t* const* const wav_buffer, uint32_t* const wav_length) {
|
static void convert_audio(audio_device const* const dev, SDL_AudioSpec const wav_spec, uint8_t** const wav_buf, uint32_t* const wav_len) {
|
||||||
// build the audio converter with the audio given
|
// build the audio converter with the audio given
|
||||||
SDL_AudioCVT cvt = {0};
|
SDL_AudioCVT cvt = {0};
|
||||||
SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, audio_device->fmt, audio_device->channels, audio_device->freq);
|
SDL_BuildAudioCVT(&cvt, wav_spec.format, wav_spec.channels, wav_spec.freq, dev->fmt, dev->channels, dev->freq);
|
||||||
|
cvt.len = (*wav_len) * wav_spec.channels; // calculate the size of the source data in bytes by multiplying the length by the amount of channels (warn: uint32_t -> int32_t)
|
||||||
|
cvt.buf = realloc(*wav_buf, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
|
||||||
|
|
||||||
// suddenly becomes signed
|
// performs the conversion
|
||||||
cvt.len = (*wav_length) * wav_spec.channels; // the buffer length
|
if (SDL_ConvertAudio(&cvt) != 0)
|
||||||
cvt.buf = (uint8_t*)SDL_malloc(cvt.len * cvt.len_mult); // allocate size for the new buffer
|
error(ERROR_SDL_AUDIO_INIT, "something went wrong when converting an audio buffer! SDL Error: %s", SDL_GetError());
|
||||||
memcpy(cvt.buf, *wav_buffer, *wav_length); // copy wav data to cvt buffer;
|
|
||||||
|
|
||||||
// convert
|
// set the length to the new length
|
||||||
SDL_ConvertAudio(&cvt);
|
*wav_len = cvt.len_cvt;
|
||||||
|
|
||||||
// output
|
// reallocate the conversion buffer to match the new size
|
||||||
*wav_length = cvt.len_cvt; // set the length to the new length
|
*wav_buf = realloc(cvt.buf, cvt.len_cvt);
|
||||||
memcpy(*wav_buffer, cvt.buf, cvt.len_cvt); // copy converted cvt buffer back to wav buffer
|
if (*wav_buf == NULL)
|
||||||
|
error(ERROR_MISC, "memory pointer changed upon reallocation of audio buffer after conversion");
|
||||||
free(cvt.buf); // free the memory allocated to the cvt buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// audio / audio device management
|
||||||
|
//
|
||||||
// loads a WAV file and returns the relevant information
|
// loads a WAV file and returns the relevant information
|
||||||
audio_data audio_load_wav(audio_device const* const dev, char const* const fpath) {
|
audio_data audio_wav_load(audio_device const* const dev, char const* const fpath) {
|
||||||
SDL_AudioSpec wav_spec = {0};
|
SDL_AudioSpec wav_spec = {0};
|
||||||
audio_data audio = {0};
|
audio_data audio = {0};
|
||||||
|
|
||||||
SDL_LoadWAV(fpath, &wav_spec, &audio.buf, &audio.len);
|
SDL_LoadWAV(fpath, &wav_spec, &audio.buf, &audio.len);
|
||||||
convert_audio(dev, wav_spec, &audio.buf, &audio.len);
|
convert_audio(dev, wav_spec, &audio.buf, &audio.len);
|
||||||
audio.mix_amt = audio.len;
|
|
||||||
|
|
||||||
return audio;
|
return audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
// initializes the audio device
|
// initializes the audio device
|
||||||
audio_device const* audio_device_init(int32_t const freq, SDL_AudioFormat const fmt, uint8_t const channels, uint16_t const samples) {
|
audio_device* audio_device_init(int32_t const freq, SDL_AudioFormat const fmt, uint8_t const channels, uint16_t const samples) {
|
||||||
// allocate memory for the audio device
|
// allocate memory for the audio device
|
||||||
audio_device* const dev = malloc(sizeof(audio_device));
|
audio_device* const dev = malloc(sizeof(audio_device));
|
||||||
|
|
||||||
@@ -103,15 +109,27 @@ audio_device const* audio_device_init(int32_t const freq, SDL_AudioFormat const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// plays the audio
|
// plays the audio
|
||||||
void audio_play(audio_device const* const dev, audio_data const audio) {
|
void audio_play(audio_device const* const dev, audio_data const* audio) {
|
||||||
audio_data* const playing_audio = dev->playing_audio;
|
audio_data* const playing_audio = dev->playing_audio;
|
||||||
|
|
||||||
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
for (int32_t i = 0; i < MAX_SOUNDS; i++) {
|
||||||
// overrite audio that has been deallocated
|
// overrite audio that has been deallocated
|
||||||
if (playing_audio[i].len <= 0) {
|
if (playing_audio[i].len <= 0) {
|
||||||
// override the audio
|
// override the audio
|
||||||
playing_audio[i] = audio;
|
playing_audio[i] = *audio;
|
||||||
break; // don't continue. :3
|
break; // don't continue. :3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// frees the audio device
|
||||||
|
void audio_device_free(audio_device* const dev) {
|
||||||
|
SDL_CloseAudioDevice(dev->id);
|
||||||
|
free(dev->playing_audio);
|
||||||
|
free(dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
// frees the buffer of the audio data
|
||||||
|
void audio_wav_unload(audio_data* dat) {
|
||||||
|
free(dat->buf);
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
uint32_t mix_amt;
|
|
||||||
uint8_t* buf;
|
uint8_t* buf;
|
||||||
} audio_data;
|
} audio_data;
|
||||||
|
|
||||||
@@ -18,7 +17,9 @@ typedef struct {
|
|||||||
} audio_device;
|
} audio_device;
|
||||||
|
|
||||||
|
|
||||||
audio_data audio_load_wav(audio_device const* audio_device, char const* file_path);
|
audio_data audio_wav_load(audio_device const* audio_device, char const* file_path);
|
||||||
audio_device const* audio_device_init(int freq, SDL_AudioFormat format, uint8_t channels, uint16_t samples);
|
audio_device* audio_device_init(int freq, SDL_AudioFormat format, uint8_t channels, uint16_t samples);
|
||||||
void audio_play(audio_device const* audio_device, audio_data const audio);
|
void audio_play(audio_device const* audio_device, audio_data const* audio);
|
||||||
void audio_device_free(audio_device const* dev); // TODO: implement this
|
|
||||||
|
void audio_device_free(audio_device* dev);
|
||||||
|
void audio_wav_unload(audio_data* dat);
|
||||||
|
|||||||
Reference in New Issue
Block a user