我是iOS音频技术的新手。
我正在开发一个播放流媒体音频(mp3)的应用程序,计划添加一些效果,如iPod Equalizer,Pan Control。
实现这一目标的最佳方法是什么。
_ 我曾尝试使用Matt Gallagher的 AudioStreamer API(http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html)。我能够播放流媒体音频。但我不知道如何使用 AudioQueue _ 添加效果。
从Apple文档中,我了解 AudioUnit 可用于添加效果。但是流格式应该是线性PCM。
基本上我想添加效果并播放流媒体音频。
我现在感到很困惑。
有人可以指明前进的方向。任何帮助都非常感谢。
由于
Sasikumar
答案 0 :(得分:4)
我认为你应该明确地使用AudioUnits。
看看它有多简单:
1)创建AudioUnits
// OUTPUT unit
AudioComponentDescription iOUnitDescription;
iOUnitDescription.componentType = kAudioUnitType_Output;
iOUnitDescription.componentSubType = kAudioUnitSubType_RemoteIO;
iOUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
iOUnitDescription.componentFlags = 0;
iOUnitDescription.componentFlagsMask = 0;
// MIXER unit
AudioComponentDescription MixerUnitDescription;
MixerUnitDescription.componentType = kAudioUnitType_Mixer;
MixerUnitDescription.componentSubType = kAudioUnitSubType_MultiChannelMixer;
MixerUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
MixerUnitDescription.componentFlags = 0;
MixerUnitDescription.componentFlagsMask = 0;
// PLAYER unit
AudioComponentDescription playerUnitDescription;
playerUnitDescription.componentType = kAudioUnitType_Generator;
playerUnitDescription.componentSubType = kAudioUnitSubType_AudioFilePlayer;
playerUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
// EQ unit
AudioComponentDescription EQUnitDescription;
EQUnitDescription.componentType = kAudioUnitType_Effect;
EQUnitDescription.componentSubType = kAudioUnitSubType_AUiPodEQ;
EQUnitDescription.componentManufacturer = kAudioUnitManufacturer_Apple;
EQUnitDescription.componentFlags = 0;
EQUnitDescription.componentFlagsMask = 0;
等
2)创建节点
////
//// EQ NODE
////
err = AUGraphAddNode(processingGraph, &EQUnitDescription, &eqNode);
if (err) { NSLog(@"eqNode err = %ld", err); }
////
//// FX NODE
////
err = AUGraphAddNode(processingGraph, &FXUnitDescription, &fxNode);
if (err) { NSLog(@"fxNode err = %ld", err); }
////
//// VFX NODE
////
err = AUGraphAddNode(processingGraph, &VFXUnitDescription, &vfxNode);
if (err) { NSLog(@"vfxNode err = %ld", err); }
///
/// MIXER NODE
///
err = AUGraphAddNode (processingGraph, &MixerUnitDescription, &mixerNode );
if (err) { NSLog(@"mixerNode err = %ld", err); }
///
/// OUTPUT NODE
///
err = AUGraphAddNode(processingGraph, &iOUnitDescription, &ioNode);
if (err) { NSLog(@"outputNode err = %ld", err); }
////
/// PLAYER NODE
///
err = AUGraphAddNode(processingGraph, &playerUnitDescription, &audioPlayerNode);
if (err) { NSLog(@"audioPlayerNode err = %ld", err); }
3)连接他们
//// mic /lineIn ----> vfx bus 0
err = AUGraphConnectNodeInput(processingGraph, ioNode, 1, vfxNode, 0);
if (err) { NSLog(@"vfxNode err = %ld", err); }
//// vfx ----> mixer
err = AUGraphConnectNodeInput(processingGraph, vfxNode, 0, mixerNode, micBus );
if (err) { NSLog(@"vfxNode err = %ld", err); }
//// player ----> fx
err = AUGraphConnectNodeInput(processingGraph, audioPlayerNode, 0, fxNode, 0);
if (err) { NSLog(@"audioPlayerNode err = %ld", err); }
//// fx ----> mixer
err = AUGraphConnectNodeInput(processingGraph, fxNode, 0, mixerNode, filePlayerBus);
if (err) { NSLog(@"audioPlayerNode err = %ld", err); }
///// mixer ----> eq
err = AUGraphConnectNodeInput(processingGraph, mixerNode, 0, eqNode, 0);
if (err) { NSLog(@"mixerNode err = %ld", err); }
//// eq ----> output
err = AUGraphConnectNodeInput(processingGraph, eqNode, 0, ioNode, 0);
if (err) { NSLog(@"eqNode err = %ld", err); }
4)设置渲染回调
// let's say a mic input callback
AURenderCallbackStruct lineInrCallbackStruct = {};
lineInrCallbackStruct.inputProc = &micLineInCallback;
lineInrCallbackStruct.inputProcRefCon = (void*)self;
err = AudioUnitSetProperty(
vfxUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Global,
0,
&lineInrCallbackStruct,
sizeof(lineInrCallbackStruct));
5)处理回调中的音频缓冲区
static OSStatus micLineInCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
MixerHostAudio *THIS = (MixerHostAudio *)inRefCon;
AudioUnit rioUnit = THIS.ioUnit; // io unit which has the input data from mic/lineIn
OSStatus renderErr;
OSStatus err;
UInt32 bus1 = 1; // input bus
int i;
renderErr = AudioUnitRender(
rioUnit,
ioActionFlags,
inTimeStamp,
bus1,
inNumberFrames,
ioData);
//// do something with iOData like getting left and right channels
AudioUnitSampleType *inSamplesLeft; // convenience pointers to sample data
AudioUnitSampleType *inSamplesRight;
int isStereo; // c boolean - for deciding how many channels to process.
int numberOfChannels; // 1 = mono, 2= stereo
// Sint16 buffers to hold sample data after conversion
SInt16 *sampleBufferLeft = THIS.conversionBufferLeft;
SInt16 *sampleBufferRight = THIS.conversionBufferRight;
SInt16 *sampleBuffer;
// start the actual processing
numberOfChannels = THIS.displayNumberOfInputChannels;
isStereo = numberOfChannels > 1 ? 1 : 0; // decide stereo or mono
// copy all the input samples to the callback buffer - after this point we could bail and have a pass through
renderErr = AudioUnitRender(rioUnit, ioActionFlags,
inTimeStamp, bus1, inNumberFrames, ioData);
if (renderErr < 0) {
return renderErr;
}
inSamplesLeft = (AudioUnitSampleType *) ioData->mBuffers[0].mData; // left channel
fixedPointToSInt16(inSamplesLeft, sampleBufferLeft, inNumberFrames);
if(isStereo) {
inSamplesRight = (AudioUnitSampleType *) ioData->mBuffers[1].mData; // right channel
fixedPointToSInt16(inSamplesRight, sampleBufferRight, inNumberFrames);
}
我通过探索像Apple这样的伟大文档来了解这一点
Apple MixerHost音频单元应用
Apple的The Audio Unit Programming Guide
AudioGraph是最全面的示例代码/&#34;非官方代码&#34;您可以在真实世界的AudioUnit编程上获得文档。
希望这有帮助,祝你好运!
答案 1 :(得分:1)
看看PureData音频流程 - libpd是它的ios版本