Skip to content

Commit

Permalink
Fix shared memory bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Oct 25, 2023
1 parent d7a483d commit 5030ed4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/audio_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 18 additions & 17 deletions src/plugin_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float*>(shm->address()) + i * bufferSize;
buffer = juce::AudioBuffer<float>(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<float*>(shm->address()) + i * bufferSize;
buffer = juce::AudioBuffer<float>(buffers, channels, bufferSize);
delete[] buffers;
setInnerBuffer = false;
}
} else setInnerBuffer = false;
}
} else shm.reset();
if (setInnerBuffer) buffer = juce::AudioBuffer<float>(channels, bufferSize);
processor->prepareToPlay(sampleRate, bufferSize);
break;
Expand Down Expand Up @@ -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++) {
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5030ed4

Please sign in to comment.