gpt4 book ai didi

iphone - iOS - 播放带有效果的流媒体(mp3)音频

转载 作者:行者123 更新时间:2023-12-03 19:12:06 26 4
gpt4 key购买 nike

我是 iOS 音频技术新手。

我正在开发一个可以播放流媒体音频(mp3)的应用程序,计划添加一些效果,例如 iPod 均衡器、平移控制。

实现这一目标的最佳方法是什么。

_I have tried to use Matt Gallagher's AudioStreamer API ( http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html ). I was able to play the streaming audio. but i was not sure how to add the effects using AudioQueue _.

从Apple文档中,我了解到AudioUnit可用于添加效果。但流格式应该是线性PCM。

基本上我想添加效果并播放流音频。

我现在对前进的方向感到困惑。

有人可以指明前进的方向吗?非常感谢任何帮助。

谢谢

萨西库玛

最佳答案

我认为你应该明确使用 AudioUnits。

看看它多么简单:

1) 创建 AudioUnit

// 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音频单元应用

The Audio Unit Programming Guide由苹果公司提供

AudioGraph是有关现实世界 AudioUnit 编程的最全面的示例代码/“非官方”文档。

希望这对您有所帮助,祝您好运!

关于iphone - iOS - 播放带有效果的流媒体(mp3)音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5491060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com