-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsound.h
282 lines (258 loc) · 5.87 KB
/
sound.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef SOUND_H
#define SOUND_H
// 효과음 상태
#define SOUND_PLAYER_ATTACK 0
#define SOUND_GAMEOVER 1
#define SOUND_CLEAR 2
#define SOUND_DEATH 3
// BGM 상태
#define BGM_OFF 0
#define BGM_NORMAL 1
#define BGM_BOSS 2
#include <Arduino.h>
// 주나: 핀 번호 충돌 방지를 위해 다른 헤더로 모았어용 (보면 지우셈)
#include "constants.h"
namespace sound
{
short frequencies[14] = {
// 도, 레, 미, 파, 솔, 라, 시
261, 293, 329, 349, 392, 440, 466,
522, 586, 658, 698, 884, 880, 932};
short note[6] = {
// 사분음표, 점사분음표, 팔분음표, 점팔분음표, 십육분음표,점십육분음표
500, 750, 250, 375, 125, 187};
unsigned char melody[65][2]{
// 멜로디 배열
{0, 0},
{3, 2},
{0, 2},
{3, 2},
{4, 2},
{5, 0},
{4, 2},
{5, 4},
{6, 4},
{7, 2},
{5, 2},
{6, 4},
{0, 4},
{3, 4},
{5, 4},
{4, 1},
{3, 2},
{2, 2},
{3, 2},
{0, 2},
{3, 2},
{0, 1},
{3, 2},
{4, 4},
{5, 4},
{4, 2},
{5, 2},
{6, 4},
{4, 4},
{5, 4},
{6, 4},
{0, 1},
{3, 2},
{9, 2},
{10, 4},
{9, 2},
{6, 2},
{5, 3},
{5, 4},
{6, 2},
{0, 2},
{3, 2},
{3, 4},
{4, 4},
{5, 2},
{6, 2},
{5, 1},
{4, 2},
{0, 4},
{0, 4},
{3, 2},
{0, 2},
{3, 4},
{4, 4},
{5, 4},
{4, 4},
{5, 1},
{6, 2},
{11, 2},
{3, 2},
{9, 2},
{4, 2},
{3, 0},
{2, 2},
{3, 4},
};
void init()
{ // 사운드 핀 설정
pinMode(PIN_SOUND_BUZZER_1, OUTPUT);
pinMode(PIN_SOUND_BUZZER_2, OUTPUT);
}
int currentBgm = BGM_OFF;
unsigned long long a = 0;
unsigned long long b = 0;
int count = 0;
bool playi = false;
int effectIndex;
int effectLeft;
int effectMillis;
// buzzerpin, millis()
void bgmsetup()
{ // 배경음악 시작
count = 0;
a = millis();
b = millis() + note[melody[count][1]];
if (melody[count][0] > 0)
{
tone(PIN_SOUND_BUZZER_1, frequencies[melody[count][0]]);
}
playi = true;
}
void setBgm(int bgm) {
currentBgm = bgm;
}
// buzzerpin, millis()
void bgmLoop()
{ // 일반 배경음악 시작
if (currentBgm == BGM_OFF) return;
unsigned long currenttime = millis();
if (playi == false)
return;
if (count == 64)
{
count = 0;
}
if (currenttime >= b)
{
noTone(PIN_SOUND_BUZZER_1);
count++;
if (melody[count][0] > 0)
{
if (currentBgm == BGM_BOSS) {
a = currenttime;
b = millis() + (int)(note[melody[count][1]] * 1.5);
if (effectLeft == 0)
tone(PIN_SOUND_BUZZER_1, int(frequencies[melody[count][0]] * 1.2));
} else if (currentBgm == BGM_NORMAL) {
a = currenttime;
b = millis() + note[melody[count][1]];
if (effectLeft == 0)
tone(PIN_SOUND_BUZZER_1, frequencies[melody[count][0]] * 2);
}
}
}
}
// void boss_bgm_start()
// { // 보스 배경음악 시작
// unsigned long currenttime = millis();
// if (playi == false)
// return;
// if (count == 64)
// {
// count = 0;
// }
// if (currenttime >= b)
// {
// noTone(PIN_SOUND_BUZZER_1);
// count++;
// if (melody[count][0] > 0)
// {
// a = currenttime;
// b = millis() + note[melody[count][1]];
// if (effectLeft <= 0)
// tone(PIN_SOUND_BUZZER_1, frequencies[melody[count][0]]/2);
// }
// }
// }
// buzzerpin
void bgmstop()
{ // 배경음악 정지
noTone(PIN_SOUND_BUZZER_1);
playi = false;
}
void attacksound()
{ // 총알 소리
tone(PIN_SOUND_BUZZER_2, effectLeft * 5 + 100);
}
void deathsound()
{ // 죽음 소리
if (effectLeft / 100 % 2 == 0)
tone(PIN_SOUND_BUZZER_2, 150);
else
noTone(PIN_SOUND_BUZZER_2);
}
void gameoversound()
{ // 죽음 소리
tone(PIN_SOUND_BUZZER_2, effectLeft * 2 + 100);
}
void clearsound()
{ // 클리어 소리
tone(PIN_SOUND_BUZZER_2, (1000 - effectLeft) * 2 + 100);
}
void playEffectSound(int index)
{ // 효과음 재생
noTone(PIN_SOUND_BUZZER_2);
effectMillis = millis();
switch (index)
{
case SOUND_PLAYER_ATTACK:
// 플레이어 총알 소리 재생 코드
effectIndex = SOUND_PLAYER_ATTACK;
effectLeft = 200;
break;
case SOUND_DEATH:
// 죽음 소리 재생 코드
effectIndex = SOUND_DEATH;
effectLeft = 500;
break;
case SOUND_GAMEOVER:
// 죽음 소리 재생 코드
effectIndex = SOUND_GAMEOVER;
effectLeft = 1000;
break;
case SOUND_CLEAR:
// 죽음 소리 재생 코드
effectIndex = SOUND_CLEAR;
effectLeft = 1000;
break;
}
}
void effectLoop() {
if (effectLeft > 0) {
switch (effectIndex)
{
case SOUND_PLAYER_ATTACK:
attacksound();
break;
case SOUND_DEATH:
deathsound();
break;
case SOUND_GAMEOVER:
gameoversound();
break;
case SOUND_CLEAR:
clearsound();
break;
}
unsigned long currentMillis = millis();
effectLeft -= currentMillis - effectMillis;
effectMillis = currentMillis;
} else {
noTone(PIN_SOUND_BUZZER_2);
effectLeft = 0;
}
}
// 사운드 동작을 위한 반복 함수
// (이펙트 & BGM)
void soundLoop() {
effectLoop();
bgmLoop();
}
}
#endif