File: VSTEffect.cpp

package info (click to toggle)
audacity 0.98-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,896 kB
  • ctags: 4,089
  • sloc: cpp: 26,099; ansic: 4,961; sh: 2,465; makefile: 156; perl: 23
file content (366 lines) | stat: -rwxr-xr-x 9,710 bytes parent folder | download
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**********************************************************************

  Audacity: A Digital Audio Editor

  VSTEffect.cpp

  Dominic Mazzoni
  
  This class implements a VST Plug-in effect.  The plug-in must be
  loaded in a platform-specific way and passed into the constructor,
  but from here this class handles the interfacing.  VST plug-ins
  are used in Cubase and other Steinberg products, and all of those
  files and the information within is copyrighted by Steinberg.

**********************************************************************/

#include "AEffect.h"            // VST API

#include <wx/button.h>
#include <wx/slider.h>
#include <wx/msgdlg.h>

#include "Effect.h"             // Audacity Effect base class
#include "VSTEffect.h"          // This class's header file

VSTEffect::VSTEffect(wxString pluginName, AEffect * aEffect)
{
   this->aEffect = aEffect;
   this->pluginName = pluginName;

   buffer = NULL;
   fInBuffer = NULL;
   fOutBuffer = NULL;

   isOpened = false;
}

wxString VSTEffect::GetEffectName()
{
   return pluginName + "...";
}

wxString VSTEffect::GetEffectAction()
{
   return "Performing VST Effect: \""+pluginName+"\"";
}

bool VSTEffect::Init()
{
   if (!isOpened) {
      aEffect->dispatcher(aEffect, effOpen, 0, 0, NULL, 0.0);
      isOpened = true;
   }
   
   inputs = aEffect->numInputs;
   outputs = aEffect->numOutputs;

   mBlockSize = 0;

   if (inputs > 1) {
      TrackListIterator iter(mWaveTracks);
      VTrack *left = iter.First();
      while(left) {
         sampleCount lstart, rstart, llen, rlen;
         GetSamples((WaveTrack *)left, &lstart, &llen);
         
         if (left->linked) {
            VTrack *right = iter.Next();
            GetSamples((WaveTrack *)right, &rstart, &rlen);
            
            if (llen != rlen || ((WaveTrack *)left)->rate != ((WaveTrack *)right)->rate) {
               wxMessageBox("Sorry, VST Effects cannot be performed on stereo tracks where "
                            "the individual channels of the track do not match.");
               return false;
            }
         }
         
         left = iter.Next();
      }
   }

   return true;
}

bool VSTEffect::PromptUser()
{
   // Try to figure out how many parameters it takes by seeing how
   // many parameters have names
   char temp[8][256];
   int numParameters = 0;
   do {
      temp[numParameters][0] = 0;
      aEffect->dispatcher(aEffect, effGetParamName, numParameters, 0,
                          (void *) temp[numParameters], 0.0);

      if (temp[numParameters][0]==0)
         break;
      if (strstr(temp[numParameters], "ABOUT"))
         break;
      if (numParameters > 0
          && !strcmp(temp[numParameters], temp[numParameters - 1]))
         break;

      numParameters++;
   } while (temp[0] != 0 && numParameters < 8);

   //numParameters = aEffect->numParams;

   if (numParameters > 0) {
      VSTEffectDialog d(mParent, pluginName, numParameters, aEffect);
      d.ShowModal();

      if (!d.GetReturnCode())
         return false;
   }

   return true;
}

bool VSTEffect::Process()
{
   TrackListIterator iter(mWaveTracks);
   int count = 0;
   VTrack *left = iter.First();
   VTrack *right;
   while(left) {
      sampleCount lstart, rstart, len;
      GetSamples((WaveTrack *)left, &lstart, &len);
      
      right = NULL;
      if (left->linked && inputs>1) {
         right = iter.Next();         
         GetSamples((WaveTrack *)right, &rstart, &len);
      }
      
      bool success = ProcessStereo(count, (WaveTrack *)left, (WaveTrack *)right,
                                   lstart, rstart, len);
      
      if (!success)
         return false;
   
      left = iter.Next();
      count++;
   }
   
   return true;
}

bool VSTEffect::ProcessStereo(int count, WaveTrack *left, WaveTrack *right,
                              sampleCount lstart, sampleCount rstart, sampleCount len)
{
   if (mBlockSize == 0) {
      mBlockSize = left->GetMaxBlockSize() * 2;

      buffer = new sampleType[mBlockSize];
      fInBuffer = new float *[inputs];
      int i;
      for (i = 0; i < inputs; i++)
         fInBuffer[i] = new float[mBlockSize];
      fOutBuffer = new float *[outputs];
      for (i = 0; i < outputs; i++)
         fOutBuffer[i] = new float[mBlockSize];

   }

   aEffect->dispatcher(aEffect, effSetSampleRate, 0, 0, NULL,
                       (float) left->rate);
   aEffect->dispatcher(aEffect, effSetBlockSize, 0, mBlockSize * 2, NULL, 0.0);

   // HACK:
   //
   // Some plug-ins save a lot of state.  We attempt to flush that
   // here by feeding it two seconds of zeroes before each track.

   int i, j, c;
   for (i = 0; i < inputs; i++) {
      for (j = 0; j < mBlockSize; j++)
         fInBuffer[i][j] = 0.0;
   }
   for (c = 0; c < 2; c++)
      aEffect->processReplacing(aEffect, fInBuffer, fOutBuffer,
                                mBlockSize);

   // Actually perform the effect here

   sampleCount originalLen = len;
   sampleCount ls = lstart;
   sampleCount rs = rstart;
   while (len) {
      int block = mBlockSize;
      if (block > len)
         block = len;

      left->Get(buffer, ls, block);
      for (i = 0; i < block; i++)
         fInBuffer[0][i] = float (buffer[i] / 32767.);
      if (right) {
         right->Get(buffer, rs, block);
         for (i = 0; i < block; i++)
            fInBuffer[1][i] = float (buffer[i] / 32767.);
      }

      aEffect->processReplacing(aEffect, fInBuffer, fOutBuffer, block);

      for (i = 0; i < block; i++)
         buffer[i] = (sampleType) (fOutBuffer[0][i] * 32767.);
      left->Set(buffer, ls, block);
      
      if (right) {
         for (i = 0; i < block; i++)
            buffer[i] = (sampleType) (fOutBuffer[1][i] * 32767.);
         right->Set(buffer, rs, block);
      }      

      len -= block;
      ls += block;
      rs += block;
      
      if (inputs > 1) {      
         if (TrackGroupProgress(count, (ls-lstart)/(double)originalLen))
            break;
      }
      else {
         if (TrackProgress(count, (ls-lstart)/(double)originalLen))
            break;
      }
   }

   return true;
}

