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

MMA

添付ファイル 'sound6.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 	int time=100; // 1つの直線に使うサンプル数
  16 	short  buf[ 2*BUFSIZE / sizeof(short) ];
  17 	int    fd, i, n;
  18 
  19 	if ( ( fd = open( "/dev/dsp", O_WRONLY ) ) == -1 ) {
  20 		perror( "open()" );
  21 		return 1;
  22 	}
  23 
  24 
  25 	/* /dev/dsp の設定 */
  26 	if ( setup_dsp( fd ) != 0 ) {
  27 		fprintf( stderr, "Setup /dev/dsp failed.\n" );
  28 		close( fd );
  29 		return 1;
  30 	}
  31 	
  32 	
  33 	/* 再生用の正弦波データ作成 */
  34 	for ( i = 0, n = 0 ; i < BUFSIZE / sizeof(short); i ++ , n ++ ) {
  35 		
  36 	        if ( n < time ) {
  37 			buf[2*i] = SHRT_MAX*sin(M_PI*18/180)*n/time; // 左ch
  38 			buf[2*i+1] = SHRT_MAX - SHRT_MAX*cos(M_PI*18/180)*n/time; // 右ch
  39 		}
  40 		else if ( time <= n && n < 2*time ) {
  41 			buf[2*i] = buf[2*(time-1)] - SHRT_MAX*cos(M_PI*36/180)*(n-time)/time  ;
  42 			buf[2*i+1] = buf[2*(time-1)+1] + SHRT_MAX*sin(M_PI*36/180)*(n-time)/time ;
  43 		}
  44                 else if ( 2*time <= n && n < 3*time ) {
  45                         buf[2*i] = buf[2*(2*time-1)] + SHRT_MAX*sin(M_PI*90/180)*(n-2*time)/time  ;
  46                         buf[2*i+1] = buf[2*(2*time-1)+1] - SHRT_MAX*cos(M_PI*90/180)*(n-2*time)/time ;
  47                 }
  48 		else if ( 3*time <= n && n < 4*time ) {
  49                         buf[2*i] = buf[2*(3*time-1)] - SHRT_MAX*cos(M_PI*36/180)*(n-3*time)/time  ;
  50                         buf[2*i+1] = buf[2*(3*time-1)+1] - SHRT_MAX*sin(M_PI*36/180)*(n-3*time)/time ;
  51                 }
  52 		else if ( 4*time <= n && n < 5*time ) {
  53                         buf[2*i] = buf[2*(4*time-1)] + SHRT_MAX*sin(M_PI*18/180)*(n-4*time)/time  ;
  54                         buf[2*i+1] = buf[2*(4*time-1)+1] + SHRT_MAX*cos(M_PI*18/180)*(n-4*time)/time ;
  55 		}
  56 		else {
  57 			n = -1;
  58 			i --;	
  59 		}
  60 	}
  61 
  62 	for(;;){
  63 	if ( write( fd, buf, BUFSIZE ) == -1 ) {
  64 		perror( "write()" );
  65 		close( fd );
  66 		return 1;
  67 	}
  68 	}
  69 	close( fd );
  70 	return 0;
  71 }
  72 
  73 
  74 static int
  75 setup_dsp( int fd )
  76 {
  77 	int fmt     = AFMT_S16_LE;
  78 	int freq    = 44100;
  79 	int channel = 2;
  80 
  81 	/* サウンドフォーマットの設定 */
  82 	if ( ioctl( fd, SOUND_PCM_SETFMT, &fmt ) == -1 ) {
  83 		perror( "ioctl( SOUND_PCM_SETFMT )" );
  84 		return -1;
  85 	}
  86 
  87 	/* チャンネル数の設定 */
  88 	if ( ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) {
  89 		perror( "iotcl( SOUND_PCM_WRITE_CHANNELS )" );
  90 		return -1;
  91 	}
  92 
  93 	/* サンプリング周波数の設定 */
  94 	if ( ioctl( fd, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) {
  95 		perror( "iotcl( SOUND_PCM_WRITE_RATE )" );
  96 		return -1;
  97 	}
  98 
  99 	return 0;
 100 }

添付ファイル

添付ファイルを参照するには、(下のファイル一覧にあるように)attachment:filenameと記述します。 [get]リンクのURLは変更される可能性が高いので、利用しないでください。
 All files | Selected Files: delete move to page copy to page

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