SuperCollider Book and Writing UGens

I’m writing some UGens and using the SuperCollider book as a reference. Since it’s written around SC 3.4, are the examples in the UGen section still applicable? Is there anything specifically to be aware of if it is only partially applicable?

This is distinct from the other question re: The SuperCollider Book. They seemed to be asking about the SC scripts as opposed to the C++ code of a UGen.

You may want to start by looking at https://github.com/supercollider/example-plugins if you haven’t already.

this is probably the easiest way to get started with new plugins now: https://github.com/supercollider/cookiecutter-supercollider-plugin/

as always feel free to ask if you have any questions

Awesome; thanks for the resources. I was looking in the wrong place for them!

Reviving with an error. I need to pass in buffers to my UGen and when I call GET_BUF I get the following at the build:

roger@RAM:~/Documents/jacob/projects/simplegain/build$ cmake --build . --config Debug
Scanning dependencies of target SimpleGain_scsynth
[ 25%] Building CXX object CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o
In file included from /home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_PlugIn.h:25:0,
                 from /home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_PlugIn.hpp:25,
                 from /home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:4:
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp: In member function ‘void SimpleGain::SimpleGain::next(int)’:
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:67:20: error: ‘unit’ was not declared in this scope
 #define IN(index) (unit->mInBuf[index])
                    ^
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/Unroll.h:129:18: note: in expansion of macro ‘IN’
 #define ZIN0(i) (IN(i)[0]) // get first sample
                  ^~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:251:21: note: in expansion of macro ‘ZIN0’
     float fbufnum = ZIN0(0);                                                                                           \
                     ^~~~
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:17:5: note: in expansion of macro ‘GET_BUF’
     GET_BUF
     ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:67:20: note: suggested alternative: ‘Unit’
 #define IN(index) (unit->mInBuf[index])
                    ^
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/Unroll.h:129:18: note: in expansion of macro ‘IN’
 #define ZIN0(i) (IN(i)[0]) // get first sample
                  ^~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:251:21: note: in expansion of macro ‘ZIN0’
     float fbufnum = ZIN0(0);                                                                                           \
                     ^~~~
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:17:5: note: in expansion of macro ‘GET_BUF’
     GET_BUF
     ^~~~~~~
CMakeFiles/SimpleGain_scsynth.dir/build.make:62: recipe for target 'CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o' failed
make[2]: *** [CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SimpleGain_scsynth.dir/all' failed
make[1]: *** [CMakeFiles/SimpleGain_scsynth.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

This is just the SimpleGain from the cookiecutter plugin with GET_BUF added. Without it, it builds fine.

I am not at a computer now so cannot check, but I think these macros normally operate in C-style code where unit is a function argument. So I think you need at least:

auto* unit = this;

Before invoking GET_BUF.

Probably the C++ plugin wrapper should have a function for this.

Glad someone is making use of the cookiecutter project by the way. Please submit an issue or pull request if you see anything that could be improved!

Brian

Putting auto* unit = this; just before GET_BUF in SimpleGain::next() produces a new error:

roger@RAM:~/Documents/jacob/projects/simplegain/build$ cmake --build . --config Debug
Scanning dependencies of target SimpleGain_scsynth
[ 25%] Building CXX object CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o
In file included from /home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_PlugIn.h:25:0,
                 from /home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_PlugIn.hpp:25,
                 from /home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:4:
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp: In member function ‘void SimpleGain::SimpleGain::next(int)’:
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:255:26: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_fbufnum’
     if (fbufnum != unit->m_fbufnum) {                                                                                  \
                          ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:262:23: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_buf’; did you mean ‘mInBuf’?
                 unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
                       ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:265:23: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_buf’; did you mean ‘mInBuf’?
                 unit->m_buf = world->mSndBufs + bufnum;                                                                \
                       ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:268:19: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_buf’; did you mean ‘mInBuf’?
             unit->m_buf = world->mSndBufs + bufnum;                                                                    \
                   ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:270:15: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_fbufnum’
         unit->m_fbufnum = fbufnum;                                                                                     \
               ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
/home/roger/Documents/jacob/supercolliderSource/supercollider/include/plugin_interface/SC_Unit.h:272:25: error: ‘class SimpleGain::SimpleGain’ has no member named ‘m_buf’; did you mean ‘mInBuf’?
     SndBuf* buf = unit->m_buf;                                                                                         \
                         ^
/home/roger/Documents/jacob/projects/simplegain/plugins/SimpleGain/SimpleGain.cpp:18:3: note: in expansion of macro ‘GET_BUF’
   GET_BUF
   ^~~~~~~
CMakeFiles/SimpleGain_scsynth.dir/build.make:62: recipe for target 'CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o' failed
make[2]: *** [CMakeFiles/SimpleGain_scsynth.dir/plugins/SimpleGain/SimpleGain.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SimpleGain_scsynth.dir/all' failed
make[1]: *** [CMakeFiles/SimpleGain_scsynth.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

Do I have to define an m_fbufnum somewhere in the file beforehand as well?

Again just looking at the definition of GET_BUF and where it’s used, your unit generator class should also have members float m_bufnum and SndBuf* m_buf. But I think if you have more questions it would probably be best to look at how this macro is used in the server plugins itself, I am more or less just relaying what I see there. For instance it is used in DiskIO ugens: https://github.com/supercollider/supercollider/blob/f806ace7bd8565dd174e7d47a1b32aaa4175a46e/server/plugins/DiskIO_UGens.cpp.

It seems the SC Book doesn’t document this very thoroughly.

-Brian

Thanks, Brian.

Yes, I was just looking here: http://doc.sccode.org/Reference/ServerPluginAPI.html and the book and it doesn’t say much about that.