Skip to content

Commit c7ff05b

Browse files
committed
cleanup and architecture
1 parent b32f3b8 commit c7ff05b

File tree

9 files changed

+275
-50
lines changed

9 files changed

+275
-50
lines changed

.github/workflows/cmake-single-platform.yml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defaults:
2121
shell: bash
2222

2323
jobs:
24-
build-vst:
24+
build-juce:
2525
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
2626
# You can convert this to a matrix build if you need cross-platform coverage.
2727
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
@@ -59,6 +59,40 @@ jobs:
5959
# Build your program with the given configuration
6060
run: cmake --build "${{github.workspace}}/build" --config ${{env.BUILD_TYPE}}
6161

62+
build-clap:
63+
name: clap ${{ matrix.name }}
64+
runs-on: ${{ matrix.os }}
65+
strategy:
66+
fail-fast: false # show all errors for each platform (vs. cancel jobs on error)
67+
matrix:
68+
include:
69+
- name: Linux
70+
os: ubuntu-22.04
71+
# - name: macOS
72+
# os: macos-14
73+
# extra-flags: -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"
74+
- name: Windows
75+
os: windows-latest
76+
77+
steps:
78+
- name: Install JUCE's Linux Deps
79+
if: runner.os == 'Linux'
80+
run: |
81+
sudo apt-get update && sudo apt install ninja-build
82+
83+
- uses: actions/checkout@v4
84+
with:
85+
submodules: recursive
86+
87+
- name: Configure CMake
88+
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
89+
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
90+
run: cmake -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} "${{github.workspace}}/clap" ${{matrix.extra-flags}}
91+
92+
- name: Build
93+
# Build your program with the given configuration
94+
run: cmake --build "${{github.workspace}}/build" --config ${{env.BUILD_TYPE}}
95+
6296
#modify-plugin-version:
6397
# name: Modify plugin version
6498
# runs-on: ubuntu-latest

clap/template/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
66

77
add_library(${PROJECT_NAME} MODULE
88
src/plugin.cpp
9+
src/PluginHost.cpp
910
src/gui.cpp
1011
src/audio.cpp
1112
)

clap/template/include/PluginHost.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <functional>
5+
6+
#include "clap/id.h"
7+
8+
// forward declarations
9+
typedef struct clap_host clap_host_t;
10+
typedef struct clap_host_log clap_host_log_t;
11+
typedef struct clap_host_timer_support clap_host_timer_support_t;
12+
typedef struct clap_host_gui clap_host_gui_t;
13+
14+
enum class LogSeverity {
15+
Debug = 0,
16+
Info = 1,
17+
Warning = 2,
18+
Error = 3,
19+
Fatal = 4,
20+
};
21+
22+
/**
23+
* PluginHost wraps over the capabilities and methods that the plugin host
24+
* (aka, the DAW) provide to us. this can be held onto for the lifetime of the
25+
* plugin safely, but only call it from the main thread.
26+
*/
27+
class PluginHost {
28+
public:
29+
using TimerId = clap_id;
30+
using TimerFn = std::function<void()>;
31+
32+
PluginHost(const clap_host_t* host);
33+
34+
/** Add a message to the host's log */
35+
void Log(LogSeverity severity, const std::string& message) const;
36+
37+
/** Add a timer to be run every `periodMs` milliseconds. to run only once, call `CancelTimer()` in the body of your timer function. */
38+
TimerId AddTimer(uint32_t periodMs, TimerFn timerFn);
39+
/** Cancels an active timer with the provided id. this is safe to call from inside of timers. */
40+
void CancelTimer(TimerId id);
41+
/** implementation. TODO: friend declaration to hide this? */
42+
void OnTimer(TimerId id) const;
43+
44+
private:
45+
const clap_host_t* mHost;
46+
const clap_host_log_t* mLog;
47+
const clap_host_timer_support_t* mTimer;
48+
const clap_host_gui_t* mGui;
49+
std::unordered_map<TimerId, TimerFn> mActiveTimers;
50+
};

clap/template/include/audio.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace Audio {
6+
void Render(uint32_t start, uint32_t end, float *outputL, float *outputR);
7+
}

clap/template/include/gui.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <cstdint>
66
#include <memory>
77

