11 #include <mach/thread_policy.h>
12 #include <mach/mach_init.h>
15 kern_return_t thread_policy_set( thread_act_t thread, natural_t flavor, thread_policy_t policy_info, natural_t count );
16 mach_port_t mach_thread_self(
void );
24 bool e_already_sent =
false;
26 signed char endcallback( FSOUND_STREAM *stream,
void *buf,
int len,
int param )
43 if (
m) FSOUND_Stream_Stop(
m );
48 const float ram_limit = 47;
49 static list< FSOUND_STREAM* >
q;
51 if (q.size() > ram_limit) {
52 FSOUND_Stream_Close( q.front() );
60 m = FSOUND_Stream_OpenFile( file, FSOUND_NORMAL|FSOUND_MPEGACCURATE, 0 );
94 FSOUND_Stream_SetEndCallback(
m, endcallback, 0 );
95 FSOUND_Stream_SetSynchCallback(
m, endcallback, 0 );
96 channel = FSOUND_Stream_Play( FSOUND_FREE,
m );
97 if (!e_already_sent) {
100 for (
unsigned int i = 0;
i < fadeout*10;
i++) {
101 float ratio = ( (
float) i )/(fadeout*10.);
110 }
else if (!e_already_sent) {
115 FSOUND_Stream_SetEndCallback(
m, endcallback, 0 );
116 channel = FSOUND_Stream_Play( FSOUND_FREE,
m );
119 e_already_sent =
false;
123 FSOUND_SetSFXMasterVolume( (
int) ( vol*
GetMaxVolume() ) );
129 F_API FSOUND_SetVolume( this->channel, (
int) ( vol*
GetMaxVolume() ) );
136 #include <SDL/SDL_thread.h>
137 #include <SDL/SDL_mixer.h>
150 m = Mix_LoadMUS( file );
153 bool Play(
float fadeout,
float fadein,
Music &oldmusic )
156 int fadesteps = fadeout*100;
157 for (
int i = fadesteps;
i >= 0;
i--) {
161 Mix_FadeInMusic(
m, 1, fadein ) != -1;
171 if (
m) Mix_StopMusic(
m );
175 if (
m) Mix_FreeMusic(
m );
190 #endif //tells VCC not to generate min/max macros
193 #define sleep( sec ) do{Sleep( sec*1000 );}while(0)
208 int fadeout = 0, fadein = 0;
219 vfprintf( stderr, str, ap );
221 fprintf( stderr,
": %s.\n", SDL_GetError() );
227 static int maxVol = FSOUND_GetSFXMasterVolume();
259 static std::vector< std::string >paths;
261 char mycurpath[8192];
262 getcwd( mycurpath, 8191 );
263 mycurpath[8191] =
'\0';
264 paths.push_back( std::string( mycurpath ) );
267 struct passwd *pwent;
268 pwent = getpwuid( getuid() );
269 chdir( pwent->pw_dir );
272 chdir(
".vegastrike" );
273 }
else if ( !paths.empty() ) {
274 chdir( paths.back().c_str() );
289 if (music.
m == NULL) {
293 if (music.
m == NULL) {
302 music.
Play( fadeout, fadein, oldmusic );
306 invalid_string =
false;
315 e_already_sent =
true;
316 fNET_Write( mysocket_write,
sizeof (
char), &data );
317 printf(
"\ne\n[SONG DONE]\n" );
318 invalid_string =
true;
327 thread_extended_policy_data_t extendedPolicy;
328 thread_precedence_policy_data_t precedencePolicy;
330 extendedPolicy.timeshare = 0;
331 error = thread_policy_set( mach_thread_self(),
332 THREAD_EXTENDED_POLICY,
333 (thread_policy_t) &extendedPolicy,
334 THREAD_EXTENDED_POLICY_COUNT );
335 if (error != KERN_SUCCESS) {
337 mach_error(
"Couldn't set feeder thread's extended policy",
341 precedencePolicy.importance = 1;
342 error = thread_policy_set( mach_thread_self(),
343 THREAD_PRECEDENCE_POLICY,
344 (thread_policy_t) &precedencePolicy,
345 THREAD_PRECEDENCE_POLICY_COUNT );
346 if (error != KERN_SUCCESS) {
348 mach_error(
"Couldn't set feeder thread's precedence policy",
355 int main(
int argc,
char **argv )
359 int audio_rate, audio_channels,
362 audio_buffers = 4096;
366 if (SDL_Init( SDL_INIT_AUDIO ) < 0)
371 if ( !FSOUND_Init( 44100, 64, FSOUND_INIT_GLOBALFOCUS ) ) {
372 printf(
"SOUND Error %d\n", FSOUND_GetError() );
381 if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, audio_buffers ) < 0)
384 Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
385 bits = audio_format&0xFF;
387 printf(
"Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
388 bits, audio_channels > 1 ?
"stereo" :
"mono", audio_buffers );
391 printf(
"argv %d\n\n\n", argc );
393 for (
int i = 0;
i < 10 && mysocket_write == -1;
i++) {
397 if (mysocket_write == -1)
400 mysocket_write = open( argv[2], O_WRONLY|O_SHLOCK|O_CREAT, 0xffffffff );
401 mysocket_read = open( argv[1], O_RDONLY|O_SHLOCK|O_CREAT, 0xffffffff );
403 printf(
"\n[CONNECTED]\n" );
404 char ministr[2] = {
'\0',
'\0'};
416 while (arg !=
'\0' && arg !=
'\n') {
423 printf(
"%s", str.c_str() );
424 if ( (str != curmus || invalid_string)
426 || ( !Mix_PlayingMusic() )
431 printf(
"\n[PLAYING %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(),
fadein,
fadeout );
433 invalid_string =
false;
435 printf(
"\n[UNABLE TO PLAY %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(),
fadein,
fadeout );
439 printf(
"\n[%s WITH %d FADEIN AND %d FADEOUT IS ALREADY PLAYING]\n", str.c_str(),
fadein,
fadeout );
445 while (arg !=
'\0' && arg !=
'\n') {
452 printf(
"%s", str.c_str() );
453 fadein = atoi( str.c_str() );
454 printf(
"\n[SETTING FADEIN TO %d]\n", fadein );
459 while (arg !=
'\0' && arg !=
'\n') {
466 printf(
"%s", str.c_str() );
467 fadeout = atoi( str.c_str() );
468 printf(
"\n[SETTING FADEOUT TO %d]\n", fadeout );
473 while (arg !=
'\0' && arg !=
'\n') {
480 printf(
"%s", str.c_str() );
481 volume = atof( str.c_str() );
482 printf(
"\n[SETTING VOLUME TO %f]\n", volume );
489 if (mysocket_read != mysocket_write)
492 printf(
"\n[TERMINATING MUSIC SERVER]\n" );