forked from JailDoctor/JailAudio
OGGs preloading, deleting...
This commit is contained in:
@@ -27,7 +27,7 @@ struct JA_Music_t {
|
||||
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 *first_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) {
|
||||
SDL_memset(stream, 0, len);
|
||||
if (music.state == JA_MUSIC_PLAYING) {
|
||||
const int size = SDL_min(len, music.samples*2-music.pos);
|
||||
SDL_memcpy(stream, music.output+music.pos, size);
|
||||
music.pos += size/2;
|
||||
if (current_music != NULL && current_music->state == JA_MUSIC_PLAYING) {
|
||||
const int size = SDL_min(len, current_music->samples*2-current_music->pos);
|
||||
SDL_memcpy(stream, current_music->output+current_music->pos, size);
|
||||
current_music->pos += size/2;
|
||||
if (size < len) {
|
||||
if (music.loop) {
|
||||
SDL_memcpy(stream+size, music.output, len-size);
|
||||
music.pos = (len-size)/2;
|
||||
if (current_music->loop) {
|
||||
SDL_memcpy(stream+size, current_music->output, len-size);
|
||||
current_music->pos = (len-size)/2;
|
||||
} else {
|
||||
music.pos = 0;
|
||||
music.state = JA_MUSIC_STOPPED;
|
||||
current_music->pos = 0;
|
||||
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);
|
||||
}
|
||||
|
||||
void JA_PlayMusic(const char* filename, const bool loop) {
|
||||
JA_Music JA_LoadMusic(const char* filename) {
|
||||
int chan, samplerate;
|
||||
if (music.output != NULL) { free(music.output); music.output = NULL; }
|
||||
music.samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music.output);
|
||||
JA_Music music = (JA_Music)SDL_malloc(sizeof(JA_Music_t));
|
||||
music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output);
|
||||
|
||||
SDL_AudioCVT cvt;
|
||||
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);
|
||||
SDL_memcpy(cvt.buf, music.output, cvt.len);
|
||||
SDL_memcpy(cvt.buf, music->output, cvt.len);
|
||||
SDL_ConvertAudio(&cvt);
|
||||
free(music.output);
|
||||
music.output = (short*)cvt.buf;
|
||||
free(music->output);
|
||||
music->output = (short*)cvt.buf;
|
||||
|
||||
music.pos = 0;
|
||||
music.state = JA_MUSIC_PLAYING;
|
||||
music.loop = loop;
|
||||
music->pos = 0;
|
||||
music->state = JA_MUSIC_STOPPED;
|
||||
|
||||
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() {
|
||||
if (music.state == JA_MUSIC_INVALID) return;
|
||||
music.state = JA_MUSIC_PAUSED;
|
||||
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PAUSED;
|
||||
}
|
||||
|
||||
void JA_ResumeMusic() {
|
||||
if (music.state == JA_MUSIC_INVALID) return;
|
||||
music.state = JA_MUSIC_PLAYING;
|
||||
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->state = JA_MUSIC_PLAYING;
|
||||
}
|
||||
|
||||
void JA_StopMusic() {
|
||||
if (music.state == JA_MUSIC_INVALID) return;
|
||||
music.pos = 0;
|
||||
music.state = JA_MUSIC_STOPPED;
|
||||
if (current_music->state == JA_MUSIC_INVALID) return;
|
||||
current_music->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user