title: iOS音频编程之混音

date: 2017-04-19

tags: Audio Unit,AUGraph, Mixer,混音

博客地址

iOS音频编程之混音

需求:多个音频源混合后输出,

项目说明:项目中采样4路音频源混合,音频源包含44100hz采样率,3000hz采样率,单声道和立体声;使用MixerVoiceHandle封装混音处理,用户只需要初始化音频文件路径数组,调用启动混音接口,就可实现多路音频混合输出

AVAudioSession设置

没有把对AVAudioSession的设置封装进MixerVoiceHandle中,用户的app可能对Session会有不同的设置(如录音),对于混音只要保证session能播放,bufferDuration和采样率为MixerVoiceHandle申请的一样即可

AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];

[sessionInstance setCategory:AVAudioSessionCategoryPlayback error:&error];

handleError(error);

NSTimeInterval bufferDuration = kSessionBufDuration;

[sessionInstance setPreferredIOBufferDuration:bufferDuration error:&error];

handleError(error);

double hwSampleRate = kGraphSampleRate;

[sessionInstance setPreferredSampleRate:hwSampleRate error:&error];

handleError(error);

//接下来设置AVAudioSessionInterruptionNotification和AVAudioSessionRouteChangeNotification,省略

kSessionBufDuration为0.05s,kGraphSampleRate44100hz,

session的IOBufferDuration的意思就是在各Audio Unit的回调函数中提供0.005s时间的数据,如录音时,采集到0.005s的数据会进入一次回调函数

读取音频数据

读取音频数据到内存中,耗时比较久,放到后台线程中执行,而初始化AUGraph时,用到了读取出的音频信息,所以干脆将读取音频数据,混音设置都放在了一个后台的串行队列中。

_mSoundBufferP = (SoundBufferPtr)malloc(sizeof(SoundBuffer) * self.sourceArr.count);

for (int i = 0; i < self.sourceArr.count; i++) {

NSLog(@"read Audio file : %@",self.sourceArr[i]);

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)self.sourceArr[i], kCFURLPOSIXPathStyle, false);

ExtAudioFileRef fp;

//open the audio file

CheckError(ExtAudioFileOpenURL(url, &fp), "cant open the file");

AudioStreamBasicDescription fileFormat;

UInt32 propSize = sizeof(fileFormat);

//read the file data format , it represents the file's actual data format.

CheckError(ExtAudioFileGetProperty(fp, kExtAudioFileProperty_FileDataFormat,

&propSize, &fileFormat),

"read audio data format from file");

double rateRatio = kGraphSampleRate/fileFormat.mSampleRate;

UInt32 channel = 1;

if (fileFormat.mChannelsPerFrame == 2) {

channel = 2;

}

AVAudioFormat *clientFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32

sampleRate:kGraphSampleRate

channels:channel

interleaved:NO];

propSize = sizeof(AudioStreamBasicDescription);

//设置从文件中读出的音频格式

CheckError(ExtAudioFileSetProperty(fp, kExtAudioFileProperty_ClientDataFormat,

propSize, clientFormat.streamDescription),

"cant set the file output format");

//get the file's length in sample frames

UInt64 numFrames = 0;

propSize = sizeof(numFrames);

CheckError(ExtAudioFileGetProperty(fp, kExtAudioFileProperty_FileLengthFrames,

&propSize, &numFrames),

"cant get the fileLengthFrames");

numFrames = numFrames * rateRatio;

_mSoundBufferP[i].numFrames = (UInt32)numFrames;

_mSoundBufferP[i].channelCount = channel;

_mSoundBufferP[i].asbd = *(clientFormat.streamDescription);

_mSoundBufferP[i].leftData = (Float32 *)calloc(numFrames, sizeof(Float32));

if (channel == 2) {

_mSoundBufferP[i].rightData = (Float32 *)calloc(numFrames, sizeof(Float32));

}

_mSoundBufferP[i].sampleNum = 0;

//如果是立体声,还要多为AudioBuffer申请一个空间存放右声道数据

AudioBufferList *bufList = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer)*(channel-1));

AudioBuffer emptyBuffer = {0};

for (int arrayIndex = 0; arrayIndex < channel; arrayIndex++) {

bufList->mBuffers[arrayIndex] = emptyBuffer;

}

bufList->mNumberBuffers = channel;

bufList->mBuffers[0].mNumberChannels = 1;

