microsa/main.cpp

199 lines
7.0 KiB
C++
Raw Normal View History

2023-08-23 18:44:23 +08:00
#include <iostream>
2023-08-23 15:06:05 +08:00
#include <cstdlib>
2023-08-23 18:44:23 +08:00
#include <cstring>
2023-08-23 15:53:50 +08:00
#include <thread>
#include <atomic>
#include <mutex>
2023-08-23 18:44:23 +08:00
#include <vector>
2023-08-23 15:06:05 +08:00
2023-08-22 23:41:16 +08:00
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
2023-08-23 19:46:36 +08:00
#include <alsa/asoundlib.h>
2023-08-23 18:44:23 +08:00
2023-08-23 15:53:50 +08:00
static std::atomic<bool> terminate_dsp;
2023-08-23 18:44:23 +08:00
2023-08-23 19:46:36 +08:00
static snd_pcm_t* pcm;
2023-08-23 18:44:23 +08:00
2023-08-23 15:53:50 +08:00
static std::mutex waterfall_data_mutex;
static int waterfall_width = 1600;
static int waterfall_height = 700;
static unsigned int waterfall_data[1600*700];
static void dsp_thread() {
2023-08-23 19:46:36 +08:00
std::vector<short int> frames(6400);
2023-08-23 15:53:50 +08:00
while(!terminate_dsp) {
2023-08-23 19:46:36 +08:00
int read_count = snd_pcm_readi(pcm, frames.data(), 6400);
if(read_count < 0) {
std::cerr << "read from audio interface failed: " << snd_strerror(read_count) << std::endl;
break;
}
2023-08-23 15:53:50 +08:00
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
2023-08-23 18:44:23 +08:00
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], 4*waterfall_width*(waterfall_height - 1));
for(int i=0;i<waterfall_width;i++) {
2023-08-23 19:46:36 +08:00
waterfall_data[i] = 0xff000000 | 0x010101*(frames[i*read_count/waterfall_width]/256 + 128);
2023-08-23 18:44:23 +08:00
}
2023-08-23 15:53:50 +08:00
}
};
}
2023-08-23 19:46:36 +08:00
int main(int argc, char* argv[])
2023-08-22 23:41:16 +08:00
{
2023-08-23 19:46:36 +08:00
if(argc != 2) {
std::cerr << "please specify ALSA device on the command line" << std::endl;
std::cerr << "use arecord -L to list, and select raw device without pulseaudio" << std::endl;
return 1;
}
char* alsa_device = argv[1];
2023-08-23 18:44:23 +08:00
if (!glfwInit()) {
std::cerr << "failed to initialize GLFW" << std::endl;
2023-08-22 23:41:16 +08:00
return 1;
2023-08-23 18:44:23 +08:00
}
2023-08-23 15:06:05 +08:00
std::atexit(glfwTerminate);
2023-08-22 23:41:16 +08:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
2023-08-23 15:06:05 +08:00
static GLFWwindow* window = glfwCreateWindow(1900, 720, "microsa", nullptr, nullptr);
2023-08-22 23:41:16 +08:00
if (window == nullptr) {
2023-08-23 18:44:23 +08:00
std::cerr << "failed to create GLFW window" << std::endl;
2023-08-22 23:41:16 +08:00
return 1;
}
2023-08-23 15:06:05 +08:00
static auto DestroyWindow = []() {
glfwDestroyWindow(window);
};
std::atexit(DestroyWindow);
2023-08-22 23:41:16 +08:00
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
2023-08-23 13:12:28 +08:00
IMGUI_CHECKVERSION();
2023-08-22 23:41:16 +08:00
ImGui::CreateContext();
2023-08-23 15:06:05 +08:00
static auto ImGuiDestroyContext = []() {
ImGui::DestroyContext();
};
std::atexit(ImGuiDestroyContext);
2023-08-23 13:12:28 +08:00
ImGuiIO& io = ImGui::GetIO();
2023-08-22 23:41:16 +08:00
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
2023-08-23 15:06:05 +08:00
std::atexit(ImGui_ImplGlfw_Shutdown);
2023-08-22 23:41:16 +08:00
ImGui_ImplOpenGL3_Init("#version 130");
2023-08-23 15:06:05 +08:00
std::atexit(ImGui_ImplOpenGL3_Shutdown);
2023-08-22 23:41:16 +08:00
2023-08-23 13:12:28 +08:00
GLuint waterfall;
glGenTextures(1, &waterfall);
glBindTexture(GL_TEXTURE_2D, waterfall);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2023-08-23 19:46:36 +08:00
int err;
if ((err = snd_pcm_open(&pcm, alsa_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
std::cerr << "cannot open ALSA device: " << snd_strerror(err) << std::endl;
2023-08-23 18:44:23 +08:00
return 1;
}
static auto PCMClose = []() {
2023-08-23 19:46:36 +08:00
snd_pcm_close(pcm);
2023-08-23 18:44:23 +08:00
};
std::atexit(PCMClose);
2023-08-23 19:46:36 +08:00
snd_pcm_hw_params_t* pcm_hw_params;
if((err = snd_pcm_hw_params_malloc(&pcm_hw_params)) < 0) {
std::cerr << "cannot allocate ALSA hardware parameter structure: " << snd_strerror(err) << std::endl;
return 1;
}
if((err = snd_pcm_hw_params_any(pcm, pcm_hw_params)) < 0) {
std::cerr << "cannot initialize ALSA hardware parameter structure: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params_set_access(pcm, pcm_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
std::cerr << "cannot set ALSA access type: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params_set_format(pcm, pcm_hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
std::cerr << "cannot set sample format: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params_set_rate(pcm, pcm_hw_params, 192000, 0)) < 0) {
std::cerr << "cannot set ALSA sample rate: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params_set_channels(pcm, pcm_hw_params, 1)) < 0) {
std::cerr << "cannot set ALSA channel count: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params(pcm, pcm_hw_params)) < 0) {
std::cerr << "cannot set ALSA parameters: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
snd_pcm_hw_params_free(pcm_hw_params);
if((err = snd_pcm_prepare(pcm)) < 0) {
std::cerr << "cannot prepare ALSA interface: " << snd_strerror (err) << std::endl;
2023-08-23 18:44:23 +08:00
return 1;
}
2023-08-23 15:53:50 +08:00
terminate_dsp = false;
static std::thread dsp_thread_h = std::thread(dsp_thread);
static auto TerminateDSP = []() {
terminate_dsp = true;
dsp_thread_h.join();
};
std::atexit(TerminateDSP);
2023-08-23 13:12:28 +08:00
bool exit = false;
2023-08-23 15:53:50 +08:00
while(!exit && !glfwWindowShouldClose(window)) {
2023-08-22 23:41:16 +08:00
glfwPollEvents();
2023-08-23 13:12:28 +08:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, waterfall);
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
2023-08-23 15:53:50 +08:00
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, waterfall_width, waterfall_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, waterfall_data);
}
2023-08-22 23:41:16 +08:00
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2023-08-23 13:12:28 +08:00
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::Begin("microsa", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize);
ImGui::BeginTable("microsa", 2, ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable);
ImGui::TableSetupColumn("", 0, 280.0);
ImGui::TableSetupColumn("", 0, 1600.0);
ImGui::TableNextColumn();
if(ImGui::Button("Exit"))
exit = true;
ImGui::TableNextColumn();
ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height));
ImGui::EndTable();
ImGui::End();
ImGui::PopStyleVar(1);
2023-08-22 23:41:16 +08:00
ImGui::Render();
2023-08-23 13:12:28 +08:00
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2023-08-22 23:41:16 +08:00
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glfwSwapBuffers(window);
}
2023-08-22 23:24:17 +08:00
return 0;
}