-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency_wave.c
180 lines (149 loc) · 3.72 KB
/
frequency_wave.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
/* Copyright (C) 2014, 2015 Joren Van Onder */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License, or */
/* (at your option) any later version. */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software Foundation, */
/* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include <GL/freeglut.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <fftw3.h>
#include "common.h"
#include "frequency_wave.h"
#include "pipe_buffer.h"
#include "fft.h"
#include "render.h"
#include "util.h"
int camera_position_x = 0;
int camera_position_y = 0;
int camera_position_z = 0;
double camera_rotate_x = 0;
double camera_rotate_y = 0;
void update()
{
read_named_pipe();
if (samples_in_pipe_buffer(AMOUNT_COLUMNS)) {
fftw_complex *fft_out = 0;
if (get_render_mode() == RENDER_FFT_MODE || get_render_mode() == RENDER_3D_MODE)
fft_out = calculate_fft();
render(fft_out);
flush_pipe_buffer();
glutSwapBuffers();
}
}
void timer(int value)
{
UNUSED(value);
glutPostRedisplay();
glutTimerFunc(1000 / TARGET_FPS, timer, 0);
}
void keyboard(unsigned char key, int x, int y)
{
UNUSED(x);
UNUSED(y);
switch (key) {
case '1':
set_render_mode(RENDER_WAVE_MODE);
setup_appropriate_projection();
break;
case '2':
set_render_mode(RENDER_FFT_MODE);
setup_appropriate_projection();
break;
case '3':
set_render_mode(RENDER_3D_MODE);
setup_appropriate_projection();
break;
case 'q':
glutLeaveMainLoop();
break;
case 'X':
increment_fft_base_x_scale();
break;
case 'x':
decrement_fft_base_x_scale();
break;
case 'Y':
increment_fft_base_y_scale();
break;
case 'y':
decrement_fft_base_y_scale();
break;
case 'C':
toggle_use_color();
break;
case 'u':
--camera_position_x;
break;
case 'o':
++camera_position_x;
break;
case 'p':
--camera_position_y;
break;
case ',':
++camera_position_y;
break;
case '.':
++camera_position_z;
break;
case 'e':
--camera_position_z;
break;
default:
break;
}
}
int last_x = -1;
int last_y = -1;
void mouse(int x, int y)
{
if (last_x == -1 && last_y == -1) {
last_x = x;
last_y = y;
}
int diff_x = x - last_x;
int diff_y = y - last_y;
last_x = x;
last_y = y;
camera_rotate_x += (float) diff_y / 8;
camera_rotate_y += (float) diff_x / 8;
}
void cleanup()
{
printf("cleaning up...\n");
cleanup_fft();
cleanup_pipe_buffer();
}
void print_fps_info()
{
printf("assuming samplerate of 44100 Hz\nmax framerate is %f\n", (double) SAMPLE_RATE / AMOUNT_COLUMNS);
}
int main(int argc, char **argv)
{
print_fps_info();
setup_scale_arrays();
glutInit(&argc, argv);
srand(time(NULL));
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(1920, 1080);
glutCreateWindow("mpd vis");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
/* callbacks */
glutDisplayFunc(update);
glutKeyboardFunc(keyboard);
glutPassiveMotionFunc(mouse);
setup_appropriate_projection();
setup_pipe_buffer();
timer(0);
glutMainLoop();
cleanup();
return 0;
}