如何使电脑发声

如何使电脑发声

声音的原理,通常,我们可以用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;
}
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值