[转载]C语言编写的PC喇叭发音程序
信息来源:[url]http://post.baidu.com/f?kz=5417859[/url]游戏声音:
音效在游戏里是非常重要的
这里介绍用pc喇叭产生的最简单的声音,以后在给大家介绍如何驱动声卡使音箱发声
c语言函数库提供了两个声音函数 sound(),nosound()和延迟函数delay()
sound()函数中频率参数的单位是(Hz),根据测试,调用参数18Hz以上的sound()
函数可以被人耳识别,以下是一个利用循环改变声音频率的例子,发出来的生音频率从低到高
代码如下:
#include <dos.h>
main()
{
int i;
for(i=0;i<=1000;i++)
{
sound(i);
delay(1000);
nosound();
}
}
在游戏中sound()函数通常在某一游戏动画图像画好后处于延迟的时候使用
这里使用delay()延迟函数一举两得,即使游戏动画图像进行了一定时间的停留
,又使声音进行了一定时间的播放
需要提醒大家的是:
不同的机器在使用delay()延迟函数时延迟时间不同,这是因为受cpu速度的影响
最终我们将不使用delay()函数作为延迟函数。
注:自程序本人已经在WINXP SP2+TC 2.0下编译通过 注释应该在在加一条:nosound()函数不说不行,因为如果不调用他,你这破鸟叫的声音,就一直响下去。。 不过我觉得比那个什么破鸟叫还难听...
nosound()
我在dos.h中找它的原型是
nosound (void);
不太明白它的意思
那位解释一下 VC下失败,不知道是我的问题,还是编译器的问题~
我家的VC经常出错,而且没有提示~(办公室的就好用多了~) [quote][b]下面是引用iller_k于07-30-2005 14:24发表的:[/b]
VC下失败,不知道是我的问题,还是编译器的问题~
我家的VC经常出错,而且没有提示~(办公室的就好用多了~)[/quote]
tc! 我好象在哪个课本里看见过。忘了,支持~ [quote][b]下面是引用clyfish于2005-07-30 14:59发表的:[/b]
tc![/quote]
MFC是完全兼容TC的,我想是关于函数库的问题吧。TC有哪些默认的函数库?换句话说在MFC下用SOUND函数需要哪个函数库?请赐教,不甚感谢…… 头回见C版的!以前见过pascal版的!不说别的!先收藏了! <p>dos.h 这个库可否share出来 </p><p><font size="2">sound(i);<br />delay(1000);<br />nosound();</font><br /></p><p>这几个函数 也不知道究竟干什么</p> 偶就舍生取义了 共享啦。
/* dos.h
Defines structs, unions, macros, and functions for dealing
with MSDOS and the Intel iAPX86 microprocessor family.
Copyright (c) Borland International 1987,1988
All Rights Reserved.
*/
#if __STDC__
#define _Cdecl
#else
#define _Cdecl cdecl
#endif
#if !defined(__DOS_DEF_)
#define __DOS_DEF_
/* Variables */
extern int _Cdecl _8087;
extern int _Cdecl _argc;
extern char **_Cdecl _argv;
extern char **_Cdecl environ;
extern int _Cdecl _doserrno;
extern unsigned _Cdecl _heaplen;
extern unsigned char _Cdecl _osmajor;
extern unsigned char _Cdecl _osminor;
extern unsigned _Cdecl _psp;
extern unsigned _Cdecl _stklen;
extern unsigned _Cdecl _version;
#define FA_RDONLY 0x01 /* Read only attribute */
#define FA_HIDDEN 0x02 /* Hidden file */
#define FA_SYSTEM 0x04 /* System file */
#define FA_LABEL 0x08 /* Volume label */
#define FA_DIREC 0x10 /* Directory */
#define FA_ARCH 0x20 /* Archive */
#define NFDS 20 /* Maximum number of fds */
struct fcb {
char fcb_drive; /* 0 = default, 1 = A, 2 = B */
char fcb_name[8]; /* File name */
char fcb_ext[3]; /* File extension */
short fcb_curblk; /* Current block number */
short fcb_recsize; /* Logical record size in bytes */
long fcb_filsize; /* File size in bytes */
short fcb_date; /* Date file was last written */
char fcb_resv[10]; /* Reserved for DOS */
char fcb_currec; /* Current record in block */
long fcb_random; /* Random record number */
};
struct xfcb {
char xfcb_flag; /* Contains 0xff to indicate xfcb */
char xfcb_resv[5]; /* Reserved for DOS */
char xfcb_attr; /* Search attribute */
struct fcb xfcb_fcb; /* The standard fcb */
};
struct country {
int co_date;
char co_curr[5];
char co_thsep[2];
char co_desep[2];
char co_dtsep[2];
char co_tmsep[2];
char co_currstyle;
char co_digits;
char co_time;
long co_case;
char co_dasep[2];
char co_fill[10];
};
struct DOSERROR {
int exterror;
char class;
char action;
char locus;
};
struct dfree {
unsigned df_avail;
unsigned df_total;
unsigned df_bsec;
unsigned df_sclus;
};
struct fatinfo {
char fi_sclus;
char fi_fatid;
int fi_nclus;
int fi_bysec;
};
struct devhdr {
long dh_next; /* Next device pointer */
short dh_attr; /* Attributes */
unsigned short dh_strat; /* Driver strategy routine */
unsigned short dh_inter; /* Driver interrupt routine */
char dh_name[8]; /* Device name */
};
struct time {
unsigned char ti_min; /* Minutes */
unsigned char ti_hour; /* Hours */
unsigned char ti_hund; /* Hundredths of seconds */
unsigned char ti_sec; /* Seconds */
};
struct date {
int da_year; /* Year - 1980 */
char da_day; /* Day of the month */
char da_mon; /* Month (1 = Jan) */
};
struct WORDREGS {
unsigned int ax, bx, cx, dx, si, di, cflag, flags;
};
struct BYTEREGS {
unsigned char al, ah, bl, bh, cl, ch, dl, dh;
};
union REGS {
struct WORDREGS x;
struct BYTEREGS h;
};
struct SREGS {
unsigned int es;
unsigned int cs;
unsigned int ss;
unsigned int ds;
};
struct REGPACK {
unsigned r_ax, r_bx, r_cx, r_dx;
unsigned r_bp, r_si, r_di, r_ds, r_es, r_flags;
};
#define FP_OFF(fp) ((unsigned)(fp))
#define FP_SEG(fp) ((unsigned)((unsigned long)(fp) >> 16))
typedef struct {
char drive; /* do not change */
char pattern [13]; /* these fields, */
char reserved [7]; /* Microsoft reserved */
char attrib;
short time;
short date;
long size;
char nameZ [13]; /* result of the search, asciiz */
} dosSearchInfo; /* used with DOS functions 4E, 4F */
int _Cdecl absread (int drive, int nsects, int lsect, void *buffer);
int _Cdecl abswrite(int drive, int nsects, int lsect, void *buffer);
int _Cdecl allocmem(unsigned size, unsigned *segp);
int _Cdecl bdos (int dosfun, unsigned dosdx, unsigned dosal);
int _Cdecl bdosptr (int dosfun, void *argument, unsigned dosal);
struct country *_Cdecl country (int xcode, struct country *cp);
void _Cdecl ctrlbrk (int _Cdecl (*handler)(void));
void _Cdecl delay (unsigned milliseconds);
void _Cdecl disable (void);
int _Cdecl dosexterr (struct DOSERROR *eblkp);
long _Cdecl dostounix (struct date *d, struct time *t);
void __emit__();
void _Cdecl enable (void);
int _Cdecl freemem (unsigned segx);
int _Cdecl getcbrk (void);
void _Cdecl getdate (struct date *datep);
void _Cdecl getdfree(unsigned char drive, struct dfree *dtable);
void _Cdecl getfat (unsigned char drive, struct fatinfo *dtable);
void _Cdecl getfatd (struct fatinfo *dtable);
unsigned _Cdecl getpsp (void);
int _Cdecl getswitchar (void);
void _Cdecl gettime (struct time *timep);
void interrupt (* _Cdecl getvect(int interruptno)) ();
int _Cdecl getverify (void);
void _Cdecl harderr (int _Cdecl (*handler)());
void _Cdecl hardresume (int axret);
void _Cdecl hardretn(int retn);
int _Cdecl inport (int portid);
unsigned char _Cdecl inportb(int portid);
int _Cdecl int86 (int intno, union REGS *inregs, union REGS *outregs);
int _Cdecl int86x (int intno, union REGS *inregs, union REGS *outregs,
struct SREGS *segregs);
int _Cdecl intdos (union REGS *inregs, union REGS *outregs);
int _Cdecl intdosx (union REGS *inregs, union REGS *outregs,
struct SREGS *segregs);
void _Cdecl intr (int intno, struct REGPACK *preg);
void _Cdecl keep (unsigned char status, unsigned size);
void _Cdecl nosound (void);
void _Cdecl outport (int portid, int value);
void _Cdecl outportb(int portid, unsigned char value);
char *_Cdecl parsfnm (const char *cmdline, struct fcb *fcb, int opt);
int _Cdecl peek (unsigned segment, unsigned offset);
char _Cdecl peekb (unsigned segment, unsigned offset);
void _Cdecl poke (unsigned segment, unsigned offset, int value);
void _Cdecl pokeb (unsigned segment, unsigned offset, char value);
int _Cdecl randbrd (struct fcb *fcb, int rcnt);
int _Cdecl randbwr (struct fcb *fcb, int rcnt);
void _Cdecl segread (struct SREGS *segp);
int _Cdecl setblock(unsigned segx, unsigned newsize);
int _Cdecl setcbrk (int cbrkvalue);
void _Cdecl setdate (struct date *datep);
void _Cdecl setswitchar (char ch);
void _Cdecl settime (struct time *timep);
void _Cdecl setvect (int interruptno, void interrupt (*isr) ());
void _Cdecl setverify (int value);
void _Cdecl sleep (unsigned seconds);
void _Cdecl sound (unsigned frequency);
void _Cdecl unixtodos (long time, struct date *d, struct time *t);
int _Cdecl unlink (const char *path);
/* These are in-line functions. These prototypes just clean up
some syntax checks and code generation.
*/
void _Cdecl __cli__ (void);
void _Cdecl __sti__ (void);
unsigned char _Cdecl __inportb__(int portid);
void _Cdecl __outportb__ (int portid, unsigned char value);
void _Cdecl __int__ (int interruptnum);
#define disable() __cli__() /* Clear interrupt flag */
#define enable() __sti__() /* Set interrupt flag */
#define inportb(portid) __inportb__(portid) /* Byte IN instruction */
#define outportb(portid, v) __outportb__(portid,v)/* Byte OUT instruction */
#define geninterrupt(i) __int__(i) /* Interrupt instruction */
/* some other compilers use inp, outp for inportb, outportb */
#define inp(portid) inportb(portid)
#define outp(portid,v) outportb(portid,v)
#if !__STDC__
char far *cdecl getdta(void);
void cdecl setdta(char far *dta);
#define MK_FP(seg,ofs) ((void far *) \
(((unsigned long)(seg) << 16) | (unsigned)(ofs)))
#define poke(a,b,c) (*((int far*)MK_FP((a),(b))) = (int)(c))
#define pokeb(a,b,c) (*((char far*)MK_FP((a),(b))) = (char)(c))
#define peek(a,b) (*((int far*)MK_FP((a),(b))))
#define peekb(a,b) (*((char far*)MK_FP((a),(b))))
#endif
#endif
已经共享出来了.
其实在TC下可以查到的的.
在一个文件夹里.
嘿嘿嘿 其实这个程序还是比较好理解的.
如果搞不明白,就每次去掉几个函数.
如果停止不下来了,就用ctrl-c
嘿嘿... 你共享出来的只是头文件,申明部分,并不是函数真正的实现部分...
真正的实现是在一个.o的文件里的,具体的不记得了,不好意思....
你或者可以去csdn找找..以前csdn有一篇贴讨论过这个问题的...
gcc的话是libc.so.6
函数名: sound
功 能: 以指定频率打开PC扬声器
用 法: void sound(unsigned frequency);
程序例:
/* Emits a 7-Hz tone for 10 seconds.
Your PC may not be able to emit a 7-Hz tone. */
#include
int main(void)
{
sound(7);
delay(10000);
nosound();
return 0;
}
函数名: delay
功 能: 将程序的执行暂停一段时间(毫秒)
用 法: void delay(unsigned milliseconds);
程序例:
/* Emits a 440-Hz tone for 500 milliseconds */
#include
int main(void)
{
sound(440);
delay(500);
nosound();
return 0;
}
void nosound(void );
关闭由调用 sound而发声的扬声器。 靠,游戏哪有用这个的,都用directsound.几条破语句讨论了这么长。。。 [s:41] beep()
windows。h
页:
[1]
