Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
soundserver.mac.notworking.cpp
Go to the documentation of this file.
1 int GetMaxVolume();
2 #include "lin_time.h"
3 #ifdef __APPLE__
4 #include <sys/types.h>
5 #include <pwd.h>
6 #include <unistd.h>
7 extern "C" {
8 #include "../fmod.h"
9 }
10 void music_finished();
11 signed char endcallback( FSOUND_STREAM *stream, void *buf, int len, int param )
12 {
13  if (!buf)
15  return 0;
16 }
17 struct Music
18 {
19  FSOUND_STREAM *m;
20  int channel;
21  Music()
22  {
23  m = NULL;
24  channel = 0;
25  }
26  void Stop()
27  {
28  if (m) FSOUND_Stream_Stop( m );
29  }
30  void Free()
31  {
32  if (m) FSOUND_Stream_Close( m );
33  m = NULL;
34  }
35  bool Load( const char *file )
36  {
37  m = FSOUND_Stream_OpenFile( file, FSOUND_NORMAL|FSOUND_MPEGACCURATE, 0 );
38  return true;
39  }
40  /*
41  *
42  * void Play(float fadeout, float fadein, Music &oldmusic){
43  * if (!m) return;
44  * FSOUND_Stream_SetEndCallback(m,endcallback,0);
45  * FSOUND_Stream_SetSynchCallback(m, endcallback, 0);
46  * channel = FSOUND_Stream_PlayEx(FSOUND_FREE, m, NULL, 1);
47  * FSOUND_SetPaused(channel, 0);
48  * SetVolume(0);
49  * if (fadeout*100>1) {
50  * for (unsigned int i=0;i<fadeout*100;i++) {
51  * SetVolume(i/(float)fadeout);
52  * oldmusic.SetVolume(((float)fadeout-i)/fadeout);
53  * micro_sleep (10000);
54  * }
55  * }
56  * SetVolume(1);
57  * oldmusic.Stop();
58  * }
59  * void SetVolume(float vol) {
60  * if (m) {
61  * F_API FSOUND_SetVolume(this->channel,(int)(vol*GetMaxVolume()));
62  * }
63  * }
64  */
65  void Play( float fadeout, float fadein, Music &oldmusic )
66  {
67  if (!m) return;
68  FSOUND_Stream_SetEndCallback( m, endcallback, 0 );
69  FSOUND_Stream_SetSynchCallback( m, endcallback, 0 );
70  channel = FSOUND_Stream_Play( FSOUND_FREE, m );
71  SetVolume( 0 );
72  if (fadeout*10 > 1)
73  for (unsigned int i = 0; i < fadeout*10; i++) {
74  float ratio = ( (float) i )/(fadeout*10.);
75  SetVolume( ratio );
76  oldmusic.SetVolume( 1-ratio );
77  micro_sleep( 10000 );
78  }
79  oldmusic.Stop();
80  oldmusic.Free();
81 
82  SetVolume( 1 );
83  }
84  void SetVolume( float vol )
85  {
86  if (m) {
87  printf( "Setting %d to %d\n", this->channel, (int) ( vol*GetMaxVolume() ) );
88  F_API FSOUND_SetVolume( this->channel, (int) ( vol*GetMaxVolume() ) );
89  }
90  }
91 };
92 #else
93 #define HAVE_SDL
94 #ifdef HAVE_SDL
95 #include <SDL/SDL.h>
96 #include <SDL/SDL_thread.h>
97 #include <SDL/SDL_mixer.h>
98 #else
99 typedef int Mix_Music;
100 #endif
101 struct Music
102 {
103  Mix_Music *m;
105  {
106  m = NULL;
107  }
108  bool Load( const char *file )
109  {
110  m = Mix_LoadMUS( file );
111  return m != NULL;
112  }
113  bool Play( float fadeout, float fadein, Music &oldmusic )
114  {
115  if (m) {
116  int fadesteps = fadeout*100;
117  for (int i = fadesteps; i >= 0; i--) {
118  oldmusic.SetVolume( i/(float) fadesteps );
119  micro_sleep( 10000 );
120  }
121  Mix_FadeInMusic( m, 1, fadein ) != -1;
122  micro_sleep( fadein*1000000 );
123 
124  return true;
125  } else {
126  return false;
127  }
128  }
129  void Stop()
130  {
131  if (m) Mix_StopMusic( m );
132  }
133  void Free()
134  {
135  if (m) Mix_FreeMusic( m );
136  }
137  void SetVolume( float vol )
138  {
139  if (m)
140  Mix_VolumeMusic( vol*GetMaxVolume() );
141  }
142 }
143 #ifdef _WIN32
144 #ifndef NOMINMAX
145 #define NOMINMAX
146 #endif //tells VCC not to generate min/max macros
147 #include <direct.h>
148 #include <windows.h>
149 #define sleep( sec ) Sleep( sec*1000 );
150 #else
151 #include <unistd.h>
152 #include <stdio.h>
153 #include <pwd.h>
154 #endif
155 #endif
156 #include <string>
157 #include <vector>
158 #include <stdlib.h>
159 #include <string.h>
160 #include <stdio.h>
161 #include <stdarg.h>
162 
163 #include "inet.h"
164 int fadeout = 0, fadein = 0;
165 float volume = 0;
166 int bits = 0, done = 0;
167 
168 /*
169  *****************************************************************************
170  * some simple exit and error routines
171  */
172 void errorv( char *str, va_list ap )
173 {
174 #ifdef HAVE_SDL
175  vfprintf( stderr, str, ap );
176 
177  fprintf( stderr, ": %s.\n", SDL_GetError() );
178 #endif
179 }
180 int GetMaxVolume()
181 {
182 #ifdef __APPLE__
183  static int maxVol = FSOUND_GetSFXMasterVolume();
184  return maxVol;
185 
186 #else
187 #ifdef HAVE_SDL
188  return SDL_MIX_MAXVOLUME;
189 
190 #else
191  return 100
192 #endif
193 #endif
194 }
195 void cleanExit( char *str, ... )
196 {
197 #ifdef HAVE_SDL
198  va_list ap;
199 
200  va_start( ap, str );
201  errorv( str, ap );
202  va_end( ap );
203  Mix_CloseAudio();
204  SDL_Quit();
205 #endif
206  exit( 1 );
207 }
208 
209 /*
210  *****************************************************************************
211  * The main function
212  */
213 void changehome( bool to, bool linuxhome = true )
214 {
215  static std::vector< std::string >paths;
216  if (to) {
217  char mycurpath[8192];
218  getcwd( mycurpath, 8191 );
219  mycurpath[8191] = '\0';
220  paths.push_back( std::string( mycurpath ) );
221 #ifndef _WIN32
222  if (linuxhome) {
223  struct passwd *pwent;
224  pwent = getpwuid( getuid() );
225  chdir( pwent->pw_dir );
226  }
227 #endif
228  chdir( ".vegastrike" );
229  } else if ( !paths.empty() ) {
230  chdir( paths.back().c_str() );
231  paths.pop_back();
232  }
233 }
234 #ifdef _WIN32
235 #undef main
236 #endif
237 bool sende = true;
238 bool invalid_string = true;
239 std::string curmus;
240 
241 Music PlayMusic( const char *file, Music &oldmusic )
242 {
243  Music music;
244  music.Load( file );
245  if (music.m == NULL) {
246  changehome( true, false );
247  music.Load( file );
248  changehome( false );
249  if (music.m == NULL) {
250  changehome( true, true );
251  music.Load( file );
252  changehome( false );
253  if (music.m == NULL)
254  return oldmusic;
255  }
256  }
257  sende = false;
258  music.Play( fadeout, fadein, oldmusic );
259 
260  sende = true;
261  curmus = file;
262  invalid_string = false;
263  return music;
264 }
265 int mysocket = -1;
267 {
268  if (sende) {
269  char data = 'e';
270  INET_Write( mysocket, sizeof (char), &data );
271  printf( "\ne\n[SONG DONE]\n" );
272  invalid_string = true;
273  }
274 }
275 int main( int argc, char **argv )
276 {
277  Music music;
278  int audio_rate, audio_channels,
279  //set this to any of 512,1024,2048,4096
280  //the higher it is, the more FPS shown and CPU needed
281  audio_buffers = 4096;
282 #ifdef HAVE_SDL
283  Uint16 audio_format;
284  //initialize SDL for audio and video
285  if (SDL_Init( SDL_INIT_AUDIO ) < 0)
286  cleanExit( "SDL_Init\n" );
287  Mix_HookMusicFinished( &music_finished );
288 #else
289 #ifdef __APPLE__
290  if ( !FSOUND_Init( 44100, 64, FSOUND_INIT_GLOBALFOCUS ) ) {
291  printf( "SOUND Error %d\n", FSOUND_GetError() );
292  exit( 1 );
293  }
294 #endif
295 #endif
296  INET_startup();
297  GetMaxVolume();
298  //initialize sdl mixer, open up the audio device
299 #ifdef HAVE_SDL
300  if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, audio_buffers ) < 0)
301  cleanExit( "Mix_OpenAudio\n" );
302  //print out some info on the audio device and stream
303  Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
304  bits = audio_format&0xFF;
305 #endif
306  printf( "Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
307  bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers );
308  //load the song
309  for (int i = 0; i < 10 && mysocket == -1; i++)
310  mysocket = INET_AcceptFrom( 4364, "localhost" );
311  if (mysocket == -1)
312  return 1;
313  printf( "\n[CONNECTED]\n" );
314  char ministr[2] = {'\0', '\0'};
315  while (!done) {
316 //if ((Mix_PlayingMusic() || Mix_PausedMusic())&&(!done)) {
317  char arg;
318  std::string str;
319  arg = INET_fgetc( mysocket );
320  printf( "%c", arg );
321  switch (arg)
322  {
323  case 'p':
324  case 'P':
325  arg = INET_fgetc( mysocket );
326  while (arg != '\0' && arg != '\n') {
327  if (arg != '\r') {
328  ministr[0] = arg;
329  str += ministr;
330  }
331  arg = INET_fgetc( mysocket );
332  }
333  printf( "%s", str.c_str() );
334  if ( (str != curmus || invalid_string)
335 #ifdef HAVE_SDL
336  || ( !Mix_PlayingMusic() )
337 #endif
338  ) {
339  music = PlayMusic( str.c_str(), music );
340  if (music.m) {
341  printf( "\n[PLAYING %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
342  curmus = str;
343  invalid_string = false;
344  } else {
345  printf( "\n[UNABLE TO PLAY %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
346  music_finished();
347  }
348  } else {
349  printf( "\n[%s WITH %d FADEIN AND %d FADEOUT IS ALREADY PLAYING]\n", str.c_str(), fadein, fadeout );
350  }
351  break;
352  case 'i':
353  case 'I':
354  arg = INET_fgetc( mysocket );
355  while (arg != '\0' && arg != '\n') {
356  if (arg != '\r') {
357  ministr[0] = arg;
358  str += ministr;
359  }
360  arg = INET_fgetc( mysocket );
361  }
362  printf( "%s", str.c_str() );
363  fadein = atoi( str.c_str() );
364  printf( "\n[SETTING FADEIN TO %d]\n", fadein );
365  break;
366  case 'o':
367  case 'O':
368  arg = INET_fgetc( mysocket );
369  while (arg != '\0' && arg != '\n') {
370  if (arg != '\r') {
371  ministr[0] = arg;
372  str += ministr;
373  }
374  arg = INET_fgetc( mysocket );
375  }
376  printf( "%s", str.c_str() );
377  fadeout = atoi( str.c_str() );
378  printf( "\n[SETTING FADEOUT TO %d]\n", fadeout );
379  break;
380  case 'v':
381  case 'V':
382  arg = INET_fgetc( mysocket );
383  while (arg != '\0' && arg != '\n') {
384  if (arg != '\r') {
385  ministr[0] = arg;
386  str += ministr;
387  }
388  arg = INET_fgetc( mysocket );
389  }
390  printf( "%s", str.c_str() );
391  volume = atof( str.c_str() );
392  printf( "\n[SETTING VOLUME TO %f]\n", volume );
393  music.SetVolume( volume );
394  break;
395  case 't':
396  case 'T':
397  case '\0':
398  INET_close( mysocket );
399  done = true;
400  printf( "\n[TERMINATING MUSIC SERVER]\n" );
401  break;
402  }
403  }
404  //free & close
405  INET_cleanup();
406 #ifdef HAVE_SDL
407  Mix_CloseAudio();
408  SDL_Quit();
409 #endif
410 
411  return 0;
412 }
413