/*============================================================================*/ int Snd_init_dev() { int whoami; S_fd_snddev = -1; S_son_pid = 0; if(access(S_snddev,W_OK) != 0) { perror("No access to sound device"); return EXIT_FAILURE; } S_fd_snddev = open(S_snddev,O_WRONLY); if(S_fd_snddev < 0) { fprintf(stderr,"int_snddev: Cannot open sound device \n"); return EXIT_FAILURE; } close(S_fd_snddev); if(pipe(S_fd_pipe) < 0) { fprintf(stderr,"Cannot create pipe for sound control \n"); return EXIT_FAILURE; } /* now setup 2nd process for writing the data... */ if((whoami = fork()) < 0) { fprintf(stderr,"Cannot fork sound driver\n"); return EXIT_FAILURE; } if(whoami != 0) /* successfully created son */ { close(S_fd_pipe[0]); /* close end for reading */ S_son_pid = whoami; return EXIT_SUCCESS; } /* Here is the code for the son... */ { int sound_num,ch,i; struct timeval tval = {0L,0L}; fd_set readfds,dsp; Mix mix; int frag, fragsize; Channel *chan = (Channel*)malloc( sizeof(Channel)*S_num_channels ); for (i=0; i<S_num_channels; i++) Chan_reset( chan+i ); S_fd_snddev = open(S_snddev,O_WRONLY ); if(S_fd_snddev < 0) { perror("Cannot open sound device: "); exit(1); } frag = FRAG_SPEC; /*defined in soundIt.h */ ioctl(S_fd_snddev, SNDCTL_DSP_SETFRAGMENT, &frag); if ( ioctl(S_fd_snddev,SNDCTL_DSP_SPEED, &S_playback_freq)==-1 ) perror("Sound driver ioctl "); fragsize=0; if ( ioctl(S_fd_snddev,SNDCTL_DSP_GETBLKSIZE, &fragsize)==-1 ) perror("Sound driver ioctl "); /* printf("after: block size: %d \n",fragsize); */ /* init mixer object*/ Mix_alloc( &mix, fragsize ); close(S_fd_pipe[1]); /* close end for writing */ FD_ZERO(&dsp); FD_SET(S_fd_snddev, &dsp); FD_ZERO(&readfds); FD_SET(S_fd_pipe[0], &readfds); printf("soundIt library v"SOUNDIT_VERS" initialized.\n"); for(;;) { FD_SET(S_fd_pipe[0], &readfds); tval.tv_sec=0L; tval.tv_usec=0L; select(S_fd_pipe[0]+1, &readfds,NULL,NULL,&tval); if (FD_ISSET(S_fd_pipe[0], &readfds)) { if (read(S_fd_pipe[0], &sound_num, sizeof(int))==0) break; read(S_fd_pipe[0], &ch, sizeof(int)); /* printf("chan=%d snd=%d len=%d \n", ch, sound_num, S_sounds[sound_num].len ); */ Chan_assign( &(chan[ch]), &(S_sounds[sound_num]) ); } Chan_mixAll(&mix,chan); write(S_fd_snddev, mix.Vclippedbuf, fragsize ); } Mix_dealloc( &mix ); printf("soundIt process exiting..\n"); close(S_fd_pipe[0]); close(S_fd_pipe[1]); exit (0); } /*end of child process */ }
// // Snd init dev // int Snd_init_dev() { int whoami; S_fd_snddev = -1; S_son_pid = 0; if(access(S_snddev,W_OK) != 0) { perror("No access to sound device"); return EXIT_FAILURE; } S_fd_snddev = open(S_snddev,O_WRONLY); if(S_fd_snddev < 0) { fprintf(stderr,"int_snddev: Cannot open sound device \n"); return EXIT_FAILURE; } close(S_fd_snddev); if(pipe(S_fd_pipe) < 0) { fprintf(stderr,"Cannot create pipe for sound control \n"); return EXIT_FAILURE; } if((whoami = fork()) < 0) { fprintf(stderr,"Cannot fork sound driver\n"); return EXIT_FAILURE; } if(whoami != 0) { close(S_fd_pipe[0]); S_son_pid = whoami; return EXIT_SUCCESS; } /* Here is the code for the son... */ { int sound_num,ch,i; struct timeval tval = {0L,0L}; fd_set readfds,dsp; Mix mix; int frag, fragsize; Channel *chan = (Channel*)malloc( sizeof(Channel)*S_num_channels ); for (i=0; i<S_num_channels; i++) Chan_reset( chan+i ); S_fd_snddev = open(S_snddev,O_WRONLY ); if(S_fd_snddev < 0) { perror("Cannot open sound device: "); exit(1); } frag = FRAG_SPEC; ioctl(S_fd_snddev, SNDCTL_DSP_SETFRAGMENT, &frag); if ( ioctl(S_fd_snddev,SNDCTL_DSP_SPEED, &S_playback_freq)==-1 ) perror("Sound driver ioctl "); fragsize=0; if ( ioctl(S_fd_snddev,SNDCTL_DSP_GETBLKSIZE, &fragsize)==-1 ) perror("Sound driver ioctl "); // init mixer Mix_alloc( &mix, fragsize ); close(S_fd_pipe[1]); FD_ZERO(&dsp); FD_SET(S_fd_snddev, &dsp); FD_ZERO(&readfds); FD_SET(S_fd_pipe[0], &readfds); printf("Sound driver initialized.\n"); for(;;) { FD_SET(S_fd_pipe[0], &readfds); tval.tv_sec=0L; tval.tv_usec=0L; select(S_fd_pipe[0]+1, &readfds,NULL,NULL,&tval); if (FD_ISSET(S_fd_pipe[0], &readfds)) { if (read(S_fd_pipe[0], &sound_num, sizeof(int))==0) break; read(S_fd_pipe[0], &ch, sizeof(int)); /* printf("chan=%d snd=%d len=%d \n", ch, sound_num, S_sounds[sound_num].len ); */ /* Find free channel for sample */ if(ch == -1) { printf("Reset %d\n", sound_num); Chan_resetSound(chan, sound_num); } else for(i = 0; i < S_num_channels; i++ ){ if (chan[i].Vleft==0){ Chan_assign( &(chan[i]), &(S_sounds[sound_num]), sound_num ); break; } } } Chan_mixAll(&mix,chan); write(S_fd_snddev, mix.Vclippedbuf, fragsize ); } Mix_dealloc( &mix ); printf("Sound process exiting..\n"); close(S_fd_pipe[0]); close(S_fd_pipe[1]); exit (0); } } // end of the function