我需要能够以编程方式读取 WAV(或 CAF)文件并将样本(音频)数据提取为字节数组。最简单/最快的方法是什么?
问问题
3259 次
2 回答
3
假设您在 iOS 或 OS X 上,您需要 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 回答
1
这是我从iPhone 中从音乐文件中获取 NSData并为 ARC 更新的东西
- (NSData *)readSoundFileSamples:(NSString *)filePath
{
// Get raw PCM data from the track
NSURL *assetURL = [NSURL fileURLWithPath:filePath];
NSMutableData *data = [[NSMutableData alloc] init];
const uint32_t sampleRate = 16000; // 16k sample/sec
const uint16_t bitDepth = 16; // 16 bit/sample/channel
const uint16_t channels = 2; // 2 channel/sample (stereo)
NSDictionary *opts = [NSDictionary dictionary];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:assetURL options:opts];
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:NULL];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:(float)sampleRate], AVSampleRateKey,
[NSNumber numberWithInt:bitDepth], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey, nil];
AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:[[asset tracks] objectAtIndex:0] outputSettings:settings];
[reader addOutput:output];
[reader startReading];
// read the samples from the asset and append them subsequently
while ([reader status] != AVAssetReaderStatusCompleted) {
CMSampleBufferRef buffer = [output copyNextSampleBuffer];
if (buffer == NULL) continue;
CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(buffer);
size_t size = CMBlockBufferGetDataLength(blockBuffer);
uint8_t *outBytes = malloc(size);
CMBlockBufferCopyDataBytes(blockBuffer, 0, size, outBytes);
CMSampleBufferInvalidate(buffer);
CFRelease(buffer);
[data appendBytes:outBytes length:size];
free(outBytes);
}
return data;
}
于 2015-04-24T22:21:31.183 回答