-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample_vst2_test.go
174 lines (160 loc) · 3.63 KB
/
example_vst2_test.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// +build !plugin
package vst2_test
import (
"fmt"
"log"
"path/filepath"
"runtime"
"unsafe"
"pipelined.dev/audio/vst2"
"pipelined.dev/signal"
)
// PrinterHostCallback returns closure that prints received opcode with
// provided prefix. This technique allows to provide callback with any
// context needed.
func PrinterHostCallback(prefix string) vst2.HostCallbackFunc {
return func(code vst2.HostOpcode, index int32, value int64, ptr unsafe.Pointer, opt float32) int64 {
fmt.Printf("%s: %v\n", prefix, code)
return 0
}
}
func Example_plugin() {
// sample data that we will process: 2 channels, 32 samples
data := [][]float64{
{
-0.0027160645,
-0.0039978027,
-0.0071411133,
-0.0065307617,
0.0038757324,
0.021972656,
0.041229248,
0.055511475,
0.064971924,
0.07342529,
0.08300781,
0.092681885,
0.10070801,
0.110809326,
0.12677002,
0.15231323,
0.19058228,
0.24459839,
0.3140869,
0.38861084,
0.44683838,
0.47177124,
0.46643066,
0.45007324,
0.4449768,
0.45724487,
0.47451782,
0.48321533,
0.47824097,
0.46679688,
0.45999146,
0.46765137,
},
{
-0.0027160645,
-0.0039978027,
-0.0071411133,
-0.0065307617,
0.0038757324,
0.021972656,
0.041229248,
0.055511475,
0.064971924,
0.07342529,
0.08300781,
0.092681885,
0.10070801,
0.110809326,
0.12677002,
0.15231323,
0.19058228,
0.24459839,
0.3140869,
0.38861084,
0.44683838,
0.47177124,
0.46643066,
0.45007324,
0.4449768,
0.45724487,
0.47451782,
0.48321533,
0.47824097,
0.46679688,
0.45999146,
0.46765137,
},
}
buffer := signal.Allocator{
Channels: len(data),
Length: len(data[0]),
Capacity: len(data[0]),
}.Float64()
signal.WriteStripedFloat64(data, buffer)
// Open VST library. Library contains a reference to
// OS-specific handle, that needs to be freed with Close.
vst, err := vst2.Open(pluginPath())
if err != nil {
log.Panicf("failed to open VST library: %v", err)
}
defer vst.Close()
// Load VST plugin with example callback.
plugin := vst.Plugin(PrinterHostCallback("Received opcode"))
defer plugin.Close()
plugin.Start()
// Set sample rate in Hertz.
plugin.SetSampleRate(44100)
// Set channels information.
plugin.SetSpeakerArrangement(
&vst2.SpeakerArrangement{
Type: vst2.SpeakerArrMono,
NumChannels: int32(buffer.Channels()),
},
&vst2.SpeakerArrangement{
Type: vst2.SpeakerArrMono,
NumChannels: int32(buffer.Channels()),
},
)
// Set buffer size.
plugin.SetBufferSize(buffer.Length())
// Start the plugin.
plugin.Resume()
// To process data with plugin, we need to use VST2 buffers.
// It's needed because VST SDK was written in C and expected
// memory layout differs from Golang slices.
// We need two buffers for input and output.
in := vst2.NewDoubleBuffer(buffer.Channels(), buffer.Length())
defer in.Free()
out := vst2.NewDoubleBuffer(buffer.Channels(), buffer.Length())
defer out.Free()
// Fill input with data values.
in.Write(buffer)
// Process data.
plugin.ProcessDouble(in, out)
// Copy processed data.
out.Read(buffer)
// Output:
// Received opcode: HostGetCurrentProcessLevel
// Received opcode: hostWantMidi
// Received opcode: HostGetCurrentProcessLevel
}
// pluginPath returns a path to OS-specific plugin. It will panic if OS is
// not supported.
func pluginPath() string {
os := runtime.GOOS
var path string
switch os {
case "windows":
path, _ = filepath.Abs("_testdata\\TAL-Noisemaker.dll")
case "darwin":
path, _ = filepath.Abs("_testdata/TAL-Noisemaker.vst")
default:
panic(fmt.Sprintf("unsupported OS: %v", os))
}
return path
}