Skip to content

Instantly share code, notes, and snippets.

@jpcima
Created November 8, 2020 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpcima/fbe7898d59b3e663845a86b10623d142 to your computer and use it in GitHub Desktop.
Save jpcima/fbe7898d59b3e663845a86b10623d142 to your computer and use it in GitHub Desktop.
diff --git a/src/sfizz/Messaging.h b/src/sfizz/Messaging.h
index 737fa1d4..7e5fc082 100644
--- a/src/sfizz/Messaging.h
+++ b/src/sfizz/Messaging.h
@@ -28,4 +28,29 @@ inline void Client::receive(int delay, const char* path, const char* sig, const
receive_(data_, delay, path, sig, args);
}
+///
+template <unsigned MaxArguments = 16> class BasicMessageBuffer;
+typedef BasicMessageBuffer<> MessageBuffer;
+
+template <unsigned MaxArguments>
+class BasicMessageBuffer {
+ void setPath(const char* path);
+ void addInt(int32_t value);
+ void addLong(int64_t value);
+ void addFloat(float value);
+ void addDouble(double value);
+ void addString(const char* value);
+ void addBlob(const uint8_t* value, uint32_t size);
+ void addMidi(const uint8_t value[4]);
+
+private:
+ const char* path_ = nullptr;
+ unsigned argc_ = 0;
+ char sig_[MaxArguments + 1] = {};
+ sfizz_arg_t args_[MaxArguments];
+ sfizz_blob_t blobs_[MaxArguments];
+};
+
} // namespace sfz
+
+#include "Messaging.hpp"
diff --git a/src/sfizz/Messaging.hpp b/src/sfizz/Messaging.hpp
new file mode 100644
index 00000000..9847f91d
--- /dev/null
+++ b/src/sfizz/Messaging.hpp
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: BSD-2-Clause
+
+// This code is part of the sfizz library and is licensed under a BSD 2-clause
+// license. You should have receive a LICENSE.md file along with the code.
+// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
+
+#pragma once
+#include "Messaging.h"
+#include <stdexcept>
+#include <cstring>
+
+namespace sfz {
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::setPath(const char* path)
+{
+ path_ = path;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addInt(int32_t value)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addInt");
+
+ args_[n].i = value;
+ sig_[n] = 'i';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addLong(int64_t value)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addLong");
+
+ args_[n].h = value;
+ sig_[n] = 'h';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addFloat(float value)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addFloat");
+
+ args_[n].f = value;
+ sig_[n] = 'f';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addDouble(double value)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addDouble");
+
+ args_[n].d = value;
+ sig_[n] = 'd';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addString(const char* value)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addString");
+
+ args_[n].s = value;
+ sig_[n] = 's';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addBlob(const uint8_t* value, uint32_t size)
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addBlob");
+
+ sfizz_blob_t* blob = &blobs_[n];
+ blob->data = value;
+ blob->size = size;
+ args_[n].b = blob;
+ sig_[n] = 'b';
+ argc_ = n + 1;
+}
+
+template <unsigned MaxArguments>
+void BasicMessageBuffer<MaxArguments>::addMidi(const uint8_t value[4])
+{
+ unsigned n = argc_;
+ if (n == MaxArguments)
+ throw std::length_error("BasicMessageBuffer::addMidi");
+
+ std::memcpy(args_[n].m, value, 4);
+ sig_[n] = 's';
+ argc_ = n + 1;
+}
+
+} // namespace sfz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment