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.cpp File Reference

Go to the source code of this file.

Functions

int GetMaxVolume ()
 
void changehome (bool to, bool linuxhome=true)
 
bool watch_for_sende (pid_t p)
 
std::string my_getstring (int socket)
 
void ForkedProcess (const char *file, float fade, int fd)
 
int ForkThenPlay (const char *file, float fade, int mysocket)
 
void music_finished ()
 
void FinishOlde (int open_file)
 
int main (int argc, char **argv)
 

Variables

int fadeout = 0
 
int fadein = 0
 
float volume = 0
 
float soft_volume = 0
 
int bits = 0
 
int done = 0
 
int sende = 1
 
std::string curmus
 
int mysocket = -1
 

Function Documentation

void changehome ( bool  to,
bool  linuxhome = true 
)
void FinishOlde ( int  open_file)

Definition at line 174 of file soundserver.mac.cpp.

References INET_close(), and INET_Write().

Referenced by main().

175 {
176  if (open_file != -1) {
177  if (INET_Write( open_file, 1, "k" ) > 0)
178  sende--; //kill
179  INET_close( open_file );
180  open_file = -1;
181  }
182 }
void ForkedProcess ( const char *  file,
float  fade,
int  fd 
)

Definition at line 121 of file soundserver.mac.cpp.

References Music::Free(), INET_BytesToRead(), Music::Load(), Music::m, Music::Play(), and Music::Stop().

Referenced by ForkThenPlay().

122 {
123  printf( "initing device drivers\n" );
124  if ( !FSOUND_Init( 44100, 64, FSOUND_INIT_GLOBALFOCUS ) ) {
125  printf( "SOUND Error %d\n", FSOUND_GetError() );
126  exit( 1 );
127  }
128  Music mus;
129  mus.Load( file );
130  if (mus.m) {
131  mus.Play( fade );
132 
133  printf( "f0rked playing %s\n", file );
134  while ( !INET_BytesToRead( fd ) )
135  if (AllDone) {
136  printf( "song done\n" );
137  mus.Free();
138  return;
139  }
140  mus.Stop();
141  mus.Free();
142  }
143 }
int ForkThenPlay ( const char *  file,
float  fade,
int  mysocket 
)

Definition at line 144 of file soundserver.mac.cpp.

References accountXML::file, and ForkedProcess().

Referenced by main().

145 {
146  printf( "playing %s", file );
147  int filedes[2] = {-1, -1};
148  //socketpair(AF_UNIX,SOCK_STREAM,0,filedes);
149  pipe( filedes );
150  pid_t fark = fork();
151  if (fark) {
152  close( mysocket );
153  printf( "f0rking subprocess\n" );
154  ForkedProcess( file, fade, filedes[0] );
155  Cleanup( filedes[0] );
156  exit( 0 ); //Dun!
157  return -1;
158  }
159  printf( "forking main loop\n" );
160  curmus = file;
161  return filedes[1];
162 }
int GetMaxVolume ( )

Definition at line 327 of file soundserver.mac.cpp.

Referenced by main(), and Music::SetVolume().

328 {
329 #ifdef __APPLE__
330  static int maxVol = FSOUND_GetSFXMasterVolume();
331  return maxVol;
332 
333 #else
334  return 0;
335 #endif
336 }
int main ( int  argc,
char **  argv 
)

Definition at line 184 of file soundserver.mac.cpp.

References done, fadein, fadeout, FinishOlde(), float, ForkThenPlay(), GetMaxVolume(), i, INET_AcceptFrom(), INET_BytesToRead(), INET_close(), INET_fgetc(), INET_startup(), my_getstring(), mysocket, and soft_volume.

