添付ファイル 'sound4.c'
ダウンロード 1 #include <fcntl.h>
2 #include <limits.h>
3 #include <linux/soundcard.h>
4 #include <math.h>
5 #include <stdio.h>
6 #include <sys/ioctl.h>
7 #include <unistd.h>
8
9 #define BUFSIZE 441000
10
11 static int setup_dsp( int fd );
12
13 int main( void )
14 {
15 double freq = 600;
16 double value, slope;
17 short buf[ 2*BUFSIZE / sizeof(short) ];
18 int fd, i, n;
19
20 if ( ( fd = open( "/dev/dsp", O_WRONLY ) ) == -1 ) {
21 perror( "open()" );
22 return 1;
23 }
24
25
26 /* /dev/dsp の設定 */
27 if ( setup_dsp( fd ) != 0 ) {
28 fprintf( stderr, "Setup /dev/dsp failed.\n" );
29 close( fd );
30 return 1;
31 }
32
33
34 /* 再生用の正弦波データ作成 */
35 slope = SHRT_MAX / ( 44100 / (4 * freq) );
36
37 for ( i = 0, n = 0 ; i < BUFSIZE / sizeof(short); i ++ , n ++ ) {
38
39 if ( n < 44100 / ( 2 * freq ) ) {
40 buf[2*i] = 0.9*(-SHRT_MAX + slope * n); // 左ch,三角波
41 buf[2*i+1] = 0.9*(-SHRT_MAX + slope * n); // 右ch,のこぎり波
42 } else {
43 buf[2*i] = 0.9*(3 * SHRT_MAX - slope * n);
44 buf[2*i+1] = 0.9*(-3*SHRT_MAX + slope * n);
45 }
46
47 if ( n > 44100 / freq ){
48 n = 0;
49 i --;
50 }
51
52 }
53
54 for(;;){
55 if ( write( fd, buf, BUFSIZE ) == -1 ) {
56 perror( "write()" );
57 close( fd );
58 return 1;
59 }
60 }
61 close( fd );
62 return 0;
63 }
64
65 static int
66 setup_dsp( int fd )
67 {
68 int fmt = AFMT_S16_LE;
69 int freq = 44100;
70 int channel = 2;
71
72 /* サウンドフォーマットの設定 */
73 if ( ioctl( fd, SOUND_PCM_SETFMT, &fmt ) == -1 ) {
74 perror( "ioctl( SOUND_PCM_SETFMT )" );
75 return -1;
76 }
77
78 /* チャンネル数の設定 */
79 if ( ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) {
80 perror( "iotcl( SOUND_PCM_WRITE_CHANNELS )" );
81 return -1;
82 }
83
84 /* サンプリング周波数の設定 */
85 if ( ioctl( fd, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) {
86 perror( "iotcl( SOUND_PCM_WRITE_RATE )" );
87 return -1;
88 }
89
90 return 0;
91 }
添付ファイル
添付ファイルを参照するには、(下のファイル一覧にあるように)attachment:filenameと記述します。 [get]リンクのURLは変更される可能性が高いので、利用しないでください。ファイルを添付する権限がありません。