Repository navigation
Expand file tree
/
Copy pathMain.cpp
More file actions
254 lines (213 loc) · 9.13 KB
/
Copy pathMain.cpp
File metadata and controls
254 lines (213 loc) · 9.13 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
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
#include <iostream>
#include <vector>
#include <memory>
#include <numbers>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <SNT/CaptureDevice.h>
#include <BBG/BBG.h>
#include <BBG/Buffer.h>
#include <BBG/Texture2D.h>
#include <BBG/ShaderProgram.h>
#include <BBG/Rendering.h>
#include <BBG/TimerQuery.h>
#include "LoadFile.h"
#include "Timer.h"
#include "Random.h"
#include "Logger.h"
#include "Rendering/WaveformPlot.h"
#include "Rendering/Spectrogram.h"
// Spectrogam Settings
static uint32_t ShortTimeFFTSize = 4096; // Rayleigh Frequency: (1 / ShortTimeFFTSize) * CaptureFrequency
static uint32_t CaptureFrequency = 88200; // Nyquist Frequency: CaptureFrequency / 2
static uint32_t TimeHistoryLengthInSec = 10;
static uint32_t MaxFrequency = 4000; // Frequencies >= CaptureFrequency will result in blank areas on the spectrogramPlot as they can not be captured
// Renderdata
static std::optional<Rendering::WaveformPlot> waveformPlot;
static std::optional<Rendering::Spectrogram> spectrogramPlot;
// Window Settings
static uint32_t renderWidth = 1600;
static uint32_t renderHeight = 900;
static bool windowFocused = true;
// Other
static constexpr auto AudioFormat = SNT::AudioFormat::MonoFloat32;
using AudioSample = SNT::AudioSample<AudioFormat>;
static glm::uvec2 CalcSpectrogramSize(uint32_t historyLength, uint32_t maxFrequency, uint32_t captureFrequency, uint32_t fftSize);
static void ResizeRessources(uint32_t width, uint32_t height);
static void GLAPIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
int main()
{
GLFWwindow* window;
{
constexpr uint32_t OPENGL_MAJOR_VERSION = 4;
constexpr uint32_t OPENGL_MINOR_VERSION = 6;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, OPENGL_MAJOR_VERSION);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, OPENGL_MINOR_VERSION);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); // GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_COMPAT_PROFILE
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
//glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
window = glfwCreateWindow(renderWidth, renderHeight, "Spectrogram", nullptr, nullptr);
if (window == nullptr)
{
Logger::Log("Window creation failed. Make sure you have OpenGL {}.{} support. Press Enter to exit", OPENGL_MAJOR_VERSION, OPENGL_MINOR_VERSION);
std::cin.get();
return 0;
}
glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height)
{
renderWidth = width;
renderHeight = height;
ResizeRessources(renderWidth, renderHeight);
}
);
glfwSetWindowFocusCallback(window, [](GLFWwindow* window, int focused)
{
windowFocused = focused;
}
);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glfwSwapInterval(1);
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 460 core");
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigDockingWithShift = true;
}
}
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(GLDebugCallback, 0);
BBG::Initialize();
ResizeRessources(renderWidth, renderHeight);
auto captureDevice = SNT::TypedCaptureDevice<AudioFormat>(SNT::GetDefaultCaptureDeviceName(), CaptureFrequency);
captureDevice.StartCapture();
while (!glfwWindowShouldClose(window))
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, 1);
}
{
auto newSamples = std::vector<AudioSample>(captureDevice.AvailableSamples());
captureDevice.CaptureSamples(newSamples);
auto complexInput = std::vector<Rendering::Spectrogram::ComplexType>(newSamples.size());
for (int i = 0; i < complexInput.size(); i++)
{
complexInput[i] = Rendering::Spectrogram::ComplexType(newSamples[i].channels[0], 0.0f);
}
waveformPlot.value().AddSamples(newSamples);
spectrogramPlot.value().AddSamples(complexInput);
}
{
waveformPlot.value().RenderWaveform();
spectrogramPlot.value().RenderSpectrogram();
BBG::Rendering::ScreenColorAttachment colorAttachment {
.clearColor = std::to_array({ 0.0f, 0.0f, 0.0f, 0.0f }),
};
BBG::Rendering::SetViewport({ renderWidth, renderHeight });
BBG::Rendering::SetColorAttachments(colorAttachment);
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::DockSpaceOverViewport();
waveformPlot.value().RenderGui();
spectrogramPlot.value().RenderGui();
if (ImGui::Begin("Settings"))
{
ImGui::PushItemWidth(ImGui::CalcTextSize(captureDevice.GetName()).x);
if (ImGui::BeginCombo("###0", captureDevice.GetName()))
{
static auto captureDeviceNames = SNT::GetAllCaptureDeviceNames();
for (const auto& el : captureDeviceNames)
{
bool isSelected = captureDevice.GetName() == el.c_str();
if (ImGui::Selectable(el.c_str(), isSelected))
{
captureDevice = SNT::TypedCaptureDevice<AudioFormat>(el, CaptureFrequency);
captureDevice.StartCapture();
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
ImGui::PopItemWidth();
uint32_t nyquistFreq = CaptureFrequency / 2;
ImGui::SliderInt("History in Sec", (int*)&TimeHistoryLengthInSec, 1, 20);
ImGui::SliderInt("FFT Size", (int*)&ShortTimeFFTSize, 4, 16384);
ImGui::SliderInt("Max Frequency", (int*)&MaxFrequency, 1, nyquistFreq);
if (ImGui::InputInt("Frequency", (int*)&CaptureFrequency, 1000))
{
captureDevice = SNT::TypedCaptureDevice<AudioFormat>(captureDevice.GetName(), CaptureFrequency);
captureDevice.StartCapture();
}
auto curSize = spectrogramPlot.value().GetSpectrogramTexture().GetCreateInfo().size;
auto size = CalcSpectrogramSize(TimeHistoryLengthInSec, MaxFrequency, CaptureFrequency, ShortTimeFFTSize);
if (size != curSize)
{
spectrogramPlot.value().SetResolution(size.x, size.y, ShortTimeFFTSize);
}
}
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
}
glfwPollEvents();
glfwSwapBuffers(window);
}
}
static glm::uvec2 CalcSpectrogramSize(uint32_t historyLength, uint32_t maxFrequency, uint32_t captureFrequency, uint32_t fftSize)
{
uint32_t spectrogramTimeAxisSize = std::ceilf(historyLength / ((float)fftSize / captureFrequency));
uint32_t spectrogramFrequencyAxisSize = std::ceilf(maxFrequency / (float)captureFrequency * fftSize);
return glm::uvec2(spectrogramTimeAxisSize, spectrogramFrequencyAxisSize);
}
static void ResizeRessources(uint32_t width, uint32_t height)
{
if (!windowFocused)
{
return;
}
if (width == 0 || height == 0)
{
return;
}
renderWidth = width;
renderHeight = height;
if (!waveformPlot.has_value())
{
waveformPlot = Rendering::WaveformPlot(renderWidth / 2, renderHeight, 31);
}
else
{
waveformPlot.value().SetResolution(renderWidth / 2, renderHeight);
}
auto size = CalcSpectrogramSize(TimeHistoryLengthInSec, MaxFrequency, CaptureFrequency, ShortTimeFFTSize);
if (!spectrogramPlot.has_value())
{
spectrogramPlot = Rendering::Spectrogram(size.x, size.y, ShortTimeFFTSize);
}
else
{
spectrogramPlot.value().SetResolution(size.x, size.y);
}
}
static void GLAPIENTRY GLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
if (id == 131185) return; // NVIDIA, Buffer detailed info
Logger::Log(message);
}