1717#include " pluginterfaces/vst/ivstaudioprocessor.h"
1818#include " pluginterfaces/vst/ivsteditcontroller.h"
1919#include " pluginterfaces/gui/iplugview.h"
20+ #include " public.sdk/source/common/memorystream.h"
21+ // memorystream.cpp が libsdk.a に含まれていないので、この memorystream.cpp をインクルードして直接コンパイルする。
22+ // この方法はちょっと横着なので、ここで memorystream.cpp を直接インクルードするよりも、
23+ // memorystream.cpp を IDE のプロジェクトに追加してちゃんとコンパイルするようにしたほうが良い。
24+ #include " public.sdk/source/common/memorystream.cpp"
2025#include " CEditorHost.h"
2126
2227using namespace Steinberg ;
@@ -547,17 +552,25 @@ - (IBAction)testVST:(id)sender {
547552
548553 // getControllerClassId()で取得したEditControllerのCIDを使ってcreateInstance()する
549554 TUID controllerClassId;
555+ Vst::IEditController *editController = NULL ;
556+ bool needToInitializeEditController = false ;
550557 res = iComponent->getControllerClassId (controllerClassId);
551558 if (res != kResultOk ){
552- NSLog (@" failed with getControllerClassId(fx) with %d " , res);
553- continue ;
559+ res = iComponent->queryInterface (Vst::IEditController::iid, (void **)&editController);
560+ if (res != kResultOk ) {
561+ NSLog (@" failed with getControllerClassId(fx) with %d " , res);
562+ continue ;
563+ }
564+ } else {
565+ needToInitializeEditController = true ;
566+ res = pluginFactory->createInstance (controllerClassId, Vst::IEditController::iid,
567+ (void **)&editController);
554568 }
555-
556- Vst::IEditController *editController = NULL ;
557- res = pluginFactory->createInstance (controllerClassId, Vst::IEditController::iid,
558- (void **)&editController);
569+
559570 if (editController){
560- [self IEditorControllerObtained: editController];
571+ [self IEditorControllerObtained: editController
572+ withComponent: iComponent
573+ needToInitialize: needToInitializeEditController];
561574 }else {
562575 NSLog (@" failed for createInstance for controller, IEditController. res = %d " , res);
563576 continue ;
@@ -581,18 +594,23 @@ - (IBAction)testVST:(id)sender {
581594}
582595
583596//
584- -(void )IEditorControllerObtained : (Vst::IEditController *)editorController {
597+ -(void )IEditorControllerObtained : (Vst::IEditController *)editorController
598+ withComponent : (Vst::IComponent *)component
599+ needToInitialize : (bool )needToInitialize
600+ {
585601 tresult res = 0 ;
586-
587- // 適当なやつを渡す。
588- res = editorController->initialize (&hostApplication);
589- if (res != kResultOk ){
590- NSLog (@" failed with initialize" );
591- return ;
592- }else {
593- NSLog (@" OK to initialize" );
602+
603+ if (needToInitialize) {
604+ // 適当なやつを渡す。
605+ res = editorController->initialize (&hostApplication);
606+ if (res != kResultOk ){
607+ NSLog (@" failed with initialize" );
608+ return ;
609+ }else {
610+ NSLog (@" OK to initialize" );
611+ }
594612 }
595-
613+
596614 // 適当なやつを渡す。
597615 res = editorController->setComponentHandler (&componentHandler);
598616 if (res != kResultOk ){
@@ -601,6 +619,35 @@ -(void)IEditorControllerObtained:(Vst::IEditController *)editorController{
601619 }else {
602620 NSLog (@" OK to setComponentHandler" );
603621 }
622+
623+ // IComponent と IEditController の相互接続を確立する
624+ Steinberg::FUnknownPtr<Vst::IConnectionPoint> connForComponent (component);
625+ Steinberg::FUnknownPtr<Vst::IConnectionPoint> connForEditController (editorController);
626+
627+ if ( connForComponent && connForEditController) {
628+ connForComponent->connect (connForEditController);
629+ connForEditController->connect (connForComponent);
630+ } else {
631+ NSLog (@" failed to get connection points" );
632+ }
633+
634+ // IEditController の状態を IComponent の状態を元に初期化する。
635+ Steinberg::MemoryStream stream;
636+
637+ if (component->getState (&stream) != kResultOk ) {
638+ NSLog (@" failed to get component state" );
639+ return ;
640+ }
641+
642+ stream.seek (0 , Steinberg::IBStream::IStreamSeekMode::kIBSeekSet , 0 );
643+ res = editorController->setComponentState (&stream);
644+
645+ // JUCE 製のプラグインでは、 setComponentState() の呼び出しで
646+ // kResultOk ではなく kNotImplemented が返ってくることがある。
647+ if (res != kResultOk && res != kNotImplemented ) {
648+ NSLog (@" failed to set component state to IEditController" );
649+ return ;
650+ }
604651
605652 // ここまではエラーなしで来ることを確認済み。
606653 int32 c = editorController->getParameterCount ();
0 commit comments