void VSTEffect::End()
{
   if (buffer) {
      int i;

      delete[]buffer;
      for (i = 0; i < inputs; i++)
         delete fInBuffer[i];
      for (i = 0; i < outputs; i++)
         delete fOutBuffer[i];
      delete[]fInBuffer;
      delete[]fOutBuffer;

   }
   buffer = NULL;
   fInBuffer = NULL;
   fOutBuffer = NULL;
}

const int VSTEFFECT_SLIDER_ID = 13100;

BEGIN_EVENT_TABLE(VSTEffectDialog, wxDialog)
    EVT_BUTTON(wxID_OK, VSTEffectDialog::OnOK)
    EVT_BUTTON(wxID_CANCEL, VSTEffectDialog::OnCancel)
    EVT_COMMAND_SCROLL(VSTEFFECT_SLIDER_ID, VSTEffectDialog::OnSlider)
    EVT_SLIDER(VSTEFFECT_SLIDER_ID, VSTEffectDialog::OnSlider)
END_EVENT_TABLE()

IMPLEMENT_CLASS(VSTEffectDialog, wxDialog)

    VSTEffectDialog::VSTEffectDialog(wxWindow * parent,
                                     wxString effectName,
                                     int numParams,
                                     AEffect * aEffect,
                                     const wxPoint & pos)
:wxDialog(parent, -1, effectName, pos, wxSize(320, 430),
          wxDEFAULT_DIALOG_STYLE)
{
   this->aEffect = aEffect;
   this->numParams = numParams;

   int y = 10;

   new wxStaticText(this, 0, "VST Plug-in parameters:", wxPoint(10, y),
                    wxSize(300, 15));
   y += 20;

   sliders = new wxSlider *[numParams];
   labels = new wxStaticText *[numParams];

   for (int p = 0; p < numParams; p++) {

      char paramName[256];
      aEffect->dispatcher(aEffect, effGetParamName, p, 0,
                          (void *) paramName, 0.0);
      new wxStaticText(this, 0, wxString(paramName), wxPoint(10, y),
                       wxSize(85, 15));

      float val = aEffect->getParameter(aEffect, p);

      sliders[p] =
          new wxSlider(this, VSTEFFECT_SLIDER_ID,
                       1000 * val, 0, 1000,
                       wxPoint(100, y + 5), wxSize(200, 25));

      char label[256];
      aEffect->dispatcher(aEffect, effGetParamDisplay, p, 0,
                          (void *) label, 0.0);
      char units[256];
      aEffect->dispatcher(aEffect, effGetParamLabel, p, 0, (void *) units,
                          0.0);

      labels[p] =
          new wxStaticText(this, 0,
                           wxString::Format("%s %s", label, units),
                           wxPoint(10, y + 15), wxSize(85, 15));

      y += 35;
   }

   wxButton *ok =
       new wxButton(this, wxID_OK, "OK", wxPoint(110, y), wxSize(80, 30));
   wxButton *cancel =
       new wxButton(this, wxID_CANCEL, "Cancel", wxPoint(210, y),
                    wxSize(80, 30));
   y += 40;

   wxSize size;
   size.x = 320;
   size.y = y;

#ifdef __WXMSW__
   size.y += 20;
#endif

   Centre(wxBOTH | wxCENTER_FRAME);

   SetSize(size);
}

VSTEffectDialog::~VSTEffectDialog()
{
   // TODO: proper disposal here

   delete[]sliders;
   delete[]labels;
}

void VSTEffectDialog::OnSlider(wxCommandEvent & WXUNUSED(event))
{
   for (int p = 0; p < numParams; p++) {
      float val;

      val = sliders[p]->GetValue() / 1000.;
      aEffect->setParameter(aEffect, p, val);

      char label[256];
      aEffect->dispatcher(aEffect, effGetParamDisplay, p, 0,
                          (void *) label, 0.0);
      char units[256];
      aEffect->dispatcher(aEffect, effGetParamLabel, p, 0, (void *) units,
                          0.0);
      labels[p]->SetLabel(wxString::Format("%s %s", label, units));
   }
}

void VSTEffectDialog::OnOK(wxCommandEvent & WXUNUSED(event))
{
   EndModal(TRUE);
}

void VSTEffectDialog::OnCancel(wxCommandEvent & WXUNUSED(event))
{
   EndModal(FALSE);
}