Back to index

gcompris  8.2.2
sdlplayer.c
Go to the documentation of this file.
00001 /* sdlplayer - sdlplayer.c
00002  *
00003  * Time-stamp: <2004/05/04 01:33:46 bcoudoin>
00004  *
00005  * Copyright (C) 2004 Bruno Coudoin
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 #include "SDL.h"
00022 #include "SDL_thread.h"
00023 #include "SDL_mixer.h"
00024 #include "SDL_audio.h"
00025 
00026 #include <stdarg.h>
00027 #include <unistd.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <stdio.h>
00031 
00032 #include <glib.h>
00033 
00034 Sint16 stream[2][4096];
00035 int len=4096, bits=0, which=0;
00036 
00037 // set this to any of 512,1024,2048,4096
00038 // the higher it is, the more FPS shown and CPU needed
00039 int audio_buffers=2048;
00040 
00041 static gboolean sound_closed = FALSE;
00042 
00043 /******************************************************************************/
00044 /* some simple exit and error routines                                        */
00045 
00046 void errorv(char *str, va_list ap)
00047 {
00048   vfprintf(stderr,str,ap);
00049 
00050   if(strcmp(Mix_GetError(), "Unrecognized file type (not VOC)")==0) {
00051     fprintf(stderr,"*** You need a version of \"SDL_mixer\" with OGG Vorbis supported ***\n");
00052     exit(EXIT_FAILURE);
00053   }
00054 
00055   fprintf(stderr,": %s.\n", Mix_GetError());
00056   fprintf(stderr,": %s.\n", SDL_GetError());
00057 
00058 }
00059 
00060 int cleanExit(char *str,...)
00061 {
00062   va_list ap;
00063   va_start(ap, str);
00064   errorv(str,ap);
00065   va_end(ap);
00066   Mix_CloseAudio();
00067   SDL_Quit();
00068   return(1);
00069 }
00070 
00071 /******************************************************************************/
00072 
00073 int sdlplayer_init()
00074 {
00075   int audio_rate,audio_channels;
00076   Uint16 audio_format;
00077 
00078   // initialize SDL for audio
00079   if(SDL_Init(SDL_INIT_AUDIO)<0)
00080     return(cleanExit("SDL_Init"));
00081 
00082   // initialize sdl mixer, open up the audio device
00083   if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,audio_buffers)<0)
00084     return(cleanExit("Mix_OpenAudio"));
00085 
00086   // print out some info on the audio device and stream
00087   Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
00088   bits=audio_format&0xFF;
00089   g_warning("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
00090            bits, audio_channels>1?"stereo":"mono", audio_buffers );
00091 
00092   return(0);
00093 }
00094 
00095 int sdlplayer_quit(Mix_Music *music)
00096 {
00097   // free & close
00098   Mix_FreeMusic(music);
00099   Mix_CloseAudio();
00100   g_warning("SDL PLAYER SDL_Quit\n");
00101   SDL_Quit();
00102 
00103   return 0;
00104 }
00105 
00106 int sdlplayer_music(char *filename, int volume)
00107 {
00108   Mix_Music *music;
00109 
00110   g_warning("sdlplayer_bg %s\n", filename);
00111 
00112   // load the song
00113   if(!(music=Mix_LoadMUS(filename)))
00114     return(cleanExit("Mix_LoadMUS(\"%s\")",filename));
00115 
00116   if(Mix_PlayMusic(music, 1)==-1) {
00117     return(cleanExit("Mix_LoadMUS(0x%p,1)",music));
00118   }
00119 
00120   Mix_VolumeMusic(volume);
00121 
00122   // wait for the music to complete
00123   while(Mix_PlayingMusic() || Mix_PausedMusic())
00124     {
00125       SDL_Delay(50);
00126     }
00127 
00128   return(0);
00129 }
00130 
00131 int sdlplayer_fx(char *filename, int volume)
00132 {
00133   Mix_Chunk *sample;
00134   static int channel;
00135 
00136   g_warning("sdlplayer %s\n", filename);
00137 
00138   sample=Mix_LoadWAV_RW(SDL_RWFromFile(filename, "rb"), 1);
00139   if(!sample) {
00140     return(cleanExit("Mix_LoadWAV_RW"));
00141     // handle error
00142   }
00143 
00144   Mix_VolumeChunk(sample, MIX_MAX_VOLUME);
00145 
00146   if((channel=Mix_PlayChannel(-1, sample, 0))==-1) {
00147     return(cleanExit("Mix_LoadChannel(0x%p,1)",channel));
00148   }
00149 
00150   while(Mix_Playing(channel))
00151     {
00152       SDL_Delay(50);
00153     }
00154 
00155   // free the sample
00156   // Mix_Chunk *sample;
00157   Mix_FreeChunk(sample);
00158 
00159   g_warning("sdlplayer complete playing of %s\n", filename);
00160 
00161   return(0);
00162 }
00163 
00164 void sdlplayer_pause_music()
00165 {
00166   if(!sound_closed && Mix_PlayingMusic())
00167     Mix_PauseMusic();
00168 }
00169 
00170 void sdlplayer_halt_music()
00171 {
00172   if(!sound_closed)
00173     Mix_HaltMusic();
00174 }
00175 
00176 void sdlplayer_halt_fx()
00177 {
00178   if(!sound_closed)
00179     Mix_HaltChannel(-1);
00180 }
00181 
00182 void sdlplayer_resume_music()
00183 {
00184   if(!sound_closed)
00185     Mix_ResumeMusic();
00186 }
00187 
00188 void sdlplayer_resume_fx()
00189 {
00190   if(!sound_closed)
00191     Mix_Resume(-1);
00192 }
00193 
00194 void sdlplayer_close()
00195 {
00196   sound_closed = TRUE;
00197   Mix_HaltMusic();
00198   Mix_HaltChannel(-1);
00199   Mix_CloseAudio();
00200 }
00201 void sdlplayer_reopen()
00202 {
00203   Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,audio_buffers);
00204   sound_closed = FALSE;
00205   Mix_ResumeMusic();
00206   Mix_Resume(-1);
00207 }