[2777] | 1 | #include "SDL.h" |
---|
| 2 | #include "SDL_thread.h" |
---|
| 3 | #include "SDL_mixer.h" |
---|
| 4 | |
---|
| 5 | #include <stdarg.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <string.h> |
---|
| 8 | #include <stdio.h> |
---|
| 9 | |
---|
| 10 | Sint16 stream[2][4096]; |
---|
| 11 | int len=4096, done=0, need_refresh=0, bits=0, which=0; |
---|
| 12 | SDL_Surface *s=NULL; |
---|
| 13 | Uint32 flips=0; |
---|
| 14 | Uint32 black,white; |
---|
| 15 | float dy,dx; |
---|
| 16 | |
---|
| 17 | /******************************************************************************/ |
---|
| 18 | /* some simple exit and error routines */ |
---|
| 19 | |
---|
| 20 | void errorv(char *str, va_list ap) |
---|
| 21 | { |
---|
| 22 | vfprintf(stderr,str,ap); |
---|
| 23 | fprintf(stderr,": %s.\n", SDL_GetError()); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | void cleanExit(char *str,...) |
---|
| 27 | { |
---|
| 28 | va_list ap; |
---|
| 29 | va_start(ap, str); |
---|
| 30 | errorv(str,ap); |
---|
| 31 | va_end(ap); |
---|
| 32 | Mix_CloseAudio(); |
---|
| 33 | SDL_Quit(); |
---|
| 34 | exit(1); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /******************************************************************************/ |
---|
| 38 | /* the postmix processor, only copies the stream buffer and indicates */ |
---|
| 39 | /* a need for a screen refresh */ |
---|
| 40 | |
---|
| 41 | static void postmix(void *udata, Uint8 *_stream, int _len) |
---|
| 42 | { |
---|
| 43 | // save the stream buffer and indicate that we need a redraw |
---|
| 44 | len=_len; |
---|
| 45 | memcpy(stream[(which+1)%2],_stream,len>s->w*4?s->w*4:len); |
---|
| 46 | which=(which+1)%2; |
---|
| 47 | need_refresh=1; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /******************************************************************************/ |
---|
| 51 | /* redraw the wav and reset the need_refresh indicator */ |
---|
| 52 | |
---|
| 53 | void refresh() |
---|
| 54 | { |
---|
| 55 | int x,y,Y; |
---|
| 56 | Sint16 *buf; |
---|
| 57 | |
---|
| 58 | buf=stream[which]; |
---|
| 59 | need_refresh=0; |
---|
| 60 | |
---|
| 61 | // clear the screen |
---|
| 62 | SDL_FillRect(s,NULL,black); |
---|
| 63 | |
---|
| 64 | // draw the wav from the saved stream buffer |
---|
| 65 | Y=s->h/4; |
---|
| 66 | for(x=0;x<s->w*2;x++) |
---|
| 67 | { |
---|
| 68 | y=(buf[x]*dy); |
---|
| 69 | { |
---|
| 70 | if(y<0) |
---|
| 71 | { |
---|
| 72 | SDL_Rect r={x/2,Y+y,1,-y}; |
---|
| 73 | SDL_FillRect(s,&r,white); |
---|
| 74 | } |
---|
| 75 | else |
---|
| 76 | { |
---|
| 77 | SDL_Rect r={x/2,Y,1,y}; |
---|
| 78 | SDL_FillRect(s,&r,white); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | Y=Y>s->h/2?s->h/4:s->h*3/4; |
---|
| 82 | } |
---|
| 83 | SDL_Flip(s); |
---|
| 84 | flips++; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /******************************************************************************/ |
---|
| 88 | |
---|
| 89 | int main(int argc, char **argv) |
---|
| 90 | { |
---|
| 91 | int audio_rate,audio_channels, |
---|
| 92 | // set this to any of 512,1024,2048,4096 |
---|
| 93 | // the higher it is, the more FPS shown and CPU needed |
---|
| 94 | audio_buffers=512; |
---|
| 95 | Uint16 audio_format; |
---|
| 96 | Uint32 t; |
---|
| 97 | Mix_Music *music; |
---|
| 98 | int volume=SDL_MIX_MAXVOLUME; |
---|
| 99 | |
---|
| 100 | // initialize SDL for audio and video |
---|
| 101 | if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0) |
---|
| 102 | cleanExit("SDL_Init"); |
---|
| 103 | |
---|
| 104 | // open a screen for the wav output |
---|
| 105 | //if(!(s=SDL_SetVideoMode(1024,768,0,SDL_FULLSCREEN|SDL_ANYFORMAT|SDL_HWSURFACE|SDL_DOUBLEBUF))) |
---|
| 106 | if(!(s=SDL_SetVideoMode(512,512,0,SDL_ANYFORMAT|SDL_DOUBLEBUF))) |
---|
| 107 | cleanExit("SDL_SetVideoMode"); |
---|
| 108 | SDL_WM_SetCaption("sdlwav - SDL_mixer demo","sdlwav"); |
---|
| 109 | |
---|
| 110 | // hide the annoying mouse pointer |
---|
| 111 | SDL_ShowCursor(SDL_DISABLE); |
---|
| 112 | // get the colors we use |
---|
| 113 | white=SDL_MapRGB(s->format,0xff,0xff,0xff); |
---|
| 114 | black=SDL_MapRGB(s->format,0,0,0); |
---|
| 115 | |
---|
| 116 | // initialize sdl mixer, open up the audio device |
---|
| 117 | if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,audio_buffers)<0) |
---|
| 118 | cleanExit("Mix_OpenAudio"); |
---|
| 119 | |
---|
| 120 | // print out some info on the audio device and stream |
---|
| 121 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
| 122 | bits=audio_format&0xFF; |
---|
| 123 | printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, |
---|
| 124 | bits, audio_channels>1?"stereo":"mono", audio_buffers ); |
---|
| 125 | |
---|
| 126 | // calculate some parameters for the wav display |
---|
| 127 | dy=s->h/2.0/(float)(0x1<<bits); |
---|
| 128 | dx=s->w/(float)(0x1<<bits); |
---|
| 129 | |
---|
| 130 | // load the song |
---|
| 131 | if(!(music=Mix_LoadMUS(argv[1]))) |
---|
| 132 | cleanExit("Mix_LoadMUS(\"%s\")",argv[1]); |
---|
| 133 | |
---|
| 134 | // set the post mix processor up |
---|
| 135 | Mix_SetPostMix(postmix,argv[1]); |
---|
| 136 | |
---|
| 137 | // start playing and displaying the wav |
---|
| 138 | // wait for escape key of the quit event to finish |
---|
| 139 | t=SDL_GetTicks(); |
---|
| 140 | if(Mix_PlayMusic(music, 1)==-1) |
---|
| 141 | cleanExit("Mix_PlayMusic(0x%p,1)",music); |
---|
| 142 | Mix_VolumeMusic(volume); |
---|
| 143 | |
---|
| 144 | while((Mix_PlayingMusic() || Mix_PausedMusic()) && !done) |
---|
| 145 | { |
---|
| 146 | SDL_Event e; |
---|
| 147 | while(SDL_PollEvent(&e)) |
---|
| 148 | { |
---|
| 149 | switch(e.type) |
---|
| 150 | { |
---|
| 151 | case SDL_KEYDOWN: |
---|
| 152 | switch(e.key.keysym.sym) |
---|
| 153 | { |
---|
| 154 | case SDLK_ESCAPE: |
---|
| 155 | done=1; |
---|
| 156 | break; |
---|
| 157 | case SDLK_LEFT: |
---|
| 158 | Mix_RewindMusic(); |
---|
| 159 | break; |
---|
| 160 | case SDLK_RIGHT: |
---|
| 161 | switch(Mix_GetMusicType(NULL)) |
---|
| 162 | { |
---|
| 163 | case MUS_MP3: |
---|
| 164 | Mix_SetMusicPosition(+5); |
---|
| 165 | break; |
---|
| 166 | default: |
---|
| 167 | printf("cannot fast-forward this type of music\n"); |
---|
| 168 | break; |
---|
| 169 | } |
---|
| 170 | break; |
---|
| 171 | case SDLK_UP: |
---|
| 172 | volume=(volume+1)<<1; |
---|
| 173 | if(volume>SDL_MIX_MAXVOLUME) |
---|
| 174 | volume=SDL_MIX_MAXVOLUME; |
---|
| 175 | Mix_VolumeMusic(volume); |
---|
| 176 | break; |
---|
| 177 | case SDLK_DOWN: |
---|
| 178 | volume>>=1; |
---|
| 179 | Mix_VolumeMusic(volume); |
---|
| 180 | break; |
---|
| 181 | case SDLK_SPACE: |
---|
| 182 | if(Mix_PausedMusic()) |
---|
| 183 | Mix_ResumeMusic(); |
---|
| 184 | else |
---|
| 185 | Mix_PauseMusic(); |
---|
| 186 | break; |
---|
| 187 | default: |
---|
| 188 | break; |
---|
| 189 | } |
---|
| 190 | break; |
---|
| 191 | case SDL_QUIT: |
---|
| 192 | done=1; |
---|
| 193 | break; |
---|
| 194 | default: |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | // the postmix processor tells us when there's new data to draw |
---|
| 199 | if(need_refresh) |
---|
| 200 | refresh(); |
---|
| 201 | else |
---|
| 202 | SDL_Delay(0); |
---|
| 203 | } |
---|
| 204 | t=SDL_GetTicks()-t; |
---|
| 205 | |
---|
| 206 | // free & close |
---|
| 207 | Mix_FreeMusic(music); |
---|
| 208 | Mix_CloseAudio(); |
---|
| 209 | SDL_Quit(); |
---|
| 210 | // show a silly statistic |
---|
| 211 | printf("fps=%.2f\n",((float)flips)/(t/1000.0)); |
---|
| 212 | return(0); |
---|
| 213 | } |
---|