添付ファイル 'sound5.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 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 < 0.5 * 44100 / freq ) {
40 buf[2*i] = 0.9*(-SHRT_MAX + slope * n); // 左ch,三角波
41 buf[2*i+1] = 0.9*SHRT_MAX; // 右ch,初めMAX
42 }
43 else if ( 0.5 * 44100 / freq <= n && n < 1.0 * 44100 / freq ) {
44 buf[2*i] = 0.9*SHRT_MAX;
45 buf[2*i+1] = 0.9*(3*SHRT_MAX - slope * n);
46 }
47 else if ( 1.0 * 44100 / freq < n && n < 1.5 * 44100 / freq ) {
48 buf[2*i] = 0.9*(5*SHRT_MAX - slope * n);
49 buf[2*i+1] = -0.9*SHRT_MAX;
50 }
51 else if ( 1.5 * 44100 / freq < n && n < 2.0 * 44100 / freq ) {
52 buf[2*i] = -0.9*SHRT_MAX;
53 buf[2*i+1] = 0.9*(-7 * SHRT_MAX + slope * n);
54 }
55 else {
56 n = -1;
57 i --;
58 }
59 }
60
61 for(;;){
62 if ( write( fd, buf, BUFSIZE ) == -1 ) {
63 perror( "write()" );
64 close( fd );
65 return 1;
66 }
67 }
68 close( fd );
69 return 0;
70 }
71
72
73 static int
74 setup_dsp( int fd )
75 {
76 int fmt = AFMT_S16_LE;
77 int freq = 44100;
78 int channel = 2;
79
80 /* サウンドフォーマットの設定 */
81 if ( ioctl( fd, SOUND_PCM_SETFMT, &fmt ) == -1 ) {
82 perror( "ioctl( SOUND_PCM_SETFMT )" );
83 return -1;
84 }
85
86 /* チャンネル数の設定 */
87 if ( ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) {
88 perror( "iotcl( SOUND_PCM_WRITE_CHANNELS )" );
89 return -1;
90 }
91
92 /* サンプリング周波数の設定 */
93 if ( ioctl( fd, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) {
94 perror( "iotcl( SOUND_PCM_WRITE_RATE )" );
95 return -1;
96 }
97
98 return 0;
99 }
添付ファイル
添付ファイルを参照するには、(下のファイル一覧にあるように)attachment:filenameと記述します。 [get]リンクのURLは変更される可能性が高いので、利用しないでください。ファイルを添付する権限がありません。