-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
244 lines (197 loc) · 6.24 KB
/
main.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
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
/*
* assignment2_drums
*
* Second assignment for ECS732 RTDSP, to create a sequencer-based
* drum machine which plays sampled drum sounds in loops.
*
* This code runs on BeagleBone Black with the Bela environment.
*
* 2016, 2017
* Becky Stewart
*
* 2015
* Andrew McPherson and Victor Zappi
* Queen Mary University of London
*/
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <libgen.h>
#include <signal.h>
#include <getopt.h>
#include <sndfile.h>
#include <Bela.h>
#include "drums.h"
using namespace std;
/* Drum samples are pre-loaded in these buffers. Length of each
* buffer is given in gDrumSampleBufferLengths.
*/
float *gDrumSampleBuffers[NUMBER_OF_DRUMS];
int gDrumSampleBufferLengths[NUMBER_OF_DRUMS];
/* Patterns indicate which drum(s) should play on which beat.
* Each element of gPatterns is an array, whose length is given
* by gPatternLengths.
*/
int *gPatterns[NUMBER_OF_PATTERNS];
int gPatternLengths[NUMBER_OF_PATTERNS];
// Handle Ctrl-C by requesting that the audio rendering stop
void interrupt_handler(int var)
{
gShouldStop = true;
}
// Print usage information
void usage(const char * processName)
{
cerr << "Usage: " << processName << " [options]" << endl;
Bela_usage();
cerr << " --help [-h]: Print this menu\n";
}
int initDrums() {
/* Load drums from WAV files */
SNDFILE *sndfile ;
SF_INFO sfinfo ;
char filename[64];
for(int i = 0; i < NUMBER_OF_DRUMS; i++) {
snprintf(filename, 64, "./Audio/drum%d.wav", i);
if (!(sndfile = sf_open (filename, SFM_READ, &sfinfo))) {
printf("Couldn't open file %s\n", filename);
/* Free already loaded sounds */
for(int j = 0; j < i; j++)
free(gDrumSampleBuffers[j]);
return 1;
}
if (sfinfo.channels != 1) {
printf("Error: %s is not a mono file\n", filename);
/* Free already loaded sounds */
for(int j = 0; j < i; j++)
free(gDrumSampleBuffers[j]);
return 1;
}
gDrumSampleBufferLengths[i] = sfinfo.frames;
gDrumSampleBuffers[i] = (float *)malloc(gDrumSampleBufferLengths[i] * sizeof(float));
if(gDrumSampleBuffers[i] == NULL) {
printf("Error: couldn't allocate buffer for %s\n", filename);
/* Free already loaded sounds */
for(int j = 0; j < i; j++)
free(gDrumSampleBuffers[j]);
return 1;
}
int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
int readcount = sf_read_float(sndfile, gDrumSampleBuffers[i], gDrumSampleBufferLengths[i]);
/* Pad with zeros in case we couldn't read whole file */
for(int k = readcount; k < gDrumSampleBufferLengths[i]; k++)
gDrumSampleBuffers[i][k] = 0;
if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
double scale ;
int m ;
sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
if (scale < 1e-10)
scale = 1.0 ;
else
scale = 32700.0 / scale ;
printf("Scale = %f\n", scale);
for (m = 0; m < gDrumSampleBufferLengths[i]; m++)
gDrumSampleBuffers[i][m] *= scale;
}
sf_close(sndfile);
}
return 0;
}
void cleanupDrums() {
for(int i = 0; i < NUMBER_OF_DRUMS; i++)
free(gDrumSampleBuffers[i]);
}
void initPatterns() {
int pattern0[16] = {0x01, 0x40, 0, 0, 0x02, 0, 0, 0, 0x20, 0, 0x01, 0, 0x02, 0, 0x04, 0x04};
int pattern1[32] = {0x09, 0, 0x04, 0, 0x06, 0, 0x04, 0,
0x05, 0, 0x04, 0, 0x06, 0, 0x04, 0x02,
0x09, 0, 0x20, 0, 0x06, 0, 0x20, 0,
0x05, 0, 0x20, 0, 0x06, 0, 0x20, 0};
int pattern2[16] = {0x11, 0, 0x10, 0x01, 0x12, 0x40, 0x04, 0x40, 0x11, 0x42, 0x50, 0x01, 0x12, 0x21, 0x30, 0x20};
int pattern3[32] = {0x81, 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x81, 0, 0, 0, 0x41, 0x80, 0x80, 0x80,
0x81, 0x80, 0x80, 0, 0x41, 0, 0x80, 0x80, 0x81, 0x80, 0x80, 0x80, 0xC1, 0, 0, 0};
int pattern4[16] = {0x81, 0x02, 0, 0x81, 0x0A, 0, 0xA1, 0x10, 0xA2, 0x11, 0x46, 0x41, 0xC5, 0x81, 0x81, 0x89};
gPatternLengths[0] = 16;
gPatterns[0] = (int *)malloc(gPatternLengths[0] * sizeof(int));
memcpy(gPatterns[0], pattern0, gPatternLengths[0] * sizeof(int));
gPatternLengths[1] = 32;
gPatterns[1] = (int *)malloc(gPatternLengths[1] * sizeof(int));
memcpy(gPatterns[1], pattern1, gPatternLengths[1] * sizeof(int));
gPatternLengths[2] = 16;
gPatterns[2] = (int *)malloc(gPatternLengths[2] * sizeof(int));
memcpy(gPatterns[2], pattern2, gPatternLengths[2] * sizeof(int));
gPatternLengths[3] = 32;
gPatterns[3] = (int *)malloc(gPatternLengths[3] * sizeof(int));
memcpy(gPatterns[3], pattern3, gPatternLengths[3] * sizeof(int));
gPatternLengths[4] = 16;
gPatterns[4] = (int *)malloc(gPatternLengths[4] * sizeof(int));
memcpy(gPatterns[4], pattern4, gPatternLengths[4] * sizeof(int));
gPatternLengths[5] = 16;
gPatterns[5] = (int *)malloc(gPatternLengths[5] * sizeof(int));
memcpy(gPatterns[5], pattern4, gPatternLengths[5] * sizeof(int));
}
void cleanupPatterns() {
for(int i = 0; i < NUMBER_OF_PATTERNS; i++)
free(gPatterns[i]);
}
int main(int argc, char *argv[])
{
BelaInitSettings settings; // Standard audio settings
struct option customOptions[] =
{
{"help", 0, NULL, 'h'},
{NULL, 0, NULL, 0}
};
// Set default settings
Bela_defaultSettings(&settings);
settings.setup = setup;
settings.render = render;
settings.cleanup = cleanup;
// Parse command-line arguments
while (1) {
int c;
if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
break;
switch (c) {
case 'h':
usage(basename(argv[0]));
exit(0);
case '?':
default:
usage(basename(argv[0]));
exit(1);
}
}
// Load the drum sounds and the patterns
if(initDrums()) {
printf("Unable to load drum sounds. Check that you have all the WAV files!\n");
return -1;
}
initPatterns();
// Initialise the PRU audio device
if(Bela_initAudio(&settings, 0) != 0) {
cout << "Error: unable to initialise audio" << endl;
return -1;
}
// Start the audio device running
if(Bela_startAudio()) {
cout << "Error: unable to start real-time audio" << endl;
return -1;
}
// Set up interrupt handler to catch Control-C
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
// Run until told to stop
while(!gShouldStop) {
usleep(100000);
}
// Stop the audio device and sensor thread
Bela_stopAudio();
// Clean up any resources allocated for audio
Bela_cleanupAudio();
// Clean up the drums
cleanupPatterns();
cleanupDrums();
// All done!
return 0;
}