Commit 5b396888 authored by Bruno Coudoin's avatar Bruno Coudoin

- Large review of the way we handle sounds. Fixed the API names to better match what

	  we are doing.
	- Now the music on/off in the configure dialog acts in real time.
	- Now if music and fx are disabled we close the audio output.
	- Now we reopen the audio output properly if music or fx is selected again.
parent c3a6f5ab
2006-11-05 Bruno coudoin <bruno.coudoin@free.fr>
- Large review of the way we handle sounds. Fixed the API names to better match what
we are doing.
- Now the music on/off in the configure dialog acts in real time.
- Now if music and fx are disabled we close the audio output.
- Now we reopen the audio output properly if music or fx is selected again.
* src/gcompris/config.c: (item_event_ok):
* src/gcompris/sdlplayer.c: (sdlplayer_init), (sdlplayer_music),
(sdlplayer_fx), (sdlplayer_pause_music), (sdlplayer_halt_music),
(sdlplayer_halt_fx), (sdlplayer_resume_music),
(sdlplayer_resume_fx), (sdlplayer_close), (sdlplayer_reopen):
* src/gcompris/soundutil.c: (gc_sound_init), (gc_sound_close),
(gc_sound_reopen), (gc_sound_pause), (gc_sound_resume),
(scheduler_music), (scheduler_fx), (thread_play_ogg),
(gc_sound_play_ogg_list):
* src/gcompris/soundutil.h:
2006-11-04 Bruno coudoin <bruno.coudoin@free.fr>
* INSTALL:
......
......@@ -741,7 +741,18 @@ item_event_ok(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
if(!properties->music && !properties->fx)
gc_sound_close();
else
{
if(!properties->music)
sdlplayer_halt_music();
else
sdlplayer_resume_music();
if(!properties->fx)
sdlplayer_halt_fx();
else
sdlplayer_resume_fx();
}
gc_prop_save(properties);
}
else if(!strcmp((char *)data, "fullscreen"))
......@@ -770,7 +781,15 @@ item_event_ok(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
gnome_canvas_item_set (item,
"pixbuf", (properties->music ? pixmap_checked : pixmap_unchecked),
NULL);
if(!properties->music)
{
sdlplayer_halt_music();
}
else
{
gc_sound_init();
sdlplayer_resume_music();
}
}
else if(!strcmp((char *)data, "effect"))
{
......
......@@ -39,7 +39,6 @@ int len=4096, bits=0, which=0;
int audio_buffers=2048;
static gboolean sound_closed = FALSE;
static gboolean sound_paused = FALSE;
/******************************************************************************/
/* some simple exit and error routines */
......@@ -89,7 +88,6 @@ int sdlplayer_init()
bits=audio_format&0xFF;
g_warning("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
bits, audio_channels>1?"stereo":"mono", audio_buffers );
sound_paused = FALSE;
return(0);
}
......@@ -105,13 +103,10 @@ int sdlplayer_quit(Mix_Music *music)
return 0;
}
int sdlplayer_bg(char *filename, int volume)
int sdlplayer_music(char *filename, int volume)
{
Mix_Music *music;
if(sound_paused)
return(0);
g_warning("sdlplayer_bg %s\n", filename);
// load the song
......@@ -125,7 +120,7 @@ int sdlplayer_bg(char *filename, int volume)
Mix_VolumeMusic(volume);
// wait for the music to complete
while((Mix_PlayingMusic() || Mix_PausedMusic()))
while(Mix_PlayingMusic() || Mix_PausedMusic())
{
SDL_Delay(50);
}
......@@ -133,18 +128,13 @@ int sdlplayer_bg(char *filename, int volume)
return(0);
}
int sdlplayer(char *filename, int volume)
int sdlplayer_fx(char *filename, int volume)
{
Mix_Chunk *sample;
static int channel;
g_warning("sdlplayer %s\n", filename);
if(sound_paused)
return(0);
Mix_PauseMusic();
sample=Mix_LoadWAV_RW(SDL_RWFromFile(filename, "rb"), 1);
if(!sample) {
return(cleanExit("Mix_LoadWAV_RW"));
......@@ -162,16 +152,6 @@ int sdlplayer(char *filename, int volume)
SDL_Delay(50);
}
// fade in music. Removed, eats too much CPU on low end PCs
/* for(i=32; i<=128; i+=10) { */
/* Mix_VolumeMusic(i); */
/* SDL_Delay(20); */
/* } */
// resume music playback
if ((!sound_closed) && (!sound_paused))
Mix_ResumeMusic();
// free the sample
// Mix_Chunk *sample;
Mix_FreeChunk(sample);
......@@ -181,20 +161,39 @@ int sdlplayer(char *filename, int volume)
return(0);
}
void sdlplayer_halt()
void sdlplayer_pause_music()
{
sound_paused = TRUE;
sound_closed = TRUE;
if(!sound_closed && Mix_PlayingMusic())
Mix_PauseMusic();
}
Mix_HaltMusic();
Mix_HaltChannel(-1);
void sdlplayer_halt_music()
{
if(!sound_closed)
Mix_HaltMusic();
}
void sdlplayer_halt_fx()
{
if(!sound_closed)
Mix_HaltChannel(-1);
}
void sdlplayer_resume_music()
{
if(!sound_closed)
Mix_ResumeMusic();
}
void sdlplayer_resume_fx()
{
if(!sound_closed)
Mix_Resume(-1);
}
void sdlplayer_close()
{
sound_paused = TRUE;
sound_closed = TRUE;
Mix_HaltMusic();
Mix_HaltChannel(-1);
Mix_CloseAudio();
......@@ -202,23 +201,7 @@ void sdlplayer_close()
void sdlplayer_reopen()
{
Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,audio_buffers);
sound_closed = FALSE;
Mix_ResumeMusic();
Mix_Resume(-1);
sound_closed = FALSE;
sound_paused = FALSE;
}
void sdlplayer_pause(){
Mix_PauseMusic();
Mix_Pause(-1);
sound_paused = TRUE;
}
void sdlplayer_resume(){
// resume music playback
if (!sound_closed){
Mix_ResumeMusic();
Mix_Resume(-1);
sound_paused = FALSE;
}
}
......@@ -28,7 +28,7 @@
static GList *pending_queue = NULL;
static int sound_policy;
static gboolean is_playing;
static gboolean music_paused = FALSE;
static gboolean sound_closed = FALSE;
/* mutex */
......@@ -41,13 +41,13 @@ GCond *cond = NULL;
static guint sound_init = 0;
/* Forward function declarations */
GThread *thread_scheduler, *thread_scheduler_bgnd;
GThread *thread_scheduler_fx, *thread_scheduler_music;
static void *thread_play_ogg (gchar *file);
static char *get_next_sound_to_play( );
static gpointer scheduler (gpointer user_data);
static gpointer scheduler_bgnd (gpointer user_data);
static gpointer scheduler_fx (gpointer user_data);
static gpointer scheduler_music (gpointer user_data);
/* sound control */
GObject *gc_sound_controller = NULL;
......@@ -89,7 +89,6 @@ gc_sound_init()
cond = g_cond_new ();
sound_policy = PLAY_AFTER_CURRENT;
is_playing = FALSE;
if(sdlplayer_init()!=0) {
/* Sound init failed. Desactivate the sound */
......@@ -98,13 +97,13 @@ gc_sound_init()
return;
}
thread_scheduler = g_thread_create((GThreadFunc)scheduler, NULL, FALSE, NULL);
if (thread_scheduler == NULL)
perror("create failed for scheduler");
thread_scheduler_fx = g_thread_create((GThreadFunc)scheduler_fx, NULL, FALSE, NULL);
if (thread_scheduler_fx == NULL)
perror("create failed for fx scheduler");
thread_scheduler_bgnd = g_thread_create((GThreadFunc)scheduler_bgnd, NULL, FALSE, NULL);
if (thread_scheduler_bgnd == NULL)
perror("create failed for scheduler background");
thread_scheduler_music = g_thread_create((GThreadFunc)scheduler_music, NULL, FALSE, NULL);
if (thread_scheduler_music == NULL)
perror("create failed for music scheduler");
}
......@@ -113,36 +112,41 @@ gc_sound_close()
{
if ( !sound_closed )
{
sdlplayer_halt();
sdlplayer_halt_music();
sdlplayer_halt_fx();
g_mutex_lock(lock_fx);
g_mutex_lock(lock_music);
sdlplayer_close();
sound_closed = TRUE;
music_paused = FALSE;
}
printf("gc_sound_close done\n");
}
void
gc_sound_reopen()
{
if (sound_closed)
{
sdlplayer_reopen();
g_mutex_unlock(lock);
g_mutex_unlock(lock_fx);
g_mutex_unlock(lock_music);
sound_closed = FALSE;
music_paused = FALSE;
}
}
void
gc_sound_pause()
{
sdlplayer_pause();
sdlplayer_pause_music();
music_paused = TRUE;
}
void
gc_sound_resume()
{
sdlplayer_resume();
sdlplayer_resume_music();
music_paused = FALSE;
}
/* =====================================================================
......@@ -173,7 +177,7 @@ gc_sound_policy_get()
* in the gcompris music directory
======================================================================*/
static gpointer
scheduler_bgnd (gpointer user_data)
scheduler_music (gpointer user_data)
{
GcomprisProperties *properties = gc_prop_get();
gint i;
......@@ -221,18 +225,15 @@ scheduler_bgnd (gpointer user_data)
for(i=0; i<g_slist_length(musiclist); i++)
{
/* Music can be disabled at any time */
if ( !gc_prop_get()->music )
{
g_usleep(5000000);
continue;
}
while(!gc_prop_get()->music || music_paused)
g_usleep(1000000);
/* WARNING Displaying stuff in a thread seems to make gcompris unstable */
/* display_ogg_file_credits((char *)g_list_nth_data(musiclist, i)); */
// if(decode_ogg_file((char *)g_list_nth_data(musiclist, i))!=0)
g_mutex_lock(lock_music);
if(sdlplayer_bg((char *)g_slist_nth_data(musiclist, i), 128)!=0){
g_warning("Stopping music, sdlplayer_bg failed, try again in 5 seconds");
if(sdlplayer_music((char *)g_slist_nth_data(musiclist, i), 128)!=0){
g_warning("sdlplayer_music failed, try again in 5 seconds");
g_usleep(5000000);
}
g_mutex_unlock(lock_music);
......@@ -256,7 +257,7 @@ scheduler_bgnd (gpointer user_data)
* - the thread never ends
======================================================================*/
static gpointer
scheduler (gpointer user_data)
scheduler_fx (gpointer user_data)
{
char *sound = NULL;
......@@ -266,7 +267,6 @@ scheduler (gpointer user_data)
{
thread_play_ogg(sound);
g_free(sound);
is_playing = FALSE;
}
else
{
......@@ -294,13 +294,13 @@ thread_play_ogg (gchar *file)
g_warning(" Calling gcompris internal sdlplayer_file (%s)", absolute_file);
g_mutex_lock(lock_fx);
sdlplayer(absolute_file, 128);
sdlplayer_fx(absolute_file, 128);
g_mutex_unlock(lock_fx);
g_signal_emit (gc_sound_controller,
GCOMPRIS_SOUND_GET_CLASS (gc_sound_controller)->sound_played_signal_id,
0 /* details */,
g_strdup(file));
g_warning(" sdlplayer_file(%s) ended.", absolute_file);
g_warning(" sdlplayer_fx(%s) ended.", absolute_file);
g_free(absolute_file);
......@@ -402,7 +402,7 @@ gc_sound_play_ogg_list( GList* files )
return;
if ( sound_policy == PLAY_ONLY_IF_IDLE &&
( is_playing == TRUE || g_list_length( pending_queue ) > 0 ) )
g_list_length( pending_queue ) > 0 )
return;
g_mutex_lock (lock);
......
......@@ -53,13 +53,17 @@ void display_ao_devices();
* SDL PLAYER
*/
int sdlplayer_init();
int sdlplayer_bg();
void sdlplayer_halt();
void sdlplayer_close();
void sdlplayer_reopen();
void sdlplayer_pause();
void sdlplayer_resume();
int sdlplayer(char *filename, int volume);
int sdlplayer_music(char *filename, int volume);
void sdlplayer_halt_music();
void sdlplayer_pause_music();
void sdlplayer_resume_music();
void sdlplayer_halt_fx();
void sdlplayer_resume_fx();
int sdlplayer_fx(char *filename, int volume);
gchar *gc_sound_alphabet(gchar *chars);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment