Linuxプラットフォームオーディオテストプログラム


オーディオテストプログラムで、録音して再生します.コードは次のとおりです.
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LENGTH 3    /*      */
#define RATE 8000   /*      */
#define SIZE 8      /*      */
#define CHANNELS 1  /*      */

/*                  */
unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];

int fd = -1;	/*            */

static void sig_int(int signum)
{
    printf("
catch a SIGINT signal, you may be press Ctrl+C.
"); printf("ready to quit
"); if (fd > 0) { close(fd); fd = -1; } exit(0); } int main(int argc, char* argv[]) { int arg; /* ioctl */ int status; /* */ signal(SIGINT, sig_int); /* */ fd = open("/dev/dsp", O_RDWR); if (fd < 0) { perror("open of /dev/dsp failed"); exit(1); } /* */ arg = SIZE; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_BITS ioctl failed"); exit(1); } if (arg != SIZE) { perror("unable to set sample size"); exit(1); } /* */ arg = CHANNELS; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); exit(1); } if (arg != CHANNELS) { perror("unable to set number of channels"); exit(1); } /* */ arg = RATE; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_WRITE ioctl failed"); exit(1); } /* , Control-C */ while (1) { printf("Say something:
"); status = read(fd, buf, sizeof(buf)); /* */ if (status != sizeof(buf)) perror("read wrong number of bytes"); printf("You said:
"); status = write(fd, buf, sizeof(buf)); /* */ if (status != sizeof(buf)) perror("wrote wrong number of bytes"); /* */ status = ioctl(fd, SOUND_PCM_SYNC, 0); if (status == -1) perror("SOUND_PCM_SYNC ioctl failed"); } }

コードを記述したくない場合は、端末に次のコマンドを入力してテストできます.
cat/dev/dsp >/dev/dsp
PS:デバイスにはマイクとオーディオインターフェースが必要です.実測ではずっと「サラサラ」という音がした.研究を待つ.李遅2017.5.31水曜日