-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathVisualizer.cpp
1683 lines (1450 loc) · 58.2 KB
/
Visualizer.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------*\
| Processing Code for Keyboard Visualizer |
| |
| Adam Honse ([email protected]), 12/11/2016 |
\*---------------------------------------------------------*/
#include "Visualizer.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
/*---------------------------------------------------------*\
| Global variables |
\*---------------------------------------------------------*/
char * net_string;
int ledstrip_sections_size = 1;
int matrix_setup_pos;
int matrix_setup_size;
float fft_nrml[256];
float fft_fltr[256];
bool ledstrip_mirror_x = false;
bool ledstrip_mirror_y = false;
bool ledstrip_single_color = false;
int ledstrip_rotate_x = 0;
/*---------------------------------------------------------*\
| Visualizer class implementation |
\*---------------------------------------------------------*/
Visualizer::Visualizer()
{
}
void Visualizer::InitAudioDeviceList()
{
#ifdef WIN32
IMMDevice* pEndpoint;
IPropertyStore* pProps;
PROPVARIANT* varName;
//If using WASAPI, start WASAPI loopback capture device
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pMMDeviceEnumerator);
for (int i = 0; i < pMMDevices.size(); i++)
{
pMMDevices[i]->Release();
if (i != 0)
{
delete audio_devices[i];
}
}
pMMDevices.clear();
audio_devices.clear();
isCapture.clear();
//Enumerate default audio output
//pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint);
//audio_devices.push_back("Default Loopback Device");
//pMMDevices.push_back(pEndpoint);
//isCapture.push_back(false);
//Enumerate audio outputs
pMMDeviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pMMDeviceCollection);
if (pMMDeviceCollection != NULL)
{
UINT count;
pMMDeviceCollection->GetCount(&count);
for (UINT i = 0; i < count; i++)
{
varName = new PROPVARIANT();
//Query the item from the list
pMMDeviceCollection->Item(i, &pEndpoint);
//Open property store for the given item
pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
//Get the friendly device name string
pProps->GetValue(PKEY_Device_FriendlyName, varName);
if (varName->pwszVal != NULL)
{
int len = wcslen(varName->pwszVal) + 1;
char* new_device = new char[len + 11];
wcstombs(new_device, varName->pwszVal, len);
strncat(new_device, " (Loopback)", len);
audio_devices.push_back(new_device);
pMMDevices.push_back(pEndpoint);
isCapture.push_back(false);
}
delete varName;
pProps->Release();
}
}
pMMDeviceCollection->Release();
//Enumerate audio inputs
pMMDeviceEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pMMDeviceCollection);
if (pMMDeviceCollection != NULL)
{
UINT count;
pMMDeviceCollection->GetCount(&count);
for (UINT i = 0; i < count; i++)
{
varName = new PROPVARIANT();
//Query the item from the list
pMMDeviceCollection->Item(i, &pEndpoint);
//Open property store for the given item
pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
//Get the friendly device name string
pProps->GetValue(PKEY_Device_FriendlyName, varName);
if (varName->pwszVal != NULL)
{
int len = wcslen(varName->pwszVal) + 1;
char* new_device = new char[len];
wcstombs(new_device, varName->pwszVal, len);
audio_devices.push_back(new_device);
pMMDevices.push_back(pEndpoint);
isCapture.push_back(true);
}
delete varName;
pProps->Release();
}
}
pMMDeviceEnumerator->Release();
#else
//If using OpenAL, start OpenAL capture on default capture device
ALCchar* devices;
devices = (ALCchar *) alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
//Loop through detected capture devices and stop at the configured one
char devicestring[512];
char *devicep = devicestring;
for(int i = 0; i < 512; i++)
{
*devicep = *devices;
if(*devicep == '\0')
{
if(strlen(devicestring) > 0)
{
char* new_device = new char[strlen(devicestring) + 1];
strcpy(new_device, devicestring);
audio_devices.push_back(new_device);
}
i = 0;
devices++;
if(*devicep == '\0' && *devicestring == '\0')
{
break;
}
devicep = devicestring;
}
else
{
devices++;
devicep++;
}
}
#endif
}
void Visualizer::ChangeAudioDevice()
{
#ifdef WIN32
if (pAudioClient != NULL)
{
pAudioClient->Stop();
pAudioClient->Release();
pAudioClient = NULL;
}
if (audio_device_idx < audio_devices.size())
{
IMMDevice* pMMDevice = pMMDevices[audio_device_idx];
pMMDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&pAudioClient);
pAudioClient->GetMixFormat(&waveformat);
if (isCapture[audio_device_idx])
{
pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, waveformat, 0);
}
else
{
pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, waveformat, 0);
}
pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pAudioCaptureClient);
pAudioClient->Start();
}
else if (audio_devices.size() > 0)
{
audio_device_idx = 0;
ChangeAudioDevice();
}
#else
if(device != NULL)
{
alcCaptureStop(device);
}
if(audio_device_idx < audio_devices.size())
{
device = alcCaptureOpenDevice(audio_devices[audio_device_idx], 10000, AL_FORMAT_MONO8, 2048);
alcCaptureStart(device);
}
else if(audio_devices.size() > 0)
{
audio_device_idx = 0;
ChangeAudioDevice();
}
#endif
}
void Visualizer::Initialize()
{
InitAudioDeviceList();
netmode = NET_MODE_DISABLED;
background_timer = 0;
background_timeout = 120;
amplitude = 100;
anim_speed = 100.0f;
avg_mode = 0;
avg_size = 8;
bkgd_step = 0;
bkgd_bright = 100;
bkgd_mode = VISUALIZER_PATTERN_ANIM_RAINBOW_SINUSOIDAL;
delay = 25;
window_mode = 1;
decay = 80;
frgd_mode = VISUALIZER_PATTERN_STATIC_GREEN_YELLOW_RED;
single_color_mode = VISUALIZER_SINGLE_COLOR_FOLLOW_FOREGROUND;
reactive_bkgd = false;
start_from_bot_inv = false;
start_from_bottom = false;
audio_device_idx = 0;
filter_constant = 1.0f;
settings_changed = false;
update_ui = false;
shutdown_flag = false;
hanning(win_hanning, 256);
hamming(win_hamming, 256);
blackman(win_blackman, 256);
nrml_ofst = 0.04f;
nrml_scl = 0.5f;
pixels_render = &pixels_vs1;
pixels_out = &pixels_vs2;
ChangeAudioDevice();
SetNormalization(nrml_ofst, nrml_scl);
}
void Visualizer::InitClient(char * clientstring)
{
if (netmode == NET_MODE_DISABLED)
{
net_string = new char[strlen(clientstring) + 1];
strcpy(net_string, clientstring);
LPSTR client_name;
LPSTR port_name;
client_name = strtok_s(clientstring, ",", &port_name);
netmode = NET_MODE_CLIENT;
port = new net_port();
port->tcp_client(client_name, port_name);
}
}
void Visualizer::InitServer(char * serverstring)
{
if (netmode == NET_MODE_DISABLED)
{
net_string = new char[strlen(serverstring) + 1];
strcpy(net_string, serverstring);
netmode = NET_MODE_SERVER;
port = new net_port();
port->tcp_server(serverstring);
}
}
void Visualizer::SaveSettings()
{
std::ofstream outfile;
char filename[2048];
char out_str[1024];
//Check background flags, they both should not be set
if ((silent_bkgd == TRUE) && (reactive_bkgd == TRUE))
{
silent_bkgd = FALSE;
}
//Set filename
strcpy(filename, "settings.txt");
//Open settings file
outfile.open(filename);
//Save Amplitude
snprintf(out_str, 1024, "amplitude=%d\r\n", amplitude);
outfile.write(out_str, strlen(out_str));
//Save Background Brightness
snprintf(out_str, 1024, "bkgd_bright=%d\r\n", bkgd_bright);
outfile.write(out_str, strlen(out_str));
//Save Average Size
snprintf(out_str, 1024, "avg_size=%d\r\n", avg_size);
outfile.write(out_str, strlen(out_str));
//Save Decay
snprintf(out_str, 1024, "decay=%d\r\n", decay);
outfile.write(out_str, strlen(out_str));
//Save Delay
snprintf(out_str, 1024, "delay=%d\r\n", delay);
outfile.write(out_str, strlen(out_str));
//Save Normalization Offset
snprintf(out_str, 1024, "nrml_ofst=%f\r\n", nrml_ofst);
outfile.write(out_str, strlen(out_str));
//Save Normalization Scale
snprintf(out_str, 1024, "nrml_scl=%f\r\n", nrml_scl);
outfile.write(out_str, strlen(out_str));
//Save Filter Constant
snprintf(out_str, 1024, "fltr_const=%f\r\n", filter_constant);
outfile.write(out_str, strlen(out_str));
//Save Window Mode
snprintf(out_str, 1024, "window_mode=%d\r\n", window_mode);
outfile.write(out_str, strlen(out_str));
//Save Background Mode
snprintf(out_str, 1024, "bkgd_mode=%d\r\n", bkgd_mode);
outfile.write(out_str, strlen(out_str));
//Save Foreground Mode
snprintf(out_str, 1024, "frgd_mode=%d\r\n", frgd_mode);
outfile.write(out_str, strlen(out_str));
//Save Single Color Mode
snprintf(out_str, 1024, "single_color_mode=%d\r\n", single_color_mode);
outfile.write(out_str, strlen(out_str));
//Save Averaging Mode
snprintf(out_str, 1024, "avg_mode=%d\r\n", avg_mode);
outfile.write(out_str, strlen(out_str));
//Save Animation Speed
snprintf(out_str, 1024, "anim_speed=%f\r\n", anim_speed);
outfile.write(out_str, strlen(out_str));
//Save Reactive Background Flag
snprintf(out_str, 1024, "reactive_bkgd=%d\r\n", reactive_bkgd);
outfile.write(out_str, strlen(out_str));
//Save Silent Background Flag
snprintf(out_str, 1024, "silent_bkgd=%d\r\n", silent_bkgd);
outfile.write(out_str, strlen(out_str));
//Save Silent Background Flag
snprintf(out_str, 1024, "start_from_bottom=%d\r\n", start_from_bottom);
outfile.write(out_str, strlen(out_str));
//Save Silent Background Flag
snprintf(out_str, 1024, "start_from_bot_inv=%d\r\n", start_from_bot_inv);
outfile.write(out_str, strlen(out_str));
//Save Background Timeout
snprintf(out_str, 1024, "background_timeout=%d\r\n", background_timeout);
outfile.write(out_str, strlen(out_str));
//Save Audio Device Index
snprintf(out_str, 1024, "audio_device_idx=%d\r\n", audio_device_idx);
outfile.write(out_str, strlen(out_str));
//Save Network Mode
switch (netmode)
{
case NET_MODE_CLIENT:
//Save Client Configuration
snprintf(out_str, 1024, "client=%s\r\n", net_string);
outfile.write(out_str, strlen(out_str));
break;
case NET_MODE_SERVER:
//Save Server Configuration
snprintf(out_str, 1024, "server=%s\r\n", net_string);
outfile.write(out_str, strlen(out_str));
break;
}
//Close Output File
outfile.close();
}
void Visualizer::SetNormalization(float offset, float scale)
{
for (int i = 0; i < 256; i++)
{
fft[i] = 0.0f;
fft_nrml[i] = offset + (scale * (i / 256.0f));
}
}
void Visualizer::OnSettingsChanged()
{
settings_changed = true;
}
void Visualizer::SendSettings()
{
if (netmode == NET_MODE_SERVER)
{
settings_pkt_type settings;
settings.amplitude = amplitude;
settings.avg_mode = avg_mode;
settings.avg_size = avg_size;
settings.window_mode = window_mode;
settings.decay = decay;
settings.delay = delay;
settings.anim_speed = anim_speed;
settings.bkgd_bright = bkgd_bright;
settings.bkgd_mode = bkgd_mode;
settings.single_color_mode = single_color_mode;
settings.nrml_ofst = nrml_ofst;
settings.nrml_scl = nrml_scl;
settings.filter_constant = filter_constant;
settings.frgd_mode = frgd_mode;
settings.reactive_bkgd = reactive_bkgd;
settings.silent_bkgd = silent_bkgd;
settings.start_from_bot_inv = start_from_bot_inv;
settings.start_from_bottom = start_from_bottom;
settings.background_timeout = background_timeout;
port->tcp_write((char *)&settings, sizeof(settings));
}
}
void Visualizer::Update()
{
float fft_tmp[512];
for (int i = 0; i < 256; i++)
{
//Clear the buffers
fft_tmp[i] = 0;
//Decay previous values
fft[i] = fft[i] * (((float)decay) / 100.0f);
}
#ifdef WIN32
unsigned int buffer_pos = 0;
static float input_wave[512];
unsigned int nextPacketSize = 1;
unsigned int flags;
while (nextPacketSize > 0)
{
float *buf;
if (pAudioCaptureClient != NULL)
{
pAudioCaptureClient->GetBuffer((BYTE**)&buf, &nextPacketSize, (DWORD *)&flags, NULL, NULL);
if (buf == NULL && nextPacketSize > 0)
{
pAudioClient->Stop();
pAudioCaptureClient->Release();
pAudioClient->Release();
pAudioCaptureClient = NULL;
pAudioClient = NULL;
}
else
{
for (unsigned int i = 0; i < nextPacketSize; i += 4)
{
for (int j = 0; j < 255; j++)
{
input_wave[2 * j] = input_wave[2 * (j + 1)];
input_wave[(2 * j) + 1] = input_wave[2 * j];
}
float avg_buf = (buf[i] + buf[i + 1] + buf[i + 2] + buf[i + 3]) / 4;
input_wave[510] = avg_buf * 2.0f * amplitude;
input_wave[511] = input_wave[510];
}
buffer_pos += nextPacketSize / 4;
pAudioCaptureClient->ReleaseBuffer(nextPacketSize);
}
}
}
memcpy(fft_tmp, input_wave, sizeof(input_wave));
#else
//Only update FFT if there are at least 256 samples in the sample buffer
int samples;
if(device != NULL)
{
do
{
alcGetIntegerv(device, ALC_CAPTURE_SAMPLES, 1, &samples);
Sleep(1);
} while (samples < 512);
}
//Capture 256 audio samples
alcCaptureSamples(device, (ALCvoid *)buffer, 256);
//Scale the input into the FFT processing array
for (int i = 0; i < 512; i++)
{
fft_tmp[i] = (buffer[i / 2] - 128.0f) * (amplitude / 128.0f);
}
#endif
//Apply selected window
switch (window_mode)
{
case 0:
break;
case 1:
apply_window(fft_tmp, win_hanning, 256);
break;
case 2:
apply_window(fft_tmp, win_hamming, 256);
break;
case 3:
apply_window(fft_tmp, win_blackman, 256);
break;
default:
break;
}
//Run the FFT calculation
rfft(fft_tmp, 256, 1);
fft_tmp[0] = fft_tmp[2];
apply_window(fft_tmp, fft_nrml, 256);
//Compute FFT magnitude
for (int i = 0; i < 128; i += 2)
{
float fftmag;
//Compute magnitude from real and imaginary components of FFT and apply simple LPF
fftmag = (float)sqrt((fft_tmp[i] * fft_tmp[i]) + (fft_tmp[i + 1] * fft_tmp[i + 1]));
//Apply a slight logarithmic filter to minimize noise from very low amplitude frequencies
fftmag = ( 0.5f * log10(1.1f * fftmag) ) + ( 0.9f * fftmag );
//Limit FFT magnitude to 1.0
if (fftmag > 1.0f)
{
fftmag = 1.0f;
}
//Update to new values only if greater than previous values
if (fftmag > fft[i*2])
{
fft[i*2] = fftmag;;
}
//Prevent from going negative
if (fft[i*2] < 0.0f)
{
fft[i*2] = 0.0f;
}
//Set odd indexes to match their corresponding even index, as the FFT input array uses two indices for one value (real+imaginary)
fft[(i * 2) + 1] = fft[i * 2];
fft[(i * 2) + 2] = fft[i * 2];
fft[(i * 2) + 3] = fft[i * 2];
}
if (avg_mode == 0)
{
//Apply averaging over given number of values
int k;
float sum1 = 0;
float sum2 = 0;
for (k = 0; k < avg_size; k++)
{
sum1 += fft[k];
sum2 += fft[255 - k];
}
//Compute averages for end bars
sum1 = sum1 / k;
sum2 = sum2 / k;
for (k = 0; k < avg_size; k++)
{
fft[k] = sum1;
fft[255 - k] = sum2;
}
for (int i = 0; i < (256 - avg_size); i += avg_size)
{
float sum = 0;
for (int j = 0; j < avg_size; j += 1)
{
sum += fft[i + j];
}
float avg = sum / avg_size;
for (int j = 0; j < avg_size; j += 1)
{
fft[i + j] = avg;
}
}
}
else if(avg_mode == 1)
{
for (int i = 0; i < avg_size; i++)
{
float sum1 = 0;
float sum2 = 0;
int j;
for (j = 0; j <= i + avg_size; j++)
{
sum1 += fft[j];
sum2 += fft[255 - j];
}
fft[i] = sum1 / j;
fft[255 - i] = sum2 / j;
}
for (int i = avg_size; i < 256 - avg_size; i++)
{
float sum = 0;
for (int j = 1; j <= avg_size; j++)
{
sum += fft[i - j];
sum += fft[i + j];
}
sum += fft[i];
fft[i] = sum / (2 * avg_size + 1);
}
}
for(int i = 0; i < 256; i++)
{
float current = fft[i];
fft_fltr[i] = fft_fltr[i] + (filter_constant * (current - fft_fltr[i]));
}
}
void Visualizer::StartThread()
{
//Set application running flag to TRUE before starting threads
running = true;
VisThread = new std::thread(&Visualizer::VisThreadFunction, this);
NetConnectThread = new std::thread(&Visualizer::NetConnectThreadFunction, this);
NetUpdateThread = new std::thread(&Visualizer::NetUpdateThreadFunction, this);
}
void Visualizer::Shutdown()
{
//Initialize a fade-out by setting shutdown flag to TRUE and resetting timer
shutdown_flag = TRUE;
background_timer = 0;
//Wait for fade-out to complete before returning
while (running == true)
{
Sleep(50);
}
}
void DrawSolidColor(int bright, RGBColor color, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
pixels->pixels[y][x] = RGB(((bright * GetRValue(color)) / 256), ((bright * GetGValue(color)) / 256), ((bright * GetBValue(color)) / 256));
}
}
}
void DrawVerticalBars(int bright, RGBColor * colors, int num_colors, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
int idx = (int)((float)x * ((float)num_colors / 255.0f));
pixels->pixels[y][x] = RGB(((bright * GetRValue(colors[idx])) / 256), ((bright * GetGValue(colors[idx])) / 256), ((bright * GetBValue(colors[idx])) / 256));
}
}
}
void DrawHorizontalBars(int bright, RGBColor * colors, int num_colors, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
if (y == ROW_IDX_BAR_GRAPH)
{
if (x < 128)
{
int idx = (int)(num_colors - ((float)x * ((float)num_colors / 128.0f)));
if (idx >= num_colors)
{
idx = num_colors - 1;
}
pixels->pixels[y][x] = RGB(((bright * GetRValue(colors[idx])) / 256), ((bright * GetGValue(colors[idx])) / 256), ((bright * GetBValue(colors[idx])) / 256));
}
else
{
int idx = (int)(((float)(x - 128) * ((float)num_colors / 128.0f)));
pixels->pixels[y][x] = RGB(((bright * GetRValue(colors[idx])) / 256), ((bright * GetGValue(colors[idx])) / 256), ((bright * GetBValue(colors[idx])) / 256));
}
}
else
{
int idx = (int)(num_colors - ((float)y * ((float)num_colors / 63.0f)));
pixels->pixels[y][x] = RGB(((bright * GetRValue(colors[idx])) / 256), ((bright * GetGValue(colors[idx])) / 256), ((bright * GetBValue(colors[idx])) / 256));
}
}
}
}
void DrawRainbowSinusoidal(int bright, float bkgd_step, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
int red = (int)(127 * (sin(((((int)((x * (360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f)) + 1));
int grn = (int)(127 * (sin(((((int)((x * (360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f) - (6.28f / 3)) + 1));
int blu = (int)(127 * (sin(((((int)((x * (360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f) + (6.28f / 3)) + 1));
pixels->pixels[y][x] = RGB(((bright * red) / 256), ((bright * grn) / 256), ((bright * blu) / 256));
}
}
}
void DrawRainbow(int bright, float bkgd_step, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
int hsv_h = ((int)(bkgd_step + (256 - x)) % 360);
hsv_t hsv = { 0, 0, 0 };
hsv.hue = hsv_h;
hsv.saturation = 255;
hsv.value = (unsigned char)bright;
pixels->pixels[y][x] = hsv2rgb(&hsv);
}
}
}
void DrawColorWheel(int bright, float bkgd_step, int center_x, int center_y, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
float hue = (float)(bkgd_step + (int)(180 + atan2(y - center_y, x - center_x) * (180.0 / 3.14159)) % 360);
hsv_t hsv2 = { 0, 0, 0 };
hsv2.hue = (int)hue;
hsv2.saturation = 255;
hsv2.value = (unsigned char)bright;
pixels->pixels[y][x] = hsv2rgb(&hsv2);
}
}
}
void DrawSpectrumCycle(int bright, float bkgd_step, vis_pixels *pixels)
{
bright = (int)(bright * (255.0f / 100.0f));
hsv_t hsv2 = { 0, 0, 0 };
hsv2.hue = (int)bkgd_step;
hsv2.saturation = 255;
hsv2.value = (unsigned char)bright;
RGBColor color = hsv2rgb(&hsv2);
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
pixels->pixels[y][x] = color;
}
}
}
void DrawSinusoidalCycle(int bright, float bkgd_step, vis_pixels *pixels)
{
RGBColor color;
bright = (int)(bright * (255.0f / 100.0f));
int red = (int)(127 * (sin(((((int)(((360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f)) + 1));
int grn = (int)(127 * (sin(((((int)(((360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f) - (6.28f / 3)) + 1));
int blu = (int)(127 * (sin(((((int)(((360 / 255.0f)) - bkgd_step) % 360) / 360.0f) * 2 * 3.14f) + (6.28f / 3)) + 1));
color = RGB(((bright * red) / 256), ((bright * grn) / 256), ((bright * blu) / 256));
for (int x = 0; x < 256; x++)
{
for (int y = 0; y < 64; y++)
{
pixels->pixels[y][x] = color;
}
}
}
void DrawSingleColorBackground(float amplitude, vis_pixels *bg_pixels, vis_pixels *out_pixels)
{
if (amplitude >= 1.0f)
{
amplitude = 1.0f;
}
else if (amplitude <= 0.0f)
{
amplitude = 0.0f;
}
for (int x = 0; x < 256; x++)
{
int in_color = bg_pixels->pixels[ROW_IDX_SINGLE_COLOR][x];
int out_color = RGB(((amplitude * GetRValue(in_color))), ((amplitude * GetGValue(in_color))), ((amplitude * GetBValue(in_color))));
out_pixels->pixels[ROW_IDX_SINGLE_COLOR][x] = out_color;
}
}
void Visualizer::DrawSingleColorForeground(float amplitude, vis_pixels *fg_pixels, vis_pixels *out_pixels)
{
if (amplitude >= 1.0f)
{
amplitude = 1.0f;
}
else if (amplitude <= 0.0f)
{
amplitude = 0.0f;
}
int idx = (int)(64.0f - (amplitude * 62.0f));
int in_color = fg_pixels->pixels[idx][0];
int out_color = RGB(((amplitude * GetRValue(in_color))), ((amplitude * GetGValue(in_color))), ((amplitude * GetBValue(in_color))));
for (int x = 0; x < 256; x++)
{
if (frgd_mode >= VISUALIZER_PATTERN_ANIM_RAINBOW_SINUSOIDAL)
{
in_color = fg_pixels->pixels[ROW_IDX_SINGLE_COLOR][x];
out_color = RGB(((amplitude * GetRValue(in_color))), ((amplitude * GetGValue(in_color))), ((amplitude * GetBValue(in_color))));
}
out_pixels->pixels[ROW_IDX_SINGLE_COLOR][x] = out_color;
}
}
void DrawSingleColorStatic(float amplitude, RGBColor in_color, vis_pixels *out_pixels)
{
if (amplitude >= 1.0f)
{
amplitude = 1.0f;
}
else if (amplitude <= 0.0f)
{
amplitude = 0.0f;
}
int out_color = RGB(((amplitude * GetRValue(in_color))), ((amplitude * GetGValue(in_color))), ((amplitude * GetBValue(in_color))));
for (int x = 0; x < 256; x++)
{
out_pixels->pixels[ROW_IDX_SINGLE_COLOR][x] = out_color;
}
}
void Visualizer::DrawPattern(VISUALIZER_PATTERN pattern, int bright, vis_pixels *pixels)
{
switch (pattern)
{
case VISUALIZER_PATTERN_ANIM_RAINBOW_SINUSOIDAL:
DrawRainbowSinusoidal(bright, bkgd_step, pixels);
break;
case VISUALIZER_PATTERN_ANIM_RAINBOW_HSV:
DrawRainbow(bright, bkgd_step, pixels);
break;
case VISUALIZER_PATTERN_ANIM_COLOR_WHEEL:
DrawColorWheel(bright, bkgd_step, 128, 32, pixels);
break;
case VISUALIZER_PATTERN_ANIM_COLOR_WHEEL_2:
DrawColorWheel(bright, bkgd_step, 128, 64, pixels);
break;
case VISUALIZER_PATTERN_ANIM_SPECTRUM_CYCLE:
DrawSpectrumCycle(bright, bkgd_step, pixels);
break;
case VISUALIZER_PATTERN_ANIM_SINUSOIDAL_CYCLE:
DrawSinusoidalCycle(bright, bkgd_step, pixels);
break;
default:
if(pattern <= VISUALIZER_SINGLE_COLOR_PURPLE)
DrawSolidColor(bright, colors[pattern], pixels);
else
{
std::vector<std::vector<RGBColor>> colors=
{
{ 0x0000FF00, 0x0000FFFF, 0x000000FF },
{ 0x0000FF00, 0x00FFFFFF, 0x000000FF },
{ 0x00FF0000, 0x00FFFF00, 0x00FFFFFF },
{ 0x000000FF, 0x00FFFFFF, 0x00FF0000 },
{ 0x000000FF, 0x0000FFFF, 0x0000FF00, 0x00FFFF00, 0x00FF0000, 0x00FF00FF },
{ 0x00FF00FF, 0x00FF0000, 0x00FFFF00, 0x0000FF00, 0x0000FFFF, 0x000000FF }
};
std::vector<RGBColor>& current = colors[pattern-VISUALIZER_PATTERN_STATIC_GREEN_YELLOW_RED];
DrawHorizontalBars(bright, (RGBColor*)current.data(), current.size(), pixels);
}
break;
}
}
void Visualizer::NetConnectThreadFunction()
{
while (1)
{
switch (netmode)
{
case NET_MODE_DISABLED:
return;
break;
case NET_MODE_SERVER:
//Listen for new clients
port->tcp_server_listen();
//When a new client connects, send settings
SendSettings();
break;
case NET_MODE_CLIENT:
//Try to connect to server
port->tcp_client_connect();
//Wait 1 second between tries;
Sleep(1000);
break;
}
}
}
void Visualizer::NetUpdateThreadFunction()
{
int counter = 0;
char buf[sizeof(fft_fltr)];
while (1)
{
switch (netmode)
{
case NET_MODE_DISABLED:
return;
break;
case NET_MODE_SERVER:
port->tcp_write((char *)fft_fltr, sizeof(fft_fltr));
if (counter++ > 30)
{
port->tcp_write((char *)&bkgd_step, sizeof(bkgd_step));
}
if (settings_changed)