From 312ab52ecae596a3776d7e789a09e85a50012f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bourdeauducq?= Date: Thu, 26 Dec 2024 17:33:59 +0800 Subject: [PATCH] nonblocking --- sndlock.cpp | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/sndlock.cpp b/sndlock.cpp index c4f3b77..01b6b88 100644 --- a/sndlock.cpp +++ b/sndlock.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include "imgui.h" @@ -24,7 +25,7 @@ static void dsp_thread() struct sio_hdl *hdl; struct sio_par par; - hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); + hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1); if(hdl == nullptr) { std::cerr << "failed to open sound device" << std::endl; return; @@ -35,11 +36,17 @@ static void dsp_thread() par.pchan = SND_PCHAN; par.rate = SND_RATE; par.le = SIO_LE_NATIVE; + par.xrun = SIO_ERROR; if(!sio_setpar(hdl, &par)) { std::cerr << "failed to set sound device parameters" << std::endl; sio_close(hdl); return; } + if(!sio_getpar(hdl, &par)) { + std::cerr << "failed to get back sound device parameters" << std::endl; + sio_close(hdl); + return; + } if(!sio_start(hdl)) { std::cerr << "failed to start sound device" << std::endl; @@ -48,18 +55,33 @@ static void dsp_thread() } uint32_t phase = 0; - int16_t buf[SND_BUFLEN*SND_PCHAN]; + + 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]; while(!shutdown_threads) { - uint32_t ftw = frequency*(float)UINT32_MAX/SND_RATE; - for(int i=0;i