📄 sound.c
字号:
#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/ioctl.h>#include <stdlib.h>#include <stdio.h>#include <linux/soundcard.h>#include "sound.h"/* サウンドデバイスのファイルデスクリプタ */int g_fdp;int g_fdr;int open_soundr(void){ int arg; /* I/Oコントロ〖ル脱の恃眶 */ int status; /* システムコ〖ルの觉轮を手す恃眶 */ /* サウンドデバイスを倡く */ g_fdr = open("/dev/dsp", O_RDONLY); if (g_fdr < 0){ perror("open of /dev/dsp failed"); return -1; } /* サンプリングパラメ〖タを肋年 */ arg = AFMT_S16_NE; /* オ〖ディオフォ〖マット */ status = ioctl(g_fdr, SNDCTL_DSP_SETFMT, &arg); if (status == -1) perror("SNDCTL_DSP_SETFMT ioctl failed"); if (arg != AFMT_S16_NE) perror("unable to set FORMAT"); /* サンプリングサイズ */ arg = SIZE; status = ioctl(g_fdr, SOUND_PCM_READ_BITS, &arg); if (status == -1) perror("SOUND_PCM_READ_BITS ioctl failed"); if (arg != SIZE) perror("READ unable to set sample size"); /* モノラルかステレオか */ arg = CHANNELS; status = ioctl(g_fdr, SOUND_PCM_READ_CHANNELS, &arg); if (status == -1) perror("SOUND_PCM_READ_CHANNELS ioctl failed"); if (arg != CHANNELS) perror("READ unable to set number of channels"); arg = RATE; /* サンプリングレ〖ト */ status = ioctl(g_fdr, SOUND_PCM_READ_RATE, &arg); if (status == -1) perror("SOUND_PCM_READ_RATE ioctl failed"); return 0; }int open_soundp(){ int arg; /* I/Oコントロ〖ル脱の恃眶 */ int status; /* システムコ〖ルの觉轮を手す恃眶 */ /* サウンドデバイスを倡く */ g_fdp = open("/dev/dsp", O_WRONLY); if (g_fdp < 0) { perror("open of /dev/dsp failed"); return -1; } /* サンプリングパラメ〖タを肋年 */ arg = AFMT_S16_NE; /* オ〖ディオフォ〖マット */ status = ioctl(g_fdp, SNDCTL_DSP_SETFMT, &arg); if(status == -1) perror("SND_DSP_SETFMT ioctl failed"); if (arg != AFMT_S16_NE) perror("unable to set FOMAT"); arg = SIZE; /* サンプリングサイズ */ status = ioctl(g_fdp, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_BITS ioctl failed"); if (arg != SIZE) perror("WRITE unable to set sample size"); arg = CHANNELS; /* モノラルかステレオか */ status = ioctl(g_fdp, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); if (arg != CHANNELS) perror("WRITE unable to set number of channels"); arg = RATE; /* サンプリングレ〖ト */ status = ioctl(g_fdp, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) perror("SOUND_PCM_WRITE_WRITE ioctl failed"); return 0; }int play_sound(short* buf){ int status; /* システムコ〖ルの觉轮を手す恃眶 */ status = write(g_fdp, buf, AUDIO_BUFF_SIZE*2); /* 不兰を峡不 */ if (status != AUDIO_BUFF_SIZE*2){ return 1; } status = ioctl(g_fdp, SOUND_PCM_SYNC, 0 ); if (status == -1){ perror("SOUND_PCM_SYNC ioctl failed"); return 1; } return 0;}int record_sound(short* buf){ int status; /* システムコ〖ルの觉轮を手す恃眶 */ status = read(g_fdr, buf, AUDIO_BUFF_SIZE*2); /* 不兰を峡不 */ if (status != AUDIO_BUFF_SIZE*2){ return 1; } return 0;}int close_soundp(void){ close(g_fdp); /* サウンドデバイスを誓じる */ return 0;}int close_soundr(void){ close(g_fdr); /* サウンドデバイスを誓じる */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -