From b165e7b2635ababde0dce83d9e0d189eca795220 Mon Sep 17 00:00:00 2001 From: Allanis <allanis@saracraft.net> Date: Sat, 23 Feb 2013 19:55:30 +0000 Subject: [PATCH] [Add] Set volume with command line, volume is a little flaky at the min. :s. --- src/conf.c | 58 ++++++++++++++++++++++++++++++++++++++++------------- src/land.c | 1 - src/music.c | 19 ++++++++++++++---- src/sound.c | 21 +++++++++++++++++-- src/sound.h | 1 + 5 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/conf.c b/src/conf.c index 95b64c3..7323a50 100644 --- a/src/conf.c +++ b/src/conf.c @@ -11,6 +11,7 @@ #include "player.h" #include "opengl.h" #include "input.h" +#include "music.h" #include "conf.h" #define conf_loadInt(n,i) \ @@ -20,6 +21,13 @@ if(lua_isnumber(L, -1)) { \ lua_remove(L, -1); \ } +#define conf_loadFloat(n,f) \ +lua_getglobal(L,n); \ +if(lua_isnumber(L, -1)) { \ + f = (double)lua_tonumber(L, -1); \ + lua_remove(L,-1);\ +} + #define conf_loadBool(n,b) \ lua_getglobal(L,n); \ if(lua_isnumber(L, -1)) \ @@ -54,6 +62,8 @@ static void print_usage(char** argv) { LOG("\t-d s, --data s - Set the data file to be s"); LOG("\t-j n, --joystick n - Use joystick (n)"); LOG("\t-J s, --joystick s - Use joystick whose name contains (s)"); + LOG("\t-m f, --music f - Set the music volume to f"); + LOG("\t-s f, --sound f - Set the sound volume to f"); LOG("\t-h --help - Display this message and exit."); LOG("\t-v - Print the version and exit"); } @@ -76,6 +86,7 @@ void conf_setDefaults(void) { // Ok.. Parse a config file plox. int conf_loadConfig(const char* file) { int i = 0; + double d = 0.; lua_State* L = luaL_newstate(); if(luaL_dofile(L, file) == 0) { @@ -87,18 +98,29 @@ int conf_loadConfig(const char* file) { conf_loadInt("width", gl_screen.w); conf_loadInt("height", gl_screen.h); conf_loadBool("fullscreen", i); - if(i)gl_screen.flags |= OPENGL_FULLSCREEN; + if(i) { gl_screen.flags |= OPENGL_FULLSCREEN; i = 0; } conf_loadBool("aa", i); - if(i)gl_screen.flags |= OPENGL_AA_POINT | OPENGL_AA_LINE || OPENGL_AA_POLYGON; + if(i) { + gl_screen.flags |= OPENGL_AA_POINT | OPENGL_AA_LINE || OPENGL_AA_POLYGON; + i = 0; + } conf_loadBool("aa_point", i); - if(i)gl_screen.flags |= OPENGL_AA_POINT; + if(i) { gl_screen.flags |= OPENGL_AA_POINT; i = 0; } conf_loadBool("aa_line", i) - if(i)gl_screen.flags |= OPENGL_AA_LINE; + if(i) { gl_screen.flags |= OPENGL_AA_LINE; i = 0; } conf_loadBool("aa_polygon", i); - if(i)gl_screen.flags |= OPENGL_AA_POLYGON; - conf_loadBool("showfps", show_fps); + if(i) { gl_screen.flags |= OPENGL_AA_POLYGON; i = 0; } + + // FPS. + conf_loadBool("showfps", show_fps); conf_loadInt("maxfps", max_fps); + // Sound. + conf_loadFloat("sound", d); + if(d) { sound_volume(d); d = 0.; } + conf_loadFloat("music", d); + if(d) { music_volume(d); d = 0.; } + // Joystick. lua_getglobal(L, "joystick"); if(lua_isnumber(L, -1)) { @@ -172,19 +194,21 @@ int conf_loadConfig(const char* file) { // Parse some CLI options. void conf_parseCLI(int argc, char** argv) { static struct option long_options[] = { - { "fullscreen", no_argument, 0, 'f' }, - { "fps", required_argument, 0, 'F' }, - { "data", required_argument, 0, 'd' }, - { "joystick", required_argument, 0, 'j' }, - { "Joystick", required_argument, 0, 'J' }, - { "help", no_argument, 0, 'h' }, - { "version", no_argument, 0, 'v' }, + { "fullscreen", no_argument, 0, 'f' }, + { "fps", required_argument, 0, 'F' }, + { "data", required_argument, 0, 'd' }, + { "joystick", required_argument, 0, 'j' }, + { "Joystick", required_argument, 0, 'J' }, + { "music", required_argument, 0, 'm' }, + { "sound", required_argument, 0, 's' }, + { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'v' }, { NULL, 0, 0, 0 } }; int option_index = 0; int c = 0; - while((c = getopt_long(argc, argv, "fF:d:J:j:hv", long_options, &option_index)) != -1) { + while((c = getopt_long(argc, argv, "fF:d:J:j:sm:V:hv", long_options, &option_index)) != -1) { switch(c) { case 'f': gl_screen.flags |= OPENGL_FULLSCREEN; @@ -201,6 +225,12 @@ void conf_parseCLI(int argc, char** argv) { case 'J': namjoystick = strdup(optarg); break; + case 'm': + music_volume(atof(optarg)); + break; + case 's': + sound_volume(atof(optarg)); + break; case 'v': LOG(APPNAME": version %d.%d.%d", VMAJOR, VMINOR, VREV); case 'h': diff --git a/src/land.c b/src/land.c index f7a2026..94a66f3 100644 --- a/src/land.c +++ b/src/land.c @@ -104,7 +104,6 @@ void land(Planet* p) { // Change music. music_load(MUSIC_LAND); - music_play(); planet = p; land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); diff --git a/src/music.c b/src/music.c index afc9e79..572eb92 100644 --- a/src/music.c +++ b/src/music.c @@ -42,6 +42,9 @@ static ALuint music_source = 0; static char** music_selection = NULL; static int nmusic_selection = 0; +// Volume +static ALfloat mvolume = 1.; + // 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); @@ -161,6 +164,7 @@ int music_init(void) { alGenBuffers(2, music_buffer); alGenSources(1, &music_source); + alSourcef(music_source, AL_GAIN, mvolume); alSourcef(music_source, AL_ROLLOFF_FACTOR, 0.); alSourcei(music_source, AL_SOURCE_RELATIVE, AL_TRUE); @@ -251,11 +255,18 @@ static void music_free(void) { } void music_volume(const double vol) { - SDL_mutexP(sound_lock); - - alSourcef(music_source, AL_GAIN, (ALfloat)vol); + // Sanity check! + ALfloat fvol = ABS(vol); + if(fvol > 1.) fvol = 1.; - SDL_mutexV(sound_lock); + mvolume = fvol; + + // Only needed if playing! + if(music_set(MUSIC_PLAYING)) { + SDL_mutexP(sound_lock); + alSourcef(music_source, AL_GAIN, fvol); + SDL_mutexV(sound_lock); + } } // Music control functions. diff --git a/src/sound.c b/src/sound.c index 6cdc6b1..357b0ac 100644 --- a/src/sound.c +++ b/src/sound.c @@ -51,6 +51,9 @@ static alVoice** voice_stack = NULL; static int nvoice_stack = 0; static int mvoice_stack = 0; +// Volume. +static ALfloat svolume = 0.5; + static int sound_makeList(void); static int sound_load(ALuint* buffer, char* filename); static void sound_free(alSound* snd); @@ -86,7 +89,7 @@ int sound_init(void) { } // Set the master gain. - alListenerf(AL_GAIN, .5); + alListenerf(AL_GAIN, .1); // Set the distance model. alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); @@ -240,6 +243,20 @@ void sound_update(void) { SDL_mutexV(sound_lock); } +// Set all the sounds volume to vol. +void sound_volume(const double vol) { + int i; + + svolume = (ALfloat) vol; + + SDL_mutexP(sound_lock); + for(i = 0; i < nvoice_stack; i++) + if(voice_set(voice_stack[i], VOICE_PLAYING)) + alSourcef(voice_stack[i]->source, AL_GAIN, svolume); + SDL_mutexV(sound_lock); +} + +// Attempt to alloc a source for a voice. static int voice_getSource(alVoice* voc) { int ret; ALenum err; @@ -265,7 +282,7 @@ static int voice_getSource(alVoice* voc) { alSourcef(voc->source, AL_REFERENCE_DISTANCE, 50.); alSourcei(voc->source, AL_SOURCE_RELATIVE, AL_FALSE); - alSourcef(voc->source, AL_GAIN, 0.5); + alSourcef(voc->source, AL_GAIN, svolume); alSource3f(voc->source, AL_POSITION, voc->px, voc->py, 0.); //alSource3f(voc->source, AL_VELOCITY, voc->vx, voc->vy, 0.); if(voice_is(voc, VOICE_LOOPING)) diff --git a/src/sound.h b/src/sound.h index aac79c1..a83a73e 100644 --- a/src/sound.h +++ b/src/sound.h @@ -26,6 +26,7 @@ void sound_update(void); // Sound manupulation functions. ALuint sound_get(char* name); +void sound_volume(const double vol); // Voice manipulation function. alVoice* sound_addVoice(int priority, double px, double py, double vx, double vy,