-
Notifications
You must be signed in to change notification settings - Fork 11
/
draw2.cpp
521 lines (395 loc) · 11.1 KB
/
draw2.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
//g++ -std=c++11 -c draw.cpp
//g++ -std=gnu++11 finalDraw.o ../kiss_fft130/kiss_fft.c -L /home/<username>/mylib/lib/ -lAquila -lOoura_fft -lm -lglut -lGLEW -lGL -lsfml-audio ../common/shader_utils.o -o finalDraw
#include "visualizer.hpp"
#include <unistd.h>
#define N 2048 //14700
typedef unsigned long long timestamp_t;
static timestamp_t
get_timestamp ()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}
std::vector<int> array(0);
std::vector<int>::iterator it;
GLuint program;
GLint attribute_coord1d;
GLint uniform_offset_x;
GLint uniform_scale_x;
GLuint texture_id;
GLint uniform_mytexture;
float offset_x = 0.0;
//float scale_x = 1.0/(1.5*10)/(1.5*7);
float scale_x =1.0;
bool interpolate = false;
bool clamp = false;
bool showpoints = true;
GLuint vbo;
int graph[N/2];
int framePointer = 0;
char fileName[50];
bool calledFromInit = true;
bool dataEnd = false;
sf::Time totalMusicDuration;
bool playFlag = true;
sf::Music music;
kiss_fft_cpx in[N],out[N];
void getData();
void display();
void getFft(const kiss_fft_cpx in[N], kiss_fft_cpx out[N])
{
kiss_fft_cfg cfg;
if ((cfg = kiss_fft_alloc(N, 0/*is_inverse_fft*/, NULL, NULL)) != NULL)
{
size_t i;
kiss_fft(cfg, in, out);
free(cfg);
}
else
{
printf("not enough memory?\n");
exit(-1);
}
}
void moveWav()
{
glFlush();
glutSwapBuffers();
getData();
display();
}
float windoFunction(float freq)
{
float a = 0.54, b = 0.46;
return a - b * cos((2*M_PI)/(freq)-1);
}
int graphPtr = 0;
int tmpGraph[N/2];
int magN(int n)
{
int max = tmpGraph[0];
for(int i=1; i<n; i++)
{
graph[i]>max;
max = tmpGraph[i];
}
graphPtr ++;
return max;
}
int plotPtr = 0;
int pltGraph[100];
timestamp_t tmain;
bool soundStatFirstCall = true;
void getData()
{
timestamp_t t0 = get_timestamp();
int i,j,x;
Aquila::WaveFile wav(fileName);
double mag[N/2];
double roof = wav.getSamplesCount();
for( i = framePointer, j = 0; i < (framePointer + N/2)
&& framePointer < roof - N/2 ; i++,j++ )
graph[j] = array[i];
if(framePointer < roof-N/2 -1){
framePointer = i;
}
else {
timestamp_t t1 = get_timestamp();
double secs = (t1 - tmain) / 1000000.0L;
sf::Time musicPlayingOffset = music.getPlayingOffset();
unsigned int musicSampleRate = music.getSampleRate();
int musicLeftToPlay = totalMusicDuration.asMilliseconds() - musicPlayingOffset.asMilliseconds();
std::cout<<"N = "<<N<<std::endl;
std::cout<<"Frame pointer > roof - N"<<std::endl;
std::cout<<"Framepointer = "<<framePointer<<std::endl;
std::cout<<"Frames Left = "<<roof - framePointer<<std::endl;
std::cout<<"Total exec time: "<<secs<<std::endl;
std::cout<<"Total Music Played Duration = "<<musicPlayingOffset.asMilliseconds()<<std::endl;
std::cout<<"Music left to play = "<<musicLeftToPlay<<std::endl;
std::cout<<"SFML Sample Rate = "<<musicSampleRate<<std::endl;
exit(0);
}
if(framePointer >= roof) {
dataEnd = true;
return ;
}
std::cout<<"Framepointer = "<<framePointer<<std::endl;
if(!calledFromInit)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 4096, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, graph);
// Create the vertex buffer object
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Create an array with only x values.
GLfloat line[101];
// Fill it in just like an array
for (int i = 0; i < 101; i++) {
line[i] = (i - 50) / 50.0;
}
// Tell OpenGL to copy our array to the buffer object
glBufferData(GL_ARRAY_BUFFER, sizeof line, line, GL_STATIC_DRAW);
// Enable point size control in vertex shader
#ifndef GL_ES_VERSION_2_0
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
#endif
}
}
int init_resources() {
timestamp_t t0 = get_timestamp();
program = create_program("graph.v.glsl", "graph.f.glsl");
if (program == 0)
return 0;
attribute_coord1d = get_attrib(program, "coord1d");
uniform_offset_x = get_uniform(program, "offset_x");
uniform_scale_x = get_uniform(program, "scale_x");
uniform_mytexture = get_uniform(program, "mytexture");
if (attribute_coord1d == -1 || uniform_offset_x == -1 || uniform_scale_x == -1 || uniform_mytexture == -1)
return 0;
//gets N/2 values in to graph
getData();
calledFromInit = !calledFromInit;
/* Upload the texture with our datapoints */
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2048, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, graph);
// Create the vertex buffer object
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Create an array with only x values.
GLfloat line[101];
// Fill it in just like an array
for (int i = 0; i < 101; i++) {
line[i] = (i - 50) / 50.0;
}
// Tell OpenGL to copy our array to the buffer object
glBufferData(GL_ARRAY_BUFFER, sizeof line, line, GL_STATIC_DRAW);
// Enable point size control in vertex shader
#ifndef GL_ES_VERSION_2_0
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
#endif
//return 1;
timestamp_t t1 = get_timestamp();
double secs = (t1 - t0) / 1000000.0L;
std::cout<<"iinit init_resources total time: "<<secs<<std::endl;
return 1;
}
int checkEnd()
{
return -1;
}
void display() {
glUseProgram(program);
glUniform1i(uniform_mytexture, 0);
glUniform1f(uniform_offset_x, offset_x);
glUniform1f(uniform_scale_x, scale_x);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//sleep(1);
/* Set texture wrapping mode */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp ? GL_CLAMP_TO_EDGE : GL_REPEAT);
/* Set texture interpolation mode */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolate ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolate ? GL_LINEAR : GL_NEAREST);
/* Draw using the vertices in our vertex buffer object */
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(attribute_coord1d);
glVertexAttribPointer(attribute_coord1d, 1, GL_FLOAT, GL_FALSE, 0, 0);
/* Draw the line */
glDrawArrays(GL_LINE_STRIP, 0, 101);
/* Draw points as well, if requested */
if (showpoints)
glDrawArrays(GL_POINTS, 0, 101);
//glFlush();
//glutSwapBuffers();
if(checkEnd() > 0){
exit(0);
}
if(dataEnd != true){
getData();
// display();
glFlush();
glutSwapBuffers();
}
else return;
}
sf::Time timePlay;
void special(int key, int x, int y) {
float t;
switch (key) {
case GLUT_KEY_F7:
interpolate = !interpolate;
printf("Interpolation is now %s\n", interpolate ? "on" : "off");
break;
case GLUT_KEY_F8:
clamp = !clamp;
printf("Clamping is now %s\n", clamp ? "on" : "off");
break;
case GLUT_KEY_F9:
showpoints = !showpoints;
printf("Showing points is now %s\n", showpoints ? "on" : "off");
break;
case GLUT_KEY_LEFT:
offset_x -= 0.1;
timePlay = music.getPlayingOffset();
t = timePlay.asSeconds();
music.setPlayingOffset(sf::seconds(t - 5));
break;
case GLUT_KEY_RIGHT:
offset_x += 0.1;
timePlay = music.getPlayingOffset();
t = timePlay.asSeconds();
music.setPlayingOffset(sf::seconds(t + 5));
break;
case GLUT_KEY_UP:
scale_x *= 1.5;
break;
case GLUT_KEY_DOWN:
scale_x /= 1.5;
break;
case GLUT_KEY_HOME:
offset_x = 0.0;
scale_x = 1.0;
break;
case GLUT_KEY_F10:
exit(0);
}
glutPostRedisplay();
}
bool muteFlag = false;
void key(unsigned char k,int,int)
{
if(k == 'p'){
if(playFlag){
music.pause();
playFlag = !playFlag;
}
else
{
music.play();
playFlag = !playFlag;
}
}
if(k == 'm'){
if(!muteFlag){
music.setVolume(0);
muteFlag=!muteFlag;
}
else{
music.setVolume(100);
muteFlag=!muteFlag;
}
}
if(k == 'r')//reload audio
{
music.setPlayingOffset(sf::seconds(0));
}
if(k == 'q')
exit(0);
}
void free_resources() {
glDeleteProgram(program);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: wave_iteration <FILENAME>" << std::endl;
return 1;
}
strcpy(fileName, argv[1]);
tmain = get_timestamp();
//sfm play music
if (!music.openFromFile(fileName))
return -1;
it = array.begin();
timestamp_t t0 = get_timestamp();
Aquila::WaveFile wav(argv[1]);
int i,j,x;
//int graph[N/2];
double mag[N/2];
double roof = wav.getSamplesCount();
double framePointer = 0;
while(framePointer < roof){
for( i = framePointer, j = 0; i < (framePointer + N)
&& framePointer < roof - N ; i++,j++ )
{
//Apply window function on the sample
double multiplier = 0.5 * (1 - cos(2*M_PI*j/(N-1)));
in[j].r = multiplier * wav.sample(i);
in[j].i = 0; //stores N samples
}
if(framePointer < roof-N -1){
framePointer = i;
}
else {
timestamp_t t1 = get_timestamp();
double secs = (t1 - t0) / 1000000.0L;
// print_vec(array);
std::cout<<"Total FFt calc time: "<<secs<<std::endl;
break;
}
getFft(in,out);
// calculate magnitude of first n/2 FFT
for(i = 0; i < N/2; i++ ){
int val;
mag[i] = sqrt((out[i].r * out[i].r) + (out[i].i * out[i].i));
val = log(mag[i]) * 10;
it = array.end();
it = array.insert(it,val);
}
}
std::vector<int>::size_type sz = array.size();
std::cout<<"array vec ==> "<<sz<<std::endl;
totalMusicDuration = music.getDuration ();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(640, 480);
glutCreateWindow("My Graph");
GLenum glew_status = glewInit();
if (GLEW_OK != glew_status) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
return 1;
}
if (!GLEW_VERSION_2_0) {
fprintf(stderr, "No support for OpenGL 2.0 found\n");
return 1;
}
GLint max_units;
glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max_units);
if (max_units < 1) {
fprintf(stderr, "Your GPU does not have any vertex texture image units\n");
return 1;
}
GLfloat range[2];
glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, range);
if (range[1] < 5.0)
fprintf(stderr, "WARNING: point sprite range (%f, %f) too small\n", range[0], range[1]);
printf("------------------------------------------------------\n");
printf("AUDIO SPECTRUM VISUALIZER\nSubmitted in partial fulfilment of the ");
printf("requirements for the Computer Graphics\nLaboratory(10CSL67) course of the 6th semester.");
printf("\nBachelor of Engineering In Computer science & Engineering\nSubmitted by: INDRAJITH I (4AI12CS042)\n");
printf("------------------------------------------------------\n\n");
printf("Use left/right to move horizontally.And seek audio by +/-5 sec\n");
printf("Use up/down to change the horizontal scale.\n");
printf("Press home to reset the position and scale.\n");
printf("Press F7 to toggle interpolation.\n");
printf("Press F8 to toggle clamping.\n");
printf("Press F9 to toggle drawing points.\n");
printf("Press q to exit.\n");
printf("Press p to toggle Play/Pause audio.\n");
printf("Press r to reload and play audio.\n");
getData();
music.play();
if (init_resources()) {
glutDisplayFunc(display);
glutSpecialFunc(special);
glutIdleFunc(moveWav);
//glutTimerFunc(100,moveWav,10);
glutKeyboardFunc(key);
glutMainLoop();
}
free_resources();
return 0;
}