switch to alsalib

This commit is contained in:
Sebastien Bourdeauducq 2023-08-23 19:46:36 +08:00
parent 715b461a46
commit b9b80c0ecb
3 changed files with 63 additions and 37 deletions

View File

@ -4,13 +4,13 @@ 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 -I$(TINYALSA_DIR)/include
CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS += -g -Wall -Wformat -O2
LIBS = -L$(TINYALSA_DIR)/lib
LIBS += -lGL `pkg-config --static --libs glfw3` -ltinyalsa
LIBS += -lGL `pkg-config --static --libs glfw3 alsa`
CXXFLAGS += `pkg-config --cflags glfw3`
CXXFLAGS += `pkg-config --cflags glfw3 alsa`
CFLAGS = $(CXXFLAGS)
##---------------------------------------------------------------------

View File

@ -12,10 +12,9 @@
name = "microsa";
src = self;
nativeBuildInputs = [ pkgs.pkg-config ];
propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.tinyalsa ];
propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.alsa-lib ];
preBuild = ''
export IMGUI_DIR=${imgui_dir}
export TINYALSA_DIR=${pkgs.tinyalsa}
'';
installPhase = ''
mkdir -p $out/bin

View File

@ -2,7 +2,6 @@
#include <cstdlib>
#include <cstring>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <vector>
@ -13,11 +12,11 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <tinyalsa/pcm.h>
#include <alsa/asoundlib.h>
static std::atomic<bool> terminate_dsp;
static struct pcm* pcm;
static snd_pcm_t* pcm;
static std::mutex waterfall_data_mutex;
static int waterfall_width = 1600;
@ -25,30 +24,32 @@ static int waterfall_height = 700;
static unsigned int waterfall_data[1600*700];
static void dsp_thread() {
using namespace std::chrono;
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();
std::vector<short int> frames(6400);
while(!terminate_dsp) {
int read_count = pcm_readi(pcm, frames.data(), frame_count);
total_read_count += read_count;
std::cout << read_count << std::endl;
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;
}
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
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];
waterfall_data[i] = 0xff000000 | 0x010101*(frames[i*read_count/waterfall_width]/256 + 128);
}
}
};
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**)
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;
@ -90,27 +91,53 @@ 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;
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;
return 1;
}
static auto PCMClose = []() {
pcm_close(pcm);
snd_pcm_close(pcm);
};
std::atexit(PCMClose);
if (!pcm_is_ready(pcm)){
std::cerr << "failed to open PCM" << std::endl;
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;
return 1;
}