185 {
186  int open_file = -1;
187 
188  INET_startup();
189  GetMaxVolume();
190  //load the song
191  for (int i = 0; i < 10 && mysocket == -1; i++) {
192  int port = 4364;
193  if (argc > 1)
194  port = atoi( argv[1] );
195  mysocket = INET_AcceptFrom( port, "localhost" );
196  }
197  if (mysocket == -1)
198  return 1;
199  printf( "\n[CONNECTED]\n" );
200  char ministr[2] = {'\0', '\0'};
201  while (!done) {
202  char arg;
203  string str;
204  if ( INET_BytesToRead( mysocket ) ) {
205  arg = INET_fgetc( mysocket );
206  printf( "%c", arg );
207  switch (arg)
208  {
209  case 'p':
210  case 'P':
211  arg = INET_fgetc( mysocket );
212  str = my_getstring( mysocket );
213  printf( "%s", str.c_str() );
214  if (str != curmus) {
215  FinishOlde( open_file );
216  open_file = ForkThenPlay( str.c_str(), fadeout, mysocket );
217  curmus = str;
218  } else {
219  printf( "\n[%s WITH %d FADEIN AND %d FADEOUT IS ALREADY PLAYING]\n", str.c_str(), fadein, fadeout );
220  }
221  break;
222  case 'i':
223  case 'I':
224  arg = INET_fgetc( mysocket );
225  str = my_getstring( mysocket );
226  printf( "%s", str.c_str() );
227  fadein = atoi( str.c_str() );
228  printf( "\n[SETTING FADEIN TO %d]\n", fadein );
229  break;
230  case 'o':
231  case 'O':
232  arg = INET_fgetc( mysocket );
233  str = my_getstring( mysocket );
234  printf( "%s", str.c_str() );
235  fadeout = atoi( str.c_str() );
236  printf( "\n[SETTING FADEOUT TO %d]\n", fadeout );
237  break;
238  case 'v':
239  case 'V':
240  {
241  int vtype = INET_fgetc( mysocket );
242  str = my_getstring( mysocket );
243  printf( "%s", str.c_str() );
244  switch (vtype)
245  {
246  case 'h':
247  case 'H':
248  volume = (float) atof( str.c_str() );
249  break;
250  case 's':
251  case 'S':
252  soft_volume = (float) atof( str.c_str() );
253  str = my_getstring( mysocket );
254  printf( "%s", str.c_str() );
255  break;
256  }
257  printf( "\n[SETTING VOLUME TO %f]\n", volume*soft_volume );
258  //music.SetVolume(volume);
259  break;
260  }
261  case 's':
262  case 'S':
263  printf( "\n[STOPPING ALL MUSIC]\n" );
264  curmus = "";
265  FinishOlde();
266  break;
267  case 'h':
268  case 'H':
269  //Softvolume not yet supported - ignore it
270  INET_fgetc( mysocket );
271  INET_fgetc( mysocket );
272  break;
273  case 't':
274  case 'T':
275  case '\0':
276  INET_close( mysocket );
277  done = true;
278  printf( "\n[TERMINATING MUSIC SERVER]\n" );
279  break;
280  }
281  }
282  }
283  Cleanup( open_file );
284 
285  return 0;
286 }
void music_finished ( )

Definition at line 164 of file soundserver.mac.cpp.

References INET_Write().

165 {
166  printf( "send e ratio %d\n", (int) sende );
167  if (sende) {
168  char data = 'e';
169  INET_Write( mysocket, sizeof (char), &data );
170  printf( "\ne\n[SONG DONE]\n" );
171  curmus = "";
172  }
173 }
std::string my_getstring ( int  socket)

Definition at line 109 of file soundserver.mac.cpp.

References INET_fgetc().

110 {
111  std::string str;
112  int arg;
113  do {
114  arg = INET_fgetc( socket );
115  if ( (arg != '\r') && (arg != '\n') && (arg != '\0') )
116  str += (char) arg;
117  } while ( (arg != '\n') && (arg != '\0') );
118  return str;
119 }
bool watch_for_sende ( pid_t  p)

Definition at line 97 of file soundserver.mac.cpp.

References music_finished(), and sende.

98 {
99  int status;
100  if ( waitpid( p, &status, WNOHANG ) ) {
101  ++sende;
102  if (sende > 0)
103  music_finished();
104  return true;
105  }
106  return false;
107 }

Variable Documentation

int bits = 0

Definition at line 93 of file soundserver.mac.cpp.

std::string curmus

Definition at line 96 of file soundserver.mac.cpp.

int done = 0

Definition at line 93 of file soundserver.mac.cpp.

int fadein = 0

Definition at line 91 of file soundserver.mac.cpp.

int fadeout = 0

Definition at line 91 of file soundserver.mac.cpp.

int mysocket = -1

Definition at line 163 of file soundserver.mac.cpp.

int sende = 1

Definition at line 95 of file soundserver.mac.cpp.

float soft_volume = 0

Definition at line 92 of file soundserver.mac.cpp.

float volume = 0

Definition at line 92 of file soundserver.mac.cpp.