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

MMA

Attachment 'sound4.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 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 }

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.