📄 music.c
字号:
/* 文件名 : mazeMain.c
* 描述 : 超级迷宫 (Super Maze) 插曲模块
* 作者 : 文曦畅 Wen Xichang 2004-11-10
*/
#define C 523
#define C_ 554
#define D 587
#define D_ 622
#define E 659
#define F 698 /* Middle */
#define F_ 740
#define G 784
#define G_ 831
#define A 880
#define A_ 932
#define B 988
#define C0 262
#define C0_ 277
#define D0 294
#define D0_ 311
#define E0 330
#define F0 349 /* Low */
#define F0_ 370
#define G0 392
#define G0_ 415
#define A0 440
#define A0_ 466
#define B0 497
#define C1 1047
#define C1_ 1109
#define D1 1175
#define D1_ 1245
#define E1 1319
#define G1 1568
#define G1_ 1661
#define A1 1760
#define A1_ 1865
#define B1 1976
#define P8_1 1 /* Length */
#define P4_1 2
#define P2_1 4
#define P1 8
#define P2 16
#define P3 24
#define P4 32
struct Note{
int s; /* 声音频率 */
int len; /* 长度 */
}musicNotes[4][110] = {{{A0,P4_1},{B0,P4_1},{C,P2_1},{C,P2_1},{C,P4_1},{B0,P4_1},{C,P4_1},{D,P4_1},{E,P2_1},{E,P2_1},
{E,P4_1},{D_,P4_1},{E,P4_1},{F,P4_1},{G,P2_1},{G,P2_1},{G,P4_1},{E,P4_1},{G,P4_1},{C1,P4_1},
{G,12},{E,P2_1},{F,P2_1},{F,P4_1},{E,P4_1},{D,P2_1},{F,P2_1},{E,P2_1},{E,P4_1},{D,P4_1},
{C,P2_1}, {E,P2_1},{D,P2_1},{A0,P2_1},{B0,P2_1},{C,P2_1},{D,12},{A0,P4_1},{B0,P4_1},{C,P2_1},
{C,P2_1},{C,P4_1},{B0,P4_1},{C,P4_1},{D,P4_1},{E,P2_1},{E,P2_1},{E,P4_1},{D_,P4_1},{E,P4_1},
{F,P4_1},{G,P2_1},{G,P2_1},{G,P4_1},{E,P4_1},{G,P4_1},{C1,P4_1},{G,12},{C,P2_1},{A,P2_1},
{G,P2_1},{F,P2_1},{E,P2_1},{D,P2_1},{C,P2_1},{B0,P2_1},{C,P2_1},{D,P2_1},{E,P4_1},{F,P4_1},
{E,P2_1},{D,P2_1},{C,12},{D,P4_1},{E,P4_1},{F,P2_1},{F,P2_1},{F,P2_1},{F,P2_1},{F,P4_1},
{E,P4_1},{F,P1},{F_,P2_1},{G,P2_1},{G,P2_1},{G,P4_1},{F,P4_1},{E,P4_1},{F,P4_1},{G,P2},
{A,P2_1},{A,P2_1},{C1,6},{A,P4_1},{G,P2_1}, {G,P2_1},{G,P4_1},{F,P4_1},{E,P2_1},{F,P2_1},
{F,P2_1},{B0,P2_1},{D,P2_1},{C,P2_1},{C,P2_1},{C,P2_1}},
{{C,P1},{C,P2_1},{D,P2_1},{E,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{E,P2},
{G,P1},{F,P2_1},{E,P2_1},{D,P2},{F,P1},{E,P2_1},{D,P2_1},{C,P2},{C,P1},{C,P2_1},
{D,P2_1},{E,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{E,P2},{G,P1},{F,P2_1},
{E,P2_1},{D,P1},{E,P2_1},{D,P2_1},{C,P4},{G,P1},{F,P2_1},{E,P2_1},{D,P1},{G0,P2_1},
{G0,P2_1},{F,P1},{E,P2_1},{D,P2_1},{C,P2},{G,P1},{F,P2_1},{E,P2_1},{D,P1},{G0,P2_1},
{G0,P2_1},{F,P1},{E,P2_1},{D,P2_1},{C,P2_1},{G0,P2_1},{A0,P2_1},{B0,P2_1},{C,P1},{C,P2_1},
{D,P2_1},{E,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{E,P2},{G,P1},{F,P2_1},
{E,P2_1},{D,P1},{E,P2_1},{D,P2_1},{C,P4}},
{{D,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{F,P1},{F,P2_1},{E,P2_1},{D,P2},
{D,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{F,P2_1},{E,P2_1},{F,P2_1},{G,P2_1},{A,P2},
{D,P1},{E,P2_1},{F,P2_1},{G,P1},{A,P2_1},{G,P2_1},{F,P1},{F,P2_1},{E,P2_1},{D,P2},
{E,P1},{E,P1},{E,P1},{F,P2_1},{E,P2_1},{D,P1},{D,P2_1},{C,P2_1},{D,P2},
{A,P1},{A,P1},{A,P1},{G,P2_1},{F,P2_1},{G,P1},{G,P2_1},{A,P2_1},{G,P2},
{F,P1},{F,P1},{F,P1},{G,P2_1},{F,P2_1},{E,P1},{E,P2_1},{F,P2_1},{E,P2},
{A,P1},{A,P1},{A,P1},{G,P2_1},{F,P2_1},{G,P1},{G,P2_1},{A,P2_1},{G,P2},
{F,P1},{F,P1},{E,P1},{F,P2_1},{E,P2_1},{D,P1},{D,P2_1},{C,P2_1},{D,P2}},
{{G,P1},{C,P2_1},{D,P2_1},{E,P2_1},{F,P2_1},{G,P1},{C,P1},{C,P1},{A,P1},{F,P2_1},
{G,P2_1},{A,P2_1},{B,P2_1},{C1,P1},{C,P1},{C,P1},{F,P1},{G,P2_1},{F,P2_1},{E,P2_1},
{D,P2_1},{E,P1},{F,P2_1},{E,P2_1},{D,P2_1},{C,P2_1},{B0,P1},{C,P2_1},{D,P2_1},{E,P2_1},
{C,P2_1},{D,P3},{G,P1},{C,P2_1},{D,P2_1},{E,P2_1},{F,P2_1},{G,P1},{C,P1},{C,P1},
{A,P1},{F,P2_1},{G,P2_1},{A,P2_1},{B,P2_1},{C1,P1},{C,P1},{C,P1},
{F,P1},{G,P2_1},{F,P2_1},{E,P2_1},{D,P2_1},{E,P1},{F,P2_1},{E,P2_1},
{D,P2_1},{C,P2_1},{D,P1},{E,P2_1},{D,P2_1},{C,P2_1},{B0,P2_1},{C,P3}}
};
struct Note soundNotes[4][4] = {{{C,P1},{E,P1},{G,P1},{C1,P1}}, /*胜利*/
{{C1,P4_1},{G,P4_1},{C1,P4_1},{G,P4_1}}, /*暂停*/
{{C0,P4_1}}, /*菜单上下*/
{{E0,P4_1},{G0,P4_1}} /*确认*/
};
struct {
int interval;
int lastTime;
} musicTimer;
int musicOn = 1;
int songLen[4] = {106,75,76,64};
int soundLen[4] = {3,3,0,1};
/*01*/ int getMemTime (void);
/*02*/ void initMusic (void);
/*03*/ void playMusic (int musIdx, int loop);
/*04*/ void playSound (int musIdx, int key);
int getMemTime(){ /* 得到储存器里0x46c这一块不断定时变动的计数器的数值 */
int ret; /* 用于构造类似于定时器的东西 */
ret = peek(0x0,0x46e);
ret <<= 8;
ret += peek(0x0,0x46c);
return (ret);
}
void initMusic(void){
musicTimer.lastTime = getMemTime();
musicTimer.interval = 0;
}
void playMusic(int musIdx, int loop){
static tmpIdx = 0;
static tmploop = 0;
static current = 0;
int vTime;
if(musIdx != tmpIdx || loop != tmploop){ /* 换了歌曲 */
current = 0;
tmpIdx = musIdx;
tmploop = loop;
}
if(musicOn){
vTime=getMemTime() - musicTimer.lastTime;
if(vTime >= musicTimer.interval){
if(current > songLen[musIdx] - 1){
if (loop != 0){
current = 0;
sound(musicNotes[musIdx][current].s);
musicTimer.interval = musicNotes[musIdx][current].len;
musicTimer.lastTime = getMemTime();
current++;
}
else
nosound();
return;
}
else{
sound(musicNotes[musIdx][current].s);
musicTimer.interval = musicNotes[musIdx][current].len;
musicTimer.lastTime = getMemTime();
current++;
}
}
}
}
void playSound(int musIdx, int key){
static tmpIdx = 0;
static tmpKey = 0;
static current = 0;
int vTime;
if(musIdx != tmpIdx || key != tmpKey){ /* 换了歌曲 */
current = 0;
tmpIdx = musIdx;
tmpKey = key;
}
if(musicOn){
vTime=getMemTime() - musicTimer.lastTime;
if(vTime >= musicTimer.interval){
if(current > soundLen[musIdx]){
nosound();
return;
}
else{
sound(soundNotes[musIdx][current].s);
musicTimer.interval = soundNotes[musIdx][current].len;
musicTimer.lastTime = getMemTime();
current++;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -