如何使电脑发声

来源:互联网 发布:制作二维码生成器软件 编辑:程序博客网 时间:2024/06/11 09:52

如何使电脑发声

声音的原理,通常,我们可以用C语言主程序调用两个库函数来使PC机发出声音。一个库SOUND( )的功能是:接上扬声器,使它能按要求的频率发声(即给定不同的C值),另一个库函数NOSOUND( )是关掉扬声器以禁止从定时器上向扬声器的输出。下面的主程序,重复的询问用户要产生哪一种声音。程序在设计方法上采用了循环,即询问用户要听什么样声音及听多长时间,重复发音所指定的次数。每种声音都是以一种特定的速率按一种特定的顺序通过一组频率而形成的

#define FALSE 0
#define TRUE 1
#include<dos.h>
main()
{
int snd; /* which sound to produce */
int cnt; /* number of times to repeat sound */
int note; /* Current note, when sweeping frequencies */
while (TRUE) {
/* Male Sure any previous sounds ave turned off. */
nosound();
/* Ask the user for which type of sounf*/
printf ("1-siren;2-overload;3-whoop;4-phaser; 0-exit");
/* read the answer */
scanf ("%d",&snd);
/* if the answer it to exit ,do so. */
if (snd==0)
break;
/* Ask how many times to repast the sound. */
printf ("Nunger of times:");
/* get the answer */
scanf("%d",&cnt);
/* repeat the sound the number of time specifed */
while(cnt--){
/* swich on type of sound to produce*/
switch (snd)
{case 1:
/* do a siren:sweep up */
for (note=1000;note>150;note-=10)
{
sound(note);
delay(20);
}
/* Sweep down */
for (;note<1000;note+=10)
{
sound(note);
delay(20);
}
break;
case 2: /* do an overload. sweep up */
for (note=4000;note>10;note-=10)
{
sound(note);
delay(70);
}
break;
case 3:
/* do a whoop: Sweep up*/
for (note=1000;note>10;note-=10)
{
sound (note);
delay (200);
}
break;
case 4:
/* do a phaser: sweep down */
for (note=60; note<2000;note+=10 )
{
sound (note);
delay(100);
}
break;
default:
/* unknown,ask a gain */
printf ( "Invalid entry;try again /n");
break;
}
}
}
}

原创粉丝点击