-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullsystv6.c
328 lines (285 loc) · 10.8 KB
/
fullsystv6.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <sndfile.h>
#include <time.h>
#include <samplerate.h>
#include <pthread.h>
#include <bcm2835.h>
#define PCM_DEVICE "default"
#define converter 1
#define MAX_PLAY_RATE 5
#define NUMFILES 5
// GLOBAL STRUCT
typedef struct InputData {
float speed; // slide bar potentiometers, slave 1
float gain; // slide bar potentiometers, slave 1
//uint8 high_eq; // knobs, slave 1
//uint8 mid_eq; // knobs, slave 1
//uint8 low_eq; // knobs, slave 1
//uint8 fx1; // knobs, slave 1
//uint8 fx2; // knobs, slave 1
//uint8 fx3; // knobs, slave 1
//uint8 encoder; // turntable encoder count, slave 1
int next; // buttons, slave 2
int back; // buttons, slave 2
int play_pause; // buttons, slave 2
int trackno; //menu toggle encoder
//uint8 fx_onoff; // buttons, slave 2
} InputData;
InputData data = {1,.5,0,0,0,0};
int flag = 0;
void * thread_sampling(void * unused);
void gainfn(float *inputarr, int samples, int channels, float mult);
/**********************************************************************/
main(int argc, char **argv) {
// DECLARATIONS
float *filein[NUMFILES];
float *buffin = NULL;
float *eofin;
float *buffout;
short *final;
char *file[NUMFILES] = {"/home/pi/HI THIS IS DJ/mp2/mini-project/musik.wav", "/home/pi/HI THIS IS DJ/mp2/mini-project/wtm.wav", "/home/pi/HI THIS IS DJ/mp2/mini-project/aboveandbeyond.wav",
"/home/pi/HI THIS IS DJ/mp2/mini-project/TDFW.wav", "/home/pi/HI THIS IS DJ/mp2/mini-project/zedd.wav"};
//char *file2 = "got.wav";
int pcm, pcmrc;
int tmp;
int i = 0;
int *blah;
// SPI THREAD
pthread_t * spithread = malloc(sizeof(pthread_t));
// INITIALIZATIONS
SNDFILE *inputf[NUMFILES];//SNDFILE *inputf2
SF_INFO sfinf[NUMFILES];
SRC_DATA datastr;
SRC_STATE *statestr;
int error;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
// Open file and create pointer
for(i = 0; i < NUMFILES; i++){
inputf[i] = sf_open(file[i], SFM_READ, &(sfinf[i]));
if (inputf[i] == NULL) printf("COULD NOT OPEN FILE SORRY %d\n\n", i);
if ((filein[i] = malloc(sizeof(float)*sfinf[i].channels*sfinf[i].frames)) == NULL)
{
printf("MAN YOU OUT OF MEM for file %d\n\n", i);
return 0;
}
sf_readf_float(inputf[i], filein[i], sfinf[i].frames);
}
// sf_readf_float(inputf, filein , sfinf.frames);
if (pcm =snd_pcm_open(&handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0)< 0){
printf("Error: CANNOT OPEN PCM DEVICE\n");
}
//allocate default parameters
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
//set parameters
if (pcm = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)< 0){
printf("Cannot set interleaved mode\n");
}
if (pcm = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)< 0){
printf("CANNOT SET FORMAT\n");
}
if (pcm = snd_pcm_hw_params_set_channels(handle, params, sfinf[1].channels)< 0){
printf("CANNOT SET CHANNELS \n");
}
if (pcm = snd_pcm_hw_params_set_rate(handle, params, sfinf[1].samplerate, 0)< 0){
printf("CANNOT SET SAMPLERATES \n");
}
//write parameters
if (pcm = snd_pcm_hw_params(handle, params)< 0){
printf("CANNOT SET HARDWARE PARAMETERS\n");
}
printf("PCm NAME: %s\n", snd_pcm_name(handle));
printf("PCM state: %s \n", snd_pcm_state_name(snd_pcm_state(handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i\n", tmp);
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d \n", tmp);
snd_pcm_hw_params_get_period_size(params, &frames, 0);
fprintf(stderr, "# frames in a period: %d\n", frames);
buffout = malloc(frames*sfinf[1].channels * sizeof(float));//frames
final = malloc(frames*sfinf[1].channels * sizeof(short));
statestr = src_new (converter, sfinf[1].channels, &error);
datastr.end_of_input = 0;
datastr.data_out = buffout;
datastr.output_frames = frames;//frames
while (i != 50){
memset(final, 0, frames*sfinf[1].channels);
pcmrc = snd_pcm_writei(handle, final, frames);//frames
if (pcmrc == -EPIPE){//this is where the error happens on the DAc for like 25 - 500 ms it underruns but recovers, so i dont know
printf("UNDERRUN!\n");
snd_pcm_prepare(handle);
}
else if(pcmrc < 0){
printf("ERROR WRITING!!!\n");
}
i++;
}
// create the thread wooooo
pthread_create(spithread, NULL, thread_sampling, NULL);
for (;;) {
/***************** START PLAYING MUSIC *******************/
int prevtrack = data.trackno;
int finish = 0;;
buffin = filein[prevtrack];
eofin = filein[prevtrack] + sfinf[prevtrack].frames * sfinf[prevtrack].channels ;
while(buffin != eofin){
datastr.data_in = buffin;
datastr.input_frames = frames*MAX_PLAY_RATE;
if (data.play_pause == 1 && data.next == 0 && data.back == 0 && data.trackno == prevtrack){
if (src_set_ratio(statestr, data.speed) != 0){
printf("Could not reset ratio\n");
}
if ((eofin - buffin) < frames*MAX_PLAY_RATE) {
/// if you read less than a full frame it says i am done
memset(buffout, 0.0, frames*sfinf[prevtrack].channels*sizeof(float));//frames
finish = 1;
data.trackno++;
break;
if( (eofin - buffin) < frames) {
datastr.end_of_input = SF_TRUE;
finish = 1;
break;
}
}
datastr.src_ratio = data.speed;
if ((error = src_process(statestr, &datastr))){
printf("\n\nERRORR converting: %s\n\n", src_strerror(error));
exit(1);
}
buffin = buffin + datastr.input_frames_used*sfinf[prevtrack].channels;
//datastr.input_frames -= datastr.input_frames_used;
//printf("number of frames generated %d \n", datastr.output_frames_gen);
gainfn(buffout, frames, sfinf[prevtrack].channels, data.gain);
src_float_to_short_array(buffout, final, frames*sfinf[prevtrack].channels);//frames
usleep(3000); //even with all the processing we can still sleep for like 3000;
pcmrc = snd_pcm_writei(handle, final, frames);//frames
if (pcmrc == -EPIPE){//this is where the error happens on the DAc for like 25 - 500 ms it underruns but recovers, so i dont know
printf("UNDERRUN!\n");
snd_pcm_prepare(handle);
}
else if(pcmrc < 0){
printf("ERROR WRITING!!!\n");
}
else if (pcmrc != datastr.output_frames_gen){
printf("WRITE != READ\n");
}
}
else if (data.play_pause == 0){
if (data.trackno != prevtrack)
break;
continue;
}
else if (data.next == 1 || data.back == 1 || (data.trackno != prevtrack)) {
break;
}
}
data.next = 0;
data.back = 0;
if (finish){
printf("FILE ENDED");
data.trackno++;
if (data.trackno >= NUMFILES) data.trackno = NUMFILES - 1;
continue;
}
else continue;
}
free(filein);
free(buffout);
free(final);
free(spithread);
// free(data);
return 0;
}
// entry function for the sampling thread
void * thread_sampling(void * unused)
{
// INITIALIZE SPI FOR BOTH WRITER AND READER
if (!bcm2835_init()) {return NULL;}
int i = 0;
uint8_t send_data[11] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0};
volatile uint8_t rec_data[12] = {0};
uint8_t read_data = 0;
// BEGIN SPI
bcm2835_spi_begin();
while (1){
// WRITER SPI
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
for (i=0;i<11;i++){
read_data = bcm2835_spi_transfer(send_data[i]);//should be read_data in working code
//printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data[i], read_data);
rec_data[i] = read_data;
//printf("Received data = 0x%02X\n",read_data); // DO NOT REMOVE!@$&*!^$*(!#&
read_data = 0;
//putting speed and gain inside for loop
usleep(1000);
data.speed = ((1.4/255) * rec_data[0]) + 0.3;
/* printf("Speed = %.1f\n"); */
data.gain = ((1.8/255) * rec_data[1]) + .2;
}
//data.play_pause = rec_data[10] & 0x80;
//new for loop end
// READER SPI
// initialize
// bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
// bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
//bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128);
bcm2835_spi_chipSelect(BCM2835_SPI_CS1);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1,LOW);
for(i = 0; i < 2; i++) {
read_data = bcm2835_spi_transfer(send_data[i]);
//printf("Received data = 0x%02X\n",read_data); // DO NOT REMOVE!@$&*!^$*(!#&
rec_data[i + 10] = read_data;
usleep(10);
}
data.trackno = rec_data[10];
//printf("Track no = 0x%02x\n", data.trackno);
if (rec_data[11] & 0x80){
data.next = 1;
data.trackno++;
if (data.trackno >= NUMFILES){
data.trackno = NUMFILES - 1;
data.next = 0;
}
printf("Track no = 0x%02X\n",data.trackno);
}
if (rec_data[11] & 0x10){
data.back = 1;
data.trackno--;
if (data.trackno <= 0){
data.trackno = 0;
}
printf("Track no = 0x%02X\n",data.trackno);
}
if(rec_data[11] & 0x20) {
data.play_pause = !data.play_pause;
}
// read_data = bcm2835_spi_transfer(0x10);
// usleep(10);
// data.play_pause = read_data & 0x80;
//if (read_data & 0x80) data.play_pause = !data.play_pause;
//data.next = read_data & 0x40;
//data.back = read_data & 0x20;
// read_data = bcm2835_spi_transfer(0x00);
// usleep(10);
//data.trackno = read_data;
// printf("Track no = 0x%02x\n",data.trackno); // probz keep too
}
// end SPI routine
bcm2835_spi_end();
bcm2835_close();
return 0;
}
void gainfn(float *inputarr, int samples, int channels , float mult){
int i = 0;
for (i = 0; i<channels*samples;i++){
inputarr[i] = inputarr[i] * mult;
}
}