void esd_audio_close() { /* * I guess this chunk of code is meant to make sure that * everything that was sent to the output got written * - let's leave that in for now, but it could cause a * delay when the user hits stop * -KDT */ if (esd_audio_fd >= 0) { fd_set write_fds; FD_ZERO(&write_fds); FD_SET(esd_audio_fd, &write_fds); ALsetfillpoint(outaudioport, ESD_BUF_SIZE * 2); select(esd_audio_fd + 1, NULL, &write_fds, NULL, NULL); } if (outaudioport != (ALport) 0) { ALcloseport(outaudioport); outaudioport = (ALport) 0; } if (inaudioport != (ALport) 0) { ALcloseport(inaudioport); inaudioport = (ALport) 0; } }
int esd_audio_read(void *buffer, int buf_size) { ALsetparams(AL_DEFAULT_DEVICE, (rate_params + 2), 2); if (ALreadsamps(inaudioport, buffer, buf_size / 2) == 0) { ALsetfillpoint(inaudioport, ESD_BUF_SIZE); return buf_size; } else return 0; }
int esd_audio_write(void *buffer, int buf_size) { ALsetparams(AL_DEFAULT_DEVICE, rate_params, 2); if (ALwritesamps(outaudioport, buffer, buf_size / 2) == 0) { ALsetfillpoint(outaudioport, ESD_BUF_SIZE); return buf_size; } else return 0; }
int esd_audio_open() { ALconfig audioconfig; audioconfig = ALnewconfig(); rate_params[1] = esd_audio_rate; rate_params[3] = esd_audio_rate; if (!audioconfig) { printf( "Couldn't initialize new audio config\n" ); esd_audio_fd = -1; return esd_audio_fd; } else { long pvbuf[] = { AL_OUTPUT_COUNT, 0, AL_MONITOR_CTL, 0, AL_OUTPUT_RATE, 0 }; if (ALgetparams(AL_DEFAULT_DEVICE, pvbuf, 6) < 0) if (oserror() == AL_BAD_DEVICE_ACCESS) { esd_audio_fd = -1; return esd_audio_fd; } if (pvbuf[1] == 0 && pvbuf[3] == AL_MONITOR_OFF) { ALsetparams(AL_DEFAULT_DEVICE, rate_params, 2); } else if (pvbuf[5] != esd_audio_rate) { printf("audio device is already in use with wrong sample output rate\n"); esd_audio_fd = -1; return esd_audio_fd; } /* ALsetsampfmt(audioconfig, AL_SAMPFMT_TWOSCOMP); this is the default */ /* ALsetwidth(audioconfig, AL_SAMPLE_16); this is the default */ if ( (esd_audio_format & ESD_MASK_CHAN) == ESD_MONO) ALsetchannels(audioconfig, AL_MONO); /* else ALsetchannels(audioconfig, AL_STEREO); this is the default */ ALsetqueuesize(audioconfig, ESD_BUF_SIZE * 2); outaudioport = ALopenport("esd", "w", audioconfig); if (outaudioport == (ALport) 0) { switch (oserror()) { case AL_BAD_NO_PORTS: printf( "system is out of ports\n"); esd_audio_fd = -1; return esd_audio_fd; break; case AL_BAD_DEVICE_ACCESS: printf("couldn't access audio device\n"); esd_audio_fd = -1; return esd_audio_fd; break; case AL_BAD_OUT_OF_MEM: printf("out of memory\n"); esd_audio_fd = -1; return esd_audio_fd; break; } /* don't know how we got here, but it must be bad */ esd_audio_fd = -1; return esd_audio_fd; } ALsetfillpoint(outaudioport, ESD_BUF_SIZE); esd_audio_fd = ALgetfd(outaudioport); /* * If we are recording, open a second port to read from * and return that fd instead */ if ( (esd_audio_format & ESD_MASK_FUNC) == ESD_RECORD ) { inaudioport = ALopenport("esd", "r", audioconfig); if (inaudioport == (ALport) 0) { switch (oserror()) { case AL_BAD_NO_PORTS: printf( "system is out of ports\n"); esd_audio_fd = -1; return esd_audio_fd; break; case AL_BAD_DEVICE_ACCESS: printf("couldn't access audio device\n"); esd_audio_fd = -1; return esd_audio_fd; break; case AL_BAD_OUT_OF_MEM: printf("out of memory\n"); esd_audio_fd = -1; return esd_audio_fd; break; default: printf( "Unknown error opening port\n" ); } /* don't know how we got here, but it must be bad */ esd_audio_fd = -1; return esd_audio_fd; } ALsetfillpoint(inaudioport, ESD_BUF_SIZE); ALsetparams(AL_DEFAULT_DEVICE, (rate_params + 2), 2); esd_audio_fd = ALgetfd(inaudioport); } } return esd_audio_fd; }
void MainLoop(ALport alp, FileDescriptor dacfd, FileDescriptor sockfd, SynthState *v1, SynthState *v2) { /* int hwm = 300, lwm = 256; */ int hwm = 1000, lwm = 800; fd_set read_fds, write_fds; /* largest file descriptor to search for */ int nfds = BIGGER_OF(dacfd, sockfd) + 1; printf("MainLoop: dacfd %d, sockfd %d, nfds %d\n", dacfd, sockfd, nfds); time_to_quit = 0; sigset(SIGINT, catch_sigint); /* set sig handler */ while(!time_to_quit) { /* compute sine wave samples while the sound output buffer is below the high water mark */ while (ALgetfilled(alp) < hwm) { Synthesize(alp, v1, v2); } /* Figure out the time tag corresponding to the time in the future that we haven't computed any samples for yet. */ OSCInvokeAllMessagesThatAreReady(OSCTT_PlusSeconds(OSCTT_CurrentTime(), ALgetfilled(alp) / the_sample_rate)); /* set the low water mark, i.e. when we want control from select(2) */ ALsetfillpoint(alp, OUTPUTQUEUESIZE - lwm); /* set up select */ FD_ZERO(&read_fds); /* clear read_fds */ FD_ZERO(&write_fds); /* clear write_fds */ FD_SET(dacfd, &write_fds); FD_SET(sockfd, &read_fds); FD_SET(0, &read_fds); /* stdin */ /* give control back to OS scheduler to put us to sleep until the DAC queue drains and/or a character is available from standard input */ if (select(nfds, &read_fds, &write_fds, (fd_set * )0, (struct timeval *)0) < 0) { /* select reported an error */ perror("bad select"); goto quit; } if(FD_ISSET(sockfd, &read_fds)) { ReceivePacket(sockfd); } /* is there a character in the queue? */ if (FD_ISSET(0, &read_fds)) { /* this will never block */ char c = getchar(); if (c == 'q') { /* quit */ break; } else if ((c <= '9') && (c >= '0')) { /* tweak frequency */ v1->f = 440.0 + 100.0 * (c - '0'); } } } quit: ALcloseport(alp); closeudp(sockfd); }
int sgi_audio_open(audio_desc_t ad, audio_format* ifmt, audio_format *ofmt) { ALconfig c; long cmd[8]; if (audio_fd != -1) { sgi_audio_close(ad); } if (ifmt->encoding != DEV_S16) return FALSE; if ((c = ALnewconfig()) == NULL) { fprintf(stderr, "ALnewconfig error\n"); exit(1); } switch(ifmt->channels) { case 1: ALsetchannels(c, AL_MONO); break; case 2: ALsetchannels(c, AL_STEREO); break; default: sgi_audio_close(ad); } ALsetwidth(c, AL_SAMPLE_16); ALsetqueuesize(c, QSIZE); ALsetsampfmt(c, AL_SAMPFMT_TWOSCOMP); if ((wp = ALopenport("RAT write", "w", c)) == NULL) { fprintf(stderr, "ALopenport (write) error\n"); sgi_audio_close(ad); return FALSE; } if ((rp = ALopenport("RAT read", "r", c)) == NULL) { fprintf(stderr, "ALopenport (read) error\n"); sgi_audio_close(ad); return FALSE; } cmd[0] = AL_OUTPUT_RATE; cmd[1] = ofmt->sample_rate; cmd[2] = AL_INPUT_RATE; cmd[3] = ifmt->sample_rate; cmd[4] = AL_MONITOR_CTL; cmd[5] = AL_MONITOR_OFF; /*cmd[6] = AL_INPUT_SOURCE;*/ /*cmd[7] = AL_INPUT_MIC;*/ if (ALsetparams(AL_DEFAULT_DEVICE, cmd, 6L/*was 8L*/) == -1) { fprintf(stderr, "audio_open/ALsetparams error\n"); sgi_audio_close(ad); } /* Get the file descriptor to use in select */ audio_fd = ALgetfd(rp); if (ALsetfillpoint(rp, ifmt->bytes_per_block) < 0) { debug_msg("ALsetfillpoint failed (%d samples)\n", ifmt->bytes_per_block); } bytes_per_block = ifmt->bytes_per_block; /* We probably should free the config here... */ return TRUE; }