sndlock/sndlock.cpp

173 lines
4.9 KiB
C++
Raw Normal View History

2024-12-26 11:48:03 +08:00
#include <iostream>
2024-12-26 16:18:43 +08:00
#include <thread>
#include <cstdint>
2024-12-26 11:48:03 +08:00
2024-12-26 12:33:11 +08:00
#include <GLFW/glfw3.h>
2024-12-26 16:18:43 +08:00
#include <sndio.h>
2024-12-26 17:33:59 +08:00
#include <poll.h>
2024-12-26 16:18:43 +08:00
#include <math.h>
2024-12-26 12:33:11 +08:00
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
2024-12-26 16:18:43 +08:00
static std::atomic<bool> shutdown_threads;
static std::atomic<float> frequency = 440.0f;
#define SND_SIG 1
#define SND_BITS 16
#define SND_PCHAN 2
#define SND_RATE 44100
#define SND_BUFLEN 4096
static void dsp_thread()
{
struct sio_hdl *hdl;
struct sio_par par;
2024-12-26 17:33:59 +08:00
hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1);
2024-12-26 16:18:43 +08:00
if(hdl == nullptr) {
std::cerr << "failed to open sound device" << std::endl;
return;
}
sio_initpar(&par);
par.sig = SND_SIG;
par.bits = SND_BITS;
par.pchan = SND_PCHAN;
par.rate = SND_RATE;
par.le = SIO_LE_NATIVE;
2024-12-26 17:33:59 +08:00
par.xrun = SIO_ERROR;
2024-12-26 16:18:43 +08:00
if(!sio_setpar(hdl, &par)) {
std::cerr << "failed to set sound device parameters" << std::endl;
sio_close(hdl);
return;
}
2024-12-26 17:33:59 +08:00
if(!sio_getpar(hdl, &par)) {
std::cerr << "failed to get back sound device parameters" << std::endl;
sio_close(hdl);
return;
}
2024-12-26 16:18:43 +08:00
if(!sio_start(hdl)) {
std::cerr << "failed to start sound device" << std::endl;
sio_close(hdl);
return;
}
uint32_t phase = 0;
2024-12-26 17:33:59 +08:00
int16_t buf_out[SND_BUFLEN*SND_PCHAN];
size_t buf_out_idx = SND_BUFLEN*SND_PCHAN;
nfds_t nfds = sio_nfds(hdl);
struct pollfd pfd[nfds];
2024-12-26 16:18:43 +08:00
while(!shutdown_threads) {
2024-12-26 17:33:59 +08:00
sio_pollfd(hdl, pfd, POLLOUT);
poll(pfd, nfds, INFTIM);
int revents = sio_revents(hdl, pfd);
if(revents & POLLOUT) {
uint32_t ftw = frequency*(float)UINT32_MAX/SND_RATE;
if(buf_out_idx == SND_BUFLEN*SND_PCHAN) {
for(int i=0;i<SND_BUFLEN;i++) {
buf_out[2*i] = buf_out[2*i+1] = (INT16_MAX-1)*sin(phase*2.0f*(float)M_PI/(float)UINT32_MAX);
phase += ftw;
}
buf_out_idx = 0;
}
size_t written = sio_write(hdl, &buf_out[buf_out_idx], sizeof(buf_out) - buf_out_idx*sizeof(buf_out[0]));
buf_out_idx += written/sizeof(buf_out[0]);
if(!written) {
std::cerr << "failed to write to sound device" << std::endl;
sio_stop(hdl);
sio_close(hdl);
return;
}
2024-12-26 16:18:43 +08:00
}
}
sio_stop(hdl);
sio_close(hdl);
}
2024-12-26 12:33:11 +08:00
int main(int argc, char* argv[])
{
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(1024, 768, "sndlock", nullptr, nullptr);
if(window == nullptr) {
std::cerr << "failed to create GLFW window" << std::endl;
return 1;
}
static auto DestroyWindow = []() {
glfwDestroyWindow(window);
};
std::atexit(DestroyWindow);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
static auto ImGuiDestroyContext = []() {
ImGui::DestroyContext();
};
std::atexit(ImGuiDestroyContext);
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
std::atexit(ImGui_ImplGlfw_Shutdown);
ImGui_ImplOpenGL3_Init("#version 130");
std::atexit(ImGui_ImplOpenGL3_Shutdown);
2024-12-26 16:18:43 +08:00
static std::thread dsp_thread_h = std::thread(dsp_thread);
static auto JoinDSP = []() {
dsp_thread_h.join();
};
std::atexit(JoinDSP);
shutdown_threads = false;
static auto SetShutdown = []() {
shutdown_threads = true;
};
std::atexit(SetShutdown);
2024-12-26 12:33:11 +08:00
bool exit = false;
while(!exit && !glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::Begin("sndlock", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize);
2024-12-26 16:18:43 +08:00
float frequency_l = frequency;
ImGui::SliderFloat("Frequency", &frequency_l, 50.0f, 8000.0f);
frequency = frequency_l;
2024-12-26 12:33:11 +08:00
if(ImGui::Button("Exit"))
exit = true;
ImGui::End();
ImGui::PopStyleVar(1);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glfwSwapBuffers(window);
}
2024-12-26 11:48:03 +08:00
return 0;
}