3
3
#include < Audio.h>
4
4
#include < TeensyVariablePlayback.h>
5
5
#include " flashloader.h"
6
-
7
6
#include < Audio.h>
8
7
#include < Wire.h>
9
8
#include < SPI.h>
@@ -56,7 +55,7 @@ AudioPlayArrayResmp *voices[2][4] = {
56
55
{ &voice1, &voice2, &voice3, &voice4 },
57
56
{ &voice5, &voice6, &voice7, &voice8 } };
58
57
59
- newdigate::flashloader loader ( 16 * 1024 );
58
+ newdigate::flashloader loader (1024 );
60
59
const char * fileNames[2 ][4 ] = {
61
60
{" loop1.raw" , " loop3.raw" , " loop5.raw" , " loop7.raw" },
62
61
{" loop2.raw" , " loop4.raw" , " loop6.raw" , " loop8.raw" } };
@@ -71,11 +70,25 @@ const double durationOfOneBeatInMilliseconds = 1000.0 * (60.0 / tempo);
71
70
const uint32_t durationOf32BeatsInMilliseconds = durationOfOneBeatInMilliseconds * 32.0 ;
72
71
73
72
void setup () {
73
+ AudioMemory (64 );
74
74
Serial.begin (9600 );
75
- AudioMemory (32 );
75
+
76
+ // while (!Serial) { delay(100); };
77
+ delay (100 );
78
+
79
+ /*
80
+ voice1.enableInterpolation(false);
81
+ voice2.enableInterpolation(false);
82
+ voice3.enableInterpolation(false);
83
+ voice4.enableInterpolation(false);
84
+ voice5.enableInterpolation(false);
85
+ voice6.enableInterpolation(false);
86
+ voice7.enableInterpolation(false);
87
+ voice8.enableInterpolation(false);
88
+ */
76
89
77
90
sgtl5000_1.enable ();
78
- sgtl5000_1.volume (0 . 5f , 0 . 5f );
91
+ sgtl5000_1.volume (1 , 1 );
79
92
80
93
Serial.print (" Initializing SD card..." );
81
94
while (!SD.begin (BUILTIN_SDCARD)) {
@@ -86,14 +99,18 @@ void setup() {
86
99
}
87
100
88
101
inline void heap_switch () {
102
+ // Serial.printf("Heap switch init: currentWriteHeap=%d currentReadHeap=%d !\n", currentWriteHeap, currentReadHeap);
89
103
currentWriteHeap ++;
90
104
currentWriteHeap %= 2 ;
91
105
currentReadHeap = 1 - currentWriteHeap;
92
106
numLoaded = 0 ;
107
+ Serial.printf (" Heap switch complete: currentWriteHeap=%d currentReadHeap=%d !\n " , currentWriteHeap, currentReadHeap);
93
108
}
109
+
94
110
void loop () {
111
+ const uint32_t now = millis ();
95
112
// if (audio_sd_buffered_play_needs_loading) {
96
- // load from sd to audio buffers
113
+ // load from sd to audio buffers
97
114
// } else
98
115
if (numLoaded < 4 ) {
99
116
if (asyncOpening) {
@@ -106,62 +123,75 @@ void loop() {
106
123
return ;
107
124
}
108
125
asyncOpening = false ;
109
- Serial.printf (" loading '%s'... (heap:%d, index:%d) \n " , filename, currentWriteHeap, numLoaded);
126
+ Serial.printf (" loading '%s'... (heap:%d, index:%d, pointer:%x) \t\t " , filename, currentWriteHeap, numLoaded, samples[currentWriteHeap][numLoaded]-> sampledata );
110
127
} else if (loader.continueAsyncLoadPartial ()) {
111
- Serial.printf (" loading complete... (heap:%d, index:%d)\n " , currentWriteHeap, numLoaded);
112
128
numLoaded++;
113
129
asyncLoadFile.close ();
114
130
asyncOpening = true ;
115
131
}
116
132
} else if (!isPlaying) {
117
133
isPlaying = true ;
118
134
Serial.println (" Audio loaded, playback enabled!" );
135
+
136
+ patternStartTime = now;
137
+ // const uint32_t patternEndMillis = patternStartTime + durationOf32BeatsInMilliseconds;
138
+ // Serial.printf("patternStartTime: %d patternEndTime: %d\n", patternStartTime, patternEndMillis);
119
139
loader.toggle_beforeNewPatternVoicesStart ();
120
140
loader.toggle_afterNewPatternStarts ();
121
- patternStartTime = millis ();
122
141
heap_switch ();
123
142
}
124
143
125
144
if (!isPlaying) return ;
126
145
127
- for (int i=0 ; i < 4 ; i++) {
128
- auto voice = voices[currentReadHeap][i];
129
- if (voice && !voice->isPlaying ()) {
130
- newdigate::audiosample *sample = samples[currentReadHeap][i];
131
- voice->playRaw (sample->sampledata , sample->samplesize / 2 , 1 );
132
- Serial.printf (" playing... (heap:%d, index:%d)\n " , currentWriteHeap, i);
146
+ if (!asyncChanging) { // dont play any new samples after the pre-pattern switch event
147
+ for (int i=0 ; i < 4 ; i++) {
148
+ auto voice = voices[currentReadHeap][i];
149
+ if (voice && !voice->isPlaying ()) {
150
+ newdigate::audiosample *sample = samples[currentReadHeap][i];
151
+ voice->playRaw (sample->sampledata , sample->samplesize / 2 , 1 );
152
+ voice->setLoopStart (0 );
153
+ voice->setLoopFinish (sample->samplesize /2 );
154
+ voice->setLoopType (loop_type::looptype_repeat);
155
+ Serial.printf (" playing... (heap:%d, index:%d - size: %d)\t\t\t %x\n " , currentReadHeap, i, sample->samplesize , sample->sampledata );
156
+ }
133
157
}
134
158
}
135
159
136
- const uint32_t now = millis ();
137
160
const uint32_t patternEndMillis = patternStartTime + durationOf32BeatsInMilliseconds;
138
- // 0.5 second before the next pattern starts to play, get ready
139
- if (now >= patternEndMillis - 500 ) {
140
- // current samples can keep playing
141
- if (!asyncChanging) {
142
- Serial.println (" Pattern change coming soon!" );
143
- loader.toggle_beforeNewPatternVoicesStart ();
144
- asyncChanging = true ;
145
- }
146
- } else if (now >= patternEndMillis) {
161
+ if (now >= patternEndMillis) {
162
+ Serial.println (" Pattern switch" );
147
163
asyncChanging = false ;
148
164
for (int i=0 ; i < 4 ; i++) {
149
165
auto voice = voices[currentReadHeap][i];
150
- if (! voice->isPlaying ()) {
166
+ if (voice && voice->isPlaying ()) {
151
167
voice->stop ();
152
168
}
153
- voices [currentReadHeap][i] = nullptr ;
169
+ // samples [currentReadHeap][i] = nullptr;
154
170
}
155
171
if (numLoaded < 4 ) {
156
172
Serial.println (" WARN: not all the samples managed to load in time!" );
157
173
}
158
- Serial. println ( " Audio loaded, playback enabled! " );
174
+
159
175
loader.toggle_afterNewPatternStarts ();
176
+
160
177
patternStartTime = now;
161
178
heap_switch ();
179
+ } else if (now >= patternEndMillis - 500 ) {
180
+ // 0.5 second before the next pattern starts to play, get ready
181
+ // current samples can keep playing
182
+ if (!asyncChanging) {
183
+ Serial.println (" pre-pattern switch event, pattern change coming soon!" );
184
+ loader.toggle_beforeNewPatternVoicesStart ();
185
+ asyncChanging = true ;
186
+ }
162
187
}
188
+
189
+ // my thinking re: delay(1) is to give the mcu a some time between partial loads, not sure thou...
190
+ delay (1 );
163
191
}
164
192
165
- void std::__throw_length_error (char const *) {
166
193
167
- }
194
+ // this method below is needed for c++17 - it doesn't link otherwise, but for c++14 this is not needed:
195
+ void std::__throw_length_error (char const *) {
196
+ // don't worry, be happy!
197
+ }
0 commit comments