-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSound.cpp
More file actions
212 lines (189 loc) · 5.18 KB
/
Sound.cpp
File metadata and controls
212 lines (189 loc) · 5.18 KB
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
#include "Sound.h"
#include "Config.h"
#include <TimerOne.h>
// ISR Variables
volatile word soundBuffers[SND_BUFFSIZE][2] = { 0 };
volatile byte currentPosition = 0; // 0...SND_BUFFSIZE-1
volatile byte currentBuffer = 0; // 0...1
volatile byte isBufferSwapped = HIGH;
volatile byte isSoundPaused = HIGH;
volatile byte pinState = LOW;
volatile bool isPauseBlock = false;
volatile bool wasPauseBlock = false;
// ID15 switch
volatile byte ID15switch = 0;
volatile word tStatesPerSample = 0;
// This variable can be changed in menu (not realized yet)
const bool flipPolarity = false;
// Private Functions
void soundISR();
// Common sound functions
void sound(uint8_t val)
{
digitalWrite(SND_SPEAKER, val);
digitalWrite(SND_OUTPUT, val);
}
// Sound Timer
void setupSound()
{
pinState = LOW;
isSoundPaused = HIGH;
// setup sound
pinMode(SND_SPEAKER, OUTPUT);
pinMode(SND_OUTPUT, OUTPUT);
sound(pinState);
// 100ms pause prevents anything bad happening before we're ready
Timer1.initialize(100000);
Timer1.attachInterrupt(soundISR);
//Stop the timer until we're ready
Timer1.stop();
}
void startSound(unsigned long microseconds)
{
pinState = LOW; //Always Start on a LOW output for simplicity
isSoundPaused = LOW;
// clear sound buffer
for (int i = 0; i < SND_BUFFSIZE; ++i)
{
soundBuffers[i][0] = 0;
soundBuffers[i][1] = 0;
}
// set PIN and timer
sound(pinState);
Timer1.setPeriod(microseconds);
}
void pauseSound(byte pause)
{
isSoundPaused = pause;
}
void stopSound()
{
Timer1.stop();
isSoundPaused = HIGH;
ID15switch = 0; // ID15switch
}
void setPeriod(byte pos, word period)
{
soundBuffers[pos][currentBuffer ^ 1] = period; //add period to the buffer
}
bool checkIfBufferSwapped()
{
if (isBufferSwapped == HIGH)
{
isBufferSwapped = LOW;
return true;
}
return false;
}
// ID 15 - Direct Recording
void setID15()
{
ID15switch = 1;
}
void setTStates(word value)
{
tStatesPerSample = value;
}
word getTStates()
{
return tStatesPerSample;
}
//
// Sound Timer ISR
//
void soundISR()
{
word workingPeriod = soundBuffers[currentPosition][currentBuffer];
byte pauseFlipBit = false;
unsigned long newTime = 1;
if (isSoundPaused == LOW && workingPeriod >= 1)
{
if (bitRead(workingPeriod, 15))
{
//If bit 15 of the current period is set we're about to run a pause
//Pauses start with a 1.5ms where the output is untouched after which the output is set LOW
//Pause block periods are stored in milliseconds not microseconds
isPauseBlock = true;
bitClear(workingPeriod, 15); //Clear pause block flag
pinState = !pinState;
pauseFlipBit = true;
wasPauseBlock = true;
}
else
{
if (workingPeriod >= 1 && wasPauseBlock == false)
{
pinState = !pinState;
}
else if (wasPauseBlock == true && isPauseBlock == false)
{
wasPauseBlock = false;
}
}
if (ID15switch == 1)
{
if (bitRead(workingPeriod, 14) == 0)
{
sound(pinState);
}
else
{
if (bitRead(workingPeriod, 13) == 0)
sound(LOW);
else
{
sound(HIGH);
bitClear(workingPeriod, 13);
}
bitClear(workingPeriod, 14); //Clear ID15 flag
workingPeriod = tStatesPerSample;
}
}
else
{
sound(pinState);
}
if (pauseFlipBit == true)
{
newTime = 1500; //Set 1.5ms initial pause block
pinState = flipPolarity ? HIGH : LOW;
soundBuffers[currentPosition][currentBuffer] = workingPeriod - 1; //reduce pause by 1ms as we've already pause for 1.5ms
pauseFlipBit = false;
}
else
{
if (isPauseBlock == true)
{
newTime = long(workingPeriod) * 1000; //Set pause length in microseconds
isPauseBlock = false;
}
else
{
newTime = workingPeriod; //After all that, if it's not a pause block set the pulse period
}
currentPosition += 1;
if (currentPosition >= SND_BUFFSIZE) //Swap buffer pages if we've reached the end
{
currentPosition = 0;
currentBuffer ^= 1;
isBufferSwapped = HIGH; //Request more data to fill inactive page
}
}
}
else if (isSoundPaused == LOW && workingPeriod <= 1)
{
newTime = 1000; //Just in case we have a 0 in the buffer
currentPosition += 1;
if (currentPosition >= SND_BUFFSIZE)
{
currentPosition = 0;
currentBuffer ^= 1;
isBufferSwapped = HIGH;
}
}
else
{
newTime = 1000000; //Just in case we have a 0 in the buffer
}
Timer1.setPeriod(newTime + 4); //Finally set the next pulse length
}