-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectrometer.cpp
673 lines (637 loc) · 22.5 KB
/
Spectrometer.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include "Spectrometer.h"
using namespace std;
using namespace rgb_matrix;
Spectrometer::Spectrometer(char* config_path)
{
srand (time(NULL));
this->displayMode = Bars;
this->running = false;
this->InitializeAudioDevice();
this->InitializeFFT();
this->InitializeLEDMatrix(config_path);
this->InitializeBitmaps();
return;
}
void Spectrometer::GetBins(short* buffer, int* bins)
{
// initialize parameters
int full_count = 1<<FFT_LOG;
float frequencies[BIN_COUNT + 1]= {20.0,50.0,100.0,150.0,200.0,250.0,300.0,350.0,400.0,500.0,600.0,750.0,1000.0,2000.0,3000.0,5000.0,7500.0};
float maxs[BIN_COUNT];
float value = 0.0;
int i=0, j=0;
// assign fft input
for (i=0; i<full_count; i++)
{
fft->in[i].re = (float)buffer[i];
fft->in[i].im = 0.0;
}
// reset maxs
for (j=0; j<BIN_COUNT; j++)
{
maxs[j] = 0.0;
}
// execute fft
gpu_fft_execute(this->fft); // call one or many times
// calculate results
for(i=0; i<full_count/2; i++)
{
// calculate frequency
float frequency = (float)i * ((float)(SAMP_RATE)/(float)(full_count));
// iterate through frequency bins
// sort results into discrete frequency bins
for(j=0; j<BIN_COUNT; j++)
{
if(frequency >= frequencies[j] && frequency < frequencies[j+1])
{
// calculate result vector
value = sqrt(pow(this->fft->out[i].re, 2) + pow(this->fft->out[i].re, 2));
maxs[j] = fmax(maxs[j], value);
bins[j] = (int)maxs[j];
break;
}
}
}
return;
}
unsigned int Spectrometer::GetBitmapSetIndex(float seconds)
{
float full_cycle = this->config->getImageSetDuration();
float divisor = this->logos.size();
float interval = full_cycle/divisor;
int value = (int)seconds%(int)full_cycle;
unsigned int index = fmin((float)value/interval, (float)(this->logos.size()-1));
return index;
}
unsigned int Spectrometer::GetBitmapIndex(float seconds, int set_index)
{
int weight = (int)(100.0*this->config->getAnimationDuration(set_index));
int value = (int)(seconds*100.0)%weight;
int image_count = this->config->getImageCount(set_index);
int loop_image_count = 1;
if(image_count > 1)
loop_image_count = ((image_count-2)*2)+2;
int divisor = weight/loop_image_count;
int index = value/divisor;
if(index >= image_count)
{
index = image_count - 2 - (index - image_count);
}
index = (int)fmax(fmin(index, image_count-1), 0);
return index;
}
int Spectrometer::GetRandomNumber(int min, int max)
{
return rand() % max + min;
}
void Spectrometer::InitializeAudioDevice()
{
fprintf(stderr, "Initializing Audio Device\n");
int err;
/* Open the PCM device in playback mode */
if((err = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_CAPTURE , 0)) < 0)
{
fprintf (stderr, "cannot open audio device %s (%s)\n", PCM_DEVICE, snd_strerror (err));
exit (1);
}
/* Allocate parameters object and fill it with default values*/
fprintf(stderr, "\tAllocating parameters object\n");
snd_pcm_hw_params_t *params;
if((err = snd_pcm_hw_params_malloc(¶ms)) < 0)
{
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
exit (1);
}
fprintf(stderr, "Initializing parameters object\n");
if((err = snd_pcm_hw_params_any(pcm_handle, params)) < 0)
{
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
}
/* Set parameters */
if((err = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
exit (1);
}
if((err = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE)) < 0)
{
fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
exit (1);
}
if((err = snd_pcm_hw_params_set_channels(pcm_handle, params, 1)) < 0)
{
fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
exit (1);
}
if((err = snd_pcm_hw_params_set_rate(pcm_handle, params, SAMP_RATE, 0)) < 0)
{
fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
exit (1);
}
if((err = snd_pcm_hw_params(pcm_handle, params)) < 0)
{
fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (params);
if ((err = snd_pcm_prepare (pcm_handle)) < 0)
{
fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
exit (1);
}
fprintf(stderr,"Successfully Initialized Audio Device\n");
return;
}
/*
SNDFILE* InitializeAudioFile(char* path)
{
SNDFILE *infile = NULL;
fprintf(stderr, "\tFile: %s\n", path);
infile = sf_open(path, SFM_READ, &sfinfo);
fprintf(stderr,"\tChannels: %d\n", sfinfo.channels);
fprintf(stderr,"\tSample rate: %d\n", sfinfo.samplerate);
fprintf(stderr,"\tSections: %d\n", sfinfo.sections);
fprintf(stderr,"\tFormat: %d\n", sfinfo.format);
return infile;
}
*/
void Spectrometer::InitializeBitmaps()
{
this->logos.clear();
for(int i=0; i<this->config->getImageSetCount(); i++)
{
std::vector<unsigned char*> *new_logos = new std::vector<unsigned char*>();
for(int j=0; j<this->config->getImageCount(i); j++)
{
new_logos->push_back(new unsigned char[this->panelWidth * this->panelHeight * 3]);
const char * path = this->config->getImage(i,j);
this->ReadBitmap(path, (*new_logos)[j]);
}
this->logos.push_back(new_logos);
}
return;
}
void Spectrometer::InitializeFFT()
{
fprintf(stderr, "Initializing FFT\n");
this->mailbox = mbox_open();
int ret = gpu_fft_prepare(this->mailbox, FFT_LOG, GPU_FFT_REV, FFT_JOBS, &(this->fft));
switch(ret)
{
case -1: throw std::runtime_error("Unable to enable V3D. Please check your firmware is up to date.\n");
case -2: throw std::runtime_error("log2_N=%d not supported. Try between 8 and 22.\n");
case -3: throw std::runtime_error("Out of memory. Try a smaller batch or increase GPU memory.\n");
case -4: throw std::runtime_error("Unable to map Videocore peripherals into ARM memory space.\n");
case -5: throw std::runtime_error("Can't open libbcm_host.\n");
}
int buffer_size = 1<<FFT_LOG;
fprintf(stderr, "\tBuffer Size: %d\n", buffer_size);
fprintf(stderr, "Successfully Initialized FFT\n");
return;
}
void Spectrometer::InitializeLEDMatrix(char* config_path)
{
fprintf(stderr, "Initializing LED Matrix\n");
fprintf(stderr, "\tFile: %s\n", config_path);
// initialize parameters
this->config = new Config(config_path);
int width = this->config->getPanelWidth();
int height = this->config->getPanelHeight();
int chain_length = this->config->getChainLength();
int parallel_count = this->config->getParallelCount();
this->panelWidth = width * chain_length;
this->panelHeight = height;
fprintf(stderr, "\tSize: %d x %d\n", this->panelWidth, this->panelHeight);
// Initialize GPIO
if (!this->io.Init())
{
throw runtime_error("Error while initializing rpi-led-matrix library");
}
// Initialize Canvas
this->canvas = new RGBMatrix(&io, height, chain_length, parallel_count);
this->grid = this->config->getGridTransformer();
// set cutoff (at least one color value must be greater than this value in order for the pixel to be displayed)
int cutoff = this->config->getLEDCutoff();
fprintf(stderr, "\tCutoff: %d\n", cutoff);
this->grid->SetCutoff(cutoff);
// set max pixel value (aka 'brightness')
int max_brightness = this->config->getLEDMaxBrightness();
fprintf(stderr, "\tBrightness: %d\n", max_brightness);
this->grid->SetMaxBrightness(max_brightness);
// turn on horizontal mirroring (due to incorrect wiring)
this->grid->SetMirrorX(true);
// set grid transformer (for virtual calls)
this->canvas->SetTransformer(grid);
// clear canvas
this->canvas->Fill(0, 0, 0);
fprintf(stderr, "Successfully Initialized LED Matrix\n");
return;
}
void Spectrometer::NormalizeBins(int bins[][BIN_COUNT], int normalized_bins[][BIN_COUNT], FFTOptions options)
{
// initialize parameters
int i=0, j=0;
int min = 999999999, max = -999999999, bin_max = -999999999;
// calculate highest and lowest peaks of a given frequency bin
for(j=0; j<BIN_COUNT; j++)
{
// reset current frequency bin max
bin_max = 0;
// iterate through all bin history (even not displayed ones)
for(i=0; i<TOTAL_BIN_DEPTH; i++)
{
// convert to db
if((options & Logarithmic) != 0)
{
normalized_bins[i][j] = 20.0 * log10(bins[i][j]);
}
// ignore negative values/db
normalized_bins[i][j] = fmax(normalized_bins[i][j], 0);
// calculate max for all history of current frequency bin
bin_max = fmax(bin_max, normalized_bins[i][j]);
// calculate max for all history of all frequency bins
max = fmax(max, normalized_bins[i][j]);
}
// calculate smallest peak occurring to any given frequency bin over all history
min = fmin(min, bin_max);
}
// calculate range
int range = max-min;
float ratio = 0.5;
// normalize displayed bins only
for(i=0; i<BIN_DEPTH; i++)
{
for(j=0; j<BIN_COUNT; j++)
{
// autoscale
if((options & Autoscale) != 0)
{
// calculate ratio (0.0 -> 1.0)
ratio = (float)(normalized_bins[i][j] - min)/(float)range;
// cull negative values (i.e. amplitudes which are underrange)
ratio = fmax(ratio, 0.0);
//fprintf(stderr, "%f\n", ratio);
normalized_bins[i][j] = FULL_SCALE*ratio;
}
// apply sigmoid approximation (emphasize peaks and scale 0.0-100.0)
if((options & Sigmoid) != 0)
{
normalized_bins[i][j] = this->SigmoidFunction((double)normalized_bins[i][j]);
}
}
}
return;
}
void Spectrometer::PrintBars(int bins[][BIN_COUNT])
{
// intialize parameters
int col_width = (float)this->panelWidth/(float)BIN_COUNT;
int i=0,j=0,r=0,g=0,b=0,x=0,x_index=0,y=0,bar_height=0;
float ratio=0.0,gain=0.0,r_gain=0.0,g_gain=0.0,b_gain=0.0;
for(i=0; i<BIN_DEPTH; i++)
{
// calculate base gain (based on age)
gain = (float)(BIN_DEPTH - i - 1)/(float)BIN_DEPTH;
// iterate through bins
for(j=0; j<BIN_COUNT; j++)
{
// calculate amplitude
ratio = (float)bins[i][j] / FULL_SCALE;
bar_height = (int)fmax(ratio * (float)this->panelHeight, 0.0);
// iterate across
for(x=0; x<col_width; x++)
{
// calculate x index
x_index = j * col_width + x;
// iterate upwards
for(y=0; y<=bar_height && y<this->panelHeight; y++)
{
// check if pixel is already occupied
if(this->grid->GetPixelState(x_index, y))
continue;
// calculate colors
r_gain = fmax(gain * ratio, 0.2); // increases with amplitude
g_gain = gain * ((float)(j+1)/(float)BIN_COUNT); // increases with frequency
b_gain = gain * ((float)(BIN_COUNT-j)/(float)BIN_COUNT);
r = 120.0*r_gain;
g = 120.0*g_gain;
b = 120.0*b_gain;
// draw
this->canvas->SetPixel(x_index, y, r, g, b);
}
}
}
}
return;
}
void Spectrometer::PrintBitmap(int bins[][BIN_COUNT], unsigned char * data)
{
// initialize parameters
int x = 0, y = 0, r = 0, g = 0, b = 0, i = 0, j=0;
float decay = 0.0, bin_gain = 0.0, red_gain = 0.0, green_gain = 0.0, blue_gain = 0.0;
// calculate color gains based on audio frequency content
// iterate through bins, depthwise
for(i=0; i<BIN_DEPTH; i++)
{
// calculate decay (based on age)
decay = (float)(BIN_DEPTH - i)/(float)BIN_DEPTH;
// iterate through all frequency bins (of a given depth)
for(j=0; j<BIN_COUNT; j++)
{
// calculate base gain (based on bin amplitude) (0.0 -> 2.0)
bin_gain = (float)bins[i][j] / (FULL_SCALE/2.0);
// increases with bin frequency (0.1 -> 1.0)
blue_gain = fmax(bin_gain * ((float)(j+1)/(float)BIN_COUNT)*decay, blue_gain);
// increases towards center frequency (0.1 -> 1.0 -> 0.1)
green_gain = fmax(bin_gain * ((float)(BIN_COUNT/2-abs((j+1)-BIN_COUNT/2))/(float)(BIN_COUNT/2))*decay, green_gain);
// decreases with bin frequency (1.0 -> 0.1)
red_gain = fmax(bin_gain * ((float)(BIN_COUNT - j)/(float)BIN_COUNT)*decay, red_gain);
}
}
// iterate through each pixel in the bitmap
for(x =0; x<this->panelWidth; x++)
{
for(y=0; y<this->panelHeight; y++)
{
// check if pixel is already occupied
if(this->grid->GetPixelState(x,y))
continue;
// calculate index into single dimensional array of pixel data
int index = y * this->panelWidth * 3 + x*3;
// check for black pixel (reduce calculation time)
if(((int)data[index] < 1) && ((int)data[index+1] < 1) && ((int)data[index+2] < 1))
{
this->grid->SetPixel(x, y, 0, 0, 0);
continue;
}
// calculate color
r = (float)data[index]*red_gain;
g = (float)data[index+1]*green_gain;
b = (float)data[index+2]*blue_gain;
// draw
this->grid->SetPixel(x, y, r, g, b);
}
}
return;
}
void Spectrometer::PrintText(int x, int y, const string& message, int r, int g, int b)
{
// Loop through all the characters and print them starting at the provided
// coordinates.
for (auto c: message) {
// Loop through each column of the character.
for (int i=0; i<5; ++i) {
unsigned char col = glcdfont[c*5+i];
x += 1;
// Loop through each row of the column.
for (int j=0; j<8; ++j)
{
// Put a pixel for each 1 in the column byte.
if ((col >> j) & 0x01)
{
this->canvas->SetPixel(x, y+j, r, g, b);
}
}
}
// Add a column of padding between characters.
x += 1;
}
}
void Spectrometer::PrintRadial(int bins[][BIN_COUNT], float seconds)
{
// intialize parameters
int i=0,j=0,k=0,m=0,r=0,g=0,b=0,x=0,y=0,x_vector=0,y_vector=0;
int base_vector = fmax(this->panelWidth/2,this->panelHeight/2);
float freq_ratio=0.0,amplitude_ratio=0.0,decay=0.0,base_angle=0.0,angle=0.0,red_gain=1.0,blue_gain=1.0,green_gain=1.0;
float angle_offset = (M_PI * 2.0)*(seconds/5.0);
this->canvas->SetPixel(31, 15, 0, 0, 0);
// iterate through bins depthwise
for(i=0; i<BIN_DEPTH; i++)
{
// calculate decay (1 -> 0)
decay = (float)(BIN_DEPTH-i)/(float)BIN_DEPTH;
// iterate through bins
for(j=0; j<BIN_COUNT; j++)
{
// frequency ratio ([low frequency] 0.0 -> 1.0 [high frequency])
freq_ratio = (float)(j)/(float)BIN_COUNT;
// red gain ([low frequency] 0.0 -> 2.0 [high frequency])
red_gain = freq_ratio*2.0;
// green gain ([low frequency] 0.0 -> 2.0 [center frequency] -> 0.0 [high frequency])
green_gain = ((float)(BIN_COUNT/2-abs(j-BIN_COUNT/2))/(float)(BIN_COUNT/2))*2.0;
// blue gain ([low frequency] 2.0 -> 0.0 [high frequency])
blue_gain = (1.0 - freq_ratio)*2.0;
// amplitude ratio ([low amplitude] 0.0 -> 1.0 [high amplitude])
amplitude_ratio = (float)(bins[i][j]) / FULL_SCALE;
//fprintf(stderr,"%f\n",amplitude_ratio);
// angle ([low frequency] 0 rad -> 2pi rad [high frequency])
base_angle = freq_ratio * M_PI * 2.0 +angle_offset;
// calculate color(s) (based on frequency and age)
r = 100.0*red_gain*decay;
b = 100.0*blue_gain*decay;
g = 100.0*green_gain*decay;
// iterate through set of close angles (i.e. "fan out")
for(k =0; k<RADIAL_FAN_COUNT; k++)
{
// fan angle
angle = base_angle + (M_PI*(k-2))/RADIAL_FAN_SPACING;
// calculate output coordinates (center point + vectors)
x_vector = (int)((float)base_vector * sin(angle)*amplitude_ratio);
y_vector = (int)((float)base_vector * cos(angle)*amplitude_ratio);
float factor = 1.0, color_factor = 1.0;;
// draw a "ray" from center point outwards
for(m=50; m>0; m--)
{
// start in middle, add x and y vectors
x = (int)((float)this->panelWidth/2.0 + (float)x_vector*factor);
y = (int)((float)this->panelHeight/2.0 + (float)y_vector*factor);
factor -= 0.02;
color_factor = sqrt(factor);
this->canvas->SetPixel(x, y, r*color_factor, g*color_factor, b*color_factor);
}
}
}
}
return;
}
void Spectrometer::ReadBitmap(const char* filename, unsigned char* data)
{
if(filename == NULL or sizeof(filename) < 1)
throw "Invalid bitmap filename";
fprintf(stderr, "Reading bitmap (%s)\n", filename);
// open file
FILE* f = fopen(filename, "rb");
if(f == NULL)
throw "Argument Exception";
// read header
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f);
int width = *(int*)&info[18];
int height = *(int*)&info[22];
// assert correct dimensions
assert(width == this->config->getPanelWidth() * this->config->getChainLength());
assert(height == this->config->getPanelHeight());
//
int row_padded = (width*3 + 3) & (~3);
unsigned char tmp;
int x = 0, y = 0, index = 0;
for(y = 0; y < height; y++)
{
fread(&data[y*row_padded], sizeof(unsigned char), row_padded, f);
for(x = 0; x < width; x++)
{
// Convert (B, G, R) to (R, G, B)
index = y*row_padded+(x*3);
tmp = data[index];
data[index] = data[index+2];
data[index+2] = tmp;
}
}
fclose(f);
fprintf(stderr, "Successfully read bitmap\n");
return;
}
double Spectrometer::SigmoidFunction(double value)
{
/*
through testing I have found that in order to approach a desired full scale value, the left-hand side constant (in the demoninator)
needs to be equal to the numerator divided by the desired full scale
*/
double constant = SIGMOID_NUMERATOR/FULL_SCALE;
return SIGMOID_NUMERATOR/(constant+pow(M_E,-1.0*((value-SIGMOID_OFFSET)/SIGMOID_SLOPE)));
}
void Spectrometer::Start()
{
fprintf(stderr, "Initializing display loop\n");
// initialize
this->running = true;
this->canvas->Fill(0, 0, 0);
this->canvas->SetPixel(1, 1, 50, 50, 50);
int bins[TOTAL_BIN_DEPTH][BIN_COUNT];
int normalized_bins[TOTAL_BIN_DEPTH][BIN_COUNT];
int i=0, j=0;
for(i=0; i<TOTAL_BIN_DEPTH; i++)
{
for(j=0; j<BIN_COUNT; j++)
{
bins[i][j] = normalized_bins[i][j] = 0;
}
}
// create buffers
int err;
int buffer_size = 1<<FFT_LOG;
short buf[buffer_size];
// loop
fprintf(stderr, "Successfully initialized display loop\n");
fprintf(stderr, "Starting display loop\n");
const clock_t begin_time = clock();
float seconds = 0.0;
while((err = snd_pcm_readi (pcm_handle, buf, buffer_size)) == buffer_size)
{
if(!this->running)
{
fprintf(stderr, "\nAborting\n");
break;
}
// move bins
for(i=TOTAL_BIN_DEPTH-1; i>0; i--)
{
for(j=0; j<BIN_COUNT; j++)
{
bins[i][j] = bins[i-1][j];
}
}
// retrieve new bins
this->GetBins(buf, &bins[0][0]);
seconds = (float)( clock () - begin_time ) / (float)CLOCKS_PER_SEC;
FFTOptions options = None;
/*
if((int)seconds%60<=20 && this->displayMode != Bitmap)
{
this->displayMode = Bitmap;
}
else if((int)seconds%60>20 && (int)seconds%60<=40 && this->displayMode != Radial)
{
this->displayMode = Radial;
}
else if((int)seconds%60>40 && this->displayMode != Bars)
{
this->displayMode = Bars;
}*/
this->displayMode = Bitmap;
options = Logarithmic|Autoscale|Sigmoid;
this->NormalizeBins(bins, normalized_bins, options);
unsigned int bitmap_set_index = this->GetBitmapSetIndex(seconds);
unsigned int bitmap_index = this->GetBitmapIndex(seconds, bitmap_set_index);
this->PrintBitmap(normalized_bins, (*this->logos[bitmap_set_index])[bitmap_index]);
// display
/*switch(this->displayMode)
{
case Bars:
options = Logarithmic|Autoscale|Sigmoid;
this->NormalizeBins(bins, normalized_bins, options);
this->PrintBars(normalized_bins);
break;
case Bitmap:
<<<<<<< HEAD
options = Logarithmic|Autoscale|Sigmoid;
this->NormalizeBins(bins, normalized_bins, options);
this->PrintBitmap(normalized_bins, this->logos[this->GetBitmapIndex(seconds)]);
=======
>>>>>>> ImageSet
break;
case Radial:
options = Logarithmic|Autoscale;
this->NormalizeBins(bins, normalized_bins, options);
this->PrintRadial(normalized_bins, seconds);
break;
default:
break;
}*/
this->grid->FillRemaining(0,0,0);
// sleep
usleep(1000);
}
fprintf(stderr, "Exiting display loop\n");
return;
}
void Spectrometer::Stop()
{
this->running = false;
return;
}
Spectrometer::~Spectrometer()
{
// abort
fprintf(stderr, "Cleaning up...\n");
this->Stop();
// clear canvas
fprintf(stderr, "\tClearing canvas\n");
this->canvas->Clear();
// close audio device
fprintf(stderr, "\tReleasing audio device\n");
snd_pcm_drain(this->pcm_handle);
snd_pcm_close(this->pcm_handle);
// release FFT
fprintf(stderr, "\tReleasing fft data\n");
gpu_fft_release(this->fft);
fprintf(stderr, "\tDeleting logo pointers\n");
for(unsigned int i=0; i<this->logos.size(); i++)
{
for(unsigned int j=0; j<this->logos[i]->size(); j++)
{
delete (*this->logos[i])[j];
}
delete this->logos[i];
}
//delete this->logos;
fprintf(stderr, "\tDeleting grid pointer\n");
delete this->grid;
fprintf(stderr, "\tDeleting canvas pointer\n");
delete this->canvas;
fprintf(stderr, "\tDeleting LED configuration pointer\n");
delete this->config;
fprintf(stderr, "Successfully cleaned up\n");
return;
}