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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
#if ENABLE_VOCALOID
/*
* vstidrv.cs
* Copyright © 2008-2011 kbinani
*
* This file is part of org.kbinani.cadencii.
*
* org.kbinani.cadencii is free software; you can redistribute it and/or
* modify it under the terms of the GPLv3 License.
*
* org.kbinani.cadencii is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#if !JAVA
#define TEST
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using org.kbinani.java.awt;
using org.kbinani.java.util;
using org.kbinani.vsq;
using VstSdk;
namespace org.kbinani.cadencii
{
using boolean = System.Boolean;
using VstInt32 = Int32;
using VstIntPtr = Int32;
delegate void VoidDelegate();
public struct TempoInfo
{
/// <summary>
/// テンポが変更される時刻を表すクロック数
/// </summary>
public int Clock;
/// <summary>
/// テンポ
/// </summary>
public int Tempo;
/// <summary>
/// テンポが変更される時刻
/// </summary>
public double TotalSec;
}
public class vstidrv
{
protected delegate IntPtr PVSTMAIN( [MarshalAs( UnmanagedType.FunctionPtr )]audioMasterCallback audioMaster );
public boolean loaded = false;
public String path = "";
public RendererKind kind = RendererKind.NULL;
/// <summary>
/// プラグインのUI
/// </summary>
protected FormPluginUi ui = null;
private boolean isUiOpened = false;
protected PVSTMAIN mainDelegate;
private IntPtr mainProcPointer;
protected audioMasterCallback audioMaster;
/// <summary>
/// 読込んだdllから作成したVOCALOID2の本体。VOCALOID2への操作はs_aeffect->dispatcherで行う
/// </summary>
protected AEffectWrapper aEffect;
protected IntPtr aEffectPointer;
/// <summary>
/// 読込んだdllのハンドル
/// </summary>
protected IntPtr dllHandle;
/// <summary>
/// 波形バッファのサイズ。
/// </summary>
protected int blockSize;
/// <summary>
/// サンプリングレート。VOCALOID2 VSTiは限られたサンプリングレートしか受け付けない。たいてい44100Hzにする
/// </summary>
protected int sampleRate;
/// <summary>
/// バッファ(bufferLeft, bufferRight)の長さ
/// </summary>
const int BUFLEN = 44100;
/// <summary>
/// 左チャンネル用バッファ
/// </summary>
private IntPtr bufferLeft = IntPtr.Zero;
/// <summary>
/// 右チャンネル用バッファ
/// </summary>
private IntPtr bufferRight = IntPtr.Zero;
/// <summary>
/// 左右チャンネルバッファの配列(buffers={bufferLeft, bufferRight})
/// </summary>
private IntPtr buffers = IntPtr.Zero;
/// <summary>
/// パラメータの,ロード時のデフォルト値
/// </summary>
private float[] paramDefaults = null;
/// <summary>
/// UIウィンドウのサイズ
/// </summary>
private Dimension uiWindowRect = new Dimension( 373, 158 );
/* /// <summary>
/// win32.LoadLibraryExを使うかどうか。trueならwin32.LoadLibraryExを使い、falseならutil.dllのLoadDllをつかう。既定ではtrue
/// </summary>
private boolean useNativeDllLoader = true;*/
protected MemoryManager memoryManager = new MemoryManager();
private Object mSyncRoot = new Object();
public int getSampleRate()
{
return sampleRate;
}
public void resetAllParameters()
{
if ( paramDefaults == null ) {
return;
}
for ( int i = 0; i < paramDefaults.Length; i++ ) {
setParameter( i, paramDefaults[i] );
}
}
public virtual float getParameter( int index )
{
float ret = 0.0f;
try {
ret = aEffect.GetParameter( index );
} catch ( Exception ex ) {
serr.println( "vstidrv#getParameter; ex=" + ex );
}
return ret;
}
public virtual void setParameter( int index, float value )
{
try {
aEffect.SetParameter( index, value );
} catch ( Exception ex ) {
serr.println( "vstidrv#setParameter; ex=" + ex );
}
}
private String getStringCore( int opcode, int index, int str_capacity )
{
byte[] arr = new byte[str_capacity + 1];
for ( int i = 0; i < str_capacity; i++ ) {
arr[i] = 0;
}
IntPtr ptr = IntPtr.Zero;
try {
unsafe {
fixed ( byte* bptr = &arr[0] ) {
ptr = new IntPtr( bptr );
aEffect.Dispatch( opcode, index, 0, ptr, 0.0f );
}
}
} catch ( Exception ex ) {
serr.println( "vstidrv#getStringCore; ex=" + ex );
}
String ret = Encoding.ASCII.GetString( arr );
return ret;
}
public String getParameterDisplay( int index )
{
return getStringCore( AEffectOpcodes.effGetParamDisplay, index, VstStringConstants.kVstMaxParamStrLen );
}
public String getParameterLabel( int index )
{
return getStringCore( AEffectOpcodes.effGetParamLabel, index, VstStringConstants.kVstMaxParamStrLen );
}
public String getParameterName( int index )
{
return getStringCore( AEffectOpcodes.effGetParamName, index, VstStringConstants.kVstMaxParamStrLen );
}
private void initBuffer()
{
if ( bufferLeft == IntPtr.Zero ) {
bufferLeft = Marshal.AllocHGlobal( sizeof( float ) * BUFLEN );
}
if ( bufferRight == IntPtr.Zero ) {
bufferRight = Marshal.AllocHGlobal( sizeof( float ) * BUFLEN );
}
if ( buffers == IntPtr.Zero ) {
unsafe {
buffers = Marshal.AllocHGlobal( sizeof( float* ) * 2 );
}
}
}
private void releaseBuffer()
{
if ( bufferLeft != IntPtr.Zero ) {
Marshal.FreeHGlobal( bufferLeft );
bufferLeft = IntPtr.Zero;
}
if ( bufferRight != IntPtr.Zero ) {
Marshal.FreeHGlobal( bufferRight );
bufferRight = IntPtr.Zero;
}
if ( buffers != IntPtr.Zero ) {
Marshal.FreeHGlobal( buffers );
buffers = IntPtr.Zero;
}
}
public unsafe void process( double[] left, double[] right, int length )
{
if ( left == null || right == null ) {
return;
}
//int length = Math.Min( left.Length, right.Length );
try {
initBuffer();
int remain = length;
int offset = 0;
float* left_ch = (float*)bufferLeft.ToPointer();
float* right_ch = (float*)bufferRight.ToPointer();
float** out_buffer = (float**)buffers.ToPointer();
out_buffer[0] = left_ch;
out_buffer[1] = right_ch;
while ( remain > 0 ) {
int proc = (remain > BUFLEN) ? BUFLEN : remain;
aEffect.ProcessReplacing( IntPtr.Zero, new IntPtr( out_buffer ), proc );
for ( int i = 0; i < proc; i++ ) {
left[i + offset] = left_ch[i];
right[i + offset] = right_ch[i];
}
remain -= proc;
offset += proc;
}
} catch ( Exception ex ) {
serr.println( "vstidrv#process; ex=" + ex );
}
}
public virtual void send( MidiEvent[] events )
{
unsafe {
MemoryManager mman = null;
try {
mman = new MemoryManager();
int nEvents = events.Length;
VstEvents* pVSTEvents = (VstEvents*)mman.malloc( sizeof( VstEvent ) + nEvents * sizeof( VstEvent* ) ).ToPointer();
pVSTEvents->numEvents = 0;
pVSTEvents->reserved = (VstIntPtr)0;
for ( int i = 0; i < nEvents; i++ ) {
MidiEvent pProcessEvent = events[i];
//byte event_code = (byte)pProcessEvent.firstByte;
VstEvent* pVSTEvent = (VstEvent*)0;
VstMidiEvent* pMidiEvent;
pMidiEvent = (VstMidiEvent*)mman.malloc( (int)(sizeof( VstMidiEvent ) + (pProcessEvent.data.Length + 1) * sizeof( byte )) ).ToPointer();
pMidiEvent->byteSize = sizeof( VstMidiEvent );
pMidiEvent->deltaFrames = 0;
pMidiEvent->detune = 0;
pMidiEvent->flags = 1;
pMidiEvent->noteLength = 0;
pMidiEvent->noteOffset = 0;
pMidiEvent->noteOffVelocity = 0;
pMidiEvent->reserved1 = 0;
pMidiEvent->reserved2 = 0;
pMidiEvent->type = VstEventTypes.kVstMidiType;
pMidiEvent->midiData[0] = (byte)(0xff & pProcessEvent.firstByte);
for ( int j = 0; j < pProcessEvent.data.Length; j++ ) {
pMidiEvent->midiData[j + 1] = (byte)(0xff & pProcessEvent.data[j]);
}
pVSTEvents->events[pVSTEvents->numEvents++] = (int)(VstEvent*)pMidiEvent;
}
aEffect.Dispatch( AEffectXOpcodes.effProcessEvents, 0, 0, new IntPtr( pVSTEvents ), 0 );
} catch ( Exception ex ) {
serr.println( "vstidrv#send; ex=" + ex );
} finally {
if ( mman != null ) {
try {
mman.dispose();
} catch ( Exception ex2 ) {
serr.println( "vstidrv#send; ex2=" + ex2 );
}
}
}
}
}
protected virtual VstIntPtr AudioMaster( ref AEffect effect, VstInt32 opcode, VstInt32 index, VstIntPtr value, IntPtr ptr, float opt )
{
VstIntPtr result = 0;
switch ( opcode ) {
case AudioMasterOpcodes.audioMasterVersion: {
result = Constants.kVstVersion;
break;
}
}
return result;
}
public FormPluginUi getUi( org.kbinani.windows.forms.BForm main_window )
{
if ( ui == null ) {
if ( main_window != null ) {
VoidDelegate temp = new VoidDelegate( this.createPluginUi );
if ( temp != null ) {
// mainWindowのスレッドで、uiが作成されるようにする
main_window.Invoke( temp );
}
}
}
return ui;
}
private void createPluginUi()
{
boolean hasUi = (aEffect.aeffect.flags & VstAEffectFlags.effFlagsHasEditor) == VstAEffectFlags.effFlagsHasEditor;
if ( !hasUi ) {
return;
}
if ( ui == null ) {
ui = new FormPluginUi();
}
if ( !isUiOpened ) {
// Editorを持っているかどうかを確認
if ( (aEffect.aeffect.flags & VstAEffectFlags.effFlagsHasEditor) == VstAEffectFlags.effFlagsHasEditor ) {
try {
// プラグインの名前を取得
String product = getStringCore( AEffectXOpcodes.effGetProductString, 0, VstStringConstants.kVstMaxProductStrLen );
ui.Text = product;
ui.Location = new System.Drawing.Point( 0, 0 );
unsafe {
aEffect.Dispatch( AEffectOpcodes.effEditOpen, 0, 0, ui.Handle, 0.0f );
}
//Thread.Sleep( 250 );
updatePluginUiRect();
ui.ClientSize = new System.Drawing.Size( uiWindowRect.width, uiWindowRect.height );
ui.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
isUiOpened = true;
} catch ( Exception ex ) {
serr.println( "vstidrv#open; ex=" + ex );
isUiOpened = false;
}
}
}
return;
}
public virtual void setSampleRate( int sample_rate )
{
sampleRate = sample_rate;
int ret1 = aEffect.Dispatch( AEffectOpcodes.effSetSampleRate, 0, 0, IntPtr.Zero, (float)sampleRate );
int ret2 = aEffect.Dispatch( AEffectOpcodes.effSetBlockSize, 0, sampleRate, IntPtr.Zero, 0 );
#if DEBUG
sout.println( "vstidrv#setSampleRate; ret1=" + ret1 + "; ret2=" + ret2 );
#endif
}
public virtual bool open( int block_size, int sample_rate )
{
dllHandle = win32.LoadLibraryExW( path, IntPtr.Zero, win32.LOAD_WITH_ALTERED_SEARCH_PATH );
if ( dllHandle == IntPtr.Zero ) {
serr.println( "vstidrv#open; dllHandle is null" );
return false;
}
mainProcPointer = win32.GetProcAddress( dllHandle, "main" );
mainDelegate = (PVSTMAIN)Marshal.GetDelegateForFunctionPointer( mainProcPointer,
typeof( PVSTMAIN ) );
if ( mainDelegate == null ) {
serr.println( "vstidrv#open; mainDelegate is null" );
return false;
}
audioMaster = new audioMasterCallback( AudioMaster );
if ( audioMaster == null ) {
serr.println( "vstidrv#open; audioMaster is null" );
return false;
}
aEffectPointer = IntPtr.Zero;
try {
aEffectPointer = mainDelegate( audioMaster );
} catch ( Exception ex ) {
serr.println( "vstidrv#open; ex=" + ex );
return false;
}
if ( aEffectPointer == IntPtr.Zero ) {
serr.println( "vstidrv#open; aEffectPointer is null" );
return false;
}
blockSize = block_size;
sampleRate = sample_rate;
aEffect = new AEffectWrapper();
aEffect.aeffect = (AEffect)Marshal.PtrToStructure( aEffectPointer, typeof( AEffect ) );
aEffect.Dispatch( AEffectOpcodes.effOpen, 0, 0, IntPtr.Zero, 0 );
int ret = aEffect.Dispatch( AEffectOpcodes.effSetSampleRate, 0, 0, IntPtr.Zero, (float)sampleRate );
#if DEBUG
sout.println( "vstidrv#open; dll_path=" + path + "; ret for effSetSampleRate=" + ret );
#endif
aEffect.Dispatch( AEffectOpcodes.effSetBlockSize, 0, blockSize, IntPtr.Zero, 0 );
// デフォルトのパラメータ値を取得
int num = aEffect.aeffect.numParams;
paramDefaults = new float[num];
for ( int i = 0; i < num; i++ ) {
paramDefaults[i] = aEffect.GetParameter( i );
}
return true;
}
private void updatePluginUiRect()
{
if ( ui != null ) {
try {
win32.EnumChildWindows( ui.Handle, enumChildProc, 0 );
} catch ( Exception ex ) {
serr.println( "vstidrv#updatePluginUiRect; ex=" + ex );
}
}
}
private bool enumChildProc( IntPtr hwnd, int lParam )
{
RECT rc = new RECT();
try {
win32.GetWindowRect( hwnd, ref rc );
} catch ( Exception ex ) {
serr.println( "vstidrv#enumChildProc; ex=" + ex );
}
if ( ui != null ) {
ui.childWnd = hwnd;
}
uiWindowRect = new Dimension( rc.right - rc.left, rc.bottom - rc.top );
return false; //最初のやつだけ検出できればおkなので
}
public virtual void close()
{
lock ( mSyncRoot ) {
#if TEST
sout.println( "vstidrv#close" );
#endif
if ( ui != null && !ui.IsDisposed ) {
ui.close();
}
try {
sout.println( "vstidrv#close; (aEffect==null)=" + (aEffect == null) );
if ( aEffect != null ) {
aEffect.Dispatch( AEffectOpcodes.effClose, 0, 0, IntPtr.Zero, 0.0f );
}
sout.println( "vstidrv#close; dllHandle=" + dllHandle );
if ( dllHandle != IntPtr.Zero ) {
win32.FreeLibrary( dllHandle );
}
aEffect = null;
dllHandle = IntPtr.Zero;
mainDelegate = null;
audioMaster = null;
} catch ( Exception ex ) {
serr.println( "vstidrv#close; ex=" + ex );
}
releaseBuffer();
}
}
~vstidrv()
{
lock ( mSyncRoot ) {
close();
}
}
}
}
#endif // !JAVA
#endif // ENABLE_VOCALOID
|