-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHWControl.cpp
176 lines (153 loc) · 4.75 KB
/
HWControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "stdafx.h"
#include "HWControl.h"
#include "stdio.h"
#include "windows.h"
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
BOOL isLocalSound = false;
int freq_list_basic[7]={523,575,659,698,784,880,988};
int freq_list[8]={0,523,575,659,698,784,880,988};
char tone_list[7][2] = {{'D','o'}, {'R','a'},{'M','i'},{'F','a'},{'S','o'},{'L','a'},{'X','i'}};
float flag=1;
int aliasI = 0;
float beat = 125;
int music1[][2]={
{2,1},{3,1},{4,1},{5,1},{6,1},{6,1},{2,2},{1,2},{6,1},{2,1},{6,1},{5,1},{4,1},{3,1},
{2,1},{3,1},{4,1},{5,1},{6,1},{5,1},{4,1},{3,1},{2,1},{3,1},{4,1},{3,1},{2,1},{2,1},{3,1},
{2,1},{3,1},{4,1},{5,1},{6,1},{2,2},{1,2},{6,1},{2,1},{6,1},{5,1},{4,1},{3,1},
{2,1},{3,1},{4,1},{5,1},{6,1},{5,1},{4,1},{3,1},{4,1},{5,1},{6,1},
{1,2},{2,2},{6,1},{5,1},{6,1},
{5,1},{6,1},{1,2},{2,2},{6,1},{5,1},{6,1},{5,1},{6,1},{5,1},{4,1},{3,1},{1,1},{2,1},
{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{2,1},{5,1},{6,1},{1,2},{2,2},{6,1},{5,1},{6,1},
{5,1},{6,1},{1,2},{2,2},{6,1},{5,1},{6,1},{5,1},{6,1},{5,1},{4,1},{3,1},{1,1},{2,1},
{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{2,1},{5,1},{6,1},{1,2},{2,2},{6,1},{5,1},{6,1},
{5,1},{6,1},{1,2},{2,2},{6,1},{5,1},{6,1},
{2,2},{3,2},{4,2},{3,2},{2,2},{1,2},{6,1},{5,1},{6,1},{5,1},{4,1},{3,1},{1,1},{2,1}
};
int ttt3[]={
1,1,1,1,2,1,1,2,2,1,1,1,1,
1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,2,1,1,2,2,2,2,
1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
1,1,1,1,1,1,2,
1,1,1,1,1,1,2,1,1,1,1,1,1,2,
2
};
int music2[156][2]={{0,0},{0,0},{0,0},{6,1},{7,1},{1,2},{7,1},{1,2},{3,2},
{7,1},{7,1},{7,1},{3,1},{3,1},{6,1},{5,1},{6,1},{1,2},
{5,1},{5,1},{5,1},{3,1},{4,1},{3,1},{4,1},{1,2},{3,1},
{3,1},{0,0},{1,2},{1,2},{1,2},{7,1},{4,1},{4,1},{7,1},
{7,1},{7,1},{0,0},{6,1},{7,1},{1,2},{7,1},{1,2},{3,2},
{7,1},{7,1},{7,1},{3,1},{3,1},{6,1},{5,1},{6,1},{1,2},
{5,1},{5,1},{5,1},{2,1},{3,1},{4,1},{1,2},{7,1},{7,1},
{1,2},{1,2},{2,2},{2,2},{3,2},{1,2},{1,2},{1,2},{1,2},
{7,1},{6,1},{6,1},{7,1},{5,1},{6,1},{6,1},{6,1},{1,2},
{2,2},{3,2},{2,2},{3,2},{5,2},{2,2},{2,2},{2,2},{5,1},
{5,1},{1,2},{7,1},{1,2},{3,2},{3,2},{3,2},{3,2},{3,2},
{6,1},{7,1},{1,2},{7,1},{2,2},{2,2},{1,2},{5,1},{5,1},
{5,1},{4,2},{3,2},{2,2},{1,2},{3,2},{3,2},{3,2},{3,2},
{6,2},{6,2},{5,2},{5,2},{3,2},{2,2},{1,2},{1,2},{0,0},
{1,2},{2,2},{1,2},{2,2},{2,2},{5,2},{3,2},{3,2},{3,2},
{3,2},{6,2},{6,2},{5,2},{5,2},{3,2},{2,2},{1,2},{1,2},
{0,0},{1,2},{2,2},{1,2},{2,2},{2,2},{7,1},{6,1},{6,1},
{6,1},{6,1},{7,1}};
float ttt2[]={
1,1,1,0.5,0.5,1+0.5,0.5,1,1,1,1,1,0.5,0.5,
1+0.5,0.5,1,1,1,1,1,1,1+0.5,0.5,1,1,
1,1,0.5,0.5,0.5,0.5,1+0.5,0.5,1,1,1,1,1,0.5,0.5,
1+0.5,0.5,1,1,1,1,1,0.5,0.5,1+0.5,0.5,1,1,
1,1,1,0.5,0.5,1,0.5,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.25,0.5,1,
0.5,0.5,0.5,0.5,1,1,1,1,1,0.5,0.5,1+0.5,0.5,1,1,
1,1,1,0.5,0.5,1.5,0.5,1,1,1,1,1,1,
0.5,0.5,1,1,0.5,0.5,1.5,0.25,0.5,1,1,1,1,1,
1,1,1,1,1,1,1,1,0.5,0.5,1,1,0.5,0.5,
1,0.5,0.5,1,1,1,1,1,1,1,1,1,1,
0.5,0.5,1,1,0.5,0.5,1,0.5,0.25,0.5,1,1,1,1,0.5,0.5
};
int get_div_ratio(int freqnum,float flag)
{
if (freqnum==0){
return 0;
}
float tmp;
float real_freq=freq_list[freqnum]*flag;
tmp=BASEFREQ/real_freq+0.5;
return (int)tmp;
}
void play_music(int music_num)
{
if(music_num==1){
int n=135;
for(int i=0;i<n;i++){
int div_ratio=get_div_ratio(music1[i][0],music1[i][1]);
set_freq_dividing_ratio(div_ratio,(int)(200*ttt3[i]));
}
}
if(music_num==2){
int n=156;
for(int i=0;i<n;i++){
int div_ratio=get_div_ratio(music2[i][0],music2[i][1]);
set_freq_dividing_ratio(div_ratio,(int)(375*ttt2[i]));
}
}
}
void play_one_tone(int freqnum,float flag)
{
//printf("\b%c%c\n",tone_list[freqnum-1][0],tone_list[freqnum-1][1]);
int div_ratio = get_div_ratio(freqnum, flag);
if (isLocalSound){
if(flag < 1) flag = 0;
play_one_toneL('a'+freqnum-1 + flag*7);
}else{
set_freq_dividing_ratio(div_ratio,beat);
}
}
void play_one_toneL(char c){
char cmd[100];
sprintf(cmd, "open sound\\%c.mp3 alias s%d", c, aliasI);
mciSendString(cmd,NULL,0,NULL);
sprintf(cmd, "play s%d", aliasI);
mciSendString(cmd,NULL,0,NULL);
sprintf(cmd, "close s%d",(aliasI + 1)%9);
mciSendString(cmd,NULL,0,NULL);
aliasI = (aliasI + 1)%9;
}
BOOL init8254()
{
return Init9054();
}
void set_freq_dividing_ratio(int ratio,int time)
{
if(ratio!=0){
IO8bWrite(P8254MODE,0x14);//0x03
char input = (char)(ratio);
// printf(" ·ÖƵϵÊýΪ: %d\n",(int)ratio);
IO8bWrite(P8254A,input);
}
Sleep(time);
IO8bWrite(P8254MODE,0x10);
}
void delay(int nms)
{
Sleep(nms);
return;
}
void setBeat(float b){
beat = b;
}
void setRhythm(bool rhythm){
if(rhythm){
for(int i = 1; i < 8; i++){
freq_list[i] = freq_list_basic[i-1];
}
}else{
for(int i = 0; i < 7; i++){
freq_list[i] = freq_list_basic[i];
}
freq_list[7] = 1109;
}
}