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

MMA

Attachment 'sound3.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 	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 	        if ( n < 44000 / ( 2 * freq ) ) {
  36 			buf[2*i] = SHRT_MAX ;
  37 			buf[2*i+1] = -SHRT_MAX;
  38 		} else {
  39 			buf[2*i] = - SHRT_MAX;
  40 			buf[2*i+1] = SHRT_MAX;
  41 		}
  42 		
  43 		if ( n > 44100 / freq ){
  44 			n = -1;
  45 			i --;
  46 		}
  47 
  48 	}
  49 
  50 	for(;;){
  51 	if ( write( fd, buf, BUFSIZE ) == -1 ) {
  52 		perror( "write()" );
  53 		close( fd );
  54 		return 1;
  55 	}
  56 	}
  57 	close( fd );
  58 	return 0;
  59 }
  60 
  61 
  62 /*
  63  * /dev/dsp を以下の様に設定する。
  64  *
  65  * 量子化ビット数     : 16   bits
  66  * サンプリング周波数 : 44.1 KHz
  67  * チャンネル数       : 1
  68  * PCM データは符号付き、リトルエンディアン
  69  *
  70  */
  71 static int
  72 setup_dsp( int fd )
  73 {
  74 	int fmt     = AFMT_S16_LE;
  75 	int freq    = 44100;
  76 	int channel = 2;
  77 
  78 	/* サウンドフォーマットの設定 */
  79 	if ( ioctl( fd, SOUND_PCM_SETFMT, &fmt ) == -1 ) {
  80 		perror( "ioctl( SOUND_PCM_SETFMT )" );
  81 		return -1;
  82 	}
  83 
  84 	/* チャンネル数の設定 */
  85 	if ( ioctl( fd, SOUND_PCM_WRITE_CHANNELS, &channel ) == -1 ) {
  86 		perror( "iotcl( SOUND_PCM_WRITE_CHANNELS )" );
  87 		return -1;
  88 	}
  89 
  90 	/* サンプリング周波数の設定 */
  91 	if ( ioctl( fd, SOUND_PCM_WRITE_RATE, &freq ) == -1 ) {
  92 		perror( "iotcl( SOUND_PCM_WRITE_RATE )" );
  93 		return -1;
  94 	}
  95 
  96 	return 0;
  97 }

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.