Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/src/sdlwav.c @ 2777

Last change on this file since 2777 was 2777, checked in by simon, 20 years ago

/branches/sound: brought up sound_control.cc which doesn't do anything at the moment.

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