ios - 使用扩展音频文件写入和读取音频文件
<p><p>我在使用设备录制时使用 ExtAudioFileWriteAsync 写入音频文件,但是一旦我完成录制,我会尝试使用 ExtAudioFileRead 函数读取它,我得到的样本与我正在编写的样本不同......任何人都知道为什么会这样发生了什么?</p>
<p>写作:</p>
<pre><code>self.audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
for (int i = 0; i < numFrames*numChannels; i++) {
printf("write*%f\n", data);
}
UInt32 numIncomingBytes = numFrames*numChannels*sizeof(float);
UInt32 *outputBuffer =(UInt32*)malloc(numIncomingBytes);
memcpy(outputBuffer, recordedData, numIncomingBytes);
AudioBufferList outgoingAudio;
outgoingAudio.mNumberBuffers = 1;
outgoingAudio.mBuffers.mNumberChannels = numChannels;
outgoingAudio.mBuffers.mDataByteSize = numIncomingBytes;
outgoingAudio.mBuffers.mData = self.outputBuffer;
if( 0 == pthread_mutex_trylock( &outputAudioFileLock ) )
{
ExtAudioFileWriteAsync(outputFile, numFrames, &outgoingAudio);
}
pthread_mutex_unlock( &outputAudioFileLock );
};
;
</code></pre>
<p>阅读:</p>
<pre><code>UInt32 *outputBuffer = (UInt32 *)malloc(numFrames*numChannels*sizeof(float));
AudioBufferList convertedData;
convertedData.mNumberBuffers = 1;
convertedData.mBuffers.mNumberChannels = numChannels;
convertedData.mBuffers.mDataByteSize = numFrames*numChannels*sizeof(float);
convertedData.mBuffers.mData = outputBuffer;
NSMutableArray *samplesArray = [init];
while (numFrames > 0) {
ExtAudioFileRead(inputFile, &numFrames, &convertedData);
if (numFrames > 0){
AudioBuffer audioBuffer = convertedData.mBuffers;
float *samples = (float *)audioBuffer.mData;
for (int i = 0; i < frameCount*numChannels; i++) {
printf("read*%f\n", samples);
}
}
}
</code></pre>
<p>顺便说一下,我使用的是 <a href="https://github.com/alexbw/novocaine" rel="noreferrer noopener nofollow">Novocaine</a>项目以获取设备音频。我可以使用 Novocaine 代码或任何其他播放器重现保存的音频。</p>
<p>写入 ExtAudioFileRef 输出时:</p>
<pre><code> ExtAudioFileCreateWithURL(audioFileRef, kAudioFileM4AType, &outputFileDesc, NULL, kAudioFileFlags_EraseFile, &outputFile);
</code></pre>
<p>outputFileDesc 在哪里</p>
<p>AudioStreamBasicDescription outputFileDesc = {44100.0, kAudioFormatMPEG4AAC, 0, 0, 1024, 0, thisNumChannels, 0, 0};
outputFileDesc.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;</p>
<p>读取 ExtAudioFileRef 时输入文件:</p>
<pre><code>ExtAudioFileOpenURL(audioFileRef, &inputFile):
</code></pre>
<p>在两种情况下(写作和阅读)它都采用相同的格式:</p>
<pre><code>AudioStreamBasicDescription outputFormat;
_outputFormat.mSampleRate = self.samplingRate;
_outputFormat.mFormatID = kAudioFormatLinearPCM;
_outputFormat.mFormatFlags = kAudioFormatFlagIsFloat;
_outputFormat.mBytesPerPacket = 4*self.numChannels;
_outputFormat.mFramesPerPacket = 1;
_outputFormat.mBytesPerFrame = 4*self.numChannels;
_outputFormat.mChannelsPerFrame = self.numChannels;
_outputFormat.mBitsPerChannel = 32;
ExtAudioFileSetProperty(outputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_outputFormat);
ExtAudioFileSetProperty(inputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_outputFormat);
</code></pre>
<p>顺便说一句,即使读取样本不等于写入样本,两个信号的平均值也非常相似。但我不完全明白为什么不完全相等! </p>
<p>知道我做错了什么吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这听起来像是来自 ExtaudioFileRefs 之一或两者的隐式格式转换,并且您看到不同的样本作为转换的结果。您有三种格式:audio_in_format、file_format 和 audio_out_format。如果 audio_in_format 与 file_format 不同,写入 ExtAudioFileRef 将创建一个音频转换器,以便您在写入磁盘之前将输入音频转换为 file_format。如果 file_format 与 audio_out_format 不同,读取 ExtAudioFileRef 也会创建一个转换器。</p>
<p>意见:
令人困惑的是,您将写作 ExtAudioFileRef 命名为“outputFile”,而将阅读 ExtAudioFileRef 命名为“inputFile”。我会使用类似 audioWriter 和 audioReader 的东西。</p></p>
<p style="font-size: 20px;">关于ios - 使用扩展音频文件写入和读取音频文件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32690423/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32690423/
</a>
</p>
页:
[1]