tinyalsa attempt

This commit is contained in:
Sebastien Bourdeauducq 2023-08-23 18:44:23 +08:00
parent 9c47f72298
commit 715b461a46
3 changed files with 55 additions and 12 deletions

View File

@ -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)

View File

@ -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

View File

@ -1,8 +1,11 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <vector>
#include <GLFW/glfw3.h>
@ -10,37 +13,52 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <tinyalsa/pcm.h>
static std::atomic<bool> 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<int> 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<std::mutex> guard(waterfall_data_mutex);
for(int y=0;y<waterfall_height;y++)
for(int x=0;x<waterfall_width;x++)
waterfall_data[y*waterfall_width+x] = (x+y+t) | 0xff000000;
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], 4*waterfall_width*(waterfall_height - 1));
for(int i=0;i<waterfall_width;i++) {
waterfall_data[i] = 0xff000000 | frames[2*i*read_count/waterfall_width];
}
}
std::this_thread::sleep_for(20ms);
};
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "measured sample rate: " << total_read_count/((end - begin)/1s) << std::endl;
}
int main(int, char**)
{
if (!glfwInit())
if (!glfwInit()) {
std::cerr << "failed to initialize GLFW" << std::endl;
return 1;
}
std::atexit(glfwTerminate);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
static GLFWwindow* window = glfwCreateWindow(1900, 720, "microsa", nullptr, nullptr);
if (window == nullptr) {
std::cerr << "failed to create GLFW window" << std::endl;
return 1;
}
static auto DestroyWindow = []() {
@ -72,6 +90,30 @@ int main(int, char**)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
const struct pcm_config config = {
.channels = 2,
.rate = 192000,
.period_size = 1024,
.period_count = 2,
.format = PCM_FORMAT_S32_LE,
.start_threshold = 1024,
.stop_threshold = 1024 * 2,
.silence_threshold = 1024 * 2,
};
pcm = pcm_open(0, 0, PCM_IN, &config);
if(pcm == nullptr) {
std::cerr << "failed to allocate memory for PCM" << std::endl;
return 1;
}
static auto PCMClose = []() {
pcm_close(pcm);
};
std::atexit(PCMClose);
if (!pcm_is_ready(pcm)){
std::cerr << "failed to open PCM" << std::endl;
return 1;
}
terminate_dsp = false;
static std::thread dsp_thread_h = std::thread(dsp_thread);
static auto TerminateDSP = []() {