Audio QueueでSin波再生

Post on 13-Jan-2015

1.533 views 2 download

description

Audio QueueでSin波再生。 第49回 Cocoa勉強会関西の発表資料。

Transcript of Audio QueueでSin波再生

Audio QueueでSin波再生

2013.03.30ふじしげ ゆういち@nakiwo

• ふじしげ ゆういち• @nakiwo• 株式会社フィードテイラーhttp://feedtailor.jp/

そら案内

8bitter

• ふじしげ ゆういち• @nakiwo• http://www.nakiwo.com/

洞窟物語

めがね(Mac AppStore)

今日のテーマ

• Audio Queue Services でSin波を再生する

• https://github.com/nakiwo/AudioQueueToneSample

音の再生• System Sound Services • AVAudioPlayer• Audio Unit• Open AL• Audio Queue Services

Audio Queue Services

• Core Audio• AudioToolbox

Audio Queue Services

•なにがうれしいか

Audio Queue Services

•めんどくさいだけかも!

Audio Queue Services

•単純にサウンドファイルを鳴らすだけなら他の方法をお勧めします。

Audio Queue Services

•録音と再生。今日は再生だけ。

Audio Queue

•オーディオのキュー

Audio Queue

• Audio BufferのQueue

Audio Buffer

•波形のバッファ

•波形バッファに音を詰めてキューに放り込むと音が鳴る

波形データのフォーマット

http://upload.wikimedia.org/wikipedia/commons/b/bf/Pcm.svg

Linear PCM

サンプリングレート:時間軸の解像度

量子化bit数:1サンプルのbit数

サンプル

フレーム

L

R

パケット• フォーマット上都合のよいデータの区切り• Linear PCMは1パケット1フレーム

Packet

L

R

LPCM

Packet

L

R

L

R

L

R

L

R

AAC等

Audio Queueで再生• AudioQueueNewOutput• AudioQueueAllocateBuffer• AudioQueueEnqueueBuffer• AudioQueueStart•コールバックが呼ばれるのでバッファを詰める

AudioQueueNewOutput

•再生するデータのフォーマットとコールバックを登録

AudioStreamBasicDescription _outpufFormat;

_outpufFormat.mFormatID = kAudioFormatLinearPCM;_outpufFormat.mSampleRate = 44100;_outpufFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked;_outpufFormat.mBitsPerChannel = 16;_outpufFormat.mChannelsPerFrame = 1;_outpufFormat.mBytesPerFrame = 2;_outpufFormat.mFramesPerPacket = 1;_outpufFormat.mBytesPerPacket = 2;

AudioStreamBasicDescription

AudioQueueNewOutput(&_outpufFormat, audioQueueOutputCallback, (__bridge void *)self, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &_audioQueue);

UInt32 bufferSize = kFramesPerBuffer * _outpufFormat.mBytesPerFrame;

for (int i = 0; i < kNumBuffers; i++) { AudioQueueBufferRef buffer; AudioQueueAllocateBuffer(_audioQueue, bufferSize, &buffer);

[self audioQueueOutputWithAudioQueue:_audioQueue buffer:buffer];}

AudioQueueStart(_audioQueue, NULL);

static void audioQueueOutputCallback( void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer){ AppDelegate *object = (__bridge AppDelegate *)inUserData;

[object audioQueueOutputWithAudioQueue:inAQ buffer:inBuffer];}

再生コールバック

- (void)audioQueueOutputWithAudioQueue:(AudioQueueRef)inAQ buffer:(AudioQueueBufferRef)inBuffer{ inBuffer->mAudioDataByteSize = _outpufFormat.mBytesPerFrame * kFramesPerBuffer;

float freq = 440.0f; float phasePerSample = freq / kSampleRate; int16_t *sampleBuffer = (int16_t *)inBuffer->mAudioData;

for (int i = 0; i < kFramesPerBuffer; i++) { *sampleBuffer = (int16_t)((sinf((float)_frameIndex * phasePerSample * (M_PI*2.0f))) * 32767.0f); sampleBuffer++;

_frameIndex++; }

AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);}

• Audio Queue Services Programming Guide

•WWDC 2010 - Fundamentals of Digital Audio for Mac OS X and iPhone OS

• iPhone Core Audioプログラミング」•これを読めば無敵。しかし入手困難

おわり