bufList->mBuffers[0].mData = _mSoundBufferP[i].leftData;

bufList->mBuffers[0].mDataByteSize = (UInt32)numFrames*sizeof(Float32);

if (2 == channel) {

bufList->mBuffers[1].mNumberChannels = 1;

bufList->mBuffers[1].mDataByteSize = (UInt32)numFrames*sizeof(Float32);

bufList->mBuffers[1].mData = _mSoundBufferP[i].rightData;

}

UInt32 numberOfPacketsToRead = (UInt32) numFrames;

CheckError(ExtAudioFileRead(fp, &numberOfPacketsToRead,

bufList),

"cant read the audio file");

free(bufList);

ExtAudioFileDispose(fp);

}

这段代码就是把音频文件以设置的kExtAudioFileProperty_ClientDataFormat音频格式,读出到_mSoundBufferP数组中

如果您想使用自己准备的音频文件,ExtAudioFileRead读取时返回-50的code,一般是设置读出的目的音频格式(kExtAudioFileProperty_ClientDataFormat)不正确,如源文件是单声道,而想读出的目的格式是立体声

混音设置

CheckError(NewAUGraph(&_mGraph), "cant new a graph");

AUNode mixerNode;

AUNode outputNode;

AudioComponentDescription mixerACD;

mixerACD.componentType = kAudioUnitType_Mixer;

mixerACD.componentSubType = kAudioUnitSubType_MultiChannelMixer;

mixerACD.componentManufacturer = kAudioUnitManufacturer_Apple;

mixerACD.componentFlags = 0;

mixerACD.componentFlagsMask = 0;

AudioComponentDescription outputACD;

outputACD.componentType = kAudioUnitType_Output;

outputACD.componentSubType = kAudioUnitSubType_RemoteIO;

outputACD.componentManufacturer = kAudioUnitManufacturer_Apple;

outputACD.componentFlags = 0;

outputACD.componentFlagsMask = 0;

CheckError(AUGraphAddNode(_mGraph, &mixerACD,

&mixerNode),

"cant add node");

CheckError(AUGraphAddNode(_mGraph, &outputACD,

&outputNode),

"cant add node");

CheckError(AUGraphConnectNodeInput(_mGraph, mixerNode, 0, outputNode, 0),

"connect mixer Node to output node error");

CheckError(AUGraphOpen(_mGraph), "cant open the graph");

CheckError(AUGraphNodeInfo(_mGraph, mixerNode,

NULL, &_mMixer),

"generate mixer unit error");

CheckError(AUGraphNodeInfo(_mGraph, outputNode, NULL, &_mOutput),

"generate remote I/O unit error");

UInt32 numberOfMixBus = (UInt32)self.sourceArr.count;

//配置混音的路数,有多少个音频文件要混音

CheckError(AudioUnitSetProperty(_mMixer, kAudioUnitProperty_ElementCount, kAudioUnitScope_Input, 0,

&numberOfMixBus, sizeof(numberOfMixBus)),

"set mix elements error");

// Increase the maximum frames per slice allows the mixer unit to accommodate the

// larger slice size used when the screen is locked.

UInt32 maximumFramesPerSlice = 4096;

CheckError( AudioUnitSetProperty (_mMixer,

kAudioUnitProperty_MaximumFramesPerSlice,

kAudioUnitScope_Global,

0,

&maximumFramesPerSlice,

sizeof (maximumFramesPerSlice)

), "cant set kAudioUnitProperty_MaximumFramesPerSlice");

for (int i = 0; i < numberOfMixBus; i++) {

// setup render callback struct

AURenderCallbackStruct rcbs;

rcbs.inputProc = &renderInput;

rcbs.inputProcRefCon = _mSoundBufferP;

CheckError(AUGraphSetNodeInputCallback(_mGraph, mixerNode, i, &rcbs),

"set mixerNode callback error");

AVAudioFormat *clientFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32

sampleRate:kGraphSampleRate

channels:_mSoundBufferP[i].channelCount

interleaved:NO];

CheckError(AudioUnitSetProperty(_mMixer, kAudioUnitProperty_StreamFormat,

kAudioUnitScope_Input, i,

clientFormat.streamDescription, sizeof(AudioStreamBasicDescription)),

"cant set the input scope format on bus[i]");

}

double sample = kGraphSampleRate;

CheckError(AudioUnitSetProperty(_mMixer, kAudioUnitProperty_SampleRate,

kAudioUnitScope_Output, 0,&sample , sizeof(sample)),

"cant the mixer unit output sample");

