プログラムで WAV (または CAF) ファイルを読み取り、サンプル (オーディオ) データだけをバイト配列として抽出できる必要があります。これを行う最も簡単/迅速な方法は何ですか?
3259 次
2 に答える
3
iOSまたはOSXを使用していると仮定すると、AudioToolboxフレームワーク、具体的にはのAPIが必要ですAudioFile.h
(またはExtAudioFile.h
、読み取り時にオーディオデータを別の形式に変換する必要がある場合)。
例えば、
#include <AudioToolbox/AudioFile.h>
...
AudioFileID audioFile;
OSStatus err = AudioFileOpenURL(fileURL, kAudioFileReadPermission, 0, &audioFile);
// get the number of audio data bytes
UInt64 numBytes = 0;
UInt32 dataSize = sizeof(numBytes);
err = AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataByteCount, &dataSize, &numBytes);
unsigned char *audioBuffer = (unsigned char *)malloc(numBytes);
UInt32 toRead = numBytes;
UInt64 offset = 0;
unsigned char *pBuffer = audioBuffer;
while(true) {
err = AudioFileReadBytes(audioFile, true, offset, &toRead, &pBuffer);
if (kAudioFileEndOfFileError == err) {
// cool, we're at the end of the file
break;
} else if (noErr != err) {
// uh-oh, some error other than eof
break;
}
// advance the next read offset
offset += toRead;
// advance the read buffer's pointer
pBuffer += toRead;
toRead = numBytes - offset;
if (0 == toRead) {
// got to the end of file but no eof err
break;
}
}
// Process audioBuffer ...
free(audioBuffer);
于 2012-12-21T19:55:38.480 に答える