ラベル Core Audio の投稿を表示しています。 すべての投稿を表示
ラベル Core Audio の投稿を表示しています。 すべての投稿を表示

2013年4月6日土曜日

Core Audio を学ぶメモ(編集中)

Core Audio について詳しくなりたい。

・参考にしているWebサイト

My Codex Leicester     Mac, iOS サンプルや知識も充実しています

Objective-Audio     こちらもサンプルが充実, 細かいデータの設定なども載っています

この2つのwebサイトは本当にすばらしいです。大変お世話になりました。

Apple Dev のサンプル  -  正攻法で, Apple の Developer のサイトでサンプルをがんばって探す

Stack Overflow     いつもおせわになっているプログラミング関係のQ&Aサイト

・書籍 iPhone Core Audio プログラミング    やっと手に入りました。細かいところまで結構書いている有名な本です

Learning Core Audio  英語です。ちらっと読んでみましたが, 上の本の方が強力です


・資料

•ツール
Audacity  フリーのオーディオソフト, Mac 版, Win版とある, 音声処理, 信号処理などかなり豊富な機能がついています。よくテストで使っていました。しかもオープンソース(C++)。ソースが読めます。信号処理の参考に使えるかも 。でもソースは結構読みづらいC++ です。

ここにサンプルを追加していきます。(編集中)
1. iOS 音声再生(AVAudioPlayer)   (iOS, Mac OS X)

2. byte -> short -> byte そしてfloat(Objective-C) ( iOS, Mac OS X)

3. iOS 音声録音(AVAudioRecorder) ( iOS, Mac OS X)

4. NSError, OSStatus用のログをつくる (iOS, Mac OS X)

5. Audio Unitを使おう (iOS)

・仕事記・開発記

(編集中)






2013年2月11日月曜日

Audio Unitを使おう(iOS)

まだまだ勉強がたりないです。さて, Audio Unitです。

Audio Unit というのは, Core Audio のオーディオ処理の機能を持つ1つの単位のこと。Input, Effect, Output などが Audio Unit になります。 詳しい説明は, 各所ありますのでこれくらいです。

Mac OS X と, iOS ではプログラムの書き方が違います。
Mac OS Xでは, こちら のサンプルが完璧です。

これを参考にさせていただきながら, iOSとどのように違うのか, やってみました。

My Codex Leister さんでも言及のとおり, iOSでは, kAudioUnitType_Generatorつまり, 入力を発生させるような Audio Unit がないような気がします。

Output, Effect あたりはできそうな気がします。
コンポーネントの違い

Mac OS X     AudioUnit.framework      ComponentDescription
iOS              AudioToolbox.framework AudioComponentDescripton

iOSでは,

struct AudioComponentDescription
{
    OSType  componentType;
    OSType  componentSubStype;
    OSType  componentManufacturer;
    UInt32  componentFlags;
    UInt32  componentFlagsMask;
};
typedef struct AudioComponentDescription AudioComponentDescription;

を使って, AudioComponent を取り出します。
その違い以外は, ほぼ Mac OS Xと同じコードです。

iOS で, Audio Unit で sin波を発生させて聞いてみるサンプルです。
重要そうなところは太字もしくは赤字にしてみました。

•AudioUnitViewController.h

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>

#define kRate 44100
#define kFreq 440

@interface AudioUnitViewController : ViewController

@property (nonatomic)AudioUnit unit;
@property (nonatomic)float phase;

- (IBAction)startClick:(id)sender;
- (IBAction)stopClick:(id)sender;
@end


•AudioUnitViewController.m

#import "AudioUnitViewController.h"

@interface AudioUnitViewController ()

@end

@implementation AudioUnitViewController

@synthesize unit;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

OSStatus AuRenderCallback(void *inRefCon,
                          AudioUnitRenderActionFlags *ioActionFlags,
                          const AudioTimeStamp *inTimeStamp,
                          UInt32 inBusNumber,
                          UInt32 inNumberFrames,
                          AudioBufferList *ioData)
{
    //NSLog(@"inNumberFrames: %ld", inNumberFrames);
    //NSLog(@"Bufs %ld", ioData->mNumberBuffers);
    
    AudioUnitViewController *controller = (__bridge AudioUnitViewController *)inRefCon;
    
    float *buf = ioData->mBuffers[0].mData;
    float freq = kFreq * 2 * M_PI / kRate;
    
    for ( int i=0; i < inNumberFrames; i++ )
    {
        float wave = sin(controller.phase);
        *buf++ = wave;
        controller.phase += freq;
    }
    return noErr;
}


-(void)audioUnit
{
    OSStatus status;
    AudioComponentDescription cd;
    cd.componentType = kAudioUnitType_Output;
    cd.componentSubType = kAudioUnitSubType_RemoteIO;
    cd.componentManufacturer = kAudioUnitManufacturer_Apple;
    cd.componentFlags = 0;
    cd.componentFlagsMask = 0;
    
    // Find compoent and create instance
    AudioComponent comp  = AudioComponentFindNext(NULL, &cd);
    status = AudioComponentInstanceNew(comp, &unit);
    
    // Set Callback
    AURenderCallbackStruct input;
    input.inputProc = AuRenderCallback;
    input.inputProcRefCon = (__bridge void *)(self);
    
    status = AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &input, sizeof(input));
    
    
    status = AudioUnitInitialize(unit);
    status = AudioOutputUnitStart(unit);
    self.phase = 0;
}

-(void)stop
{
    AudioOutputUnitStop(unit);
}

- (IBAction)startClick:(id)sender
{
    [self audioUnit];
}

- (IBAction)stopClick:(id)sender
{
    [self stop];
}
@end