hamano #1480(2007/07/26 17:00 GMT) [ C ] Rating6/6=1.00
サンプリングレート: 8000 PCMフォーマット: 8bit % gcc sound.c -lm でコンパイル出来ます、linux で鳴るはずです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
include <stdio.h> #include <string.h> #include <math.h> #include <unistd.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/soundcard.h> #define RATE 8000 double sol_fa[] = {880.0, 987.0, 523.0, 587.0, 659.0, 698.0, 784.0}; void play(int fd, const char *str){ int i, j; unsigned char buf[RATE]; double freq; int len = strlen(str); for(i=0; i<len; i++){ if('a' <= str[i] && str[i] <= 'g') freq = sol_fa[str[i] - 'a']; else freq = 0; for (j = 0;j < RATE; j++) buf[j] = 255 * sin(2.0 * M_PI * freq * j / RATE); write(fd, buf, RATE); } } int main( void ) { int fd; int fmt = AFMT_U8; int channels = 1; int rate = RATE; fd = open("/dev/dsp", O_WRONLY); ioctl(fd, SOUND_PCM_SETFMT, &fmt); ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels); ioctl(fd, SOUND_PCM_WRITE_RATE, &rate); play(fd, "cdefedcrefgagfercrcrcrcrcdefedcr"); close(fd); return 0; }
Rating6/6=1.00-0+
1 reply [ reply ]
hamano
#1480()
[
C
]
Rating6/6=1.00
Rating6/6=1.00-0+
1 reply [ reply ]