-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (90 loc) · 2.65 KB
/
main.cpp
File metadata and controls
101 lines (90 loc) · 2.65 KB
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
#include <GLFW/glfw3.h>
#include <iostream>
#include <fftw3.h>
#include <fstream>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <vector>
#include <time.h>
#include "Visualizer.h"
#include "FreqSpectrum.h"
#include "SoundWave.h"
#define SAMPLES 2048
#define COL_COUNT 400
using namespace std;
vector<Visualizer *> mVis;
unsigned int mCurrentVis;
int mStream;
int16_t mBuf[SAMPLES];
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE) {
mVis[mCurrentVis]->shutdown();
if (++mCurrentVis >= mVis.size()) mCurrentVis = 0;
mVis[mCurrentVis]->init();
} else if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void openStream(char *file) {
mStream = open(file, O_RDONLY | O_NONBLOCK);
}
int getFreq(int i) {
return i * 44100 / SAMPLES;
}
bool readData() {
int samples = read(mStream, mBuf, SAMPLES * sizeof(*mBuf)) / sizeof(*mBuf);
if (samples != -1) {
mVis[mCurrentVis]->draw(mBuf, samples);
return true;
}
return false;
}
void closeStream() {
close(mStream);
}
int main(int argc, char *argv[]) {
mVis.push_back(new FreqSpectrum(SAMPLES));
mVis.push_back(new SoundWave(SAMPLES));
mCurrentVis = 0;
mVis[mCurrentVis]->init();
openStream("/tmp/mpd.fifo");
cout << "i = " << getFreq(1) << "Hz" << endl;
GLFWwindow *window;
if (!glfwInit()) return -1;
const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
window = glfwCreateWindow(vidmode->width, vidmode->height, "Visualizer", glfwGetPrimaryMonitor(), NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
int width, height;
clock_t lasttime = clock();
long diff;
long minTime = (SAMPLES / 44100.d) * 1e6;
bool res = true;
while (!glfwWindowShouldClose(window)) {
// Skip frames if reading music data failed
if (res) {
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
}
diff = (clock() - lasttime) / (CLOCKS_PER_SEC / 1e6);
diff = minTime - diff;
if (diff > 0) usleep(diff);
lasttime = clock();
res = readData();
if (res) {
glfwSwapInterval(1);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
glfwTerminate();
mVis[mCurrentVis]->shutdown();
closeStream();
return 0;
}