41 #include "soundheader.h"
42 #include <input/InputFunctions.h>
45 int freewrlSystem (
char *
string);
52 int registered[MAXSOURCES];
53 int active[MAXSOURCES];
60 int currentSource = -1;
64 int S_Server_IPC = -1;
75 void rewind_to_beginning (
SNDFILE *wavfile) {
76 if (wavfile != NULL) {
77 if (wavfile->fd != NULL) {
83 wavfile->bytes_remaining = wavfile->DataChunk.chunkSize;
84 fseek (wavfile->fd, wavfile->wavdataoffset, SEEK_SET);
87 if (wavfile->bytes_remaining <= 0) {
88 printf (
"Error in getting wavfile DataChunk\n");
98 int chunk (
char *buf,
char *find,
int len) {
102 while (mycnt < (len) - strlen(find)) {
103 if (strncmp (&buf[mycnt], find, strlen(find)) == 0) {
119 int querySoundType(
SNDFILE *me) {
122 br = fread(me->data,1,BUFSIZE,me->fd);
123 me->dataptr = chunk (me->data,
"RIFF",BUFSIZE);
126 if (me->dataptr < 0) {
127 printf (
"SoundEngine:not a RIFF file\n\t%s\n",cp2);
131 br = chunk (&me->data[me->dataptr],
"WAVE",BUFSIZE);
134 printf (
"SoundEngine:not a WAVE file\n\t%s\n",cp2);
139 br = chunk (&me->data[me->dataptr],
"fmt ",BUFSIZE);
142 printf (
"SoundServer:no fmt found in WAVE file\n\t%s\n",cp2);
149 memcpy (&me->FormatChunk, &me->data[me->dataptr], sizeof (
fmtChnk));
163 if (me->FormatChunk. wFormatTag != 1) {
164 printf (
"SoundServer:compressed WAV not handled yet\n\t%s\n",
170 me->dataptr += 8 + me->FormatChunk.chunkSize;
173 br = chunk (&me->data[me->dataptr],
"data",BUFSIZE);
176 printf (
"SoundServer:no data found in WAVE file\n\t%s\n",cp2);
181 memcpy (&me->DataChunk, &me->data[me->dataptr], sizeof (
datChnk));
192 if (me->DataChunk.chunkSize <= 0) {
193 printf (
"SoundServer:WAV DataChunk size invalid\n\t%s\n",cp2);
199 me->wavdataoffset = me->dataptr+8;
205 SNDFILE *openSound (
char *path,
int soundNo) {
211 if (!mysound)
return NULL;
213 mysound->fd = fopen(path,
"r");
214 mysound->bytes_remaining = UNINITWAV;
216 if (mysound->fd == NULL) {
224 switch (querySoundType(mysound)) {
226 return initiateWAVSound(mysound,soundNo);
230 mysound->type = MP3FILE;
234 mysound->type = MPGFILE;
238 printf (
"unknown file type: %s\n",cp2);
250 void toclnt(
char *message_to_send) {
252 (void) strcpy(msg.msg, message_to_send);
255 while((xx=msgsnd(msq_toclnt, &msg,strlen(msg.msg)+1,IPC_NOWAIT)) != 0);
257 printf (
"SoundEngineServer - error sending ready msg\n");
264 return msgrcv(msq_fromclnt,&msg,256,1,0);
269 void process_command () {
282 if (strncmp (
"REGS",msg.msg,4) == 0) {
290 strcpy(cp,SOUNDCONV);
296 while ((a<strlen(msg.msg)-1) && (b<300) && (msg.msg[a]>
' ')) {
298 cp2[cp2len] = msg.msg[a];
301 cp[b]=
'\0'; cp2[cp2len]=
'\0';
305 sscanf (&msg.msg[a],
" %d %d %f %f %f",&mysource,&myloop,&x,&y,&z);
309 if ((x > 1.01) || (x < 0.98)) {
311 sprintf (pitch,
"%d ",(
int) ((
float)22050.0/x));
313 sprintf (pitch,
"%d ",22050);
318 strcat (cp,
"-c2 -w /tmp/sound");
321 sprintf (st,
"%d.wav",mysource);
331 strcpy (cp,
"/tmp/sound");
336 if (mysource > current_max) current_max = mysource;
342 sndfile[mysource] = openSound(cp,mysource);
343 if (sndfile[mysource] == NULL) {
344 printf (
"SoundServer:open problem for:\n\t %s\n",cp2);
348 registered[mysource] = 1;
349 loop[mysource] = myloop;
350 sndfile[mysource]->pitch = x;
352 sndfile[mysource]->ampl = 0;
353 sndfile[mysource]->balance = 50;
355 duration = (double) sndfile[mysource]->DataChunk.chunkSize / (
double) sndfile[mysource]->FormatChunk.dwAvgBytesPerSec;
358 sprintf (cp,
"REGS %d %f",mysource,(
float)duration);
361 }
else if (strncmp (
"AMPL",msg.msg,4) == 0) {
367 sscanf (msg.msg,
"AMPL %d %f %f %f",&a,&x,&bal,&fps);
369 if ((registered[a] == 1) && (a>=0) && (a<MAXSOURCES)) {
370 sndfile[a]->ampl = (int) (x*100.0);
372 sndfile[a]->balance = (int) ((
float)bal * 100.0);
375 }
else if (strncmp (
"ACTV",msg.msg,4) == 0) {
377 sscanf (msg.msg,
"ACTV %d %d",&a,&b);
378 if ((a>=0) && (a<MAXSOURCES)) {
382 rewind_to_beginning (sndfile[a]);
393 int main(
int argc,
char **argv) {
397 printf (
"Server: too few args\n");
401 if ((argc == 2) && !strcmp(argv[1],
"-v")) {
402 printf(
"FreeWRL sound server\nVersion: %s\n", freewrl_snd_get_version());
407 for (xx=0; xx<MAXSOURCES; xx++) {
417 S_Server_IPC=getppid();
420 if (!strncmp(
"INIT",argv[0],4)) {
421 sscanf (argv[0],
"%s%d",msg.msg,&S_Server_IPC);
423 printf (
"SoundServer: no Client_IPC on command line\n");
429 if ((msq_fromclnt = msgget(S_Server_IPC,0666)) < 0) {
430 printf (
"SoundServer: no IPC queue available\n");
433 if ((msq_toclnt = msgget(S_Server_IPC+1,0666)) < 0) {
434 printf (
"SoundServer: no IPC queue available\n");
452 }
while (strncmp (
"QUIT",msg.msg,4));
455 char fileRemove[200];
456 for (count=0; count<current_max; count++) {
457 sprintf (fileRemove,
"/tmp/sound%d.wav",count);
459 unlinkShadowFile(fileRemove);
469 int freewrlSystem (
char *sysline) {
471 #define MAXEXECPARAMS 10
472 #define EXECBUFSIZE 2000
474 char *paramline[MAXEXECPARAMS];
475 char buf[EXECBUFSIZE];
489 if (strlen(sysline)>=EXECBUFSIZE)
return FALSE;
490 strcpy (buf,sysline);
493 for (count=0; count<MAXEXECPARAMS; count++) paramline[count] = NULL;
497 while (internbuf != NULL) {
498 paramline[count] = internbuf;
499 internbuf = strchr(internbuf,
' ');
500 if (internbuf != NULL) {
506 if (count >= MAXEXECPARAMS)
return -1;
518 if (strncmp(paramline[count],
"&",strlen(paramline[count])) == 0) {
520 paramline[count] =
'\0';
524 switch (childProcess=fork()) {
526 perror (
"fork"); exit(1);
533 Xrv = execl(paramline[0],
534 paramline[0],paramline[1], paramline[2],
535 paramline[3],paramline[4],paramline[5],
536 paramline[6],paramline[7],NULL);
549 waitpid (childProcess,&pidStatus,0);
556 printf (
"System call failed :%s:\n",sysline);