-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdnam~.cpp
More file actions
111 lines (88 loc) · 2.67 KB
/
pdnam~.cpp
File metadata and controls
111 lines (88 loc) · 2.67 KB
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
// PDNAM
// Copyright (C) 2026 Wasted Audio
//
// SPDX-License-Identifier: MIT
#include "m_pd.h"
#include "g_canvas.h"
#include "NeuralAudio/NeuralModel.h"
#include <iostream>
#include <string>
#include <filesystem>
static t_class *pdnam_tilde_class;
typedef struct _pdnam_tilde {
t_object x_obj;
t_sample f;
NeuralAudio::NeuralModel* model;
t_symbol* model_path;
} t_pdnam_tilde;
static void pdnam_tilde_load(t_pdnam_tilde *x)
{
if (x->model) {
delete x->model;
x->model = nullptr;
}
if (x->model_path == &s_) return;
// Resolve relative path
char buf[MAXPDSTRING];
canvas_makefilename(canvas_getcurrent(), x->model_path->s_name, buf, MAXPDSTRING);
std::filesystem::path path(buf);
if (!std::filesystem::exists(path) || !std::filesystem::is_regular_file(path)) {
pd_error(x, "pdnam~: model file not found: %s", buf);
return;
}
// Set load mode
NeuralAudio::EModelLoadMode mode = NeuralAudio::EModelLoadMode::Internal;
NeuralAudio::NeuralModel::SetWaveNetLoadMode(mode);
NeuralAudio::NeuralModel::SetLSTMLoadMode(mode);
x->model = NeuralAudio::NeuralModel::CreateFromFile(path);
if (x->model) {
post("pdnam~: loaded model %s", x->model_path->s_name);
} else {
pd_error(x, "pdnam~: failed to load model %s", x->model_path->s_name);
}
}
void *pdnam_tilde_new(t_symbol *s, int argc, t_atom *argv)
{
t_pdnam_tilde *x = (t_pdnam_tilde *)pd_new(pdnam_tilde_class);
x->model = nullptr;
x->model_path = &s_;
if (argc >= 1 && argv[0].a_type == A_SYMBOL) {
x->model_path = atom_getsymbol(&argv[0]);
}
pdnam_tilde_load(x);
outlet_new(&x->x_obj, &s_signal);
return (void *)x;
}
t_int *pdnam_tilde_perform(t_int *w)
{
t_pdnam_tilde *x = (t_pdnam_tilde *)(w[1]);
t_sample *in = (t_sample *)(w[2]);
t_sample *out = (t_sample *)(w[3]);
int n = (int)(w[4]);
if (x->model) {
x->model->Process(in, out, n);
} else {
while (n--) {
*out++ = *in++;
}
}
return (w+5);
}
void pdnam_tilde_dsp(t_pdnam_tilde *x, t_signal **sp)
{
dsp_add(pdnam_tilde_perform, 4, x,
sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}
void pdnam_tilde_free(t_pdnam_tilde *x)
{
if (x->model) delete x->model;
}
extern "C" void pdnam_tilde_setup(void) {
pdnam_tilde_class = class_new(gensym("pdnam~"),
(t_newmethod)pdnam_tilde_new,
(t_method)pdnam_tilde_free, sizeof(t_pdnam_tilde),
CLASS_DEFAULT, A_GIMME, 0);
class_addmethod(pdnam_tilde_class,
(t_method)pdnam_tilde_dsp, gensym("dsp"), A_CANT, 0);
CLASS_MAINSIGNALIN(pdnam_tilde_class, t_pdnam_tilde, f);
}