ホームに戻る
/*
  nmVst3 サンプルの解説

 オリジナルウィンドウ(1プログラム、1パラメーター)の例

  オリジナルウィンドウの場合は AEffEditor を継承したクラスを使う。
  これをプラグインのクラスに setEditor で登録することになる。
  ウィンドウのクラスには open と close がある
  open のときにウィンドウクラスを登録してウィンドウを開く。
  2つ目以降はウィンドウクラスの登録は必要無いのでそのまま開く。
  最後のウィンドウを閉じる際にウィンドウクラスを片付けるので、
  ウィンドウをいくつ開いているかはカウントしておく必要がある。
  
  静的なプロシージャ内でウィンドウを識別できない問題に対しては、
  SetProp と GetProp で対応する。

  パラメーターの変更は次の用件を満たす必要がある。

  GUIを操作することでパラメーターが変化しなければならない。
  オートメーションに対してGUIが動かなければならない。

  このあたりで値の食い違いが起こるので注意したい。
  特に setParameterAutomated は必ず使わなければならないようだ。

  Microsoft Visual C++ 2008
  vst_sdk 2.4 を使用
*/

#include <windows.h>
#include <stdio.h>
#include "aeffeditor.h"
#include "audioeffectx.h"

#define WIDTH 300
#define HEIGHT 200

#define ID_B1 100

#define PROP_WINPROC "PropClassWindowProc"

extern void* hInstance;

static int regist_count = 0;
static LPCTSTR lpszAppName = "nmVst3";

class nmVstEditor : public AEffEditor
{
public:
  nmVstEditor(AudioEffect *effect);
  virtual ~nmVstEditor(){};

  virtual bool getRect(ERect **erect);
  virtual bool open(void *ptr);
  virtual void close();
  virtual void idle();
  virtual void setParam1(float param1){fParam1 = param1;}
  virtual float getParam1(){return fParam1;}
  virtual void setParameter(VstInt32 index, float value);
  virtual void valueChanged(VstInt32 index, float value);

  static LONG WINAPI WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

private:
  HWND hwnd_e;
  float fParam1;
};

nmVstEditor::nmVstEditor(AudioEffect *effectx)
: AEffEditor(effectx), hwnd_e(NULL), fParam1(1.0f){}

bool nmVstEditor::getRect(ERect **erect){
  static ERect r = {0, 0, HEIGHT, WIDTH};
  *erect = &r;
  return true;
}

bool nmVstEditor::open(void *ptr){
  systemWindow = ptr;

  if(regist_count == 0){
    WNDCLASS wd;
    wd.style = 0;
    wd.lpfnWndProc = WindowProc;
    wd.cbClsExtra = 0;
    wd.cbWndExtra = 0;
    wd.hInstance = (HINSTANCE)hInstance;
    wd.hIcon = NULL;
    wd.hCursor = NULL;
    wd.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
    wd.lpszMenuName = NULL;
    wd.lpszClassName = lpszAppName;

    RegisterClass(&wd);
  }

  regist_count++;

  HWND hwnd = CreateWindowEx(
                NULL,
                lpszAppName,
                "",
                WS_CHILD | WS_VISIBLE, 
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                WIDTH,
                HEIGHT,
                (HWND)systemWindow,
                NULL,
                (HINSTANCE)hInstance,
                NULL);

  hwnd_e = hwnd;

  SetProp(hwnd, PROP_WINPROC, this);      

  SetWindowLong(hwnd, GWL_USERDATA, (long)this);

  return true;
}

void nmVstEditor::close(){
  hwnd_e = NULL;

  regist_count--;

  if(regist_count == 0){
    UnregisterClass(lpszAppName, (HINSTANCE)hInstance);
  }
}

void nmVstEditor::idle (){
  AEffEditor::idle();
}

void nmVstEditor::setParameter(VstInt32 index, float value){
  if(hwnd_e == NULL){
    return;
  }

  setParam1(effect->getParameter(index));

  InvalidateRect(hwnd_e, NULL, TRUE);
}

void nmVstEditor::valueChanged(VstInt32 index, float value){
  effect->setParameterAutomated(index, value);
}

