LoginSignup
16

More than 5 years have passed since last update.

ギター入力ができるまで

Posted at

注) @7ganoさん著書『iPhone Core Audio プログラミング』 のパクリ記事です。

Sample Code: github/GuitarInput


上にも書いた通りパクリ記事です。
本の中ではマイクからスピーカーのPlayThroughとして紹介されています。

ギターとiPhoneはApogee Jamを使いました。(iRigでも問題無く動きます)

説明する事がほとんど無いぐらい簡潔に書けてしまう。
ギター入力するためのコードがたった20行程度で書けてしまった。
けっこうすごいねAudioUnit。

- (void)prepareAUGraph {

    NewAUGraph(&_graph);
    AUGraphOpen(_graph);

    AudioComponentDescription cd;
    cd.componentType            = kAudioUnitType_Output;
    cd.componentSubType         = kAudioUnitSubType_RemoteIO;
    cd.componentManufacturer    = kAudioUnitManufacturer_Apple;
    cd.componentFlags           = 0;
    cd.componentFlagsMask       = 0;

    AUNode remoteIONode;
    AUGraphAddNode(_graph, &cd, &remoteIONode);

    AudioUnit remoteIOUnit;
    AUGraphNodeInfo(_graph, remoteIONode, &cd, &remoteIOUnit);

    UInt32 flag = 1;
    AudioUnitSetProperty(remoteIOUnit,
                         kAudioOutputUnitProperty_EnableIO,
                         kAudioUnitScope_Input,
                         1,
                         &flag,
                         sizeof(flag));

    AUGraphConnectNodeInput(_graph, remoteIONode, 1, remoteIONode, 0);

    AUGraphInitialize(_graph);

}

AUGraph作って、remoteIO用のAudioUnit作って、繋ぐだけ。
ポイントはremoteIOのenableIOスイッチをオンする所ぐらいでしょうか。

追伸

ギター入力してbluetoothスピーカーへの出力がしたかったのですができませんでした。
カテゴリをAVAudioSessionCategoryPlayAndRecordにするとbluetoothスピーカー読んでくれなくなります。
(パフォーマンスの問題で絞ってるのかも。ただでさえややラグだし)

ちなみにiOS6からカテゴリにAVAudioSessionCategoryMultiRouteができたのですが、
これでもbluetoothスピーカー出力できませんでした。
WWDC2012のセッション見るとHDMI(2ch)とイヤホン(2ch)の表記があるので、
いい音で聞きたかったらiRig+HDMIが良いのかな(できるのかは未確認)
ただそれだとノイズ出そうだし悩ましいw

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
16