AudioUnit简单的使用流程

#define kOutputBus 0

#define kInputBus 1

#define kSampleRate 8000

#define kFramesPerPacket 1

#define kChannelsPerFrame 1

#define kBitsPerChannel 16

#define SEND_PORT 9533

#define REC_PORT 9534

#define MAX_CLIENT_COUNT 20

#define BUFFER_SIZE 1024

@interface RemoteAudioProcessor : NSObject

@property (readonly) AudioComponentInstance audioUnit;

@property (readonly) AudioBuffer audioBuffer;

@property (strong, readwrite) NSMutableData *mIn;

@property (strong, readwrite) NSMutableData *mOut;

- (void)hasError:(int)statusCode file:(char*)file line:(int)line;

- (void)processBuffer: (AudioBufferList* )audioBufferList;

@end

//  RemoteAudioProcessor.m

//  AOIAudioSimulator

#import "RemoteAudioProcessor.h"

static NSMutableData *mIn;

static NSMutableData *mOut;

static bool mIsStarted; // audio unit start

static bool mSendServerStart; // send server continue loop

static bool mRecServerStart; // rec server continue loop

static bool mIsTele; // telephone call

static OSStatus recordingCallback(void *inRefCon,

AudioUnitRenderActionFlags *ioActionFlags,

const AudioTimeStamp *inTimeStamp,

UInt32 inBusNumber,

UInt32 inNumberFrames,

AudioBufferList *ioData) {

// the data gets rendered here

AudioBuffer buffer;

// a variable where we check the status

OSStatus status;

//This is the reference to the object who owns the callback.

RemoteAudioProcessor *audioProcessor = (__bridge RemoteAudioProcessor* )inRefCon;

/**

on this point we define the number of channels, which is mono

for the iphone. the number of frames is usally 512 or 1024.

*/

buffer.mDataByteSize = inNumberFrames * 2; // sample size

buffer.mNumberChannels = 1; // one channel

buffer.mData = malloc( inNumberFrames * 2 ); // buffer size

// we put our buffer into a bufferlist array for rendering

AudioBufferList bufferList;

bufferList.mNumberBuffers = 1;

bufferList.mBuffers[0] = buffer;

// render input and check for error

status = AudioUnitRender([audioProcessor audioUnit], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);

[audioProcessor hasError:status file:__FILE__ line:__LINE__];

// process the bufferlist in the audio processor

[audioProcessor processBuffer: &bufferList];

// clean up the buffer

free(bufferList.mBuffers[0].mData);

return noErr;

}

#pragma mark Playback callback

static OSStatus playbackCallback(void *inRefCon,

AudioUnitRenderActionFlags *ioActionFlags,

const AudioTimeStamp *inTimeStamp,

UInt32 inBusNumber,

UInt32 inNumberFrames,

AudioBufferList *ioData) {

long len = [mIn length];

len = len > 1024 ? 1024 : len;

if (len <= 0) {

return noErr;

}

// to be changed

//    RemoteAudioProcessor *audioProcessor = (__bridge RemoteAudioProcessor* )inRefCon;

for (int i = 0; i < ioData -> mNumberBuffers; i++) {

ASLog( @"len:%ld", len);

AudioBuffer buffer = ioData -> mBuffers[i];

NSData *pcmBlock = [mIn subdataWithRange: NSMakeRange(0, len)];

UInt32 size = (UInt32)MIN(buffer.mDataByteSize, [pcmBlock length]);// ? buffer.mDataByteSize : [pcmBlock length];

memcpy(buffer.mData, [pcmBlock bytes], size);

[mIn replaceBytesInRange: NSMakeRange(0, size) withBytes: NULL length: 0];

buffer.mDataByteSize = size;

}

return noErr;

}

@implementation RemoteAudioProcessor

@synthesize audioUnit;

@synthesize audioBuffer;

/*

* It's Singleton pattern

* the flow is init(if there isn't existed self) -> initializeAudioConfig(set audio format, io pipe and callback functions)

*                                              -> recordingCallback -> processBuffer

*                                              -> playbackCallback

*/

- (RemoteAudioProcessor* )init {

self = [super init];

if (self) {

[self initializeAudioConfig];

mIn = [[NSMutableData alloc] init];

mOut = [[NSMutableData alloc] init];

mIsStarted = false;

mSendServerStart = false;

mRecServerStart = false;

mIsTele = false;

[NSThread detachNewThreadSelector:@selector(initSendSocketServer)

toTarget:self

withObject:nil];

[NSThread detachNewThreadSelector:@selector(initRecSocketServer)

toTarget:self

withObject:nil];

}

return self;

}