8+
// Forward declares
9+
class PluginHost;
10+
811
// from reddit
912
template<typename T>
1013
struct c_deleter;
@@ -18,10 +21,16 @@ struct c_deleter<SDL_Window> final {
1821
}
1922
};
2023

24+
/**
25+
* Gui is the main abstraction for windowing and graphics.
26+
* Right now we use SDL_Video to manage the window on all platforms, OpenGL for rendering, and imgui as the UI toolkit.
27+
* Future version may make this an abstract class that can have multiple different implementations for different toolkits.
28+
*/
2129
class Gui {
2230
// constants
2331
public:
2432
enum class WindowingApi {
33+
None = 0,
2534
X11,
2635
Wayland,
2736
Win32,
@@ -34,13 +43,19 @@ class Gui {
3443
uint32_t aspectRatioWidth;
3544
uint32_t aspectRatioHeight;
3645
};
46+
static constexpr char kDefaultWindowTitle[] = "Template";
47+
static constexpr int32_t kDefaultWindowWidth = 400;
48+
static constexpr int32_t kDefaultWindowHeight = 400;
3749

3850
// API methods
3951
public:
4052
static bool IsApiSupported(WindowingApi api, bool isFloating);
4153
static bool GetPreferredApi(WindowingApi& outApi, bool& outIsFloating);
4254

43-
Gui(WindowingApi api, bool isFloating); // Create
55+
static bool OnAppInit();
56+
static bool OnAppQuit();
57+
58+
Gui(PluginHost* host, WindowingApi api, bool isFloating); // Create
4459
~Gui(); // Destroy
4560

4661
bool SetScale(double scale);
@@ -51,7 +66,8 @@ class Gui {
5166
bool SetSize(uint32_t width, uint32_t height);
5267
bool SetParent(WindowingApi api, void* windowPointer);
5368
bool SetTransient(WindowingApi api, void* windowPointer);
54-
void SuggestTitle(std::string_view title);
69+
// _must_ be c-string
70+
void SuggestTitle(const char* title);
5571
bool Show();
5672
bool Hide();
5773
void Update(float dt);
@@ -60,7 +76,10 @@ class Gui {
6076
private:
6177
c_unique_ptr<SDL_Window> mWindowHandle;
6278
c_unique_ptr<SDL_Window> mParentHandle;
79+
PluginHost* mHost;
80+
SDL_GLContext mCtx;
6381

6482
void CreateWindow();
6583
void CreateParentWindow(WindowingApi api, void* windowPointer);
84+
void OnGui();
6685
};

clap/template/src/PluginHost.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "PluginHost.h"
2+
3+
#include "clap/clap.h"
4+
5+
PluginHost::PluginHost(const clap_host_t* host):
6+
mHost(host),
7+
mLog(static_cast<const clap_host_log_t *>(host->get_extension(host, CLAP_EXT_LOG))),
8+
mTimer(static_cast<const clap_host_timer_support_t *>(host->get_extension(host, CLAP_EXT_TIMER_SUPPORT))),
9+
mGui(static_cast<const clap_host_gui_t *>(host->get_extension(host, CLAP_EXT_GUI)))
10+
{
11+
}
12+
13+
void PluginHost::Log(LogSeverity severity, const std::string& message) const
14+
{
15+
return mLog->log(mHost, static_cast<clap_log_severity>(severity), message.c_str());
16+
}
17+
18+
PluginHost::TimerId PluginHost::AddTimer(uint32_t periodMs, PluginHost::TimerFn fn)
19+
{
20+
TimerId id;
21+
mTimer->register_timer(mHost, periodMs, &id);
22+
mActiveTimers.emplace(id, fn);
23+
return id;
24+
}
25+
26+
void PluginHost::OnTimer(PluginHost::TimerId id) const
27+
{
28+
mActiveTimers.at(id) ();
29+
}
30+
31+
void PluginHost::CancelTimer(PluginHost::TimerId id)
32+
{
33+
mActiveTimers.erase(id);
34+
mTimer->unregister_timer(mHost, id);
35+
}

clap/template/src/audio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#include "audio.h"
22

33
namespace Audio {
44
void Render(uint32_t start, uint32_t end, float *outputL, float *outputR) {

0 commit comments

Comments
 (0)