|
| 1 | +// PDNAM |
| 2 | +// Copyright (C) 2026 Wasted Audio |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +#include "m_pd.h" |
| 7 | +#include "g_canvas.h" |
| 8 | +#include "NeuralAudio/NeuralModel.h" |
| 9 | +#include <iostream> |
| 10 | +#include <string> |
| 11 | +#include <filesystem> |
| 12 | + |
| 13 | +static t_class *pdnam_plus_tilde_class; |
| 14 | + |
| 15 | +typedef struct _pdnam_plus_tilde { |
| 16 | + t_object x_obj; |
| 17 | + t_sample f; |
| 18 | + t_canvas* canvas; |
| 19 | + NeuralAudio::NeuralModel* model; |
| 20 | +} t_pdnam_plus_tilde; |
| 21 | + |
| 22 | +static void pdnam_plus_tilde_load(t_pdnam_plus_tilde *x, t_symbol *model_path) |
| 23 | +{ |
| 24 | + if (x->model) { |
| 25 | + delete x->model; |
| 26 | + x->model = nullptr; |
| 27 | + } |
| 28 | + |
| 29 | + if (model_path == &s_) return; |
| 30 | + |
| 31 | + // Resolve relative path |
| 32 | + char buf[MAXPDSTRING]; |
| 33 | + canvas_makefilename(x->canvas, model_path->s_name, buf, MAXPDSTRING); |
| 34 | + std::filesystem::path path(buf); |
| 35 | + |
| 36 | + if (!std::filesystem::exists(path) || !std::filesystem::is_regular_file(path)) { |
| 37 | + pd_error(x, "pdnam+~: model file not found: %s", buf); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + x->model = NeuralAudio::NeuralModel::CreateFromFile(path); |
| 43 | + if (x->model) { |
| 44 | + post("pdnam+~: loaded model %s", model_path->s_name); |
| 45 | + } else { |
| 46 | + pd_error(x, "pdnam+~: failed to load model %s", model_path->s_name); |
| 47 | + } |
| 48 | + } catch (const std::exception& e) { |
| 49 | + pd_error(x, "pdnam+~: exception loading model: %s", e.what()); |
| 50 | + } catch (...) { |
| 51 | + pd_error(x, "pdnam+~: unknown exception loading model"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void *pdnam_plus_tilde_new(t_symbol *s, int argc, t_atom *argv) |
| 56 | +{ |
| 57 | + t_pdnam_plus_tilde *x = (t_pdnam_plus_tilde *)pd_new(pdnam_plus_tilde_class); |
| 58 | + x->canvas = canvas_getcurrent(); |
| 59 | + x->model = nullptr; |
| 60 | + |
| 61 | + t_symbol* model_path = (argc >= 1 && argv[0].a_type == A_SYMBOL) |
| 62 | + ? atom_getsymbol(&argv[0]) |
| 63 | + : &s_; |
| 64 | + |
| 65 | + pdnam_plus_tilde_load(x, model_path); |
| 66 | + |
| 67 | + outlet_new(&x->x_obj, &s_signal); |
| 68 | + |
| 69 | + return (void *)x; |
| 70 | +} |
| 71 | + |
| 72 | +t_int *pdnam_plus_tilde_perform(t_int *w) |
| 73 | +{ |
| 74 | + t_pdnam_plus_tilde *x = (t_pdnam_plus_tilde *)(w[1]); |
| 75 | + t_sample *in = (t_sample *)(w[2]); |
| 76 | + t_sample *out = (t_sample *)(w[3]); |
| 77 | + int n = (int)(w[4]); |
| 78 | + |
| 79 | + if (x->model) { |
| 80 | + x->model->Process(in, out, n); |
| 81 | + } else { |
| 82 | + while (n--) { |
| 83 | + *out++ = *in++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return (w+5); |
| 88 | +} |
| 89 | + |
| 90 | +void pdnam_plus_tilde_dsp(t_pdnam_plus_tilde *x, t_signal **sp) |
| 91 | +{ |
| 92 | + dsp_add(pdnam_plus_tilde_perform, 4, x, |
| 93 | + sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); |
| 94 | +} |
| 95 | + |
| 96 | +void pdnam_plus_tilde_free(t_pdnam_plus_tilde *x) |
| 97 | +{ |
| 98 | + if (x->model) delete x->model; |
| 99 | +} |
| 100 | + |
| 101 | +extern "C" void setup_pdnam0x2b_tilde(void) { |
| 102 | + pdnam_plus_tilde_class = class_new(gensym("pdnam+~"), |
| 103 | + (t_newmethod)pdnam_plus_tilde_new, |
| 104 | + (t_method)pdnam_plus_tilde_free, sizeof(t_pdnam_plus_tilde), |
| 105 | + CLASS_DEFAULT, A_GIMME, 0); |
| 106 | + |
| 107 | + class_addmethod(pdnam_plus_tilde_class, |
| 108 | + (t_method)pdnam_plus_tilde_dsp, gensym("dsp"), A_CANT, 0); |
| 109 | + CLASS_MAINSIGNALIN(pdnam_plus_tilde_class, t_pdnam_plus_tilde, f); |
| 110 | +} |
0 commit comments