- (void)initializeAudioConfig {

OSStatus status;

AudioComponentDescription desc;

desc.componentType = kAudioUnitType_Output; // we want to ouput

desc.componentSubType = kAudioUnitSubType_RemoteIO; // we want in and ouput

desc.componentFlags = 0; // must be zero

desc.componentFlagsMask = 0; // must be zero

desc.componentManufacturer = kAudioUnitManufacturer_Apple; // select provider

AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

status = AudioComponentInstanceNew(inputComponent, &audioUnit);

[self hasError:status file:__FILE__ line:__LINE__];

// define that we want record io on the input bus

UInt32 flag = 1;

status = AudioUnitSetProperty(audioUnit,

kAudioOutputUnitProperty_EnableIO, // use io

kAudioUnitScope_Input, // scope to input

kInputBus, // select input bus (1)

&flag, // set flag

sizeof(flag));

[self hasError:status file:__FILE__ line:__LINE__];

// define that we want play on io on the output bus

status = AudioUnitSetProperty(audioUnit,

kAudioOutputUnitProperty_EnableIO, // use io

kAudioUnitScope_Output, // scope to output

kOutputBus, // select output bus (0)

&flag, // set flag

sizeof(flag));

[self hasError:status file:__FILE__ line:__LINE__];

// specifie our format on which we want to work.

AudioStreamBasicDescription audioFormat;

audioFormat.mSampleRate = kSampleRate;

audioFormat.mFormatID = kAudioFormatLinearPCM;

audioFormat.mFormatFlags = kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;

audioFormat.mFramesPerPacket = kFramesPerPacket;

audioFormat.mChannelsPerFrame = kChannelsPerFrame;

audioFormat.mBitsPerChannel = kBitsPerChannel;

audioFormat.mBytesPerPacket = kBitsPerChannel * kChannelsPerFrame * kFramesPerPacket / 8;

audioFormat.mBytesPerFrame = kBitsPerChannel * kChannelsPerFrame / 8;

// set the format on the output stream

status = AudioUnitSetProperty(audioUnit,

kAudioUnitProperty_StreamFormat,

kAudioUnitScope_Output,

kInputBus,

&audioFormat,

sizeof(audioFormat));

[self hasError:status file:__FILE__ line:__LINE__];

// set the format on the input stream

status = AudioUnitSetProperty(audioUnit,

kAudioUnitProperty_StreamFormat,

kAudioUnitScope_Input,

kOutputBus,

&audioFormat,

sizeof(audioFormat));

[self hasError:status file:__FILE__ line:__LINE__];

/**

We need to define a callback structure which holds

a pointer to the recordingCallback and a reference to

the audio processor object

*/

AURenderCallbackStruct callbackStruct;

// set recording callback struct

callbackStruct.inputProc = recordingCallback; // recordingCallback pointer

callbackStruct.inputProcRefCon = (__bridge void * _Nullable)(self);

// set input callback to recording callback on the input bus

status = AudioUnitSetProperty(audioUnit,

kAudioOutputUnitProperty_SetInputCallback,

kAudioUnitScope_Global,

kInputBus,

&callbackStruct,

sizeof(callbackStruct));

[self hasError:status file:__FILE__ line:__LINE__];

// set playback callback struct

callbackStruct.inputProc = playbackCallback;

callbackStruct.inputProcRefCon = (__bridge void * _Nullable)(self);

// set playbackCallback as callback on our renderer for the output bus

status = AudioUnitSetProperty(audioUnit,

kAudioUnitProperty_SetRenderCallback,

kAudioUnitScope_Global,

kOutputBus,

&callbackStruct,

sizeof(callbackStruct));

[self hasError:status file:__FILE__ line:__LINE__];

// reset flag to 0

flag = 0;

/*

we need to tell the audio unit to allocate the render buffer,

that we can directly write into it.

*/

status = AudioUnitSetProperty(audioUnit,

kAudioUnitProperty_ShouldAllocateBuffer,

kAudioUnitScope_Output,

kInputBus,

&flag,

sizeof(flag));

/*

we set the number of channels to mono and allocate our block size to

1024 bytes.

kiki: I don't know where the size 1024 bytes comes from...

*/

audioBuffer.mNumberChannels = kChannelsPerFrame;

audioBuffer.mDataByteSize = 512 * 2;

audioBuffer.mData = malloc( 512 * 2 );

// Initialize the Audio Unit and cross fingers =)

status = AudioUnitInitialize(audioUnit);

[self hasError:status file:__FILE__ line:__LINE__];

}

- (void)processBuffer: (AudioBufferList* )audioBufferList {

AudioBuffer sourceBuffer = audioBufferList -> mBuffers[0];

// we check here if the input data byte size has changed

if (audioBuffer.mDataByteSize != sourceBuffer.mDataByteSize) {

// clear old buffer

free(audioBuffer.mData);

// assing new byte size and allocate them on mData

audioBuffer.mDataByteSize = sourceBuffer.mDataByteSize;

audioBuffer.mData = malloc(sourceBuffer.mDataByteSize);

}

// copy incoming audio data to the audio buffer

memcpy(audioBuffer.mData, audioBufferList -> mBuffers[0].mData, audioBufferList -> mBuffers[0].mDataByteSize);

NSData *pcmBlock = [NSData dataWithBytes:sourceBuffer.mData length:sourceBuffer.mDataByteSize];

[mOut appendData: pcmBlock];

//

}

