确切的说应该从主板上的扬声器中发出的
永不放弃!
tc编译出来好像有点问题不知何故
?Compiling WINDOWS\DESKTOP\222.C: ?Error WINDOWS\DESKTOP\222.C 23: Declaration terminated incorrectly 焙 ?Error WINDOWS\DESKTOP\222.C 52: Cannot convert 'void (interrupt *)(...)' to '焙 ?Error WINDOWS\DESKTOP\222.C 53: Cannot convert 'int *' to 'void (interrupt *)焙 ?match in parameter '__isr' in call to 'setvect(int,void (interrupt *)(...))' 焙 ?Error WINDOWS\DESKTOP\222.C 57: Cannot convert 'void (interrupt *)()' to 'voi焙 ?Error WINDOWS\DESKTOP\222.C 57: Type mismatch in parameter '__isr' in call to焙 ?Warning WINDOWS\DESKTOP\222.C 60: Function should return a value 焙 ? 焙
//看看这个如何
#include<stdlib.h> #include<stdio.h> #include<dos.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("Parameters 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; break; case '5': str[i++][0] = 1568; break; case '6': str[i++][0] = 1760; break; case '7': str[i++][0] = 1975.5; break; default: printf("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } break; } case '#': if(sound_high[1] == '#') sign = 1; break; default: printf("\nErrors in music doc\n"); exit(1);
} } for(j = 0;j <= i-1;j++) { sound(str[j][0]);/*按频率值发出歌曲的音调*/ delay(str[j][1]); } nosound();/*关闭扬声器*/
}
//600 m5 1 m3 0.5 m5 0.5 h1 2 m6 1 h1 0.5 m6 0.5 m5 2 m5 1 m1 0.5 m2 0.5 m3 1 m2 0.5 m1 0.5 m2 3 # # 将音乐代码比如上面一段加入文本文件,拖入生成的.exe文件即可放音
/*格式整理后,便于大家学习研究*/
#include<stdio.h> #include<dos.h> int n=0; int fg=0; int b; void soundkid(int x,int y) { int a; outportb(0x43,0xb6); outportb(0x42,x); outportb(0x42,y); a=inportb(0x61); a=a|3; outportb(0x61,a); }; void nosoundkid() { int a; a=inportb(0x61); a=a&0xfd; outportb(0x61,a); }; void interrupt(*old)(void); void interrupt new() { switch(b) { case 1: soundkid(200,17); break; case 2: soundkid(216,15); break; case 3: soundkid(30,14); break; case 4: soundkid(109,13); break; case 5: soundkid(226,11); break; case 6: soundkid(150,10); break; case 7: soundkid(110,9); break; case 0: nosoundkid(); break; case'e':nosoundkid(); break; default:; } if(b!=0&b!='e') { n++; if(n>10){ n=0; fg=1; } } else{ n++; if(n>0){ n=0; fg=1; } } }; int main(void) { int code[42]={3,0,3,4,5,0,5, 4,3,2,1,0,1,2, 3,0,3,2,0,2,3, 0,3,4,5,0,5,4, 3,2,1,0,1,2,3, 2,1,0,1,0,1,'e' }; int i; for(i=0;i<42;i++) { b=code[i]; old=getvect(0x1C); setvect(0x1c,new); for(;;){ if(fg==1){ fg=0; setvect(0x1c,old); break; } } } getch(); return 0; }
/*同样,改变一下风格,供大家研究分析*/
#include<stdlib.h> #include<stdio.h> #include<dos.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("Parameters 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; break; case '5': str[i++][0] = 1568; break; case '6': str[i++][0] = 1760; break; case '7': str[i++][0] = 1975.5; break; default: printf("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } 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("\nErrors in music doc\n"); exit(1); } break; } case '#': if(sound_high[1] == '#') { sign = 1; } break; default: printf("\nErrors in music doc\n"); exit(1);
} } for(j = 0;j <= i-1;j++) { sound(str[j][0]);/*按频率值发出歌曲的音调*/ delay(str[j][1]); } nosound(); /*关闭扬声器*/
}