数字音乐编写音乐程序时,首先需要制作被演奏音乐的乐谱文件,制作乐谱文件可用不同的符号标记,只要所编制的程序易于识别即可。MUSIC乐谱文件,程序如下:#include<stdlib.h>#include<stdio.h>#include<dos.h>#include<conio.h>void main(int argc,char *argv[]){FILE *fp;int rate;char sound_high[3];float sound_long;register int i=0, j ;int sign=0;float str[100][2];if(argc!=2) /* 命令行参数个数不正确 */ {printf("Pararneters Errors! /n") ;exit(1);}if((fp=fopen(argv[1],"r"))==NULL) /* 文件打开失败 */{printf("open file music, doc Errors! /n" );exit (1);}fscanf(fp,"%d", &rate ); /* 读取音长基数的值 */while(!feof(fp)&&!sign ) /* 文件没有结束并且数据还是乐谱 */{fscanf(fp,"%s %f",sound_high,&sound_long); /* 得到音频、音长的数值 */str[i][1]=rate*sound_long; /* 音乐= 音长基数*节拍数*/switch(sound_high[0]) {case '*': /* 最高音 */switch (sound_high[1]) { /* 确定发音的频率 */case '1':str[i++][0]=1046.5;break;case '2':str[i++][0]=1174.7;break;case '3':str[i++][0]=1318.5;break;case '4':str[i++][0]=1396.9;brcak:case '5':str[i++][0]=1568;break;case '6':str[i++][0]=1760;break;case '7':str[i++][0]=1975.5;break;default:printf ( "/n Errors in music.doc /n" );exit(1);break;}break;case 'h': /* 高音*/switch(sound_high[1]) {case '1':str[i++][0]=523.3;break;case '2':str[i++][0]=587.3;break;case '3':str[i++][0]=659.3;break;case '4':str[i++][0]=698.5;break;case '5':str[i++][0]=784.0;break;case '6':str[i++][0]=880;break;case '7':str[i++][0]=987.8;break;default:printf("/n Errors in music.doc /n " );exit (1);break;}break;case 'm': /* 中音 */switch(sound_high[1]) {case '1':str[i++][0]=262;break;case '2':str[i++][0]=296;break;case '3':str[i++][0]=329.6;break;case '4':str[i++][0]=349.2;break;case '5':str[i++][0]=392;break;case '6':str[i++][0]=440;break;case '7':str[i++][0]=493.9;break;default:printf ( "/n Errors in music.doc /n" );exit(1);break;}break;case 'l': /* 低音 */switch(sound_high[1]) {case '1':str[i++][0]=131;break;case '2':str[i++][0]=147;break;case '3':str[i++][0]=165;break;case '4':str[i++][0]=176;break;case '5':str[i++][0]=196;break;case '6':str[i++][0]=220;break;case '7':str[i++][0]=247;break;default:printf( "/n Errors in music.doc /n " );exit(1);break;}break;case '#':if(sound_high[1]=='#') sign=1;break;default:printf ( "/n Errors in music.doc /n " );exit (1);}}for(j=0;j<i;j++){sound(str[j][0]); /* 按频率值发出歌曲的音调 */delay(str[j][1]);}nosound(); /* 关闭扬声器 */}