From 15303721800f53acc76468b31bf9af9ecb73b631 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 24 Aug 2023 00:14:27 +0800 Subject: [PATCH] actually use pulseaudio (works around several ALSA bugs/problems) --- README | 4 ++++ main.cpp | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..0650df5 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +* set pulseaudio to 192kHz sample rate: + hardware.pulseaudio.daemon.config = { "default-sample-rate" = 192000; }; +* check that sound card is operating at 192kHz: + cat /proc/asound/card0/pcm0c/sub0/hw_params diff --git a/main.cpp b/main.cpp index 58a372e..b7a48b3 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -27,7 +28,11 @@ static int waterfall_width = 1600; static int waterfall_height = 700; static unsigned int waterfall_data[1600*700]; +static std::atomic fps; + static void dsp_thread() { + using namespace std::literals::chrono_literals; + size_t len = 6400; std::vector frames(len); @@ -43,6 +48,8 @@ static void dsp_thread() { for(size_t i=0; i> frames_ft(len); + int iterations = 0; + auto last_second = std::chrono::steady_clock::now(); while(!terminate_dsp) { int read_count = snd_pcm_readi(pcm, frames.data(), len); if(read_count < 0) { @@ -59,18 +66,17 @@ static void dsp_thread() { waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(4*waterfall_width)])/10000.), 255); } } + iterations++; + if((std::chrono::steady_clock::now() - last_second) >= 1s) { + fps = iterations; + iterations = 0; + last_second += 1s; + } }; } int main(int argc, char* argv[]) { - 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]; - if (!glfwInit()) { std::cerr << "failed to initialize GLFW" << std::endl; return 1; @@ -113,7 +119,7 @@ int main(int argc, char* argv[]) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); int err; - if ((err = snd_pcm_open(&pcm, alsa_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) { + if ((err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) { std::cerr << "cannot open ALSA device: " << snd_strerror(err) << std::endl; return 1; } @@ -200,6 +206,7 @@ int main(int argc, char* argv[]) ImGui::TableNextColumn(); if(ImGui::Button("Exit")) exit = true; + ImGui::Text("FPS: %d", (int)fps); ImGui::TableNextColumn(); ImGui::Image((void*)(intptr_t)waterfall, ImVec2(waterfall_width, waterfall_height)); ImGui::EndTable();