/* プロシージャ */
LONG WINAPI nmVstEditor::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
  static char buf[256];
  static HWND hButton1;

  switch(message){
    case WM_CREATE:
      hButton1 = CreateWindow(
        "BUTTON",
        "0.5",
        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
        10,
        10,
        80,
        20,
        hWnd,
        (HMENU)ID_B1,
        (HINSTANCE)hInstance
        ,NULL);
      return 0;
    case WM_COMMAND:
      switch(LOWORD(wParam)){
      //ボタン1を押したとき
        case ID_B1:
          nmVstEditor* nmve = (nmVstEditor*)GetProp(hWnd, PROP_WINPROC);
          nmve->valueChanged(0, 0.5f);
          return 0;
      }
      break;
    case WM_PAINT:
      {
        RECT rect;
        SIZE size;
        PAINTSTRUCT ps;
        HDC hDC = BeginPaint(hWnd, &ps);

        GetClientRect(hWnd, &rect);

        nmVstEditor* nmve = (nmVstEditor*)GetProp(hWnd, PROP_WINPROC);

        sprintf(buf, "%f", nmve->getParam1());

        GetTextExtentPoint32(hDC, buf, strlen(buf), &size);

        SetBkMode(hDC, TRANSPARENT);

        TextOut(hDC, ((rect.right - rect.left) - size.cx) / 2, ((rect.bottom - rect.top) - size.cy) / 2, buf, strlen(buf));

        EndPaint(hWnd, &ps);
      }
    return 0;
    case WM_DESTROY:
      RemoveProp(hWnd, PROP_WINPROC);   
      PostQuitMessage(0);
      return 0;
    default:
      break;
  }

  return DefWindowProc (hWnd, message, wParam, lParam);
}

class nmVst3 : public AudioEffectX{
public:
  nmVst3(audioMasterCallback audioMaster);
  ~nmVst3 ();

  // Processing
  virtual void processReplacing(float** inputs, float** outputs, VstInt32 sampleFrames);

  // Program
  virtual void setProgramName(char* name);
  virtual void getProgramName(char* name);

  // Parameters
  virtual void setParameter(VstInt32 index, float value);
  virtual float getParameter(VstInt32 index);
  virtual void getParameterLabel(VstInt32 index, char* label);
  virtual void getParameterDisplay(VstInt32 index, char* text);
  virtual void getParameterName(VstInt32 index, char* text);

protected:
  float fParam1;
  char programName[kVstMaxProgNameLen + 1];

  nmVstEditor* editor;
};

AudioEffect* createEffectInstance(audioMasterCallback audioMaster){
  return new nmVst3(audioMaster);
}

nmVst3::nmVst3(audioMasterCallback audioMaster) : AudioEffectX(audioMaster, 1, 1){

  try{
    editor = new nmVstEditor(this);
  }
  catch(int){
    throw -1;
  }

  setEditor(editor);

  setNumInputs(2);        // ステレオ入力
  setNumOutputs(2);       // ステレオ出力
  setUniqueID('nmV3');   // ID
  canProcessReplacing();

  fParam1 = 1.f;          // 現在のパラメーター1
  vst_strncpy(programName, "Default", kVstMaxProgNameLen);
}

nmVst3::~nmVst3(){}

// programName に name をセット
void nmVst3::setProgramName(char* name){
  vst_strncpy(programName, name, kVstMaxProgNameLen);
}

// name に programName をセット
void nmVst3::getProgramName(char* name){
  vst_strncpy(name, programName, kVstMaxProgNameLen);
}

void nmVst3::setParameter(VstInt32 index, float value){
  fParam1 = value;

  if(editor){
    ((nmVstEditor*)editor)->setParameter(index, value);
  }
}

float nmVst3::getParameter(VstInt32 index){
  return fParam1;
}

void nmVst3::getParameterName(VstInt32 index, char* label){
  vst_strncpy(label, "Param1", kVstMaxParamStrLen);
}

void nmVst3::getParameterDisplay(VstInt32 index, char* text){
  dB2string(fParam1, text, kVstMaxParamStrLen);
}

void nmVst3::getParameterLabel(VstInt32 index, char* label){
  vst_strncpy(label, "dB", kVstMaxParamStrLen);
}

void nmVst3::processReplacing(float** inputs, float** outputs, VstInt32 sampleFrames){
  float* in1  =  inputs[0];
  float* in2  =  inputs[1];
  float* out1 = outputs[0];
  float* out2 = outputs[1];

  while(--sampleFrames >= 0){
    (*out1++) = (*in1++) * fParam1;
    (*out2++) = (*in2++) * fParam1;
  }
}
inserted by FC2 system