Login
Immutable PageDiscussionInfoAttachments
attachment:sound5.c of mernao/hyakuman2013spring

MMA

Attachment 'sound5.c'

Download

   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 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-03-03 17:31:20, 1.8 KB) [[attachment:sound1.c]]
  • [get | view] (2021-03-03 17:31:20, 1.8 KB) [[attachment:sound2.c]]
  • [get | view] (2021-03-03 17:31:20, 1.8 KB) [[attachment:sound3.c]]
  • [get | view] (2021-03-03 17:31:20, 1.8 KB) [[attachment:sound4.c]]
  • [get | view] (2021-03-03 17:31:20, 2.1 KB) [[attachment:sound5.c]]
  • [get | view] (2021-03-03 17:31:20, 2.5 KB) [[attachment:sound6.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.