OGGs preloading, deleting...
This commit is contained in:
@@ -27,7 +27,7 @@ struct JA_Music_t {
|
|||||||
bool loop {false};
|
bool loop {false};
|
||||||
};
|
};
|
||||||
|
|
||||||
JA_Music_t music;
|
JA_Music current_music{NULL};
|
||||||
JA_Sound_Playing_t sounds[JA_MAX_SIMULTANEOUS_SOUNDS];
|
JA_Sound_Playing_t sounds[JA_MAX_SIMULTANEOUS_SOUNDS];
|
||||||
JA_Sound_Playing_t *first_sound {NULL};
|
JA_Sound_Playing_t *first_sound {NULL};
|
||||||
JA_Sound_Playing_t *last_sound {NULL};
|
JA_Sound_Playing_t *last_sound {NULL};
|
||||||
@@ -39,17 +39,17 @@ Uint8 JA_channels {2};
|
|||||||
|
|
||||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||||
SDL_memset(stream, 0, len);
|
SDL_memset(stream, 0, len);
|
||||||
if (music.state == JA_MUSIC_PLAYING) {
|
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||||
const int size = SDL_min(len, music.samples*2-music.pos);
|
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
|
||||||
SDL_memcpy(stream, music.output+music.pos, size);
|
SDL_memcpy(stream, current_music->output+current_music->pos, size);
|
||||||
music.pos += size/2;
|
current_music->pos += size/2;
|
||||||
if (size < len) {
|
if (size < len) {
|
||||||
if (music.loop) {
|
if (current_music->loop) {
|
||||||
SDL_memcpy(stream+size, music.output, len-size);
|
SDL_memcpy(stream+size, current_music->output, len-size);
|
||||||
music.pos = (len-size)/2;
|
current_music->pos = (len-size)/2;
|
||||||
} else {
|
} else {
|
||||||
music.pos = 0;
|
current_music->pos = 0;
|
||||||
music.state = JA_MUSIC_STOPPED;
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,43 +94,62 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels) {
|
|||||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JA_PlayMusic(const char* filename, const bool loop) {
|
JA_Music JA_LoadMusic(const char* filename) {
|
||||||
int chan, samplerate;
|
int chan, samplerate;
|
||||||
if (music.output != NULL) { free(music.output); music.output = NULL; }
|
JA_Music music = (JA_Music)SDL_malloc(sizeof(JA_Music_t));
|
||||||
music.samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music.output);
|
music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
|
||||||
|
|
||||||
SDL_AudioCVT cvt;
|
SDL_AudioCVT cvt;
|
||||||
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
SDL_BuildAudioCVT(&cvt, AUDIO_S16, chan, samplerate, JA_format, JA_channels, JA_freq);
|
||||||
cvt.len = music.samples * chan * 2;
|
cvt.len = music->samples * chan * 2;
|
||||||
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
|
||||||
SDL_memcpy(cvt.buf, music.output, cvt.len);
|
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||||
SDL_ConvertAudio(&cvt);
|
SDL_ConvertAudio(&cvt);
|
||||||
free(music.output);
|
free(music->output);
|
||||||
music.output = (short*)cvt.buf;
|
music->output = (short*)cvt.buf;
|
||||||
|
|
||||||
music.pos = 0;
|
music->pos = 0;
|
||||||
music.state = JA_MUSIC_PLAYING;
|
music->state = JA_MUSIC_STOPPED;
|
||||||
music.loop = loop;
|
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_PlayMusic(JA_Music music, const bool loop) {
|
||||||
|
int chan, samplerate;
|
||||||
|
if (current_music != NULL) {
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
}
|
||||||
|
current_music = music;
|
||||||
|
current_music->pos = 0;
|
||||||
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
|
current_music->loop = loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JA_PauseMusic() {
|
void JA_PauseMusic() {
|
||||||
if (music.state == JA_MUSIC_INVALID) return;
|
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||||
music.state = JA_MUSIC_PAUSED;
|
current_music->state = JA_MUSIC_PAUSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JA_ResumeMusic() {
|
void JA_ResumeMusic() {
|
||||||
if (music.state == JA_MUSIC_INVALID) return;
|
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||||
music.state = JA_MUSIC_PLAYING;
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JA_StopMusic() {
|
void JA_StopMusic() {
|
||||||
if (music.state == JA_MUSIC_INVALID) return;
|
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||||
music.pos = 0;
|
current_music->pos = 0;
|
||||||
music.state = JA_MUSIC_STOPPED;
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JA_IsMusicPlaying() {
|
bool JA_IsMusicPlaying() {
|
||||||
return music.state == JA_MUSIC_PLAYING;
|
return current_music->state == JA_MUSIC_PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
void JA_DeleteMusic(JA_Music music) {
|
||||||
|
if (current_music == music) current_music = NULL;
|
||||||
|
free(music->output);
|
||||||
|
free(music);
|
||||||
}
|
}
|
||||||
|
|
||||||
JA_Sound JA_LoadSound(const char* filename) {
|
JA_Sound JA_LoadSound(const char* filename) {
|
||||||
|
|||||||
@@ -2,14 +2,17 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
typedef struct JA_Sound_t *JA_Sound;
|
typedef struct JA_Sound_t *JA_Sound;
|
||||||
|
typedef struct JA_Music_t *JA_Music;
|
||||||
|
|
||||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
void JA_Init(const int freq, const SDL_AudioFormat format, const int channels);
|
||||||
|
|
||||||
void JA_PlayMusic(const char* filename, const bool loop = true);
|
JA_Music JA_LoadMusic(const char* filename);
|
||||||
|
void JA_PlayMusic(JA_Music music, const bool loop = true);
|
||||||
void JA_PauseMusic();
|
void JA_PauseMusic();
|
||||||
void JA_ResumeMusic();
|
void JA_ResumeMusic();
|
||||||
void JA_StopMusic();
|
void JA_StopMusic();
|
||||||
bool JA_IsMusicPlaying();
|
bool JA_IsMusicPlaying();
|
||||||
|
void JA_DeleteMusic(JA_Music music);
|
||||||
|
|
||||||
JA_Sound JA_LoadSound(const char* filename);
|
JA_Sound JA_LoadSound(const char* filename);
|
||||||
void JA_PlaySound(JA_Sound sound);
|
void JA_PlaySound(JA_Sound sound);
|
||||||
|
|||||||
6
main.cpp
6
main.cpp
@@ -11,9 +11,11 @@ int main(int argc, char **argv) {
|
|||||||
sdlWindow = SDL_CreateWindow("JailAudio", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN);
|
sdlWindow = SDL_CreateWindow("JailAudio", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, SDL_WINDOW_SHOWN);
|
||||||
JA_Init(48000, AUDIO_S16, 2);
|
JA_Init(48000, AUDIO_S16, 2);
|
||||||
|
|
||||||
JA_PlayMusic("intro2.ogg");
|
JA_Music music = JA_LoadMusic("intro2.ogg");
|
||||||
JA_Sound peiv = JA_LoadSound("menu_select.wav");
|
JA_Sound peiv = JA_LoadSound("menu_select.wav");
|
||||||
|
|
||||||
|
JA_PlayMusic(music, true);
|
||||||
|
|
||||||
bool should_exit = false;
|
bool should_exit = false;
|
||||||
while(!should_exit) {
|
while(!should_exit) {
|
||||||
while(SDL_PollEvent(&event)) {
|
while(SDL_PollEvent(&event)) {
|
||||||
@@ -30,6 +32,8 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
JA_DeleteSound(peiv);
|
||||||
|
JA_DeleteMusic(music);
|
||||||
SDL_DestroyWindow(sdlWindow);
|
SDL_DestroyWindow(sdlWindow);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user