static void vox_configure (struct sound_device *sd) { int val; #ifdef USABLE_SIGIO sigset_t oldset, blocked; #endif eassert (sd->fd >= 0); /* On GNU/Linux, it seems that the device driver doesn't like to be interrupted by a signal. Block the ones we know to cause troubles. */ turn_on_atimers (0); #ifdef USABLE_SIGIO sigemptyset (&blocked); sigaddset (&blocked, SIGIO); pthread_sigmask (SIG_BLOCK, &blocked, &oldset); #endif val = sd->format; if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0 || val != sd->format) sound_perror ("Could not set sound format"); val = sd->channels != 1; if (ioctl (sd->fd, SNDCTL_DSP_STEREO, &val) < 0 || val != (sd->channels != 1)) sound_perror ("Could not set stereo/mono"); /* I think bps and sampling_rate are the same, but who knows. Check this. and use SND_DSP_SPEED for both. */ if (sd->sample_rate > 0) { val = sd->sample_rate; if (ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->sample_rate) < 0) sound_perror ("Could not set sound speed"); else if (val != sd->sample_rate) sound_warning ("Could not set sample rate"); } if (sd->volume > 0) { int volume = sd->volume & 0xff; volume |= volume << 8; /* This may fail if there is no mixer. Ignore the failure. */ ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &volume); } turn_on_atimers (1); #ifdef USABLE_SIGIO pthread_sigmask (SIG_SETMASK, &oldset, 0); #endif }
static void vox_write (struct sound_device *sd, const char *buffer, int nbytes) { int nwritten = emacs_write (sd->fd, buffer, nbytes); if (nwritten < 0) sound_perror ("Error writing to sound device"); }
static void au_play (struct sound *s, struct sound_device *sd) { struct au_header *header = (struct au_header *) s->header; sd->sample_size = 0; sd->sample_rate = header->sample_rate; sd->bps = 0; sd->channels = header->channels; sd->choose_format (sd, s); sd->configure (sd); if (STRINGP (s->data)) sd->write (sd, SSDATA (s->data) + header->data_offset, SBYTES (s->data) - header->data_offset); else { int blksize = sd->period_size ? sd->period_size (sd) : 2048; char *buffer; int nbytes; /* Seek */ lseek (s->fd, header->data_offset, SEEK_SET); /* Copy sound data to the device. */ buffer = (char *) alloca (blksize); while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0) sd->write (sd, buffer, nbytes); if (nbytes < 0) sound_perror ("Error reading sound file"); } }
static void vox_open (struct sound_device *sd) { /* Open the sound device (eg /dev/dsp). */ char const *file = string_default (sd->file, DEFAULT_SOUND_DEVICE); sd->fd = emacs_open (file, O_WRONLY, 0); if (sd->fd < 0) sound_perror (file); }
static void vox_open (struct sound_device *sd) { const char *file; /* Open the sound device. Default is /dev/dsp. */ if (sd->file) file = sd->file; else file = DEFAULT_SOUND_DEVICE; sd->fd = emacs_open (file, O_WRONLY, 0); if (sd->fd < 0) sound_perror (file); }
static void wav_play (struct sound *s, struct sound_device *sd) { struct wav_header *header = (struct wav_header *) s->header; /* Let the device choose a suitable device-dependent format for the file. */ sd->choose_format (sd, s); /* Configure the device. */ sd->sample_size = header->sample_size; sd->sample_rate = header->sample_rate; sd->bps = header->bytes_per_second; sd->channels = header->channels; sd->configure (sd); /* Copy sound data to the device. The WAV file specification is actually more complex. This simple scheme worked with all WAV files I found so far. If someone feels inclined to implement the whole RIFF-WAVE spec, please do. */ if (STRINGP (s->data)) sd->write (sd, SSDATA (s->data) + sizeof *header, SBYTES (s->data) - sizeof *header); else { char *buffer; int nbytes = 0; int blksize = sd->period_size ? sd->period_size (sd) : 2048; int data_left = header->data_length; buffer = (char *) alloca (blksize); lseek (s->fd, sizeof *header, SEEK_SET); while (data_left > 0 && (nbytes = emacs_read (s->fd, buffer, blksize)) > 0) { /* Don't play possible garbage at the end of file */ if (data_left < nbytes) nbytes = data_left; data_left -= nbytes; sd->write (sd, buffer, nbytes); } if (nbytes < 0) sound_perror ("Error reading sound file"); } }
static void vox_write (struct sound_device *sd, const char *buffer, EMACS_INT nbytes) { if (emacs_write (sd->fd, buffer, nbytes) != nbytes) sound_perror ("Error writing to sound device"); }
int esd_play_sound_data (Binbyte *data, size_t length, int UNUSED (vol)) { /* #### FIXME: vol is ignored */ size_t (*parsesndfile)(void **dayta,size_t *sz,void **outbuf); size_t (*sndcnv)(void **dayta,size_t *sz,void **); fmtType ffmt; int fmt,speed,tracks; unsigned char *pptr,*optr,*cptr,*sptr; Bytecount wrtn, crtn; size_t prtn; int flags, sock; /* analyze_format needs at least this many bytes to work with */ if (length < HEADERSZ) return 0; ffmt = analyze_format(data,&fmt,&speed,&tracks,&parsesndfile); if (ffmt != fmtRaw && ffmt != fmtSunAudio && ffmt != fmtWave) { sound_warn("Unsupported file format (neither RAW, nor Sun/DECAudio, nor WAVE)"); return 0; } /* convert header information into ESD flags */ flags = ESD_STREAM|ESD_PLAY; sndcnv = sndcnvnop; switch (fmt) { case AFMT_MU_LAW: sndcnv = sndcnvULaw_2linear; flags |= ESD_BITS8; break; case AFMT_S8: sndcnv = sndcnv2unsigned; /* ESD needs unsigned bytes */ case AFMT_U8: flags |= ESD_BITS8; break; case AFMT_S16_BE: sndcnv = sndcnv16swap; /* ESD wants little endian */ case AFMT_S16_LE: flags |= ESD_BITS16; break; default: { Extbyte warn_buf[255]; sprintf (warn_buf, "byte format %d unimplemented", fmt); sound_warn (warn_buf); return 0; } } switch (tracks) { case 1: flags |= ESD_MONO; break; case 2: flags |= ESD_STEREO; break; default: { Extbyte warn_buf[255]; sprintf (warn_buf, "%d channels - only 1 or 2 supported", tracks); sound_warn (warn_buf); return 0; } } sock = esd_play_stream(flags, speed, NULL, "xemacs"); if (sock < 0) return 0; reset_parsestate(); for (pptr = data; (prtn = parsesndfile((void **)&pptr,&length, (void **)&optr)) > 0; ) for (cptr = optr; (crtn = sndcnv((void **)&cptr,&prtn, (void **)&sptr)) > 0; ) { if ((wrtn = write(sock,sptr,crtn)) < 0) { sound_perror ("write error"); goto END_OF_PLAY; } if (wrtn != crtn) { Extbyte warn_buf[255]; sprintf (warn_buf, "only wrote %ld of %ld bytes", wrtn, crtn); sound_warn (warn_buf); goto END_OF_PLAY; } } if (ffmt == fmtWave) parse_wave_complete(); END_OF_PLAY: /* Now cleanup all used resources */ close(sock); return 1; }