如何用c语言编写歌曲

如何用c语言编写歌曲,第1张

我们知道,音乐是音高和音长的有序组合,设计微机音乐最重要的就是如何定义音高和音长,以及如何让扬声器发出指定的音符。下面给出音符与频率的关系表。C语言提供的三个函数sound( )、nosound( )和clock( )可以很方便地解决上述的问题。sound( )函数可以用指定频率打开PC机扬声器直到用nosound( )函数来关闭它; clock( )函数正好用来控制发声时间,而且它不受PC机主频高低的影响。下面这段程序可使微机发出c调1的声音。

音符与频率关系表

音符 c d e f g a b

1 2 3 4 5 6 7

频率 262 294 330 349 392 440 494

音符 c d e f g a b

1 2 3 4 5 6 7

频率 523 587 659 698 784 880 988

音符 c d e f g a b

1 2 3 4 5 6 7

频率 1047 1175 1319 1397 2568 1760 1976

#include<stdio.h>

#include<dos.h>

void pause(int)

void sound1(int,int)

void main(void)

{

int i,freq,speed=5

int time=4*speed

char *qm="iddgwwwQQgfff dddfghhhggg ddgwwwqqgfff\

ddffhjqqqqq wpggjhgddgqq hhqwwqjjjggg\

ddgwwwqqqgfff ddffhjqqqqqq"/*定义歌曲*/

while (*qm++ !='\0'){

i=1

switch(*qm){

case 'k':

time=1*speedi=0

break

case 'i':

time=6*speedi=0

break

case 'o':

time=10*speedi=0

break

case 'p':

pause(time)i=0

break

case 'a':

freq=523

break

case 's':

freq=587

break

case 'd':

freq=659

break

case 'f':

freq=698

break

case 'g':

freq=784

break

case 'h':

freq=880

break

case 'j':

freq=988

break

case 'z':

freq=262

break

case 'X':

freq=294

break

case 'c':

freq=330

break

case 'v':

freq=349

break

case 'b':

freq=392

break

case 'n':

freq=440

break

case 'm':

freq=494

break

case 'q':

freq=1047

break

case 'w':

freq=1175

break

case 'e':

freq=1319

break

case 'r':

freq=1397

break

case 't':

freq=2568

break

case 'y':

freq=1760

break

case 'u':

freq=1976

break

default:

i=0

break

}

if(i)

sound1(freq,time)

}

}

void sound1(int freq,int time) /*freq为频率,time为持续时间*/

{

union{

long divisor

unsigned char c[2]

}count

unsigned char ch;

count.divisor=1193280/freq/* 1193280 是系统时钟速率*/

outp(67,182)

outp(66,count.c[0])

outp(66,count.c[1])

ch=inp(97)

outp(97,ch|3)

pause(time)

outp(97,ch)

}

void pause(int time)

{

int t1,t2

union REGS in,out

in.h.ah=0X2c

int86(0X21,&in,&out)/* 取当前时间*/

t1=t2=100*out.h.dh+out.h.dl/*out.h.dh 为秒值,out.h.dl 为1/100 秒值*/

while(t2-t1<time)

{

int86(0X21,&in,&out)

t2=100*out.h.dh+out.h.dl

if (t2<t1)t2+=6000/* 增加一分钟*/

}

}

编译能通过---------- 编译时把注释去掉

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct song{ //定义一个歌曲结构

int id//歌曲号

char title[20]//歌曲名

char singer[20]//歌手名

}

typedef struct song SONG//把结构命名为 SONG

int main( void )

{

SONG songlist[100]//定义一个存储歌曲信息的列表,长100

int i=0,j

int c

clrscr()

while (i<=100) {

printf( "please enter the song's id, title and singer:\n" )

//输入歌曲号 然后空格 输入歌曲名 然后空格 输入歌手名 然后回车

scanf( "%d%s%s", &songlist[i].id, songlist[i].title, songlist[i].singer)

i++

//每次输入完一条信息后,把列表中的歌曲信息全部打印出来看一下

printf("the songlist is below:\n\n")

for( j=0j<=ij++){

printf( "%d %s %s\n", songlist[j].id, songlist[j].title, songlist[j].singer )

}

printf("\n")

}

getch()

return 0

}

或者是

#include "dos.h"

#include "stdio.h"

#define time 10000 /*预定义节拍长度 time(一拍)*/

#define time0 5000 /*预定义半节拍长度 time0*/

#define time1 15000 /*预定义1.5节拍长度 time1*/

#define time2 20000 /*预定义2节拍长度 time2*/

#define time3 30000 /*预定义3节拍长度 time3*/

#define _a 262 /*预定义低音音符1~7*/

#define _b 294

#define _c 330

#define _d 349

#define _e 392

#define _f 440

#define _g 494

#define a 523/*预定义中音音符1~7*/

#define b 587

#define c 659

#define d 698

#define e 784

#define f 880

#define g 988

#define a_ 1047/*预定义高音音符1~7*/

#define b_ 1175

#define c_ 1319

#define d_ 1397

#define e_ 2568

#define f_ 1760

#define g_ 1976

main()

{

int i=0,j

unsigned milliseconds

int music[1000]={

f,time,

f,time,

g,time0,

f,time,

f,time,

g,time0,

f,time,

g,time,

a_,time,

g,time,

f,time,

g,time0,

f,time0,

d,time,

c,time,

a,time,

c,time,

d,time,

c,time,

c,time0,

a,time0,

_g,time,

f,time,

g,time,

a_,time,

g,time,

f,time,

g,time0,

f,time0,

d,time1,

c,time,

a,time,

c,time,

d,time,

c,time,

c,time0,

a,time0,

g,time1,

f,time,

f,time,

g,time1,

f,time,

f,time,

g,time1,

c,time,

d,time,

g,time0,

f,time0,

c,time,

d,time,

g,time0,

f,time0,

d,time0,

d,time,

c,time3

}

while(music[i]!='\0')

{

if(music[i]<=494) /*判断不是低音*/

{

milliseconds=music[i+1]

for(j=1j<8j++)

{

switch(j)

{

case 1: sound(a)

delay(milliseconds)break

case 2: sound(b)

delay(milliseconds)break

case 3: sound(c)

delay(milliseconds)break

case 4: sound(d)

delay(milliseconds)break

case 5: sound(e)

delay(milliseconds)break

case 6: sound(f)delay(milliseconds)break

case 7: sound(g)

delay(milliseconds)break

}

nosound()

}

}

if(music[i]>494&&music[i]<988) /*判断不是中音*/

{

milliseconds=music[i+1]

for(j=1j<8j++)

{

switch(j)

{

case 1: sound(_a)

delay(milliseconds)break

case 2: sound(_b)

delay(milliseconds)break

case 3: sound(_c)

delay(milliseconds)break

case 4: sound(_d)

delay(milliseconds)break

case 5: sound(_e)

delay(milliseconds)break

case 6: sound(_f)

delay(milliseconds)break

case 7: sound(_g)

delay(milliseconds)break

}

nosound()

}

}

if(music[i]>988) /*判断不是高音*/

{

milliseconds=music[i+1]

for(j=1j<8j++)

{

switch(j)

{

case 1: sound(a_)

delay(milliseconds)break

case 2: sound(b_)

delay(milliseconds)break

case 3: sound(c_)

delay(milliseconds)break

case 4: sound(d_)

delay(milliseconds)break

case 5: sound(e_)

delay(milliseconds)break

case 6: sound(f_)

delay(milliseconds)break

case 7: sound(g_)

delay(milliseconds)break

}

nosound()

}

}

nosound()

i=i+2

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12049816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存