The Game Engine  1
Audio.cpp
Go to the documentation of this file.
1 #include "Audio.h"
2 #include "Game.h"
3 #include <vector>
4 
5 int SOUND_CAP=100;
6 int index=0;
7 vector<string> soundlist (SOUND_CAP);
8 
9 
10 struct sample {
11  Uint8 *data;
12  Uint32 dpos;
13  Uint32 dlen;
14  SDL_AudioCVT cvt;
15 } sounds[100];
16 
18 {
19  if(SDL_Init(SDL_INIT_AUDIO) == -1)
20  {
21  //
22  }
23 
24  extern void mixaudio(void *unused, Uint8 *stream, int len);
25  SDL_AudioSpec fmt;
26 
27  /* Set 16-bit stereo audio at 22Khz */
28  fmt.freq = 22050;
29  fmt.format = AUDIO_S16;
30  fmt.channels = 2;
31  fmt.samples = 512; /* A good value for games */
32  fmt.callback = mixaudio;
33  fmt.userdata = NULL;
34 
35  if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
36  fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
37  exit(1);
38  }
39  SDL_PauseAudio(0);
40 }
41 
42 int Audio::LoadAudio(std::string name, string location, bool play_on_start, int play_count)
43 {
44  //Declare the audio file and Initialize
45 
46  //SDL_Init(SDL_AUDIO);
47  int index;
48  SDL_AudioSpec wave;
49  Uint8 *data;
50  Uint32 dlen;
51  SDL_AudioCVT cvt;
52 
53 
54 
55 
56  //From Documentation
57  /* Look for an empty (or finished) sound slot */
58  for ( index=0; index<SOUND_CAP; ++index ) {
59  if ( sounds[index].dpos == sounds[index].dlen )
60  {
61  break;
62  }
63  }
64  if ( index == SOUND_CAP )
65  return -1;
66 
67  /* Load the sound file and convert it to 16-bit stereo at 22kHz */
68  if ( SDL_LoadWAV(location.c_str(), &wave, &data, &dlen) == NULL ) {
69  fprintf(stderr, "Couldn't load %s: %s\n", location.c_str(), SDL_GetError());
70  return -1;
71  }
72  SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
73  AUDIO_S16, 2, 22050);
74 
75  cvt.buf = (Uint8*) malloc(dlen*cvt.len_mult);
76  memcpy(cvt.buf, data, dlen);
77  cvt.len = dlen;
78  SDL_ConvertAudio(&cvt);
79  SDL_FreeWAV(data);
80 
81  if ( sounds[index].data ) {
82  free(sounds[index].data);
83  }
84 
85 
86  sounds[index].cvt=cvt;
87 
88  return index;
89 
90 }
91 
92 
93 
94 void Audio::play(int index)
95 {
96  SDL_AudioCVT cvt=sounds[index].cvt;
97  //play an audio file
98 
99  if ( sounds[index].data ) {
100  free(sounds[index].data);
101  }
102  SDL_LockAudio();
103  sounds[index].data = cvt.buf;
104  sounds[index].dlen = cvt.len_cvt;
105  sounds[index].dpos = 0;
106  SDL_UnlockAudio();
107 }
108 
110 {
111  SDL_AudioCVT cvt=sounds[index].cvt;
112 
113  SDL_LockAudio();
114  sounds[index].data = cvt.buf;
115  sounds[index].dlen = cvt.len_cvt;
116  sounds[index].dpos = cvt.len;
117  SDL_UnlockAudio();
118 }
119 
121 {
122  SDL_CloseAudio();
123 }
124 
125 void create_tone( void *userdata, Uint8 *stream, int len ) {
126  static double angle = 0.0 ;
127  int i = 0 ;
128  fprintf( stderr, "Filling %d\n", len ) ;
129  for(i=0;i<len;i++) {
130  *stream++ = 255*cos(angle) ;
131  angle += 3.14159/100 ;
132  if( angle > 2.0*3.14159 ) {
133  angle -= 2.0*3.14159 ;
134  }
135  }
136 }
137 
139 {
140  SDL_Surface *screen;
141  SDL_Event event;
142  int done = 0;
143 
144  /* Audio Setup */
145  SDL_AudioSpec *desired, *obtained;
146  SDL_AudioSpec *hardware_spec;
147 
148  desired = (SDL_AudioSpec*)malloc(sizeof(SDL_AudioSpec));
149  obtained = (SDL_AudioSpec*)malloc(sizeof(SDL_AudioSpec));
150 
151 
152  desired->freq=22050;
153  desired->format=AUDIO_S16LSB;
154  desired->channels=0;
155  desired->samples=8192;
156  desired->callback=create_tone;
157  desired->userdata=NULL;
158 
159  SDL_Init(SDL_INIT_AUDIO);
160 
161  //Open Audio Device
162 
163 }
164 
165 void mixaudio(void *unused, Uint8 *stream, int len)
166 {
167  int i;
168  Uint32 amount;
169 
170  for ( i=0; i<SOUND_CAP; ++i ) {
171  amount = (sounds[i].dlen-sounds[i].dpos);
172  if ( amount > len ) {
173  amount = len;
174  }
175  SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME);
176  sounds[i].dpos += amount;
177  }
178 }