-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioProcessor.cs
162 lines (142 loc) · 5.21 KB
/
AudioProcessor.cs
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
using Jacobi.Vst.Core;
using Jacobi.Vst.Plugin.Framework;
using Jacobi.Vst.Plugin.Framework.Plugin;
using System;
using System.Diagnostics;
using VstNetAudioPlugin.Dsp;
using NodeSysCore;
namespace VstNetAudioPlugin
{
/// <summary>
/// This object performs audio processing for your plugin.
/// </summary>
internal sealed class AudioProcessor : VstPluginAudioProcessor, IVstPluginBypass
{
/// <summary>
/// TODO: assign the input count.
/// </summary>
private const int AudioInputCount = 2;
/// <summary>
/// TODO: assign the output count.
/// </summary>
private const int AudioOutputCount = 2;
/// <summary>
/// TODO: assign the tail size.
/// </summary>
private const int InitialTailSize = 0;
// TODO: change this to your specific needs.
private readonly VstTimeInfoFlags _defaultTimeInfoFlags = VstTimeInfoFlags.ClockValid;
// set after the plugin is opened
private IVstHostSequencer? _sequencer;
/// <summary>
/// Default constructor.
/// </summary>
public AudioProcessor(IVstPluginEvents pluginEvents, PluginParameters parameters)
: base(AudioInputCount, AudioOutputCount, InitialTailSize, noSoundInStop: false)
{
Throw.IfArgumentIsNull(pluginEvents, nameof(pluginEvents));
Throw.IfArgumentIsNull(parameters, nameof(parameters));
// one set of parameters is shared for both channels.
Left = new Delay(parameters.DelayParameters);
Right = new Delay(parameters.DelayParameters);
pluginEvents.Opened += Plugin_Opened;
}
internal Delay Left { get; private set; }
internal Delay Right { get; private set; }
/// <summary>
/// Override the default implementation to pass it through to the delay.
/// </summary>
public override float SampleRate
{
get { return Left.SampleRate; }
set
{
Left.SampleRate = value;
Right.SampleRate = value;
}
}
private VstTimeInfo? _timeInfo;
/// <summary>
/// Gets the current time info.
/// </summary>
/// <remarks>The Time Info is refreshed with each call to Process.</remarks>
internal VstTimeInfo? TimeInfo
{
get
{
if (_timeInfo == null && _sequencer != null)
{
_timeInfo = _sequencer.GetTime(_defaultTimeInfoFlags);
}
return _timeInfo;
}
}
private void Plugin_Opened(object? sender, EventArgs e)
{
var plugin = (VstPlugin?)sender;
// A reference to the host is only available after
// the plugin has been loaded and opened by the host.
_sequencer = plugin?.Host?.GetInstance<IVstHostSequencer>();
}
/// <summary>
/// Called by the host to allow the plugin to process audio samples.
/// </summary>
/// <param name="inChannels">Never null.</param>
/// <param name="outChannels">Never null.</param>
public override void Process(VstAudioBuffer[] inChannels, VstAudioBuffer[] outChannels)
{
// by resetting the time info each cycle, accessing the TimeInfo property will fetch new info.
_timeInfo = null;
if (!Bypass)
{
// check assumptions
Debug.Assert(outChannels.Length == inChannels.Length);
// TODO: Implement your audio (effect) processing here.
for (int i = 0; i < outChannels.Length; i++)
{
Process(i % 2 == 0 ? Left : Right,
inChannels[i], outChannels[i]);
}
base.Process(inChannels, outChannels);
}
else
{
// calling the base class transfers input samples to the output channels unchanged (bypass).
base.Process(inChannels, outChannels);
}
}
float[] amplitudebuffer = new float[8];
byte index;
// process a single audio channel
private void Process(Delay delay, VstAudioBuffer input, VstAudioBuffer output)
{
index++;
if(index > 7)
{
index = 0;
}
float max = 0;
float temp;
for (int i = 0; i < input.SampleCount; i++)
{
temp = Math.Abs(input[i]);
if (temp > max)
{
max = temp;
}
//output[i] = delay.ProcessSample(input[i]);
}
amplitudebuffer[index] = max;
temp = 0;
for (int i = 0; i < amplitudebuffer.Length; i++)
{
temp += amplitudebuffer[i];
}
temp /= amplitudebuffer.Length;
Delay.networking.SendFloat("Audio" + delay.GetIndex(), temp);
}
#region IVstPluginBypass Members
public bool Bypass { get; set; }
#endregion
}
}