ExtAudioFileConvert问题
-
19-09-2019 - |
题
我正在服用压缩(MP3)的声音并将其保存为PCM一些进展。另外,我想分割原始文件转换成不超过2秒长的块,在同一进程中。我似乎是成功的,但我有点困惑,为什么。
当我读到音频块和写入文件的时候,我检查,看看如果我要写一大块,这将使我的文件超过我的2秒的限制。如果是这样,我写足以让到2秒,关闭该文件,然后打开一个新的文件,并写入其余为新的文件,然后读取更多的数据。是这样的:
framesInTimedSegment += numFrames;
if ((framesInTimedSegment > (2.0 * sampleRate)) && (j < 5)) {
UInt32 newNumFrames = numFrames;
numFrames = framesInTimedSegment - (2.0 * sampleRate);
newNumFrames -= numFrames;
// Question A
UInt32 segmentOffset = newNumFrames * numChannels * 2;
error = ExtAudioFileWrite(segmentFile, newNumFrames, &fillBufList);
// Question B
// handle this error! We might have an interruption
if (segmentFile) ExtAudioFileDispose(segmentFile);
XThrowIfError(ExtAudioFileCreateWithURL(urlArray[++j], kAudioFileCAFType, &dstFormat, NULL, kAudioFileFlags_EraseFile, &breakoutFile), "ExtAudioFileCreateWithURL failed! - segmentFile");
size = sizeof(clientFormat);
XThrowIfError(ExtAudioFileSetProperty(segmentFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat), "couldn't set destination client format");
fillBufList.mBuffers[0].mData = srcBuffer + segmentOffset;
fillBufList.mBuffers[0].mDataByteSize = numFrames * fillBufList.mBuffers[0].mNumberChannels * 2;
framesInTimedSegment = numFrames;
}
error = ExtAudioFileWrite(segmentFile, numFrames, &fillBufList);
下面是我的问题(我曾尝试标签的相关行):
A:有没有更好的办法找到,所以我不会错误地硬编码在有一定的价值偏移到我的缓冲区?例如,有一个祝福的方式来获得该数据从帧号偏移?
B:如果ExtAudioFileWrite从压缩做转换为解压缩,然后我写的数据尚未解压缩(右?),所以我不应该担心,与帧序号和偏移玩的时候我正在打交道的压缩数据?我应该改为转换文件第一,要么一个PCM文件或到内存中,然后拆分PCM?
谢谢!
-mahboud
PS。
在clientFormat定义如下:
clientFormat = dstFormat;
和dstFormat:
dstFormat.mFormatID = outputFormat;
dstFormat.mChannelsPerFrame = srcFormat.NumberChannels();
dstFormat.mBitsPerChannel = 16;
dstFormat.mBytesPerPacket = dstFormat.mBytesPerFrame = 2 * dstFormat.mChannelsPerFrame;
dstFormat.mFramesPerPacket = 1;
dstFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger; // little-endian
解决方案
这是很难正确回答没有看到多一点的代码。但是,假设clientFormat为交错的PCM格式:
从压缩到解压缩的B)ExtAudioFileWrite不进行转换,ExtAudioFileRead取决于所设的客户格式does-。假设一个MP3的源文件和一个“标准”的16位44.1千赫PCM客户的格式,则调用ExtAudioFileRead将从MP3字节PCM数据转换。这是通过使用的AudioFile和AudioConverter API的发动机罩下进行。
A)这是有点硬,没有看到srcBuffer是如何定义的(I假设int16_t的阵列)来回答。如果您正在使用PCM数据时,你在做什么看起来OK。你也可以使用newNumFrames * clientFormat.mBytesPerFrame * clientFormat.mChannelsPerFrame,但假设16位PCM数据,mBytesPerFrame == == mBytesPerPacket 2.如果你用非CBR数据的工作,你需要使用数据包描述来关心自己,但这似乎不是这种情况。