更改传递给 OpenAL 的音频数据的字节序



我在我正在开发的iOS应用程序中使用OpenAL,我们的客户给了我们一堆音频文件,这些文件都是大端格式。我的问题是OpenAL只播放小端格式。

所以我想我需要做的就是找到音频数据,然后反转它,对吗?

我用于提取音频数据的代码(取自 OpenAL 教程):

-(void)addSound:(NSString*)theSound
{
// get the full path of the file
NSString* fileName = [[NSBundle mainBundle] pathForResource:theSound ofType:@"caf"];
// first, open the file
NSLog(@"fileNAme is ===== %@", fileName);
AudioFileID fileID = [self openAudioFile:fileName];
// find out how big the actual audio data is
UInt32 fileSize = [self audioFileSize:fileID];
// this is where the audio data will live for the moment
unsigned char * outData = malloc(fileSize);
// this where we actually get the bytes from the file and put them
// into the data buffer
OSStatus result = noErr;
result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);
NSLog(@"OUT DATA IS == %d", outData);
AudioFileClose(fileID); //close the file
if (result != 0) NSLog(@"cannot load effect: %@",fileName);
NSUInteger bufferID;
// grab a buffer ID from openAL
alGenBuffers(1, &bufferID);
// jam the audio data into the new buffer
alBufferData(bufferID,AL_FORMAT_STEREO16,outData,fileSize,44100);
// save the buffer so I can release it later
[bufferStorageArray addObject:[NSNumber numberWithUnsignedInteger:bufferID]];
NSUInteger sourceID;
// grab a source ID from openAL
alGenSources(1, &sourceID);
// attach the buffer to the source
alSourcei(sourceID, AL_BUFFER, bufferID);
// set some basic source prefs
alSourcef(sourceID, AL_PITCH, 1.0f);
alSourcef(sourceID, AL_GAIN, 1.0f);
//alSourcei(sourceID, AL_LOOPING, AL_TRUE);
// store this for future use
[soundDictionary setObject:[NSNumber numberWithUnsignedInt:sourceID] forKey:theSound];
// clean up the buffer
if (outData)
{
    free(outData);
    outData = NULL;
}
}

我想unsigned char * outdata;是我需要反转的(当然,在将音频文件数据加载到其中之后),但我不知道该怎么做。任何人都可以帮我解决这个问题吗?

在这种情况下,你应该在配置ExtAudioFileAudioConvertor格式后使用ExtAudioFileRead。 这将为您节省大量时间。 如果您知道所有输入文件都具有相同的示例格式,则可以交换自己。

ExtAudioFile在OS X和iOS上的AudioToolbox.framework中(基本上是AudioFile之上一层,具有更高级的功能),您可以通过包装AudioFile来创建一个。

AudioConvertor也可以在没有ExtAudioFile的情况下使用。

在某些情况下,您只需使用 OS X afonvert 实用程序将它们交换为 LE,即可完成此操作,而无需增加(不必要的)编程挑战。

为数组编写自己的字节序交换例程并不难 - 它只是耗时、乏味,并且您通常需要确保它快速且能够支持不自然对齐的示例数据。 这涉及:

for (each sample) {
  reverse order of bytes in sample
}

W'rkncacnter 提供了这个链接:从二进制文件读取时将大端序转换为小端序

如果自然对齐,则可以使用该方法。 并且您需要针对您在输入中看到的每个大小和样本描述进行变体。 操作系统还提供了OSSwap例程,但这些例程并不处理所有示例格式 - 它们专注于内置类型。

最新更新