Skip to content

Commit 7e7dad6

Browse files
author
Nathan Ho
committed
add example 01b demonstrating Tim's C++ header
1 parent 41b99d5 commit 7e7dad6

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

01-BoringMixer/BoringMixer.cpp renamed to 01a-BoringMixer/BoringMixer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ static void BoringMixer_Ctor(BoringMixer* unit);
1717
// it MUST be named "PluginName_Ctor", and the argument must be "unit."
1818
void BoringMixer_Ctor(BoringMixer* unit) {
1919
// in later examples, we will initialize state variables here.
20-
20+
2121
// set a calculation function. for now, we only have one calculation function.
2222
SETCALC(BoringMixer_next);
2323
// calculate one sample of output.
24-
// this allows plugging into other ugens with initial-rate inputs
24+
// this allows plugging into other ugens with initial-rate inputs
2525
BoringMixer_next(unit, 1);
2626
}
2727

@@ -30,8 +30,11 @@ void BoringMixer_Ctor(BoringMixer* unit) {
3030
// Don't change the names of the arguments, or the helper macros won't work.
3131
void BoringMixer_next(BoringMixer* unit, int inNumSamples) {
3232

33-
// IN and OUT are helper macros that return audio-rate input and output buffers.
33+
// IN and OUT are helper macros that return audio-rate input and output buffers. These are known as "wire buffers."
3434
// In old ugens you'll see ZIN and ZOUT, which are not recommended.
35+
36+
// scsynth saves memory by aliasing wire buffers. In this case, "out" and "left" are the same. You should either
37+
// be mindful of this behavior or turn it off in the PluginLoad section.
3538
float *left = IN(0); // first input
3639
float *right = IN(1); // second input
3740
float *out = OUT(0); // first output
@@ -48,5 +51,6 @@ PluginLoad(BoringMixerUGens) {
4851
ft = inTable; // store pointer to InterfaceTable
4952
// DefineSimpleUnit is one of four macros defining different kinds of ugens.
5053
// In later examples involving memory allocation, we'll see DefineDtorUnit.
54+
// You can disable aliasing by using DefineSimpleCantAliasUnit and DefineDtorCantAliasUnit.
5155
DefineSimpleUnit(BoringMixer);
5256
}
File renamed without changes.
File renamed without changes.

01b-BoringMixer/BoringMixer.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// This is an alternate way to write the BoringMixer plugin using a newer C++ header (introduced in 2010).
2+
3+
// Using SC_PlugIn.hpp instead of SC_PlugIn.h. SC_PlugIn.hpp includes SC_PlugIn.h. C++ and old-fashioned ugens can
4+
// coexist in the same file.
5+
#include "SC_PlugIn.hpp"
6+
7+
static InterfaceTable *ft;
8+
9+
// Inherits from SCUnit, not Unit.
10+
// Also note that the constructor, destructor, and calc functions are methods of the SCUnit.
11+
struct BoringMixer2 : public SCUnit {
12+
public:
13+
// Constructor function
14+
BoringMixer2() {
15+
// New way of setting calc function.
16+
set_calc_function<BoringMixer2, &BoringMixer2::next>();
17+
next(1);
18+
}
19+
20+
// If you want a destructor, you would declare "~BoringMixer2() { ... }" here.
21+
22+
private:
23+
// You can declare state variables here.
24+
25+
// Calc function
26+
void next(int inNumSamples) {
27+
// Note, there is no "unit" variable here, so you can't use a lot of the traditional helper macros. That's why
28+
// the C++ header offers replacements.
29+
30+
// in and out are methods of SCUnit that replace IN and OUT.
31+
// ins are const float*, not float*.
32+
const float* left = in(0);
33+
const float* right = in(1);
34+
float* outbuf = out(0);
35+
36+
for (int i = 0; i < inNumSamples; i++) {
37+
outbuf[i] = (left[i] + right[i]) * 0.5;
38+
}
39+
}
40+
};
41+
42+
PluginLoad(BoringMixer2UGens) {
43+
ft = inTable;
44+
// registerUnit takes the place of the Define*Unit functions. It automatically checks for the presence of a
45+
// destructor function.
46+
// However, it does not seem to be possible to disable buffer aliasing with the C++ header.
47+
registerUnit<BoringMixer2>(ft, "BoringMixer2");
48+
}

01b-BoringMixer/BoringMixer.sc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
BoringMixer2 : UGen {
2+
*ar { |left, right|
3+
^this.multiNew('audio', left, right);
4+
}
5+
checkInputs {
6+
[0, 1].do { |i|
7+
(inputs[i].rate != 'audio').if {
8+
^"input % is not audio rate".format(i).throw;
9+
};
10+
};
11+
^this.checkValidInputs;
12+
}
13+
}

01b-BoringMixer/CMakeLists.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
cmake_minimum_required (VERSION 2.8)
2+
set(PROJECT "BoringMixer")
3+
project (${PROJECT})
4+
5+
include_directories(${SC_PATH}/include/plugin_interface)
6+
include_directories(${SC_PATH}/include/common)
7+
include_directories(${SC_PATH}/common)
8+
9+
10+
set(CMAKE_SHARED_MODULE_PREFIX "")
11+
if(APPLE OR WIN32)
12+
set(CMAKE_SHARED_MODULE_SUFFIX ".scx")
13+
endif()
14+
15+
option(CPP11 "Build with c++11." ON)
16+
17+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
18+
add_definitions(-fvisibility=hidden)
19+
20+
include (CheckCCompilerFlag)
21+
include (CheckCXXCompilerFlag)
22+
23+
CHECK_C_COMPILER_FLAG(-msse HAS_SSE)
24+
CHECK_CXX_COMPILER_FLAG(-msse HAS_CXX_SSE)
25+
26+
if (HAS_SSE)
27+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse")
28+
endif()
29+
if (HAS_CXX_SSE)
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse")
31+
endif()
32+
33+
CHECK_C_COMPILER_FLAG(-msse2 HAS_SSE2)
34+
CHECK_CXX_COMPILER_FLAG(-msse2 HAS_CXX_SSE2)
35+
36+
if (HAS_SSE2)
37+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
38+
endif()
39+
if (HAS_CXX_SSE2)
40+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")
41+
endif()
42+
43+
CHECK_C_COMPILER_FLAG(-mfpmath=sse HAS_FPMATH_SSE)
44+
CHECK_CXX_COMPILER_FLAG(-mfpmath=sse HAS_CXX_FPMATH_SSE)
45+
46+
if (HAS_FPMATH_SSE)
47+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpmath=sse")
48+
endif()
49+
if (HAS_CXX_FPMATH_SSE)
50+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpmath=sse")
51+
endif()
52+
53+
if(NATIVE)
54+
add_definitions(-march=native)
55+
endif()
56+
57+
if(CPP11)
58+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
59+
if(CMAKE_COMPILER_IS_CLANG)
60+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
61+
endif()
62+
endif()
63+
endif()
64+
if(MINGW)
65+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mstackrealign")
66+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mstackrealign")
67+
endif()
68+
69+
add_library(${PROJECT} MODULE BoringMixer.cpp)

0 commit comments

Comments
 (0)