- (void)start {

if (mIsStarted) {

ASLog( @"-- already start --");

return;

}

ASLog( @"-- start --");

mIsStarted = true;

[mIn replaceBytesInRange: NSMakeRange(0, [mIn length]) withBytes: NULL length: 0];

[mOut replaceBytesInRange: NSMakeRange(0, [mOut length]) withBytes: NULL length: 0];

OSStatus status = AudioOutputUnitStart(audioUnit);

[self hasError:status file:__FILE__ line:__LINE__];

}

- (void)stop {

ASLog( @"-- stop --");

OSStatus status = AudioOutputUnitStop(audioUnit);

[self hasError:status file:__FILE__ line:__LINE__];

mIsStarted = false;

[mIn replaceBytesInRange: NSMakeRange(0, [mIn length]) withBytes: NULL length: 0];

[mOut replaceBytesInRange: NSMakeRange(0, [mOut length]) withBytes: NULL length: 0];

}

#pragma mark Error handling

- (void)hasError:(int)statusCode file:(char*)file line:(int)line {

if (statusCode) {

ASLog(@"Error Code responded %d in file %s on line %d", statusCode, file, line);

exit(-1);

}

}

#pragma mark Socket Implementation

- (int)initSendSocketServer {

struct sockaddr_in server_addr;

bzero(&server_addr, sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = htons(INADDR_ANY);

server_addr.sin_port = htons(SEND_PORT);

int server_socket = socket(AF_INET, SOCK_STREAM, 0);

if (server_socket < 0) {

ASLog( @"Create send socket failed");

return -1;

}

int yes = 1;

setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {

[self closeSocket:server_socket error:@"Send Server bind failed"];

}

if (listen(server_socket, MAX_CLIENT_COUNT)) {

[self closeSocket:server_socket error:@"Send Server listen failed"];

}

mSendServerStart = true;

while (mSendServerStart) {

ASLog( @"wait send client");

struct sockaddr_in client_addr;

socklen_t length = sizeof(client_addr);

int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);

if (client_socket < 0) {

ASLog( @"Create send client socket failed");

break;

}

[NSThread detachNewThreadSelector:@selector(sendClientRun:)

toTarget:self

withObject:[NSNumber numberWithInt:client_socket]];

}

close(server_socket);

return 0;

}

- (int)initRecSocketServer {

struct sockaddr_in server_addr;

bzero(&server_addr, sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = htons(INADDR_ANY);

server_addr.sin_port = htons(REC_PORT);

int server_socket = socket(AF_INET, SOCK_STREAM, 0);

if (server_socket < 0) {

ASLog( @"Create rec socket failed");

return -1;

}

int yes = 1;

setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {

[self closeSocket:server_socket error:@"Rec Server bind failed"];

}

if (listen(server_socket, MAX_CLIENT_COUNT)) {

[self closeSocket:server_socket error:@"Rec Server listen failed"];

}

mSendServerStart = true;

while (mSendServerStart) {

ASLog( @"wait rec client");

struct sockaddr_in client_addr;

socklen_t length = sizeof(client_addr);

int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);

if (client_socket < 0) {

ASLog( @"Create rec client socket failed");

break;

}

[NSThread detachNewThreadSelector:@selector(recClientRun:)

toTarget:self

withObject:[NSNumber numberWithInt:client_socket]];

}

close(server_socket);

return 0;

}

- (int)closeSocket: (int)socket

error: (NSString *)errorMsg {

if (errorMsg) {

ASLog(@"%@", errorMsg);

close(socket);

return -1;

} else {

close(socket);

return 0;

}

}

- (void)stopSocketServer {

mSendServerStart = false;

mRecServerStart = false;

}

- (void)sendClientRun: (NSNumber *)socket {

int client_socket = [socket intValue];

[self stop];

[self start];

ASLog( @"send client connection with %d", client_socket);

bool isClientAlive = true;

while (isClientAlive) {

if ([mOut length] <= 0) {

continue;

}

ssize_t res = send(client_socket, [mOut bytes], [mOut length], 0);

if (res > 0) {

[mOut replaceBytesInRange: NSMakeRange(0, res) withBytes: NULL length: 0];

} else {

isClientAlive = false;

[self closeSocket: client_socket error: @"send socket died"];

}

}

}

- (void)recClientRun: (NSNumber *)socket {

char buffer[BUFFER_SIZE];

int client_socket = [socket intValue];

ASLog( @"rec client connection with %d", client_socket);

bool isClientAlive = true;

while (isClientAlive) {

ssize_t res = recv(client_socket, buffer, BUFFER_SIZE, 0);

if (res > 0) {

NSData* pcmBlock = [[NSData alloc] initWithBytes: buffer length: res];

[mIn appendBytes: (__bridge const void * _Nonnull)(pcmBlock) length: res];

ASLog( @"rec:%zd", res);

} else {

isClientAlive = false;

[self closeSocket: client_socket error: @"rec socket died"];

}

}

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,951评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,606评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,601评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,478评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,565评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,587评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,590评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,337评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,785评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,096评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,273评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,935评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,578评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,199评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,440评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,163评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,133评论 2 352

推荐阅读更多精彩内容