From 715b461a46635da19e8e8f3c14024750114e4c3d Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 23 Aug 2023 18:44:23 +0800 Subject: [PATCH] tinyalsa attempt --- Makefile | 6 +++--- flake.nix | 3 ++- main.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ed8fc38..5f9b9e3 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui SOURCES += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) -CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends +CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(TINYALSA_DIR)/include CXXFLAGS += -g -Wall -Wformat -O2 -LIBS = +LIBS = -L$(TINYALSA_DIR)/lib -LIBS += -lGL `pkg-config --static --libs glfw3` +LIBS += -lGL `pkg-config --static --libs glfw3` -ltinyalsa CXXFLAGS += `pkg-config --cflags glfw3` CFLAGS = $(CXXFLAGS) diff --git a/flake.nix b/flake.nix index 3666de1..df5bec3 100644 --- a/flake.nix +++ b/flake.nix @@ -12,9 +12,10 @@ name = "microsa"; src = self; nativeBuildInputs = [ pkgs.pkg-config ]; - propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi ]; + propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.tinyalsa ]; preBuild = '' export IMGUI_DIR=${imgui_dir} + export TINYALSA_DIR=${pkgs.tinyalsa} ''; installPhase = '' mkdir -p $out/bin diff --git a/main.cpp b/main.cpp index d278e21..6046c3f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,11 @@ +#include #include +#include #include #include #include #include +#include #include @@ -10,37 +13,52 @@ #include #include +#include + static std::atomic terminate_dsp; + +static struct pcm* pcm; + 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() { - using namespace std::chrono_literals; + using namespace std::chrono; - int t = 0; + int frame_count = pcm_get_rate(pcm); + std::vector frames(pcm_frames_to_bytes(pcm, frame_count)/4); + int total_read_count = 0; + std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); while(!terminate_dsp) { - t++; + int read_count = pcm_readi(pcm, frames.data(), frame_count); + total_read_count += read_count; + std::cout << read_count << std::endl; { std::lock_guard guard(waterfall_data_mutex); - for(int y=0;y