Experimental commit for issue #337; valgrind fixes.

This commit is contained in:
Robin E. R. Davies
2025-07-19 21:37:46 -04:00
parent 5e9e8c78e6
commit b261f98167
43 changed files with 277 additions and 88 deletions
+67 -40
View File
@@ -1484,7 +1484,7 @@ namespace pipedal
#endif
}
std::jthread *audioThread = nullptr;
std::unique_ptr<std::jthread> audioThread;
bool audioRunning;
bool block = false;
@@ -1518,32 +1518,36 @@ namespace pipedal
} while (frames > 0);
return framesRead;
}
protected:
void ReadMidiData(uint32_t audioFrame)
{
AlsaMidiMessage message;
midiEventCount = 0;
while(alsaSequencer->ReadMessage(message,0))
while (alsaSequencer->ReadMessage(message, 0))
{
size_t messageSize = message.size;
if (messageSize == 0)
if (messageSize == 0)
{
continue;
}
if (midiEventMemoryIndex + messageSize >= this->midiEventMemory.size()) {
if (midiEventMemoryIndex + messageSize >= this->midiEventMemory.size())
{
continue;
}
if (midiEventCount >= this->midiEvents.size()) {
midiEvents.resize(midiEventCount*2);
if (midiEventCount >= this->midiEvents.size())
{
midiEvents.resize(midiEventCount * 2);
}
// for now, prevent META event messages from propagating.
if (message.data[0] == 0xFF && message.size > 1) {
// for now, prevent META event messages from propagating.
if (message.data[0] == 0xFF && message.size > 1)
{
continue;
}
MidiEvent *pEvent = midiEvents.data() + midiEventCount++;
pEvent->time = audioFrame;
pEvent->size = messageSize;
pEvent->size = messageSize;
pEvent->buffer = midiEventMemory.data() + midiEventMemoryIndex;
memcpy(
@@ -1551,11 +1555,10 @@ namespace pipedal
message.data,
message.size);
midiEventMemoryIndex += messageSize;
}
}
private:
private:
long WriteBuffer(snd_pcm_t *handle, uint8_t *buf, size_t frames)
{
long framesRead;
@@ -1759,7 +1762,7 @@ namespace pipedal
}
}
audioThread = new std::jthread([this]()
audioThread = std::make_unique<std::jthread>([this]()
{ AudioThread(); });
}
@@ -1773,8 +1776,7 @@ namespace pipedal
terminateAudio(true);
if (audioThread)
{
this->audioThread->join();
this->audioThread = 0;
this->audioThread = 0; // jthread joins.
}
Lv2Log::debug("Audio thread joined.");
}
@@ -1789,9 +1791,6 @@ namespace pipedal
AlsaSequencer::ptr alsaSequencer;
public:
virtual void SetAlsaSequencer(AlsaSequencer::ptr alsaSequencer) override
{
this->alsaSequencer = alsaSequencer;
@@ -1822,7 +1821,7 @@ namespace pipedal
{
for (size_t i = 0; i < buffer.size(); ++i)
{
// delete[] buffer[i];
delete[] buffer[i];
buffer[i] = 0;
}
buffer.clear();
@@ -1887,6 +1886,51 @@ namespace pipedal
snd_pcm_t *captureHandle = nullptr;
snd_pcm_hw_params_t *playbackHwParams = nullptr;
snd_pcm_hw_params_t *captureHwParams = nullptr;
Finally ff_playbackHandle{
[&playbackHandle]()
{
if (playbackHandle)
{
int rc = snd_pcm_close(playbackHandle);
if (rc < 0)
{
throw std::runtime_error("snd_pcm_close failed.");
}
playbackHandle = nullptr;
}
}};
Finally ff_captureHandle{
[&captureHandle]()
{
if (captureHandle)
{
int rc = snd_pcm_close(captureHandle);
if (rc < 0)
{
throw std::runtime_error("snd_pcm_close failed.");
}
captureHandle = nullptr;
}
}};
Finally ff_playbackHwParams{
[&playbackHwParams]()
{
if (playbackHwParams)
{
snd_pcm_hw_params_free(playbackHwParams);
}
}};
Finally ff_captureHwParams{
[&captureHwParams]()
{
if (captureHwParams)
{
snd_pcm_hw_params_free(captureHwParams);
}
}};
std::string alsaDeviceName = jackServerSettings.GetAlsaInputDevice();
bool result = false;
@@ -2001,28 +2045,6 @@ namespace pipedal
result = false;
throw;
}
if (playbackHwParams)
{
snd_pcm_hw_params_free(playbackHwParams);
playbackHwParams = nullptr;
}
if (captureHwParams)
{
snd_pcm_hw_params_free(captureHwParams);
captureHwParams = nullptr;
}
if (playbackHandle)
{
snd_pcm_close(playbackHandle);
playbackHandle = nullptr;
}
if (captureHandle)
{
snd_pcm_close(captureHandle);
captureHandle = nullptr;
}
return result;
}
@@ -2176,4 +2198,9 @@ namespace pipedal
}
#endif
}
void FreeAlsaGlobals()
{
snd_config_update_free_global(); // to get a clean Valgrind report.
}
} // namespace
+2
View File
@@ -40,5 +40,7 @@ namespace pipedal {
// test only.
void AlsaFormatEncodeDecodeTest(AudioDriverHost*driverHost);
void MidiDecoderTest();
void FreeAlsaGlobals(); // for valgrind. Free the Alsa configuration cache.
}
+4 -2
View File
@@ -1511,8 +1511,10 @@ public:
if (property != nullptr && value != nullptr && property->type == uris.atom_URID)
{
LV2_URID propertyUrid = ((LV2_Atom_URID *)property)->body;
this->pNotifyCallbacks->OnPatchSetReply(instanceId, propertyUrid, value);
// this->pNotifyCallbacks->OnNotifyMaybeLv2StateChanged(instanceId);
if (this->pNotifyCallbacks)
{
this->pNotifyCallbacks->OnPatchSetReply(instanceId, propertyUrid, value);
}
}
}
}
+4 -1
View File
@@ -2239,7 +2239,10 @@ void PiPedalModel::OnPatchSetReply(uint64_t instanceId, LV2_URID patchSetPropert
}
}
}
audioHost->SetListenForAtomOutput(atomOutputListeners.size() != 0);
if (audioHost)
{
audioHost->SetListenForAtomOutput(atomOutputListeners.size() != 0);
}
}
void PiPedalModel::OnNotifyPathPatchPropertyReceived(
+6 -3
View File
@@ -828,7 +828,10 @@ Lv2PluginInfo::Lv2PluginInfo(PluginHost *lv2Host, LilvWorld *pWorld, const LilvP
std::string bundleUri = bundleUriNode.AsUri();
std::string bundlePath = lilv_file_uri_parse(bundleUri.c_str(), nullptr);
char *lilvBundlePath = lilv_file_uri_parse(bundleUri.c_str(), nullptr);
std::string bundlePath = lilvBundlePath;
lilv_free(lilvBundlePath);
if (bundlePath.length() == 0)
throw std::logic_error("Bundle uri is not a file uri.");
@@ -1655,9 +1658,9 @@ Lv2PortGroup::Lv2PortGroup(PluginHost *lv2Host, const std::string &groupUri)
this->uri_ = groupUri;
AutoLilvNode uri = lilv_new_uri(pWorld, groupUri.c_str());
LilvNode *symbolNode = lilv_world_get(pWorld, uri, lv2Host->lilvUris->lv2core__symbol, nullptr);
AutoLilvNode symbolNode = lilv_world_get(pWorld, uri, lv2Host->lilvUris->lv2core__symbol, nullptr);
symbol_ = nodeAsString(symbolNode);
LilvNode *nameNode = lilv_world_get(pWorld, uri, lv2Host->lilvUris->lv2core__name, nullptr);
AutoLilvNode nameNode = lilv_world_get(pWorld, uri, lv2Host->lilvUris->lv2core__name, nullptr);
name_ = nodeAsString(nameNode);
}
+3 -2
View File
@@ -39,6 +39,7 @@
#include "ofstream_synced.hpp"
#include "ModTemplateGenerator.hpp"
#include <mutex>
#include "WebServer.hpp"
@@ -685,7 +686,7 @@ namespace pipedal
int threads = 1;
size_t maxUploadSize = 512 * 1024 * 1024;
std::thread *pBgThread = nullptr;
std::unique_ptr<std::thread> pBgThread;
std::recursive_mutex io_mutex;
boost::asio::io_context *pIoContext = nullptr;
@@ -1417,7 +1418,7 @@ namespace pipedal
throw std::runtime_error("Bad state.");
}
this->signalOnDone = signalOnDone;
this->pBgThread = new std::thread(ThreadProc, this);
this->pBgThread = std::make_unique<std::thread>(ThreadProc, this);
}
virtual void DisplayIpAddresses() override;
+3
View File
@@ -41,6 +41,7 @@
#include "HtmlHelper.hpp"
#include <thread>
#include <atomic>
#include "AlsaDriver.hpp"
#include <signal.h>
#include <semaphore.h>
@@ -422,6 +423,8 @@ int main(int argc, char *argv[])
}
Lv2Log::info("Shutdown complete.");
FreeAlsaGlobals();
if (systemd)
{
sd_notify(0, "STOPPING=1");