From 5030ed4d386df10f5f816772b5dc45186c88a867 Mon Sep 17 00:00:00 2001 From: Shirasawa <764798966@qq.com> Date: Thu, 26 Oct 2023 00:53:35 +0800 Subject: [PATCH] Fix shared memory bugs --- JavaSharedMemory | 2 +- src/audio_output.h | 4 ++-- src/plugin_host.h | 35 ++++++++++++++++++----------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/JavaSharedMemory b/JavaSharedMemory index fd40cca..13242d3 160000 --- a/JavaSharedMemory +++ b/JavaSharedMemory @@ -1 +1 @@ -Subproject commit fd40ccab13f0aa114e777737b40a90e3c45c86ec +Subproject commit 13242d3d84670b98d2d0e9104a442487a07fa3e5 diff --git a/src/audio_output.h b/src/audio_output.h index ce51697..a04f615 100644 --- a/src/audio_output.h +++ b/src/audio_output.h @@ -18,7 +18,7 @@ namespace eim { void audioDeviceIOCallbackWithContext(const float* const*, int, float* const* outputChannelData, int, int, const juce::AudioIODeviceCallbackContext&) override { streams::out.writeAction(0); - eim::streams::out.flush(); + streams::out.flush(); juce::int8 id; if (streams::in.read(id) != 1) { exit(); @@ -73,7 +73,7 @@ namespace eim { streams::out.writeVarInt(bufferSizes.size()); for (int it : bufferSizes) streams::out.writeVarInt(it); streams::out << device->hasControlPanel(); - eim::streams::out.flush(); + streams::out.flush(); int outBufferSize; streams::in >> outBufferSize; if (setup.bufferSize != bufSize) setup.bufferSize = bufSize; diff --git a/src/plugin_host.h b/src/plugin_host.h index e4d0118..b203de7 100644 --- a/src/plugin_host.h +++ b/src/plugin_host.h @@ -131,23 +131,21 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu auto channels = juce::jmax(processor->getTotalNumInputChannels(), processor->getTotalNumOutputChannels()); bool setInnerBuffer = true; if (enabledSharedMemory) { + shm.reset(); int shmSize; + juce::String shmName = streams::in.readString(); streams::in >> shmSize; - if (!shm || shmSize) { - shm.reset(); - auto shmName = args->getValueForOption("-M|--memory"); - if (shmName.isNotEmpty()) { - shm.reset(jshm::shared_memory::open(shmName.toRawUTF8(), shmSize)); - if (shm) { - auto buffers = new float* [(unsigned long)channels]; - for (int i = 0; i < channels; i++) buffers[i] = reinterpret_cast(shm->address()) + i * bufferSize; - buffer = juce::AudioBuffer(buffers, channels, bufferSize); - delete[] buffers; - setInnerBuffer = false; - } + if (!shm || (shmSize && shmName.isNotEmpty())) { + shm.reset(jshm::shared_memory::open(shmName.toRawUTF8(), shmSize)); + if (shm) { + auto buffers = new float* [(unsigned long)channels]; + for (int i = 0; i < channels; i++) buffers[i] = reinterpret_cast(shm->address()) + i * bufferSize; + buffer = juce::AudioBuffer(buffers, channels, bufferSize); + delete[] buffers; + setInnerBuffer = false; } } else setInnerBuffer = false; - } + } else shm.reset(); if (setInnerBuffer) buffer = juce::AudioBuffer(channels, bufferSize); processor->prepareToPlay(sampleRate, bufferSize); break; @@ -183,7 +181,8 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu for (int i = 0; i < numMidiEvents; i++) { int data; short time; - streams::in >> data >> time; + streams::in.readVarInt(data); + streams::in >> time; buf.addEvent(juce::MidiMessage(data & 0xFF, (data >> 8) & 0xFF, (data >> 16) & 0xFF), time); } for (int i = 0; i < numParameters; i++) { @@ -206,7 +205,7 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu streams::out.writeAction(1); if (!shm) for (int i = 0; i < numOutputChannels; i++) streams::out.writeArray(buffer.getReadPointer(i), bufferSize); - eim::streams::out.flush(); + streams::out.flush(); break; } case 2: { @@ -221,7 +220,9 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu if (!mml.lockWasGained()) return; juce::MemoryBlock memory; processor->getStateInformation(memory); - juce::File(streams::in.readString()).replaceWithData(memory.getData(), memory.getSize()); + streams::out.write((bool)juce::File(streams::in.readString()) + .replaceWithData(memory.getData(), memory.getSize())); + streams::out.flush(); break; } case 4: { @@ -277,7 +278,7 @@ class plugin_host : public juce::JUCEApplication, public juce::AudioPlayHead, pu streams::out << flags << p->getDefaultValue() << (int)p->getCategory() << p->getName(1024) << p->getLabel() << p->getAllValueStrings(); } - eim::streams::out.flush(); + streams::out.flush(); } void writeAllParameterChanges() {