280 lines
6.5 KiB
C
280 lines
6.5 KiB
C
#include <AL/al.h>
|
|
#include <AL/alc.h>
|
|
#include <vorbis/vorbisfile.h>
|
|
#include "SDL.h"
|
|
|
|
#include "main.h"
|
|
#include "log.h"
|
|
#include "pack.h"
|
|
#include "music.h"
|
|
|
|
#define MUSIC_PLAYING (2<<0)
|
|
#define MUSIC_KILL (9<<0)
|
|
#define music_is(f) (music_state & f)
|
|
#define music_set(f) (music_state |= f)
|
|
#define music_rm(f) (music_state ^= f)
|
|
|
|
#define MUSIC_PREFIX "../snd/music/"
|
|
#define MUSIC_SUFFIX ".ogg"
|
|
|
|
#define BUFFER_SIZE (4096 * 8)
|
|
|
|
// Gobal sound mutex.
|
|
extern SDL_mutex* sound_lock;
|
|
|
|
// Saves the music to ram in this structure.
|
|
typedef struct {
|
|
char name[32]; // Name.
|
|
Packfile file;
|
|
OggVorbis_File stream;
|
|
vorbis_info* info;
|
|
ALenum format;
|
|
} alMusic;
|
|
|
|
// Song currently playing.
|
|
static SDL_mutex* music_vorbis_lock;
|
|
static alMusic music_vorbis;
|
|
static ALuint music_buffer[2]; // Front and back buffer.
|
|
static ALuint music_source = 0;
|
|
|
|
// What is available?
|
|
static char** music_selection = NULL;
|
|
static int nmusic_selection = 0;
|
|
|
|
// Vorbis suff.
|
|
static size_t ovpack_read(void* ptr, size_t size, size_t nmemb, void* datasource) {
|
|
return (ssize_t) pack_read(datasource, ptr, size*nmemb);
|
|
}
|
|
|
|
ov_callbacks ovcall = {
|
|
.read_func = ovpack_read,
|
|
.seek_func = NULL,
|
|
.close_func = NULL,
|
|
.tell_func = NULL
|
|
};
|
|
|
|
static int stream_loadBuffer(ALuint buffer);
|
|
static int music_find(void);
|
|
static int music_loadOGG(const char* filename);
|
|
static void music_free(void);
|
|
|
|
// The thread.
|
|
static unsigned int music_state = 0;
|
|
int music_thread(void* unused) {
|
|
(void)unused;
|
|
|
|
int active; // Active buffer.
|
|
ALint stat;
|
|
|
|
// Main loop.
|
|
while(!music_is(MUSIC_KILL)) {
|
|
if(music_is(MUSIC_PLAYING)) {
|
|
SDL_mutexP(music_vorbis_lock); // Lock the mutex.
|
|
SDL_mutexP(sound_lock);
|
|
|
|
// Start playing current song.
|
|
active = 0; // Load first buffer.
|
|
if(stream_loadBuffer(music_buffer[active])) music_rm(MUSIC_PLAYING);
|
|
alSourceQueueBuffers(music_source, 1, &music_buffer[active]);
|
|
|
|
// Start playing with the buffer loaded.
|
|
alSourcePlay(music_source);
|
|
|
|
active = 1; // Load second buffer.
|
|
if(stream_loadBuffer(music_buffer[active])) music_rm(MUSIC_PLAYING);
|
|
alSourceQueueBuffers(music_source, 1, &music_buffer[active]);
|
|
|
|
SDL_mutexV(sound_lock);
|
|
|
|
active = 0; // dive into the loop.
|
|
while(music_is(MUSIC_PLAYING)) {
|
|
SDL_mutexP(sound_lock);
|
|
|
|
alGetSourcei(music_source, AL_BUFFERS_PROCESSED, &stat);
|
|
if(stat > 0) {
|
|
// Refill active buffer.
|
|
alSourceUnqueueBuffers(music_source, 1, &music_buffer[active]);
|
|
if(stream_loadBuffer(music_buffer[active])) music_rm(MUSIC_PLAYING);
|
|
alSourceQueueBuffers(music_source, 1, &music_buffer[active]);
|
|
|
|
active = 1 - active;
|
|
}
|
|
SDL_mutexV(sound_lock);
|
|
|
|
SDL_Delay(0);
|
|
}
|
|
SDL_mutexP(sound_lock);
|
|
|
|
alSourceStop(music_source);
|
|
alSourceUnqueueBuffers(music_source, 2, music_buffer);
|
|
|
|
SDL_mutexV(sound_lock);
|
|
SDL_mutexV(music_vorbis_lock);
|
|
}
|
|
SDL_Delay(0); // We must not kill resources.
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int stream_loadBuffer(ALuint buffer) {
|
|
int size, section, result;
|
|
char data[BUFFER_SIZE]; // Buffer to hold the data.
|
|
|
|
size = 0;
|
|
while(size < BUFFER_SIZE) {
|
|
// Fill up the entire data buffer.
|
|
result = ov_read(
|
|
&music_vorbis.stream, // Stream.
|
|
data + size, // Data.
|
|
BUFFER_SIZE - size, // Amount to read.
|
|
0, // Big endian?.
|
|
2, // 16 bit.
|
|
1, // Signed.
|
|
§ion); // Current bitstream.
|
|
|
|
if(result == 0) return 1;
|
|
|
|
size += result;
|
|
if(size == BUFFER_SIZE) break; // Buffer is full.
|
|
}
|
|
alBufferData(buffer, AL_FORMAT_STEREO16, data, size, music_vorbis.info->rate);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Init/Exit.
|
|
int music_init(void) {
|
|
music_vorbis_lock = SDL_CreateMutex();
|
|
music_find();
|
|
music_vorbis.file.fd = 0; // Indication that it's not loaded..
|
|
|
|
SDL_mutexP(sound_lock);
|
|
|
|
alGenBuffers(2, music_buffer);
|
|
alGenSources(1, &music_source);
|
|
alSourcef(music_source, AL_ROLLOFF_FACTOR, 0.);
|
|
alSourcei(music_source, AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
SDL_mutexV(sound_lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void music_exit(void) {
|
|
int i;
|
|
|
|
// Free the music.
|
|
alDeleteBuffers(2, music_buffer);
|
|
alDeleteSources(1, &music_source);
|
|
music_free();
|
|
|
|
// Free selection
|
|
for(i = 0; i < nmusic_selection; i++)
|
|
free(music_selection[i]);
|
|
free(music_selection);
|
|
|
|
SDL_DestroyMutex(music_vorbis_lock);
|
|
}
|
|
|
|
// Internal music loading ruitines.
|
|
static int music_loadOGG(const char* filename) {
|
|
// Free currently loaded ogg.
|
|
music_free();
|
|
|
|
SDL_mutexP(music_vorbis_lock);
|
|
|
|
// Load the new ogg.
|
|
pack_open(&music_vorbis.file, DATA, filename);
|
|
ov_open_callbacks(&music_vorbis.file, &music_vorbis.stream, NULL, 0, ovcall);
|
|
music_vorbis.info = ov_info(&music_vorbis.stream, -1);
|
|
|
|
if(music_vorbis.info->channels == 1) music_vorbis.format = AL_FORMAT_MONO16;
|
|
else music_vorbis.format = AL_FORMAT_STEREO16;
|
|
|
|
SDL_mutexV(music_vorbis_lock);
|
|
return 0;
|
|
}
|
|
|
|
static int music_find(void) {
|
|
char** files;
|
|
uint32_t nfiles, i;
|
|
char tmp[64];
|
|
int len;
|
|
|
|
// Get the file list.
|
|
files = pack_listfiles(data, &nfiles);
|
|
|
|
// Load the profiles.
|
|
for(i = 0; i < nfiles; i++)
|
|
if((strncmp(files[i], MUSIC_PREFIX, strlen(MUSIC_PREFIX))==0) &&
|
|
(strncmp(files[i] + strlen(files[i]) - strlen(MUSIC_SUFFIX),
|
|
MUSIC_SUFFIX, strlen(MUSIC_SUFFIX))==0)) {
|
|
|
|
// Grow the selection size.
|
|
music_selection = realloc(music_selection, ++nmusic_selection*sizeof(char*));
|
|
|
|
// Remove the prefix and suffix.
|
|
len = strlen(files[i]) - strlen(MUSIC_SUFFIX MUSIC_PREFIX);
|
|
strncpy(tmp, files[i] + strlen(MUSIC_PREFIX), len);
|
|
tmp[len] = '\0';
|
|
|
|
music_selection[nmusic_selection-1] = strdup(tmp);
|
|
}
|
|
// Free the char* allocated by pack.
|
|
for(i = 0; i < nfiles; i++)
|
|
free(files[i]);
|
|
free(files);
|
|
|
|
DEBUG("Loaded %d song%c", nmusic_selection, (nmusic_selection==1)?' ':'s');
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void music_free(void) {
|
|
SDL_mutexP(music_vorbis_lock);
|
|
|
|
if(music_vorbis.file.fd != 0) {
|
|
ov_clear(&music_vorbis.stream);
|
|
pack_close(&music_vorbis.file);
|
|
}
|
|
|
|
SDL_mutexV(music_vorbis_lock);
|
|
}
|
|
|
|
void music_volume(const double vol) {
|
|
SDL_mutexP(sound_lock);
|
|
|
|
alSourcef(music_source, AL_GAIN, (ALfloat)vol);
|
|
|
|
SDL_mutexV(sound_lock);
|
|
}
|
|
|
|
// Music control functions.
|
|
void music_load(const char* name) {
|
|
int i;
|
|
char tmp[64];
|
|
|
|
music_stop();
|
|
|
|
for(i = 0; i < nmusic_selection; i++)
|
|
if(strcmp(music_selection[i], name)==0) {
|
|
snprintf(tmp, 64, MUSIC_PREFIX"%s"MUSIC_SUFFIX, name);
|
|
music_loadOGG(tmp);
|
|
return;
|
|
}
|
|
WARN("Requested load song '%s' but it can't be found in the music stack", name);
|
|
}
|
|
|
|
void music_play(void) {
|
|
music_set(MUSIC_PLAYING);
|
|
}
|
|
|
|
void music_stop(void) {
|
|
music_rm(MUSIC_PLAYING);
|
|
}
|
|
|
|
void music_kill(void) {
|
|
music_set(MUSIC_KILL);
|
|
}
|
|
|