-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.mm
113 lines (98 loc) · 3.41 KB
/
blocks.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// blocks.cpp
// Blocks
//
// Created by Urban Lienert on 02.02.20.
// Copyright © 2020 Urban Lienert. All rights reserved.
//
#include "m_pd.h"
#include "BlockFinder.hpp"
#include <BlocksHeader.h>
#include "JuceThread.hpp"
// Pure Data 'class' declaration
static t_class *blocks_class = NULL;
// workaround for juce message thread on macOS and pure data, because we are not on the main thread
#if JUCE_MAC
CF_EXPORT CFRunLoopRef CFRunLoopGetMain(void) { return CFRunLoopGetCurrent(); };
#endif
// struct definition for blocks class
typedef struct {
t_object x_obj;
t_outlet *out_A;
t_outlet *out_B;
t_outlet *out_C;
t_outlet *out_D;
std::unique_ptr<JuceThread> juceThread;
} t_blocks;
// function declarations
static void *blocks_new(t_symbol *s, int argc, t_atom *argv);
void blocks_free(t_blocks *x);
extern "C" void blocks_setup(void);
static void blocks_setname(t_blocks *x, t_symbol *serial, t_symbol *name);
static void blocks_command(t_blocks *x, t_symbol *s, int argc, t_atom *argv);
static void blocks_bang(t_blocks *x);
static void *blocks_new(t_symbol *s, int argc, t_atom *argv)
{
t_blocks *x = (t_blocks *)pd_new(blocks_class);
x->out_A = outlet_new(&x->x_obj, &s_anything);
x->out_B = outlet_new(&x->x_obj, &s_anything);
x->out_C = outlet_new(&x->x_obj, &s_anything);
x->out_D = outlet_new(&x->x_obj, &s_bang);
bool loadProgram = true;
if (argc>0) {
t_atom arg1 = argv[0];
if (arg1.a_type==A_SYMBOL) {
if (arg1.a_w.w_symbol==gensym("noload")) {
loadProgram = false;
}
}
}
x->juceThread = {std::make_unique<JuceThread>(juce::String("blockThread"), x->out_A, x->out_B, x->out_C, x->out_D, loadProgram)};
x->juceThread->startThread();
return (x);
}
void blocks_free(t_blocks *x) {
x->juceThread->stopThread(1000);
outlet_free(x->out_A);
outlet_free(x->out_B);
outlet_free(x->out_C);
outlet_free(x->out_D);
x->juceThread = nullptr;
}
extern "C" void blocks_setup(void)
{
blocks_class = class_new(gensym("blocks"), (t_newmethod)blocks_new,
(t_method)blocks_free, sizeof(t_blocks), CLASS_DEFAULT, A_GIMME, A_NULL);
class_addmethod(blocks_class, (t_method)blocks_setname, gensym("setname"), A_DEFSYMBOL, A_DEFSYMBOL, 0);
class_addanything(blocks_class, (t_method)blocks_command);
class_addbang(blocks_class, (t_method)blocks_bang);
}
static void blocks_setname(t_blocks *x, t_symbol *serial, t_symbol *name) {
while (!x->juceThread->blockReady) {
// make sure BlockFinder is ready, when using loadbang in pd
juce::Thread::getCurrentThread()->sleep(10);
}
if (x->juceThread->mBlockFinder!=nullptr) {
x->juceThread->mBlockFinder->setPdNameForSerial(serial->s_name, name->s_name);
}
}
static void blocks_command(t_blocks *x, t_symbol *s, int argc, t_atom *argv) {
if (argc>0) {
while (!x->juceThread->blockReady) {
juce::Thread::getCurrentThread()->sleep(10);
}
if (x->juceThread->mBlockFinder!=nullptr) {
x->juceThread->mBlockFinder->doBlockCommand(s, argc, argv);
}
} else {
error("too few arguments");
}
}
static void blocks_bang(t_blocks *x) {
while (!x->juceThread->blockReady) {
juce::Thread::getCurrentThread()->sleep(10);
}
if (x->juceThread->mBlockFinder!=nullptr) {
x->juceThread->mBlockFinder->pollInfos();
}
}