This repository has been archived by the owner on Aug 8, 2020. It is now read-only.
/
kubus.cpp
457 lines (388 loc) · 11.1 KB
/
kubus.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
//-----------------------------------------------------------------------------
// name: VisualSine.cpp
// desc: hello sine wave, real-time
//
// author: Ge Wang (ge@ccrma.stanford.edu)
// date: fall 2014
// uses: RtAudio by Gary Scavone
//-----------------------------------------------------------------------------
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#ifdef __MACOSX_CORE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <string.h>
#endif
#include <time.h>
#include "RtAudio.h"
#include "kubus.h"
#include "inih/ini.h"
// our datetype
#define SAMPLE float
// corresponding format for RtAudio
#define MY_FORMAT RTAUDIO_FLOAT32
// sample rate
#ifndef MY_SRATE
#define MY_SRATE 48000
#endif
// number of channels
#define MY_CHANNELS 1
// for convenience
#define MY_PIE 3.14159265358979
#define BUFSIZE 1024
#define FFTSIZE 1024
KubusData g_data;
static void print_help()
{
cerr << "Keyboard controls: " << endl;
cerr << "\t 'q' : Quit" << endl;
cerr << "\t 'f' : FFT mode" << endl;
cerr << "\t 'p' : Pulsate mode" << endl;
cerr << "\t 'j' : Jitter mode" << endl;
cerr << "\t 'b' : Bitcrushed colors" << endl;
cerr << "\t 'r' : Rainbow mode!" << endl;
}
void kubus_cleanup(KubusData *kd)
{
RtAudio *audio = (RtAudio *) kd->audio;
if( audio->isStreamOpen() )
{
audio->stopStream();
audio->closeStream();
}
kiss_fftr_free(kd->cfg);
KISS_FFT_FREE(kd->fftbuf);
for(int i = 0; i < FFT_HIST; i++) {
KISS_FFT_FREE(kd->fftblock[i]);
}
sp_rms_destroy(&kd->rms);
sp_port_destroy(&kd->port);
sp_rms_destroy(&kd->rms_bp);
sp_butbp_destroy(&kd->bp);
delete kd->buffer;
delete kd->window;
delete kd->wbuffer;
//free(kd->fftblock);
}
static int ini_handler(void* user, const char* section, const char* name,
const char* value)
{
KubusData* kd = (KubusData *)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("audio", "sr")) {
kd->sr = atoi(value);
}else if (MATCH("audio", "gain")) {
kd->gain = atof(value);
}else if (MATCH("audio", "bp_freq")) {
kd->bp_freq = atof(value);
}else if (MATCH("audio", "bp_bw")) {
kd->bp_bw = atof(value);
}else if (MATCH("audio", "bp_gain")) {
kd->bp_gain = atof(value);
}else if (MATCH("modes", "pulse")) {
kd->tog_pulse = atoi(value);
}else if (MATCH("modes", "jitter")) {
kd->tog_jit = atoi(value);
}else if (MATCH("modes", "fft")) {
kd->showFFT = atoi(value);
}else if (MATCH("modes", "rainbow")) {
kd->tog_rainbow= atoi(value);
}else if (MATCH("modes", "8bit")) {
kd->tog_8bit = atoi(value);
}else if (MATCH("visual", "scaleDefault")) {
kd->scaleDefault = atof(value);
}else if (MATCH("visual", "scaleMin")) {
kd->scaleMin = atof(value);
}else if (MATCH("visual", "scaleMax")) {
kd->scaleMax = atof(value);
}else if (MATCH("visual", "jit_thresh")) {
kd->jit_thresh = atof(value);
}else if (MATCH("visual", "fftWrap")) {
kd->fftWrap = atoi(value);
}else if (MATCH("visual", "color_r")) {
kd->clr.r= atof(value) / 255.0;
}else if (MATCH("visual", "color_g")) {
kd->clr.g= atof(value) / 255.0;
}else if (MATCH("visual", "color_b")) {
kd->clr.b= atof(value) / 255.0;
}else if (MATCH("visual", "inertia")) {
kd->inertia = atof(value);
} else {
return 0; /* unknown section/name, error */
}
#undef MATCH
return 1;
}
void kubus_init(KubusData *kd)
{
// allocate global buffer
kd->bufferSize = BUFSIZE;
kd->buffer = new SAMPLE[kd->bufferSize];
kd->wbuffer = new SAMPLE[kd->bufferSize];
memset( kd->buffer, 0, sizeof(SAMPLE)*BUFSIZE);
// FFT init
kd->cfg = kiss_fftr_alloc(FFTSIZE, 0, NULL, NULL);
kd->fftbuf = (kiss_fft_cpx *) KISS_FFT_MALLOC(sizeof(float) * FFTSIZE);
//kd->fftblock = (kiss_fft_cpx **) malloc(sizeof(kiss_fft_cpx) * FFT_HIST);
for(int i = 0; i < FFT_HIST; i++) {
kd->fftblock[i] = (kiss_fft_cpx *) KISS_FFT_MALLOC(sizeof(float) * FFTSIZE);
kd->fftblock_pos[i] = i;
}
kd->window = new SAMPLE[BUFSIZE];
memset( kd->window , 0, sizeof(SAMPLE)*kd->bufferSize );
hanning(kd->window, BUFSIZE);
kd->fftWrap = 128;
// set scale
kd->scaleMax = 3.0;
kd->scaleMin = 1.0;
kd->scaleDefault = 2.0;
kd->scale = kd->scaleMin;
kd->scale = 3.0;
kd->scale_bp = 0;
kd->sr = 44100;
kd->jit_thresh = 0.5;
// Set default color
kd->clr.r = 0.1607;
kd->clr.g = 0.6784;
kd->clr.b = 1;
// set default toggles
kd->showFFT = 0;
kd->tog_jit = 1;
kd->tog_pulse = 1;
/* downsamples input signal */
kd->tog_8bit = 1;
/* time-signal modifies hue */
kd->tog_rainbow = 0;
/* time-signal modifies lightness */
kd->tog_amp = 1;
/* Input gain */
kd->gain = 1;
/* inertia of pulses */
kd->inertia = 0.01;
/* bandpass for jitter */
kd->bp_bw = 10;
kd->bp_freq = 300;
kd->bp_gain = 4;
//RNG seed
srand(time(NULL));
if(kd->useConfig) {
if (ini_parse(kd->config, ini_handler, kd) < 0) {
printf("Can't load '%s'\n", kd->config);
}
} else {
if (ini_parse("config.ini", ini_handler, kd) < 0) {
printf("Can't load 'config.ini', using defaults\n");
}
}
sp_rms_create(&kd->rms);
sp_rms_init(kd->sr, kd->rms);
sp_port_create(&kd->port);
sp_port_init(kd->sr, kd->port, kd->inertia);
sp_rms_create(&kd->rms_bp);
sp_rms_init(kd->sr, kd->rms_bp);
sp_butbp_create(&kd->bp);
sp_butbp_init(kd->sr, kd->bp);
kd->bp->freq = kd->bp_freq;
kd->bp->bw = kd->bp_bw;
}
int callme( void * outputBuffer, void * inputBuffer, unsigned int numFrames,
double streamTime, RtAudioStreamStatus status, void * data )
{
KubusData *kd = (KubusData *)data;
// cast!
SAMPLE * input = (SAMPLE *)inputBuffer;
SAMPLE * output = (SAMPLE *)outputBuffer;
float rms = 0, port = 0, bp = 0, rms_bp;
// fill
for( int i = 0; i < numFrames; i++ )
{
// assume mono
g_data.buffer[i] = input[i] * kd->gain;
sp_rms_compute(kd->rms, &g_data.buffer[i], &rms);
sp_port_compute(kd->port, &rms, &port);
sp_butbp_compute(kd->bp, &g_data.buffer[i], &bp);
bp *= pow(10, kd->bp_gain / 10.0);
sp_rms_compute(kd->rms_bp, &bp, &rms_bp);
kd->scale = kd->scaleMin + (kd->scaleMax - kd->scaleMin) * port;
kd->scale_bp = rms_bp;
// zero output
output[i] = 0;
}
return 0;
}
int main( int argc, char ** argv )
{
RtAudio audio;
unsigned int bufferFrames = BUFSIZE;
if(argc > 1) {
g_data.useConfig = 1;
g_data.config = argv[1];
} else {
g_data.useConfig = 0;
}
kubus_init(&g_data);
g_data.audio = &audio;
if( audio.getDeviceCount() < 1 )
{
cout << "no audio devices found!" << endl;
exit( 1 );
}
glutInit( &argc, argv );
initGfx();
audio.showWarnings( true );
RtAudio::StreamParameters iParams, oParams;
iParams.deviceId = audio.getDefaultInputDevice();
iParams.nChannels = MY_CHANNELS;
iParams.firstChannel = 0;
oParams.deviceId = audio.getDefaultOutputDevice();
oParams.nChannels = MY_CHANNELS;
oParams.firstChannel = 0;
RtAudio::StreamOptions options;
try {
audio.openStream( &oParams, &iParams, MY_FORMAT, g_data.sr, &bufferFrames, &callme, &g_data, &options );
}
catch( RtError& e )
{
cout << e.getMessage() << endl;
exit( 1 );
}
// Display controls
print_help();
// go for it
try {
// start stream
audio.startStream();
// let GLUT handle the current thread from here
glutMainLoop();
// stop the stream.
audio.stopStream();
}
catch( RtError& e )
{
// print error message
cout << e.getMessage() << endl;
goto cleanup;
}
cleanup:
kubus_cleanup(&g_data);
return 0;
}
void initGfx()
{
// double buffer, use rgb color, enable depth buffer
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
// initialize the window size
glutInitWindowSize( g_data.width, g_data.height );
// set the window postion
glutInitWindowPosition( 100, 100 );
// create the window
glutCreateWindow( "kubus" );
// set the idle function - called when idle
glutIdleFunc( idleFunc );
// set the display function - called when redrawing
glutDisplayFunc( displayFunc );
// set the reshape function - called when client area changes
glutReshapeFunc( reshapeFunc );
// set the keyboard function - called on keyboard events
glutKeyboardFunc( keyboardFunc );
// set the mouse function - called on mouse stuff
glutMouseFunc( mouseFunc );
// set clear color
glClearColor( 0, 0, 0, 1 );
// enable color material
glEnable( GL_COLOR_MATERIAL );
// enable depth test
glEnable( GL_DEPTH_TEST );
}
void reshapeFunc( GLsizei w, GLsizei h )
{
// save the new window size
g_data.width = w; g_data.height = h;
// map the view port to the client area
glViewport( 0, 0, w, h );
// set the matrix mode to project
glMatrixMode( GL_PROJECTION );
// load the identity matrix
glLoadIdentity( );
// create the viewing frustum
gluPerspective( 45.0, (GLfloat) w / (GLfloat) h, 1.0, 300.0 );
// set the matrix mode to modelview
glMatrixMode( GL_MODELVIEW );
// load the identity matrix
glLoadIdentity( );
// position the view point
gluLookAt( 0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
}
void keyboardFunc( unsigned char key, int x, int y )
{
switch( key )
{
case 'Q':
case 'q':
kubus_cleanup(&g_data);
exit(1);
break;
case 'd':
break;
case 'f':
g_data.showFFT = (g_data.showFFT == 1) ? 0 : 1;
break;
case 'p':
g_data.tog_pulse = (g_data.tog_pulse == 1) ? 0 : 1;
break;
case 'j':
g_data.tog_jit = (g_data.tog_jit == 1) ? 0 : 1;
break;
case 'b':
g_data.tog_8bit = (g_data.tog_8bit== 1) ? 0 : 1;
break;
case 'r':
g_data.tog_rainbow = (g_data.tog_rainbow == 1) ? 0 : 1;
break;
default:
break;
}
glutPostRedisplay( );
}
void mouseFunc( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON )
{
// when left mouse button is down
if( state == GLUT_DOWN )
{
}
else
{
}
}
else if ( button == GLUT_RIGHT_BUTTON )
{
// when right mouse button down
if( state == GLUT_DOWN )
{
}
else
{
}
}
else
{
}
glutPostRedisplay( );
}
void idleFunc( )
{
glutPostRedisplay( );
}
void displayFunc( )
{
kubus_draw(&g_data);
}