gpt4 book ai didi

将麦克风输入保存到原始 PCM 文件的 iOS AudioUnit 设置

转载 作者:可可西里 更新时间:2023-11-01 04:44:08 25 4
gpt4 key购买 nike

我目前正在为 iOS 开发一个 VOIP 项目。
我使用 AudioUnits 从麦克风获取数据并播放声音。
我的主要应用程序是用 C# (Xamarin) 编写的,并使用 C++ 库来加快音频和编解码器处理速度。

为了测试输入/输出结果,我目前正在同一台设备上测试录制和播放
- 将麦克风音频数据存储在 recordingCallback 的缓冲区中
- 在 playbackCallback 中播放缓冲区中的数据

按预期工作,语音质量很好。

我需要将从麦克风传入的音频数据保存到原始 PCM 文件。

我已经这样做了,但是生成的文件只包含一些短的“哔”信号。

所以我的问题是:

我需要什么音频设置,才能在生成的原始 PCM 文件中听到我的声音(真实音频信号),而不是短促的蜂鸣声?
有谁知道可能出了什么问题,或者我必须做什么才能正确重放生成的 PCM 文件?

我当前的格式设置是(C#代码):

int framesPerPacket = 1;
int channelsPerFrame = 1;
int bitsPerChannel = 16;
int bytesPerFrame = bitsPerChannel / 8 * channelsPerFrame;
int bytesPerPacket = bytesPerFrame * framesPerPacket;
AudioStreamBasicDescription audioFormat = new AudioStreamBasicDescription ()
{
SampleRate = 8000,
Format = AudioFormatType.LinearPCM,
FormatFlags = AudioFormatFlags.LinearPCMIsSignedInteger | AudioFormatFlags.LinearPCMIsPacked | AudioFormatFlags.LinearPCMIsAlignedHigh,
BitsPerChannel = bitsPerChannel,
ChannelsPerFrame = channelsPerFrame,
BytesPerFrame = bytesPerFrame,
FramesPerPacket = framesPerPacket,
BytesPerPacket = bytesPerPacket,
Reserved = 0
};

其他 C# 设置(此处简称为无错误检查):

AVAudioSession session = AVAudioSession.SharedInstance();
NSError error = null;
session.SetCategory(AVAudioSession.CategoryPlayAndRecord, out error);
session.SetPreferredIOBufferDuration(Config.packetLength, out error);
session.SetPreferredSampleRate(Format.samplingRate,out error);
session.SetActive(true,out error);

简述我当前的录音回调(仅用于PCM文件保存)(C++代码):

OSStatus 
NotSoAmazingAudioEngine::recordingCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
std::pair<BufferData*, int> bufferInfo = _sendBuffer.getNextEmptyBufferList();
AudioBufferList* bufferList = new AudioBufferList();
bufferList->mNumberBuffers = 1;
bufferList->mBuffers[0].mData = NULL;
OSStatus status = AudioUnitRender(_instance->_audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, bufferList);
if(_instance->checkStatus(status))
{
if(fout != NULL) //fout is a "FILE*"
{
fwrite(bufferList->mBuffers[0].mData, sizeof(short), bufferList->mBuffers[0].mDataByteSize/sizeof(short), fout);
}
}
delete bufferList;
return noErr;

我需要原始 PCM 文件的背景信息:

To compress the audio data I'd like to use the Opus codec.
With the codec I have the problem that there is a tiny "tick" at the end of each frame:
With a frame size of 60ms I nearly can't hear them, at 20ms its annoying, at 10 ms frame sizes my own voice can't be heared because of the ticking (for the VOIP application I try to get 10ms frames).

I don't encode & decode in the callback functions (I encode/decode the data in the functions which I use to transfer audio data from the "micbuffer" to the "playbuffer"). And everytime the playbackCallback wants to play some data, there is a frame in my buffer.

I also eliminate my Opus encoding/decoding functions as error source, because if I read PCM data from a raw PCM file, encode & decode it afterwards, and save it to a new raw PCM file, the ticking does not appear (if I play the result file with "Softe Audio Tools", the output file audio is OK).

To find out what causes the ticking, I'd like to save the raw PCM data from the mic to a file to make further investigations on that issue.

最佳答案

我自己找到了解决方案:
我的 PCM 播放器期望 44100 Hz 立体声,但我的文件只有 8000 Hz 单声道,因此我保存的文件播放速度快了大约 10 倍。

关于将麦克风输入保存到原始 PCM 文件的 iOS AudioUnit 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079752/

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