ログイン
編集不可のページディスカッション情報添付ファイル
mernao/hyakuman2013springの添付ファイル:sound2.c

MMA

添付ファイル 'sound2.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 total = 5.0, t; 
  16 	double freq_l = 600;
  17 	double freq_r = 500;
  18 	short  buf[ 2*BUFSIZE / sizeof(short) ];
  19 	int    fd, i, n;
  20 
  21 	if ( ( fd = open( "/dev/dsp", O_WRONLY ) ) == -1 ) {
  22 		perror( "open()" );
  23 		return 1;
  24 	}
  25 
  26 
  27 	/* /dev/dsp の設定 */
  28 	if ( setup_dsp( fd ) != 0 ) {
  29 		fprintf( stderr, "Setup /dev/dsp failed.\n" );
  30 		close( fd );
  31 		return 1;
  32 	}
  33 
  34 
  35 	/* 再生用の正弦波データ作成 */
  36 	for ( i = 0, n = 0; i < BUFSIZE / sizeof(short); i ++ , n ++ ) {
  37 		t = ( total / (BUFSIZE / sizeof(short)) ) * n;
  38 		buf[2*i] = SHRT_MAX * sin( 2.0 * 3.14159 * freq_l * t );
  39 		buf[2*i+1] = SHRT_MAX * sin( 2.0 * 3.14159 * freq_r * t );
  40 		if ( n > 44100 * 3 /freq_l ){
  41 			n = 0;
  42 		}
  43 	}
  44 	for(;;){
  45 		if ( write( fd, buf, BUFSIZE ) == -1 ) {
  46 			perror( "write()" );
  47 			close( fd );
  48 			return 1;
  49 		}
  50 	}
  51 	close( fd );
  52 	return 0;
  53 }
  54 
  55 
  56 /*
  57  * /dev/dsp を以下の様に設定する。
  58  *
  59  * 量子化ビット数     : 16   bits
  60  * サンプリング周波数 : 44.1 KHz
  61  * チャンネル数       : 1
  62  * PCM データは符号付き、リトルエンディアン
  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は変更される可能性が高いので、利用しないでください。
 All files | Selected Files: delete move to page copy to page

ファイルを添付する権限がありません。