//未设置mixer unit 的kAudioUnitScope_Output的0的音频格式(AudioComponentDescription) 未设置io unit kAudioUnitScope_Output 的element 1的输出AudioComponentDescription

//CheckError(AudioUnitSetProperty(_mMixer, kAudioUnitProperty_StreamFormat,

//kAudioUnitScope_Output, 0, xxxx, sizeof(AudioStreamBasicDescription)), "xxx");

CheckError(AUGraphInitialize(_mGraph), "cant initial graph");

新建AUGraph->新建AUNode(混音Node,音频输出Node)->将混音Node和音频输出Node连接(连接后,混音后的输出直流入音频输出的Audio Unit)->从AUNode中得到相应的Audio Unit->设置Mixer Audio Unit的混音路数->设置各路混音的回调函数,输入的音频格式->设置混音个输出采样率->Initialize AUGraph

Audio Unit

这张图片是一个Audio Unit; 相对于混音的Unit(type是kAudioUnitType_Mixer,subType是kAudioUnitSubType_MultiChannelMixer),我个人理解是这样的

左边是Mixer Unit,右边是Remote I/O Unit,在Mixer Unit的Input Scope下,有多少个Element(Bus),由kAudioUnitProperty_ElementCount来设置,并分别为Mixer Unit的Input Scope下的各个Element(Bus)设置音频格式和输入回调;将音频源合成到Mixer Unit的Output Scope的Element 0上。

混音输入回调

static OSStatus renderInput(void *inRefCon,

AudioUnitRenderActionFlags *ioActionFlags,

const AudioTimeStamp *inTimeStamp,

UInt32 inBusNumber, UInt32 inNumberFrames,

AudioBufferList *ioData)

{

SoundBufferPtr sndbuf = (SoundBufferPtr)inRefCon;

UInt32 sample = sndbuf[inBusNumber].sampleNum; // frame number to start from

UInt32 bufSamples = sndbuf[inBusNumber].numFrames; // total number of frames in the sound buffer

Float32 *leftData = sndbuf[inBusNumber].leftData; // audio data buffer

Float32 *rightData = nullptr;

Float32 *outL = (Float32 *)ioData->mBuffers[0].mData; // output audio buffer for L channel

Float32 *outR = nullptr;

if (sndbuf[inBusNumber].channelCount == 2) {

outR = (Float32 *)ioData->mBuffers[1].mData; //out audio buffer for R channel;

rightData = sndbuf[inBusNumber].rightData;

}

for (UInt32 i = 0; i < inNumberFrames; ++i) {

outL[i] = leftData[sample];

if (sndbuf[inBusNumber].channelCount == 2) {

outR[i] = rightData[sample];

}

sample++;

if (sample > bufSamples) {

// start over from the beginning of the data, our audio simply loops

printf("looping data for bus %d after %ld source frames rendered\n", (unsigned int)inBusNumber, (long)sample-1);

sample = 0;

}

}

sndbuf[inBusNumber].sampleNum = sample; // keep track of where we are in the source data buffer

return noErr;

}

将内存中保存的各路音频数据赋值给回调函数的ioData->mBuffer[x].mData,x=0或1

启动或停止AUGraph

初始化完成后,使用AUGraphStart(_mGraph)启动混音,手机就会输出混合后的音频了;使用AUGraphStop(_mGraph)停止输出。

音量和各路音频使能控制

可以单独控制各路音频的音量(对Mixer Unit的Input Scope下的各路Element的kMultiChannelMixerParam_Volume设置音量),也可以控制整体的音量(对Mixer Unit的Output Scope下的Element 0的kMultiChannelMixerParam_Volume设置音量);

对Mixer Unit的Input Scope下的各路Element的kMultiChannelMixerParam_Enable设置使能此路音频信号是否加入到混音中)

