1
4

More than 5 years have passed since last update.

JUCEのStandalone版AudioProcessorでバッファサイズを変更する。

Posted at

JUCE + Projucerでソフトシンセのプラグインを作ると、スタンドアロン版もProjucerの設定でチェックを入れるだけで作成できて便利だが、オーディオデバイスの設定をシンセのUIから行いたい場合にどうしたらよいか疑問だった。これはStandalonePluginHolderクラスを使って行うことが可能だとわかった。この方法を使えばデバイス設定のUIが(デフォルトでは)用意されていないiOS版のアプリでも独自に設定を追加することが簡単にできる。

仕上がりイメージ

取ってつけすぎだがこんな感じのUIを作る。ComboBoxには64,128,256,512,1024くらいの値を羅列しておく。
Simulator Screen Shot - iPhone X - 2019-01-07 at 00.48.28.png

StandalonePluginHolderのインスタンスを取得

スタンドアロン版のアプリはStandalonePluginHolderをインスタンスとして保持していて、これがシングルトンでgetInstance()メソッドで取得できる。ただし、使用するにはヘッダファイルを下記のように指定しておく必要がある。

MyEditor.cpp
#include <juce_audio_plugin_client/Standalone/juce_StandaloneFilterWindow.h>

UIのクラスのコンストラクタに下記のように記述。

MyEditor.cpp
    //[UserPreSize]
    StandalonePluginHolder* holder = StandalonePluginHolder::getInstance();
    if (holder != nullptr)
    {
        AudioDeviceManager::AudioDeviceSetup setup = holder->deviceManager.getAudioDeviceSetup();
        for (auto i = 0; i < comboBufferSize->getNumItems(); i++)
        {
            if (comboBufferSize->getItemText(i).getIntValue() == setup.bufferSize)
            {
                comboBufferSize->setSelectedItemIndex(i);
                break;
            }
        }
    }
    else
        comboBufferSize->setEnabled(false);
    //[/UserPreSize]

StandalonePluginHolderのインスタンスがnullptrでなかったら、スタンドアロンとして起動されていることになる。ただし、スタンドアロンであっても少なくともAudioProcessorのインスタンスが作られるまではnullptrが返ってくるので注意。Editorのコンストラクタでは少なくとも今使ってみてる感じでは作成完了している様子。

StandalonePluginHolderはAudioDeviceManagerを持っているので、あとはそれを使ってバッファサイズの取得なり設定なりすればいい。特定の値だけを変更するには一度AudioDeviceManager::getAudioDeviceSetup()で取得してから変更したい値だけ(この例ではバッファサイズ)を書き換えてAudioDeviceManager::setAudioDeviceSetup()でセットする。2番目の引数はtrueにしておく。

MyEditor.cpp
    if (comboBoxThatHasChanged == comboBufferSize.get())
    {
        //[UserComboBoxCode_comboBufferSize] -- add your combo box handling code here..
        String text = comboBoxThatHasChanged->getItemText(comboBoxThatHasChanged->getSelectedItemIndex());
        int bufferSize = text.getIntValue();
        StandalonePluginHolder* holder = StandalonePluginHolder::getInstance();
        if (holder != nullptr)
        {
            AudioDeviceManager::AudioDeviceSetup setup = holder->deviceManager.getAudioDeviceSetup();
            setup.bufferSize = bufferSize;
            holder->deviceManager.setAudioDeviceSetup(setup, true);
        }
        //[/UserComboBoxCode_comboBufferSize]
    }
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4