00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdlib.h>
00028 #include <SDL.h>
00029 #include <SDL_mixer.h>
00030
00031 void handleKey(SDL_KeyboardEvent key);
00032
00033
00034 Mix_Music *music;
00035 Mix_Chunk *sample1, *sample2, *sample3;
00036
00037 #ifdef main
00038 #undef main
00039 #endif
00040
00041 int main(int argc, char **argv)
00042 {
00043 SDL_Surface *screen;
00044 SDL_Event event;
00045 Uint8 *keys;
00046 int quit = 0;
00047
00048 int frequency = 44100;
00049 Uint16 format = MIX_DEFAULT_FORMAT;
00050 int channels = MIX_DEFAULT_CHANNELS;
00051 int buffers = 2048;
00052
00053 Uint8 left = 127;
00054 int volume = MIX_MAX_VOLUME;
00055
00056 printf ("Hello DMF!\n");
00057
00058 char *music_file, *sample1_file, *sample2_file, *sample3_file;
00059
00060
00061 if (argc == 1) {
00062 music_file = "sample1.ogg";
00063 sample1_file = "sample1.ogg";
00064 sample2_file = "sample2.ogg";
00065 sample3_file = "sample3.ogg";
00066 }
00067 else if (argc == 5) {
00068 music_file = argv[1];
00069 sample1_file = argv[2];
00070 sample2_file = argv[3];
00071 sample3_file = argv[4];
00072 }
00073 else {
00074 printf("The correct syntax is: %s music sample1 sample2 sample3\n", argv[0]);
00075 exit(-1);
00076 }
00077
00078
00079 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
00080 printf("SDL_Init error: %s\n", SDL_GetError());
00081 exit (-1);
00082 }
00083
00084
00085 atexit(SDL_Quit);
00086
00087
00088 if ((screen = SDL_SetVideoMode(320, 240, 0, 0)) == NULL) {
00089 printf("SDL_SetVideoMode error: %s\n", SDL_GetError());
00090 exit (-1);
00091 }
00092
00093
00094 if (Mix_OpenAudio(frequency, format, channels, buffers) == -1) {
00095 printf("Mix_OpenAudio error: %s\n", Mix_GetError());
00096 exit(-1);
00097 }
00098
00099
00100 Mix_AllocateChannels(4);
00101
00102
00103 int numtimesopened;
00104 numtimesopened = Mix_QuerySpec(&frequency, &format, &channels);
00105 if (!numtimesopened)
00106 printf("Mix_QuerySpec error: %s\n", Mix_GetError());
00107 else {
00108 char *format_str="Unknown";
00109 switch (format) {
00110 case AUDIO_U8:
00111 format_str="U8";
00112 break;
00113 case AUDIO_S8:
00114 format_str="S8";
00115 break;
00116 case AUDIO_U16LSB:
00117 format_str="U16LSB";
00118 break;
00119 case AUDIO_S16LSB:
00120 format_str="S16LSB";
00121 break;
00122 case AUDIO_U16MSB:
00123 format_str="U16MSB";
00124 break;
00125 case AUDIO_S16MSB:
00126 format_str="S16MSB";
00127 break;
00128 }
00129
00130 printf("The audio devices was opened %d times\n", numtimesopened);
00131 printf("System audio specifications:\n");
00132 printf("Frequency = %d\n", frequency);
00133 printf("Format = %s\n", format_str);
00134 printf("Channels = %d\n\n", channels);
00135 }
00136
00137
00138 if ((music = Mix_LoadMUS(music_file)) == NULL)
00139 printf("Error in music loading: %s\n", Mix_GetError());
00140
00141
00142 if ((sample1 = Mix_LoadWAV(sample1_file)) == NULL)
00143 printf("Error loading sample 1: %s\n", Mix_GetError());
00144 if ((sample2 = Mix_LoadWAV(sample2_file)) == NULL)
00145 printf("Error loading sample 2: %s\n", Mix_GetError());
00146 if ((sample3 = Mix_LoadWAV(sample3_file)) == NULL)
00147 printf("Error loading sample 3: %s\n", Mix_GetError());
00148
00149
00150 Mix_SetPanning(MIX_CHANNEL_POST, 127, 127);
00151
00152
00153 while (!quit) {
00154 while (SDL_PollEvent(&event)) {
00155 switch (event.type) {
00156 case SDL_QUIT:
00157 quit = 1;
00158 break;
00159 case SDL_KEYDOWN:
00160 handleKey(event.key);
00161 break;
00162 }
00163
00164 if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q)
00165 quit = 1;
00166 }
00167
00168 keys = SDL_GetKeyState(NULL);
00169
00170
00171 if (keys[SDLK_UP]) {
00172 if (volume < MIX_MAX_VOLUME) {
00173 volume += 4;
00174 Mix_Volume(-1, volume);
00175 Mix_VolumeMusic(volume);
00176 printf("Volume = %d\n", volume);
00177 }
00178 }
00179
00180
00181 if (keys[SDLK_DOWN]) {
00182 if (volume > 0) {
00183 volume -= 4;
00184 Mix_Volume(-1, volume);
00185 Mix_VolumeMusic(volume);
00186 printf("Volume = %d\n", volume);
00187 }
00188 }
00189
00190
00191 if (keys[SDLK_LEFT]) {
00192 if (left < 250) {
00193 left += 4;
00194 Mix_SetPanning(MIX_CHANNEL_POST, left, 254-left);
00195 printf("Panning = L:%d, R:%d\n", left, 254-left);
00196 }
00197 }
00198
00199
00200 if (keys[SDLK_RIGHT]) {
00201 if (left > 4) {
00202 left -= 4;
00203 Mix_SetPanning(MIX_CHANNEL_POST, left, 254-left);
00204 printf("Panning = L:%d, R:%d\n", left, 254-left);
00205 }
00206 }
00207
00208
00209 SDL_Delay(50);
00210 }
00211
00212
00213 Mix_FreeChunk(sample3);
00214 Mix_FreeChunk(sample2);
00215 Mix_FreeChunk(sample1);
00216 Mix_FreeMusic(music);
00217 Mix_CloseAudio();
00218 SDL_Quit();
00219
00220 return 0;
00221 }
00222
00223
00224 void handleKey(SDL_KeyboardEvent key)
00225 {
00226 switch(key.keysym.sym) {
00227
00228 case SDLK_c:
00229 Mix_SetPanning(MIX_CHANNEL_POST, 127, 127);
00230 printf("Panning = Central\n");
00231 break;
00232
00233 case SDLK_f:
00234 if (Mix_PlayingMusic())
00235 Mix_FadeOutMusic(3000);
00236 else
00237 Mix_FadeInMusic(music, -1, 3000);
00238 break;
00239
00240 case SDLK_m:
00241 if (Mix_PlayingMusic())
00242 Mix_HaltMusic();
00243 else
00244 Mix_PlayMusic(music, -1);
00245 break;
00246
00247 case SDLK_p:
00248 if (Mix_PausedMusic())
00249 Mix_ResumeMusic();
00250 else
00251 Mix_PauseMusic();
00252 break;
00253
00254 case SDLK_1:
00255 case SDLK_KP1:
00256 Mix_PlayChannel(1, sample1, 0);
00257 break;
00258
00259 case SDLK_2:
00260 case SDLK_KP2:
00261 Mix_PlayChannel(2, sample2, 0);
00262 break;
00263
00264 case SDLK_3:
00265 case SDLK_KP3:
00266 Mix_PlayChannel(3, sample3, 0);
00267 break;
00268 }
00269 }