java sound 混音_iOS音频编程之混音相关推荐

  1. pyaudio usb playback_【雅马哈(YAMAHA) UR22C声卡价格,参数】steinberg UR22C 专业录音外置声卡编曲混音USB音频接口 2019升级版–音平商城...

    UR-C系列音频接口 全新UR-C系列音频接口全部使用USB Type-C接口 可为音乐人提供改进的速度和灵活性,可连接PC/Mac,更可直连iOS设备 主要特性 • 32bit/192kHz USB ...

  2. 整理下OSS方面的资料,免得到处找,linux音频编程,open sound system

    浅析ASoC-audio驱动oss框架下/dev/dsp与alsa框架下设备节点打开和创建简易流程 http://blog.chinaunix.net/u2/70445/showart_2070710 ...

  3. Android音频焦点及混音策略

    1.前言 1.1 音频焦点官方解读 两个或两个以上的 Android 应用可同时向同一输出流播放音频.系统会将所有音频流混合在一起.虽然这是一项出色的技术,但却会给用户带来很大的困扰.为了避免所有音乐 ...

  4. Android视频编辑器(五)音频编解码、从视频中分离音频、音频混音、音频音量调节等

    前言 这篇博客,主要讲解的是android端的音频处理,在开发Android视频编辑器的时候,有一个非常重要的点就是音频的相关处理.比如如何从视频中分离音频(保存为mp3文件),然后分离出来的音频如何 ...

  5. java麦克风编程,java – Synch 2类似的音频输入(一个靠文件,一个靠麦克风)

    看看这里 这是完整的声音api文档 也 >第4章:在多行上同步播放 >第6章:使用控件处理音频 但 这是我在jsresource faq中找到的 如何同步两条或更多条播放线? 调制器中的同 ...

  6. 【Java Sound】(一)Sampled包概述

    Java Sound Java Sound (一)Sampled包概述 什么是格式化音频数据? 数据(Data)格式 文件(File)格式 什么是Mixer(混音器)? 什么是Line(线路)? Li ...

  7. java sound api_Java Sound API

    Java Sound API是javaSE平台提供底层的(low-level)处理声音接口. 例外,java也提供了简单的实用的高层媒体接口(higher-level) - JMF(Java Medi ...

  8. 【Linux系统编程应用】Linux音频编程实战(一)

    在Linux下进行音频编程时,重点在于如何正确地操作声卡驱动程序所提供的各种设备文件,由于涉及到的概念和因素比较多,所以遵循一个通用的框架无疑将有助于简化应用程序的设计. 1 DSP编程 对声卡进行编 ...

  9. OSS音频编程概述(DSP部分)

    一. 音频概念 音频信号是一种连续变化的模拟信号,但计算机只能处理和记录二进制的数字信号,由自然音源得到的音频信号必须经过一定的变换,成为数字音频信号之后,才能送到计算机中作进一步的处理. 对于OSS ...

最新文章

  1. 4_Shell语言———脚本概述
  2. 一篇blog带你了解java中的锁
  3. 关于Spring 中的PortletModeHandlerMapping
  4. Python Django 文件下载代码示例
  5. tcp假连接_总结的23 个 TCP高频面试问题
  6. 判断同构数 c语言,基于visual Studio2013解决C语言竞赛题之0413同构数
  7. python二分法查找程序_查找Python程序的输出| 套装2(基础)
  8. 基本数据结构 - 栈和队列
  9. 007-配置IP和DNS
  10. 移动滑块改变使用容量
  11. Java线程的6种状态、NEW、RUNNABLE、BLOCK、TIMED_WAITING、TERMINATED
  12. James+Javamail构建邮箱服务
  13. 计算机会计应用实训,大学excel在会计中的应用的实训心得
  14. SAS: PROC IMPORT简单入门介绍
  15. 业务流程再造和IT服务能力
  16. 智慧公厕解决方案,光明源智慧公厕解决方案全解
  17. 嵌入式工作会越来越少吗?
  18. FastAPI(55)- Events: startup - shutdown 启动/关闭事件
  19. Java正则表达式提取字符的方法实例
  20. LeetCode(Python)—— 将有序数组转换为二叉搜索树(简单)

热门文章

  1. PHP接入网易易盾验证码
  2. 轨道运营管理专业自荐书_城市轨道交通运营管理专业自荐信范文【精选】
  3. 2012年经济与股市战略
  4. 啊哈 算法 Java_《啊哈!算法》.啊哈磊.高清版.pdf
  5. lemur代码分析之入口设计
  6. 基于SSM的人事员工管理系统
  7. 工程流体力学笔记暂记3(流体运动的基本概念:流动的分类,迹线和流线+流线的计算例题)
  8. 计算机主机元件电压,电脑主机漏电的原因
  9. Java实验报告实验4
  10. 运行matlabR2014a出现找不到指定模块C:MATLAB7\bin\win32\atlas_Athlon.dll怎么办?