6 changes: 3 additions & 3 deletions dpf/dgl/src/Cairo.cpp
@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2019-2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
Expand Down Expand Up @@ -384,8 +384,8 @@ void CairoImage::loadFromMemory(const char* const rdata, const Size<uint>& s, co

cairo_surface_t* const newsurface = cairo_image_surface_create_for_data(newdata, cairoformat, width, height, stride);
DISTRHO_SAFE_ASSERT_RETURN(newsurface != nullptr,);
DISTRHO_SAFE_ASSERT_RETURN(s.getWidth() == cairo_image_surface_get_width(newsurface),);
DISTRHO_SAFE_ASSERT_RETURN(s.getHeight() == cairo_image_surface_get_height(newsurface),);
DISTRHO_SAFE_ASSERT_RETURN(static_cast<int>(s.getWidth()) == cairo_image_surface_get_width(newsurface),);
DISTRHO_SAFE_ASSERT_RETURN(static_cast<int>(s.getHeight()) == cairo_image_surface_get_height(newsurface),);

cairo_surface_destroy(surface);

Expand Down
4 changes: 2 additions & 2 deletions dpf/dgl/src/WindowPrivateData.cpp
Expand Up @@ -31,7 +31,7 @@

START_NAMESPACE_DGL

#if defined(DEBUG) && defined(DGL_DEBUG_EVENTS)
#ifdef DGL_DEBUG_EVENTS
# define DGL_DBG(msg) std::fprintf(stderr, "%s", msg);
# define DGL_DBGp(...) std::fprintf(stderr, __VA_ARGS__);
# define DGL_DBGF std::fflush(stderr);
Expand Down Expand Up @@ -614,7 +614,7 @@ void Window::PrivateData::onPuglConfigure(const double width, const double heigh

void Window::PrivateData::onPuglExpose()
{
DGL_DBG("PUGL: onPuglExpose\n");
// DGL_DBG("PUGL: onPuglExpose\n");

puglOnDisplayPrepare(view);

Expand Down
410 changes: 326 additions & 84 deletions dpf/dgl/src/pugl-upstream/src/wasm.c

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions dpf/dgl/src/pugl-upstream/src/wasm.h
Expand Up @@ -10,6 +10,8 @@

#include "pugl/pugl.h"

// #define PUGL_WASM_ASYNC_CLIPBOARD

struct PuglTimer {
PuglView* view;
uintptr_t id;
Expand All @@ -19,14 +21,24 @@ struct PuglWorldInternalsImpl {
double scaleFactor;
};

struct LastMotionValues {
double x, y, xRoot, yRoot;
};

struct PuglInternalsImpl {
PuglSurface* surface;
bool needsRepaint;
bool pointerLocked;
uint32_t numTimers;
long lastX, lastY;
double lockedX, lockedY;
double lockedRootX, lockedRootY;
LastMotionValues lastMotion;
long buttonPressTimeout;
PuglEvent nextButtonEvent;
#ifdef PUGL_WASM_ASYNC_CLIPBOARD
PuglViewHintValue supportsClipboardRead;
PuglViewHintValue supportsClipboardWrite;
#endif
PuglViewHintValue supportsTouch;
char* clipboardData;
struct PuglTimer* timers;
};

Expand Down
83 changes: 28 additions & 55 deletions dpf/dgl/src/pugl-upstream/src/wasm_gl.c
Expand Up @@ -2,19 +2,19 @@
// Copyright 2021-2022 Filipe Coelho <falktx@falktx.com>
// SPDX-License-Identifier: ISC

// #include "attributes.h"
#include "stub.h"
// #include "types.h"
#include "wasm.h"

// #include "pugl/gl.h"
#include "pugl/pugl.h"

#include <stdio.h>
#include <stdlib.h>

#include <EGL/egl.h>

// for performance reasons we can keep a single EGL context always active
#define PUGL_WASM_SINGLE_EGL_CONTEXT

typedef struct {
EGLDisplay display;
EGLConfig config;
Expand Down Expand Up @@ -42,55 +42,47 @@ static PuglStatus
puglWasmGlConfigure(PuglView* view)
{
PuglInternals* const impl = view->impl;
// const int screen = impl->screen;
// Display* const display = view->world->impl->display;

printf("TODO: %s %d | start\n", __func__, __LINE__);

const EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

if (display == EGL_NO_DISPLAY) {
printf("eglGetDisplay Failed\n");
return PUGL_CREATE_CONTEXT_FAILED;
}

int major, minor;
if (eglInitialize(display, &major, &minor) != EGL_TRUE) {
printf("eglInitialize Failed\n");
return PUGL_CREATE_CONTEXT_FAILED;
}

EGLConfig config;
int numConfigs;

if (eglGetConfigs(display, &config, 1, &numConfigs) != EGL_TRUE || numConfigs != 1) {
printf("eglGetConfigs Failed\n");
eglTerminate(display);
return PUGL_CREATE_CONTEXT_FAILED;
}

// clang-format off
const EGLint attrs[] = {
// GLX_X_RENDERABLE, True,
// GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
// GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
// GLX_RENDER_TYPE, GLX_RGBA_BIT,

// GLX_DOUBLEBUFFER, puglX11GlHintValue(view->hints[PUGL_DOUBLE_BUFFER]),

EGL_SAMPLES, puglWasmGlHintValue(view->hints[PUGL_SAMPLES]),
EGL_RED_SIZE, puglWasmGlHintValue(view->hints[PUGL_RED_BITS]),
EGL_GREEN_SIZE, puglWasmGlHintValue(view->hints[PUGL_GREEN_BITS]),
EGL_BLUE_SIZE, puglWasmGlHintValue(view->hints[PUGL_BLUE_BITS]),
EGL_ALPHA_SIZE, puglWasmGlHintValue(view->hints[PUGL_ALPHA_BITS]),
EGL_DEPTH_SIZE, puglWasmGlHintValue(view->hints[PUGL_DEPTH_BITS]),
EGL_STENCIL_SIZE, puglWasmGlHintValue(view->hints[PUGL_STENCIL_BITS]),
/*
GLX_X_RENDERABLE, True,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
EGL_SAMPLE_BUFFERS, view->hints[PUGL_MULTI_SAMPLE] ? 1 : 0,
*/
EGL_SAMPLES, puglWasmGlHintValue(view->hints[PUGL_SAMPLES]),
EGL_RED_SIZE, puglWasmGlHintValue(view->hints[PUGL_RED_BITS]),
EGL_GREEN_SIZE, puglWasmGlHintValue(view->hints[PUGL_GREEN_BITS]),
EGL_BLUE_SIZE, puglWasmGlHintValue(view->hints[PUGL_BLUE_BITS]),
EGL_ALPHA_SIZE, puglWasmGlHintValue(view->hints[PUGL_ALPHA_BITS]),
EGL_DEPTH_SIZE, puglWasmGlHintValue(view->hints[PUGL_DEPTH_BITS]),
EGL_STENCIL_SIZE, puglWasmGlHintValue(view->hints[PUGL_STENCIL_BITS]),
EGL_NONE
};
// clang-format on

if (eglChooseConfig(display, attrs, &config, 1, &numConfigs) != EGL_TRUE || numConfigs != 1) {
printf("eglChooseConfig Failed\n");
eglTerminate(display);
return PUGL_CREATE_CONTEXT_FAILED;
}
Expand Down Expand Up @@ -119,51 +111,48 @@ puglWasmGlConfigure(PuglView* view)
view->hints[PUGL_SAMPLES] =
puglWasmGlGetAttrib(display, config, EGL_SAMPLES);

// always enabled for EGL
// double-buffering is always enabled for EGL
view->hints[PUGL_DOUBLE_BUFFER] = 1;

printf("TODO: %s %d | ok\n", __func__, __LINE__);

return PUGL_SUCCESS;
}

PUGL_WARN_UNUSED_RESULT
static PuglStatus
puglWasmGlEnter(PuglView* view, const PuglExposeEvent* PUGL_UNUSED(expose))
{
// printf("DONE: %s %d\n", __func__, __LINE__);
PuglWasmGlSurface* const surface = (PuglWasmGlSurface*)view->impl->surface;
if (!surface || !surface->context || !surface->surface) {
return PUGL_FAILURE;
}

// TESTING: is it faster if we never unset context?
return PUGL_SUCCESS;

#ifndef PUGL_WASM_SINGLE_EGL_CONTEXT
return eglMakeCurrent(surface->display, surface->surface, surface->surface, surface->context) ? PUGL_SUCCESS : PUGL_FAILURE;
#else
return PUGL_SUCCESS;
#endif
}

PUGL_WARN_UNUSED_RESULT
static PuglStatus
puglWasmGlLeave(PuglView* view, const PuglExposeEvent* expose)
{
// printf("DONE: %s %d\n", __func__, __LINE__);
PuglWasmGlSurface* const surface = (PuglWasmGlSurface*)view->impl->surface;

if (expose) { // note: swap buffers always enabled for EGL
eglSwapBuffers(surface->display, surface->surface);
}

// TESTING: is it faster if we never unset context?
return PUGL_SUCCESS;

#ifndef PUGL_WASM_SINGLE_EGL_CONTEXT
return eglMakeCurrent(surface->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) ? PUGL_SUCCESS : PUGL_FAILURE;
#else
return PUGL_SUCCESS;
#endif
}

static PuglStatus
puglWasmGlCreate(PuglView* view)
{
printf("TODO: %s %d | start\n", __func__, __LINE__);
PuglWasmGlSurface* const surface = (PuglWasmGlSurface*)view->impl->surface;
const EGLDisplay display = surface->display;
const EGLConfig config = surface->config;
Expand Down Expand Up @@ -194,39 +183,25 @@ puglWasmGlCreate(PuglView* view)
surface->context = eglCreateContext(display, config, EGL_NO_CONTEXT, attrs);

if (surface->context == EGL_NO_CONTEXT) {
printf("eglCreateContext Failed\n");
return PUGL_CREATE_CONTEXT_FAILED;
}

#if 0
eglMakeCurrent(surface->display, surface->surface, surface->surface, surface->context);

printf("GL_VENDOR=%s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER=%s\n", glGetString(GL_RENDERER));
printf("GL_VERSION=%s\n", glGetString(GL_VERSION));

eglMakeCurrent(surface->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
#endif

surface->surface = eglCreateWindowSurface(display, config, 0, NULL);

if (surface->surface == EGL_NO_SURFACE) {
printf("eglCreateWindowSurface Failed\n");
return PUGL_CREATE_CONTEXT_FAILED;
}

printf("TODO: %s %d | ok\n", __func__, __LINE__);

// TESTING: is it faster if we never unset context?
#ifdef PUGL_WASM_SINGLE_EGL_CONTEXT
eglMakeCurrent(surface->display, surface->surface, surface->surface, surface->context);
#endif

return PUGL_SUCCESS;
}

static void
puglWasmGlDestroy(PuglView* view)
{
printf("DONE: %s %d\n", __func__, __LINE__);
PuglWasmGlSurface* surface = (PuglWasmGlSurface*)view->impl->surface;
if (surface) {
const EGLDisplay display = surface->display;
Expand All @@ -243,13 +218,11 @@ puglWasmGlDestroy(PuglView* view)
const PuglBackend*
puglGlBackend(void)
{
printf("DONE: %s %d\n", __func__, __LINE__);
static const PuglBackend backend = {puglWasmGlConfigure,
puglWasmGlCreate,
puglWasmGlDestroy,
puglWasmGlEnter,
puglWasmGlLeave,
puglStubGetContext};

return &backend;
}
19 changes: 16 additions & 3 deletions dpf/distrho/DistrhoStandaloneUtils.hpp
Expand Up @@ -25,11 +25,24 @@ START_NAMESPACE_DISTRHO
* Standalone plugin related utilities */

/**
@defgroup PluginRelatedUtilities Plugin related utilities
@defgroup StandalonePluginRelatedUtilities Plugin related utilities
When the plugin is running as standalone and JACK is not available, a native audio handling is in place.
It is a very simple handling, auto-connecting to the default audio interface for outputs.
!!EXPERIMENTAL!!
Still under development and testing.
@{
*/

/**
Check if the current standalone is using native audio methods.
If this function returns false, you MUST NOT use any other function from this group.
*/
bool isUsingNativeAudio() noexcept;

/**
Check if the current standalone supports audio input.
*/
Expand Down Expand Up @@ -58,7 +71,7 @@ bool isMIDIEnabled();
/**
Get the current buffer size.
*/
uint32_t getBufferSize();
uint getBufferSize();

/**
Request permissions to use audio input.
Expand All @@ -69,7 +82,7 @@ bool requestAudioInput();
/**
Request change to a new buffer size.
*/
bool requestBufferSizeChange(uint32_t newBufferSize);
bool requestBufferSizeChange(uint newBufferSize);

/**
Request permissions to use MIDI.
Expand Down
2 changes: 1 addition & 1 deletion dpf/distrho/src/DistrhoPluginJACK.cpp
Expand Up @@ -31,7 +31,7 @@
# include "../extra/Thread.hpp"
#endif

#if defined(STATIC_BUILD) && !defined(DISTRHO_OS_WASM)
#if defined(HAVE_JACK) && defined(STATIC_BUILD) && !defined(DISTRHO_OS_WASM)
# define JACKBRIDGE_DIRECT
#endif

Expand Down
1,287 changes: 661 additions & 626 deletions dpf/distrho/src/DistrhoPluginVST3.cpp

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions dpf/distrho/src/DistrhoUtils.cpp
Expand Up @@ -19,6 +19,7 @@
#endif

#include "../extra/String.hpp"
#include "../DistrhoStandaloneUtils.hpp"

#ifdef DISTRHO_OS_WINDOWS
# include <windows.h>
Expand Down Expand Up @@ -141,6 +142,20 @@ const char* getResourcePath(const char* const bundlePath) noexcept
return nullptr;
}

#ifndef DISTRHO_PLUGIN_TARGET_JACK
// all these are null for non-standalone targets
bool isUsingNativeAudio() noexcept { return false; }
bool supportsAudioInput() { return false; }
bool supportsBufferSizeChanges() { return false; }
bool supportsMIDI() { return false; }
bool isAudioInputEnabled() { return false; }
bool isMIDIEnabled() { return false; }
uint getBufferSize() { return 0; }
bool requestAudioInput() { return false; }
bool requestBufferSizeChange(uint) { return false; }
bool requestMIDI() { return false; }
#endif

// -----------------------------------------------------------------------

END_NAMESPACE_DISTRHO
73 changes: 43 additions & 30 deletions dpf/distrho/src/jackbridge/JackBridge.cpp
Expand Up @@ -34,7 +34,12 @@
#endif

#include <cerrno>
#include "../../extra/LibraryUtils.hpp"

#ifdef HAVE_JACK
# include "../../extra/LibraryUtils.hpp"
#else
typedef void* lib_t;
#endif

// in case JACK fails, we fallback to native bridges simulating JACK API
#include "NativeBridge.hpp"
Expand All @@ -56,10 +61,18 @@
#endif

#if defined(HAVE_RTAUDIO) && DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
// fix conflict between DGL and macOS names
# define Point CorePoint
# define Size CoreSize
# include "RtAudioBridge.hpp"
# ifdef RTAUDIO_API_TYPE
# include "rtaudio/RtAudio.cpp"
# endif
# ifdef RTMIDI_API_TYPE
# include "rtmidi/RtMidi.cpp"
# endif
# undef Point
# undef Size
#endif

#if defined(HAVE_SDL2) && DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
Expand Down Expand Up @@ -331,9 +344,9 @@ struct JackBridge {
jacksym_remove_all_properties remove_all_properties_ptr;
jacksym_set_property_change_callback set_property_change_callback_ptr;

#ifdef __WINE__
#ifdef __WINE__
jacksym_set_thread_creator set_thread_creator_ptr;
#endif
#endif

JackBridge()
: lib(nullptr),
Expand Down Expand Up @@ -429,15 +442,11 @@ struct JackBridge {
remove_properties_ptr(nullptr),
remove_all_properties_ptr(nullptr),
set_property_change_callback_ptr(nullptr)
#ifdef __WINE__
#ifdef __WINE__
, set_thread_creator_ptr(nullptr)
#endif
#endif
{
#ifdef DISTRHO_OS_WASM
// never use jack in wasm
return;
#endif

#ifdef HAVE_JACK
#if defined(DISTRHO_OS_MAC)
const char* const filename = "libjack.dylib";
#elif defined(DISTRHO_OS_WINDOWS) && defined(_WIN64)
Expand Down Expand Up @@ -578,14 +587,16 @@ struct JackBridge {
LIB_SYMBOL(remove_all_properties)
LIB_SYMBOL(set_property_change_callback)

#ifdef __WINE__
#ifdef __WINE__
LIB_SYMBOL(set_thread_creator)
#endif
#endif
#endif

#undef JOIN
#undef LIB_SYMBOL
}

#ifdef HAVE_JACK
~JackBridge() noexcept
{
USE_NAMESPACE_DISTRHO
Expand All @@ -596,6 +607,7 @@ struct JackBridge {
lib = nullptr;
}
}
#endif

DISTRHO_DECLARE_NON_COPYABLE(JackBridge);
};
Expand Down Expand Up @@ -2265,15 +2277,22 @@ bool jackbridge_set_property_change_callback(jack_client_t* client, JackProperty

START_NAMESPACE_DISTRHO

bool supportsAudioInput()
bool isUsingNativeAudio() noexcept
{
#if defined(JACKBRIDGE_DUMMY)
#if defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT)
return false;
#elif !defined(JACKBRIDGE_DIRECT)
#else
return usingNativeBridge;
#endif
}

bool supportsAudioInput()
{
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
return nativeBridge->supportsAudioInput();
#endif
return true;
return false;
}

bool supportsBufferSizeChanges()
Expand All @@ -2287,38 +2306,32 @@ bool supportsBufferSizeChanges()

bool supportsMIDI()
{
#if defined(JACKBRIDGE_DUMMY)
return false;
#elif !defined(JACKBRIDGE_DIRECT)
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
return nativeBridge->supportsMIDI();
#endif
return true;
return false;
}

bool isAudioInputEnabled()
{
#if defined(JACKBRIDGE_DUMMY)
return false;
#elif !defined(JACKBRIDGE_DIRECT)
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
return nativeBridge->isAudioInputEnabled();
#endif
return true;
return false;
}

bool isMIDIEnabled()
{
#if defined(JACKBRIDGE_DUMMY)
return false;
#elif !defined(JACKBRIDGE_DIRECT)
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
return nativeBridge->isMIDIEnabled();
#endif
return true;
return false;
}

uint32_t getBufferSize()
uint getBufferSize()
{
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
Expand All @@ -2336,7 +2349,7 @@ bool requestAudioInput()
return false;
}

bool requestBufferSizeChange(const uint32_t newBufferSize)
bool requestBufferSizeChange(const uint newBufferSize)
{
#if !(defined(JACKBRIDGE_DUMMY) || defined(JACKBRIDGE_DIRECT))
if (usingNativeBridge)
Expand Down
8 changes: 7 additions & 1 deletion dpf/distrho/src/jackbridge/NativeBridge.hpp
Expand Up @@ -205,10 +205,12 @@ struct NativeBridge {
(void)time;
}

void allocBuffers()
void allocBuffers(const bool audio, const bool midi)
{
DISTRHO_SAFE_ASSERT_RETURN(bufferSize != 0,);

if (audio)
{
#if DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS > 0
audioBufferStorage = new float[bufferSize*(DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS)];

Expand All @@ -219,14 +221,18 @@ struct NativeBridge {
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
std::memset(audioBufferStorage, 0, sizeof(float)*bufferSize*DISTRHO_PLUGIN_NUM_INPUTS);
#endif
}

if (midi)
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
midiInBufferCurrent.createBuffer(kMaxMIDIInputMessageSize * 512);
midiInBufferPending.createBuffer(kMaxMIDIInputMessageSize * 512);
#endif
#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
midiOutBuffer.createBuffer(2048);
#endif
}
}

void freeBuffers()
Expand Down
318 changes: 268 additions & 50 deletions dpf/distrho/src/jackbridge/RtAudioBridge.hpp
Expand Up @@ -26,28 +26,54 @@
#if defined(DISTRHO_OS_MAC)
# define __MACOSX_CORE__
# define RTAUDIO_API_TYPE MACOSX_CORE
# define RTMIDI_API_TYPE MACOSX_CORE
#elif defined(DISTRHO_OS_WINDOWS) && !defined(_MSC_VER)
# define __WINDOWS_DS__
# define RTAUDIO_API_TYPE WINDOWS_DS
#elif defined(HAVE_PULSEAUDIO)
# define __LINUX_PULSE__
# define RTAUDIO_API_TYPE LINUX_PULSE
#elif defined(HAVE_ALSA)
# define __LINUX_ALSA__
# define RTAUDIO_API_TYPE LINUX_ALSA
# define RTMIDI_API_TYPE WINDOWS_MM
#else
# if defined(HAVE_PULSEAUDIO)
# define __LINUX_PULSE__
# define RTAUDIO_API_TYPE LINUX_PULSE
# elif defined(HAVE_ALSA)
# define __LINUX_ALSA__
# define RTAUDIO_API_TYPE LINUX_ALSA
# endif
# ifdef HAVE_ALSA
# define RTMIDI_API_TYPE LINUX_ALSA
# endif
#endif

#ifdef RTAUDIO_API_TYPE
# define Point CorePoint /* fix conflict between DGL and macOS Point name */
# include "rtaudio/RtAudio.h"
# undef Point
# include "rtmidi/RtMidi.h"
# include "../../extra/ScopedPointer.hpp"
# include "../../extra/String.hpp"

using DISTRHO_NAMESPACE::ScopedPointer;
using DISTRHO_NAMESPACE::String;

struct RtAudioBridge : NativeBridge {
// pointer to RtAudio instance
ScopedPointer<RtAudio> handle;
bool captureEnabled = false;
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
std::vector<RtMidiIn> midiIns;
#endif
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
std::vector<RtMidiOut> midiOuts;
#endif

// caching
String name;
uint nextBufferSize = 512;

RtAudioBridge()
{
#if defined(RTMIDI_API_TYPE) && (DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT)
midiAvailable = true;
#endif
}

const char* getVersion() const noexcept
{
Expand All @@ -56,48 +82,8 @@ struct RtAudioBridge : NativeBridge {

bool open(const char* const clientName) override
{
ScopedPointer<RtAudio> rtAudio;

try {
rtAudio = new RtAudio(RtAudio::RTAUDIO_API_TYPE);
} DISTRHO_SAFE_EXCEPTION_RETURN("new RtAudio()", false);

uint rtAudioBufferFrames = 512;

#if DISTRHO_PLUGIN_NUM_INPUTS > 0
RtAudio::StreamParameters inParams;
inParams.deviceId = rtAudio->getDefaultInputDevice();
inParams.nChannels = DISTRHO_PLUGIN_NUM_INPUTS;
RtAudio::StreamParameters* const inParamsPtr = &inParams;
#else
RtAudio::StreamParameters* const inParamsPtr = nullptr;
#endif

#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
RtAudio::StreamParameters outParams;
outParams.deviceId = rtAudio->getDefaultOutputDevice();
outParams.nChannels = DISTRHO_PLUGIN_NUM_OUTPUTS;
RtAudio::StreamParameters* const outParamsPtr = &outParams;
#else
RtAudio::StreamParameters* const outParamsPtr = nullptr;
#endif

RtAudio::StreamOptions opts;
opts.flags = RTAUDIO_NONINTERLEAVED | RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_ALSA_USE_DEFAULT;
opts.streamName = clientName;

try {
rtAudio->openStream(outParamsPtr, inParamsPtr, RTAUDIO_FLOAT32, 48000, &rtAudioBufferFrames,
RtAudioCallback, this, &opts, nullptr);
} catch (const RtAudioError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("rtAudio->openStream()", false);

handle = rtAudio;
bufferSize = rtAudioBufferFrames;
sampleRate = handle->getStreamSampleRate();
return true;
name = clientName;
return _open(false);
}

bool close() override
Expand All @@ -111,6 +97,9 @@ struct RtAudioBridge : NativeBridge {
} DISTRHO_SAFE_EXCEPTION("handle->abortStream()");
}

#if DISTRHO_PLUGIN_NUM_INPUTS > 0
freeBuffers();
#endif
handle = nullptr;
return true;
}
Expand All @@ -137,6 +126,218 @@ struct RtAudioBridge : NativeBridge {
return true;
}

bool isAudioInputEnabled() const override
{
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
return captureEnabled;
#else
return false;
#endif
}

bool requestAudioInput() override
{
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
// stop audio first
deactivate();
close();

// try to open with capture enabled
const bool ok = _open(true);

if (ok)
captureEnabled = true;
else
_open(false);

activate();
return ok;
#else
return false;
#endif
}

bool isMIDIEnabled() const override
{
d_stdout("%s %d", __PRETTY_FUNCTION__, __LINE__);
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
if (!midiIns.empty())
return true;
#endif
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
if (!midiOuts.empty())
return true;
#endif
return false;
}

bool requestMIDI() override
{
d_stdout("%s %d", __PRETTY_FUNCTION__, __LINE__);
// clear ports in use first
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
if (!midiIns.empty())
{
try {
midiIns.clear();
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("midiIns.clear()", false);
}
#endif
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
if (!midiOuts.size())
{
try {
midiOuts.clear();
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("midiOuts.clear()", false);
}
#endif

// query port count
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
uint midiInCount;
try {
RtMidiIn midiIn(RtMidi::RTMIDI_API_TYPE, name.buffer());
midiInCount = midiIn.getPortCount();
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("midiIn.getPortCount()", false);
#endif
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
uint midiOutCount;
try {
RtMidiOut midiOut(RtMidi::RTMIDI_API_TYPE, name.buffer());
midiOutCount = midiOut.getPortCount();
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("midiOut.getPortCount()", false);
#endif

// open all possible ports
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
for (uint i=0; i<midiInCount; ++i)
{
try {
RtMidiIn midiIn(RtMidi::RTMIDI_API_TYPE, name.buffer());
midiIn.setCallback(RtMidiCallback, this);
midiIn.openPort(i);
midiIns.push_back(std::move(midiIn));
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
} DISTRHO_SAFE_EXCEPTION("midiIn.openPort()");
}
#endif
#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
for (uint i=0; i<midiOutCount; ++i)
{
try {
RtMidiOut midiOut(RtMidi::RTMIDI_API_TYPE, name.buffer());
midiOut.openPort(i);
midiOuts.push_back(std::move(midiOut));
} catch (const RtMidiError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
} DISTRHO_SAFE_EXCEPTION("midiOut.openPort()");
}
#endif

return true;
}

/* RtAudio in macOS uses a different than usual way to handle audio block size,
* where RTAUDIO_MINIMIZE_LATENCY makes CoreAudio use very low latencies (around 15 samples).
* As such, dynamic buffer sizes are meaningless there.
*/
#ifndef DISTRHO_OS_MAC
bool supportsBufferSizeChanges() const override
{
return true;
}

bool requestBufferSizeChange(const uint32_t newBufferSize) override
{
// stop audio first
deactivate();
close();

// try to open with new buffer size
nextBufferSize = newBufferSize;

const bool ok = _open(captureEnabled);

if (!ok)
{
// revert to old buffer size if new one failed
nextBufferSize = bufferSize;
_open(captureEnabled);
}

if (bufferSizeCallback != nullptr)
bufferSizeCallback(bufferSize, jackBufferSizeArg);

activate();
return ok;
}
#endif

bool _open(const bool withInput)
{
ScopedPointer<RtAudio> rtAudio;

try {
rtAudio = new RtAudio(RtAudio::RTAUDIO_API_TYPE);
} DISTRHO_SAFE_EXCEPTION_RETURN("new RtAudio()", false);

uint rtAudioBufferFrames = nextBufferSize;

#if DISTRHO_PLUGIN_NUM_INPUTS > 0
RtAudio::StreamParameters inParams;
#endif
RtAudio::StreamParameters* inParamsPtr = nullptr;

#if DISTRHO_PLUGIN_NUM_INPUTS > 0
if (withInput)
{
inParams.deviceId = rtAudio->getDefaultInputDevice();
inParams.nChannels = DISTRHO_PLUGIN_NUM_INPUTS;
inParamsPtr = &inParams;
}
#endif

#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
RtAudio::StreamParameters outParams;
outParams.deviceId = rtAudio->getDefaultOutputDevice();
outParams.nChannels = DISTRHO_PLUGIN_NUM_OUTPUTS;
RtAudio::StreamParameters* const outParamsPtr = &outParams;
#else
RtAudio::StreamParameters* const outParamsPtr = nullptr;
#endif

RtAudio::StreamOptions opts;
opts.flags = RTAUDIO_NONINTERLEAVED | RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_ALSA_USE_DEFAULT;
opts.streamName = name.buffer();

try {
rtAudio->openStream(outParamsPtr, inParamsPtr, RTAUDIO_FLOAT32, 48000, &rtAudioBufferFrames,
RtAudioCallback, this, &opts, nullptr);
} catch (const RtAudioError& err) {
d_safe_exception(err.getMessage().c_str(), __FILE__, __LINE__);
return false;
} DISTRHO_SAFE_EXCEPTION_RETURN("rtAudio->openStream()", false);

handle = rtAudio;
bufferSize = rtAudioBufferFrames;
sampleRate = handle->getStreamSampleRate();
allocBuffers(!withInput, true);
return true;
}

static int RtAudioCallback(void* const outputBuffer,
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
void* const inputBuffer,
Expand Down Expand Up @@ -177,6 +378,23 @@ struct RtAudioBridge : NativeBridge {

return 0;
}

#if defined(RTMIDI_API_TYPE) && DISTRHO_PLUGIN_WANT_MIDI_INPUT
static void RtMidiCallback(double /*timeStamp*/, std::vector<uchar>* message, void* userData)
{
const size_t len = message->size();
DISTRHO_SAFE_ASSERT_RETURN(len > 0 && len <= kMaxMIDIInputMessageSize,);

RtAudioBridge* const self = static_cast<RtAudioBridge*>(userData);

// TODO timestamp handling
self->midiInBufferPending.writeByte(static_cast<uint8_t>(len));
self->midiInBufferPending.writeCustomData(message->data(), len);
for (uint8_t i=0; i<len; ++i)
self->midiInBufferPending.writeByte(0);
self->midiInBufferPending.commitWrite();
}
#endif
};

#endif // RTAUDIO_API_TYPE
Expand Down
10 changes: 9 additions & 1 deletion dpf/distrho/src/jackbridge/SDL2Bridge.hpp
Expand Up @@ -21,6 +21,14 @@

#include <SDL.h>

#ifndef SDL_HINT_AUDIO_DEVICE_APP_NAME
# define SDL_HINT_AUDIO_DEVICE_APP_NAME "SDL_AUDIO_DEVICE_APP_NAME"
#endif

#ifndef SDL_HINT_AUDIO_DEVICE_STREAM_NAME
# define SDL_HINT_AUDIO_DEVICE_STREAM_NAME "SDL_AUDIO_DEVICE_STREAM_NAME"
#endif

#if DISTRHO_PLUGIN_NUM_INPUTS+DISTRHO_PLUGIN_NUM_OUTPUTS == 0
# error SDL without audio does not make sense
#endif
Expand Down Expand Up @@ -131,7 +139,7 @@ struct SDL2Bridge : NativeBridge {
sampleRate = receivedPlayback.freq;
#endif

allocBuffers();
allocBuffers(true, false);
return true;
}

Expand Down
43 changes: 25 additions & 18 deletions dpf/distrho/src/jackbridge/WebBridge.hpp
Expand Up @@ -52,7 +52,7 @@ struct WebBridge : NativeBridge {
return 0;
}) != 0;
#endif

#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
midiAvailable = EM_ASM_INT({
return typeof(navigator.requestMIDIAccess) === 'function' ? 1 : 0;
Expand Down Expand Up @@ -111,37 +111,45 @@ struct WebBridge : NativeBridge {
return false;
}

bufferSize = 2048;
bufferSize = EM_ASM_INT({
var WAB = Module['WebAudioBridge'];
return WAB['minimizeBufferSize'] ? 256 : 2048;
});
sampleRate = EM_ASM_INT_V({
var WAB = Module['WebAudioBridge'];
return WAB.audioContext.sampleRate;
});

allocBuffers();
allocBuffers(true, true);

EM_ASM({
var numInputs = $0;
var numOutputs = $1;
var bufferSize = $2;
var WAB = Module['WebAudioBridge'];

var realBufferSize = WAB['minimizeBufferSize'] ? 2048 : bufferSize;
var divider = realBufferSize / bufferSize;

// main processor
WAB.processor = WAB.audioContext['createScriptProcessor'](bufferSize, numInputs, numOutputs);
WAB.processor = WAB.audioContext['createScriptProcessor'](realBufferSize, numInputs, numOutputs);
WAB.processor['onaudioprocess'] = function (e) {
// var timestamp = performance.now();
for (var i = 0; i < numInputs; ++i) {
var buffer = e['inputBuffer']['getChannelData'](i);
for (var j = 0; j < bufferSize; ++j) {
// setValue($3 + ((bufferSize * i) + j) * 4, buffer[j], 'float');
HEAPF32[$3 + (((bufferSize * i) + j) << 2) >> 2] = buffer[j];
for (var k = 0; k < divider; ++k) {
for (var i = 0; i < numInputs; ++i) {
var buffer = e['inputBuffer']['getChannelData'](i);
for (var j = 0; j < bufferSize; ++j) {
// setValue($3 + ((bufferSize * i) + j) * 4, buffer[j], 'float');
HEAPF32[$3 + (((bufferSize * i) + j) << 2) >> 2] = buffer[bufferSize * k + j];
}
}
}
dynCall('vi', $4, [$5]);
for (var i = 0; i < numOutputs; ++i) {
var buffer = e['outputBuffer']['getChannelData'](i);
var offset = bufferSize * (numInputs + i);
for (var j = 0; j < bufferSize; ++j) {
buffer[j] = HEAPF32[$3 + ((offset + j) << 2) >> 2];
dynCall('vi', $4, [$5]);
for (var i = 0; i < numOutputs; ++i) {
var buffer = e['outputBuffer']['getChannelData'](i);
var offset = bufferSize * (numInputs + i);
for (var j = 0; j < bufferSize; ++j) {
buffer[bufferSize * k + j] = HEAPF32[$3 + ((offset + j) << 2) >> 2];
}
}
}
};
Expand All @@ -152,7 +160,6 @@ struct WebBridge : NativeBridge {
// resume/start playback on first click
document.addEventListener('click', function(e) {
var WAB = Module['WebAudioBridge'];
console.log(WAB.audioContext.state);
if (WAB.audioContext.state === 'suspended')
WAB.audioContext.resume();
});
Expand Down Expand Up @@ -279,7 +286,7 @@ struct WebBridge : NativeBridge {

bufferSize = newBufferSize;
freeBuffers();
allocBuffers();
allocBuffers(true, true);

if (bufferSizeCallback != nullptr)
bufferSizeCallback(newBufferSize, jackBufferSizeArg);
Expand Down
27 changes: 27 additions & 0 deletions dpf/distrho/src/jackbridge/rtmidi/LICENSE
@@ -0,0 +1,27 @@

RtMidi: realtime MIDI i/o C++ classes
Copyright (c) 2003-2021 Gary P. Scavone

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

Any person wishing to distribute modifications to the Software is
asked to send the modifications to the original developer so that
they can be incorporated into the canonical version. This is,
however, not a binding provision of this license.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3,930 changes: 3,930 additions & 0 deletions dpf/distrho/src/jackbridge/rtmidi/RtMidi.cpp

Large diffs are not rendered by default.

658 changes: 658 additions & 0 deletions dpf/distrho/src/jackbridge/rtmidi/RtMidi.h

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion dpf/distrho/src/lv2/lv2.h
Expand Up @@ -355,7 +355,9 @@ typedef struct _LV2_Descriptor {
Put this (LV2_SYMBOL_EXPORT) before any functions that are to be loaded
by the host as a symbol from the dynamic library.
*/
#ifdef _WIN32
#if defined(__EMSCRIPTEN__)
# define LV2_SYMBOL_EXPORT LV2_SYMBOL_EXTERN __attribute__((used))
#elif defined(_WIN32)
# define LV2_SYMBOL_EXPORT LV2_SYMBOL_EXTERN __declspec(dllexport)
#else
# define LV2_SYMBOL_EXPORT LV2_SYMBOL_EXTERN __attribute__((visibility("default")))
Expand Down
20 changes: 20 additions & 0 deletions dpf/utils/plugin.app/Contents/Info.plist
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>@INFO_PLIST_PROJECT_NAME@</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>studio.kx.distrho.@INFO_PLIST_PROJECT_NAME@</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSRequiresAquaSystemAppearance</key>
<false/>
<key>NSMicrophoneUsageDescription</key>
<string>@INFO_PLIST_PROJECT_NAME@ requires microphone permissions for audio input.</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion dpf/utils/plugin.vst/Contents/Info.plist
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
Expand Down