Compare commits
2 Commits
9c47f72298
...
b9b80c0ecb
Author | SHA1 | Date |
---|---|---|
Sebastien Bourdeauducq | b9b80c0ecb | |
Sebastien Bourdeauducq | 715b461a46 |
6
Makefile
6
Makefile
|
@ -6,11 +6,11 @@ OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
|
|||
|
||||
CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
|
||||
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 alsa`
|
||||
|
||||
CXXFLAGS += `pkg-config --cflags glfw3`
|
||||
CXXFLAGS += `pkg-config --cflags glfw3 alsa`
|
||||
CFLAGS = $(CXXFLAGS)
|
||||
|
||||
##---------------------------------------------------------------------
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
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.alsa-lib ];
|
||||
preBuild = ''
|
||||
export IMGUI_DIR=${imgui_dir}
|
||||
'';
|
||||
|
|
91
main.cpp
91
main.cpp
|
@ -1,8 +1,10 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
|
@ -10,37 +12,54 @@
|
|||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
static std::atomic<bool> terminate_dsp;
|
||||
|
||||
static snd_pcm_t* 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;
|
||||
|
||||
int t = 0;
|
||||
std::vector<short int> frames(6400);
|
||||
while(!terminate_dsp) {
|
||||
t++;
|
||||
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);
|
||||
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 | 0x010101*(frames[i*read_count/waterfall_width]/256 + 128);
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(20ms);
|
||||
};
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (!glfwInit())
|
||||
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;
|
||||
}
|
||||
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 +91,56 @@ 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);
|
||||
|
||||
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 = []() {
|
||||
snd_pcm_close(pcm);
|
||||
};
|
||||
std::atexit(PCMClose);
|
||||
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;
|
||||
}
|
||||
|
||||
terminate_dsp = false;
|
||||
static std::thread dsp_thread_h = std::thread(dsp_thread);
|
||||
static auto TerminateDSP = []() {
|
||||
|
|
Loading…
Reference in New Issue