From a5d4132192ff4ca582ab3f3925fcdf0f79805e81 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Sat, 17 Aug 2024 21:21:13 -0400 Subject: [PATCH] Ability to load a wavetable onto a patch in python Addresses #7763 --- src/surge-python/surgepy.cpp | 24 +++++++++++++++++++++++ src/surge-python/tests/write_wavetable.py | 19 ++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/surge-python/tests/write_wavetable.py diff --git a/src/surge-python/surgepy.cpp b/src/surge-python/surgepy.cpp index 7f440f8ce73..ea7ae6fe72e 100644 --- a/src/surge-python/surgepy.cpp +++ b/src/surge-python/surgepy.cpp @@ -617,6 +617,25 @@ class SurgeSynthesizerWithPythonExtensions : public SurgeSynthesizer void releaseNoteWithInts(int ch, int note, int vel) { releaseNote(ch, note, vel); } + bool loadWavetablePy(int scene, int osc, const std::string &s) + { + auto path = string_to_path(s); + + if (scene < 0 || scene >= n_scenes || osc < 0 || osc >= n_oscs) + { + throw std::invalid_argument("OSC and SCENE out of range in loadWavetable"); + } + if (!fs::exists(path)) + { + throw std::invalid_argument((std::string("File not found: ") + s).c_str()); + } + std::cout << "Would load " << scene << " " << osc << " with " << s << std::endl; + auto os = &(storage.getPatch().scene[scene].osc[osc]); + auto wt = &(os->wt); + storage.load_wt(s, wt, os); + + return true; + } bool loadPatchPy(const std::string &s) { auto path = string_to_path(s); @@ -1111,6 +1130,11 @@ PYBIND11_MODULE(surgepy, m) .def("savePatch", &SurgeSynthesizerWithPythonExtensions::savePatchPy, "Save the current state of Surge XT to an .fxp file.", py::arg("path")) + .def("loadWavetable", &SurgeSynthesizerWithPythonExtensions::loadWavetablePy, + "Load a wavetable file directly into a scene and oscillator immediately on this " + "thread.", + py::arg("scene"), py::arg("osc"), py::arg("path")) + .def("getModSource", &SurgeSynthesizerWithPythonExtensions::getModSource, "Given a constant from surge.constants.ms_*, provide a modulator object", py::arg("modId")) diff --git a/src/surge-python/tests/write_wavetable.py b/src/surge-python/tests/write_wavetable.py new file mode 100644 index 00000000000..c88469e0ee5 --- /dev/null +++ b/src/surge-python/tests/write_wavetable.py @@ -0,0 +1,19 @@ +""" +Tests for Surge XT Python bindings. +""" +import sys +sys.path.append('/Users/paul/dev/music/surge/cmake-build-debug/src/surge-python/') +import surgepy + +def main(): + s = surgepy.createSurge(44100) + + patch = s.getPatch() + osc0 = patch["scene"][0]["osc"][0] + otype = osc0["type"] + s.setParamVal(otype, surgepy.constants.ot_wavetable) + s.loadWavetable(0, 0, "/Users/paul/dev/music/surge/resources/data/wavetables_3rdparty/A.Liv/Droplet/Droplet 2.wav") + s.savePatch("/Users/paul/Desktop/PythonGenerated.fxp") + +if __name__ == "__main__": + main() \ No newline at end of file