-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
150 lines (113 loc) · 3.33 KB
/
sound.c
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
#include "lib.h"
#include "sound.h"
void setupSounds()
{
REG_SOUNDCNT_X = SND_ENABLED;
REG_SOUNDCNT_H = SND_OUTPUT_RATIO_100 |
DSA_OUTPUT_RATIO_100 |
DSA_OUTPUT_TO_BOTH |
DSA_TIMER0 |
DSA_FIFO_RESET |
DSB_OUTPUT_RATIO_100 |
DSB_OUTPUT_TO_BOTH |
DSB_TIMER1 |
DSB_FIFO_RESET;
REG_SOUNDCNT_L = 0;
}
void playSoundA( const unsigned char* sound, int length, int frequency, int loops) {
dma[1].cnt = 0;
int ticks = PROCESSOR_CYCLES_PER_SECOND/frequency;
DMANow(1, sound, REG_FIFO_A, DMA_DESTINATION_FIXED | DMA_AT_REFRESH | DMA_REPEAT | DMA_32);
REG_TM0CNT = 0;
REG_TM0D = -ticks;
REG_TM0CNT = TIMER_ON;
// TODO 2.1: Assign all of soundA’s appropriate struct values
soundA.data = sound;
soundA.length = length;
soundA.frequency = frequency;
soundA.loops = loops;
soundA.isPlaying = 1;
soundA.duration = (VBLANK_FREQ * length)/frequency;
soundA.vBlankCount = 0;
}
void playSoundB( const unsigned char* sound, int length, int frequency, int loops) {
dma[2].cnt = 0;
int ticks = PROCESSOR_CYCLES_PER_SECOND/frequency;
DMANow(2, sound, REG_FIFO_B, DMA_DESTINATION_FIXED | DMA_AT_REFRESH | DMA_REPEAT | DMA_32);
REG_TM1CNT = 0;
REG_TM1D = -ticks;
REG_TM1CNT = TIMER_ON;
// TODO 2.2: Assign all of soundB’s appropriate struct values
soundB.data = sound;
soundB.length = length;
soundB.frequency = frequency;
soundB.loops = loops;
soundB.isPlaying = 1;
soundB.duration = (VBLANK_FREQ * length)/frequency;
soundB.vBlankCount = 0;
}
void setupInterrupts()
{
REG_IME = 0;
// TODO 3.1 - Set up the interrupt handler
REG_INTERRUPT = (unsigned int) interruptHandler;
REG_IE |= INT_VBLANK;
REG_DISPSTAT |= INT_VBLANK_ENABLE;
REG_IME = 1;
}
void interruptHandler() {
REG_IME = 0;
if(REG_IF & INT_VBLANK) {
if (soundA.isPlaying) {
//TODO 3.2 - Handle soundA playing in the interruptHandler function
soundA.vBlankCount++;
if (soundA.vBlankCount >= soundA.duration) {
if (soundA.loops) {
playSoundA(soundA.data, soundA.length, soundA.frequency, soundA.loops);
} else {
soundA.isPlaying = 0;
dma[1].cnt = 0;
REG_TM0CNT = 0;
}
}
}
if (soundB.isPlaying) {
//TODO 3.3 - Handle soundB playing in the interruptHandler function
soundB.vBlankCount++;
if (soundB.vBlankCount >= soundB.duration) {
if (soundB.loops) {
playSoundB(soundB.data, soundB.length, soundB.frequency, soundB.loops);
} else {
soundB.isPlaying = 0;
dma[2].cnt = 0;
REG_TM1CNT = 0;
}
}
}
REG_IF = INT_VBLANK;
}
REG_IME = 1;
}
void pauseSound() {
// TODO 4.1 - Complete the pauseSound function
REG_TM0CNT = 0;
soundA.isPlaying = 0;
REG_TM1CNT = 0;
soundB.isPlaying = 0;
}
void unpauseSound() {
// TODO 4.2 - Complete the unpauseSound function
REG_TM0CNT = TIMER_ON;
soundA.isPlaying = 1;
REG_TM1CNT = TIMER_ON;
soundB.isPlaying = 1;
}
void stopSound() {
// TODO 4.3 - Complete the stopSound function
dma[1].cnt = 0;
REG_TM0CNT = 0;
soundA.isPlaying = 0;
dma[2].cnt = 0;
REG_TM1CNT = 0;
soundB.isPlaying = 0;
}