无法打开/dev/dsp

标签 c ubuntu audio-recording

我想学习录音,所以我编译了下面网上找到的代码,但是由于某些原因我打不开/dev/dsp。我尝试关闭 pulseaudio,但我一 killall 它就重新打开了。我真的不知道我在做什么;这周后半段我一直在挣扎。请帮忙。

我也试过 chmod o+rw/dev/dsp

/*
 * parrot.c
 * Program to illustrate /dev/dsp device
 * Records several seconds of sound, then echoes it back.
 * Runs until Control-C is pressed.
 */
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdlib.h> 
#include <stdio.h>
#include <linux/soundcard.h>

#define LENGTH 3    /* how many seconds of speech to store */
#define RATE 8000   /* the sampling rate */
#define SIZE 8      /* sample size: 8 or 16 bits */
#define CHANNELS 1  /* 1 = mono 2 = stereo */

/* this buffer holds the digitized audio */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int main()
{
  int fd;       /* sound device file descriptor */
  int arg;      /* argument for ioctl calls */
  int status;   /* return status of system calls */

  /* open sound device */
  fd = open("/dev/dsp", O_RDWR);
  if (fd < 0) {
    perror("open of /dev/dsp failed");
    exit(1);
  }

  /* set sampling parameters */
  arg = SIZE;      /* sample size */
  status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_BITS ioctl failed");
  if (arg != SIZE)
    perror("unable to set sample size");

  arg = CHANNELS;  /* mono or stereo */
  status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
  if (arg != CHANNELS)
    perror("unable to set number of channels");

  arg = RATE;      /* sampling rate */
  status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
  if (status == -1)
    perror("SOUND_PCM_WRITE_WRITE ioctl failed");

  while (1) { /* loop until Control-C */
    printf("Say something:\n");
    status = read(fd, buf, sizeof(buf)); /* record some sound */
    if (status != sizeof(buf))
      perror("read wrong number of bytes");
    printf("You said:\n");
    status = write(fd, buf, sizeof(buf)); /* play it back */
    if (status != sizeof(buf))
      perror("wrote wrong number of bytes");
    /* wait for playback to complete before recording again */
    status = ioctl(fd, SOUND_PCM_SYNC, 0); 
  if (status == -1)
    perror("SOUND_PCM_SYNC ioctl failed");
  }
}   

最佳答案

请看,/dev/dsp 用于旧版本的 ubuntu linux。它在较新版本的 ubuntu 中不再可用。我认为它在 10.04 中消失了。事情从 10.10 开始发生了变化。

来自其中一个网站:

10.10 Maverick finally disabled the very old OSS drivers (which provided /dev/dsp, so the padsp wrapper is the easiest way to handle if it you can't select ALSA or PulseAudio directly.

查看此 http://manpages.ubuntu.com/manpages/hardy/man1/padsp.1.html

关于无法打开/dev/dsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16756178/

相关文章:

c - 程序无法运行可能是因为段错误

c - 如何在多线程中使用 printf()

java.lang.InternalError : Can't connect to X11 window server for JVisualVM profiling session 错误

.net - 将两个音频文件合并为一个文件,然后保存在磁盘中

javascript - float 数组的音调检测

c - 通过引用访问 va_arg 宏是错误的风格还是不正确的?

C 控制台输出不连续,等待程序完成

reactjs - 将客户端和服务器部署到同一个虚拟机

javascript - nodejs process.hrtime() 挂掉是什么原因?

windows - 如何从应用程序中捕获发送到扬声器的音频数据?