Exemplo n.º 1
0
PcmDevice *
openPcmDevice (int errorLevel, const char *device) {
  PcmDevice *pcm;
  if ((pcm = malloc(sizeof(*pcm)))) {
    if (!*device) device = PCM_OSS_DEVICE_PATH;
    if ((pcm->fileDescriptor = open(device, O_WRONLY|O_NONBLOCK)) != -1) {
      /* Nonblocking if snd_seq_oss is loaded with nonblock_open=1.
       * There appears to be a bug in this case as write() always
       * returns the full count even though large chunks of sound are
       * missing. For now, therefore, force blocking output.
       */
      setBlockingIo(pcm->fileDescriptor, 1);

      pcm->driverVersion = 0X030000;
#ifdef OSS_GETVERSION
      if (ioctl(pcm->fileDescriptor, OSS_GETVERSION, &pcm->driverVersion) == -1)
        logMessage(errorLevel, "cannot get OSS driver version");
#endif /* OSS_GETVERSION */
      logMessage(LOG_DEBUG, "OPSS driver version: %06X", pcm->driverVersion);

      setPcmSampleRate(pcm, 8000);
      setPcmChannelCount(pcm, 1);
      return pcm;
    } else {
      logMessage(errorLevel, "cannot open PCM device: %s: %s", device, strerror(errno));
    }
    free(pcm);
  } else {
    logSystemError("PCM device allocation");
  }
  return NULL;
}
Exemplo n.º 2
0
static int
openSoundDevice (void) {
  if (!pcm) {
    if (!(pcm = openPcmDevice(LOG_WARNING, opt_pcmDevice))) return 0;

    speechParameters.nChannels = setPcmChannelCount(pcm, 1);
    speechParameters.nSampleFreq = setPcmSampleRate(pcm, 22050);
    {
      typedef struct {
        PcmAmplitudeFormat internal;
        int external;
      } FormatEntry;
      static const FormatEntry formatTable[] = {
        {PCM_FMT_S16L   , 16},
        {PCM_FMT_U8     ,  8},
        {PCM_FMT_UNKNOWN,  0}
      };
      const FormatEntry *format = formatTable;
      while (format->internal != PCM_FMT_UNKNOWN) {
        if (setPcmAmplitudeFormat(pcm, format->internal) == format->internal) break;
        ++format;
      }
      if (format->internal == PCM_FMT_UNKNOWN) {
        logMessage(LOG_WARNING, "No supported sound format.");
        closePcmDevice(pcm);
        pcm = NULL;
        return 0;
      }
      speechParameters.nBits = format->external;
    }
    logMessage(LOG_DEBUG, "Mikropuhe audio configuration: channels=%d rate=%d bits=%d",
               speechParameters.nChannels, speechParameters.nSampleFreq, speechParameters.nBits);
  }
  return 1;
}