因此,我正在尝试对先前录制的音频(来自AVAsset)进行简单计算,以便创建视觉波形。我目前通过平均一组样本来做到这一点,其大小是通过将音频文件大小除以我想要的波形的分辨率来确定的。
这一切都很好,除了一个问题......它太慢了。在3GS上运行,处理音频文件大约需要花费3%的时间来播放它,这是减慢速度的方法(例如,1小时音频文件需要大约2.5分钟才能处理)。我试图尽可能地优化方法,但它不起作用。我将发布用于处理文件的代码。也许有人能够提供帮助,但我真正想要的是一种处理文件的方法,而不必遍历每一个字节。所以,假设分辨率为2,000,我想访问该文件,并在2,000个点中的每个点进行采样。我认为这会更快,特别是如果文件更大。但我知道获取原始数据的唯一方法是以线性方式访问音频文件。有任何想法吗?这是我用来处理文件的代码(注意,所有类变量都以'_'开头):
所以我完全改变了这个问题。我姗姗来迟地意识到AVAssetReader有一个用于“搜索”的timeRange属性,这正是我所寻找的(参见上面的原始问题)。此外,这个问题已被提出并回答(我之前没有找到),我不想重复问题。但是,我还有问题。我的应用程序冻结了一段时间,然后在尝试copyNextSampleBuffer
时最终崩溃。我不确定发生了什么。我似乎没有任何类型的递归循环,它只是永远不会从函数调用返回。检查日志显示给我这个错误:
Exception Type: 00000020
Exception Codes: 0x8badf00d
Highlighted Thread: 0
Application Specific Information:
App[10570] has active assertions beyond permitted time:
{(
<SBProcessAssertion: 0xddd9300> identifier: Suspending process: App[10570] permittedBackgroundDuration: 10.000000 reason: suspend owner pid:52 preventSuspend preventThrottleDownCPU preventThrottleDownUI
)}
我在应用程序上使用时间分析器,是的,它只是在那里进行最少量的处理。无法弄清楚发生了什么。重要的是要注意,如果我没有设置AVAssetReader的timeRange属性,则不会发生这种情况。我已经检查过timeRange的值是有效的,但设置它是由于某种原因导致问题。这是我的处理代码:
- (void) processSampleData{
if (!_asset || CMTimeGetSeconds(_asset.duration) <= 0) return;
NSError *error = nil;
AVAssetTrack *songTrack = _asset.tracks.firstObject;
if (!songTrack) return;
NSDictionary *outputSettingsDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsNonInterleaved,
nil];
UInt32 sampleRate = 44100.0;
_channelCount = 1;
NSArray *formatDesc = songTrack.formatDescriptions;
for(unsigned int i = 0; i < [formatDesc count]; ++i) {
CMAudioFormatDescriptionRef item = (__bridge_retained CMAudioFormatDescriptionRef)[formatDesc objectAtIndex:i];
const AudioStreamBasicDescription* fmtDesc = CMAudioFormatDescriptionGetStreamBasicDescription (item);
if(fmtDesc ) {
sampleRate = fmtDesc->mSampleRate;
_channelCount = fmtDesc->mChannelsPerFrame;
}
CFRelease(item);
}
UInt32 bytesPerSample = 2 * _channelCount; //Bytes are hard coded by AVLinearPCMBitDepthKey
_normalizedMax = 0;
_sampledData = [[NSMutableData alloc] init];
SInt16 *channels[_channelCount];
char *sampleRef;
SInt16 *samples;
NSInteger sampleTally = 0;
SInt16 cTotal;
_sampleCount = DefaultSampleSize * [UIScreen mainScreen].scale;
NSTimeInterval intervalBetweenSamples = _asset.duration.value / _sampleCount;
NSTimeInterval sampleSize = fmax(100, intervalBetweenSamples / _sampleCount);
double assetTimeScale = _asset.duration.timescale;
CMTimeRange timeRange = CMTimeRangeMake(CMTimeMake(0, assetTimeScale), CMTimeMake(sampleSize, assetTimeScale));
SInt16 totals[_channelCount];
@autoreleasepool {
for (int i = 0; i < _sampleCount; i++) {
AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:_asset error:&error];
AVAssetReaderTrackOutput *trackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:songTrack outputSettings:outputSettingsDict];
[reader addOutput:trackOutput];
reader.timeRange = timeRange;
[reader startReading];
while (reader.status == AVAssetReaderStatusReading) {
CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer];
if (sampleBufferRef){
CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef);
size_t length = CMBlockBufferGetDataLength(blockBufferRef);
int sampleCount = length / bytesPerSample;
for (int i = 0; i < sampleCount ; i += _channelCount) {
CMBlockBufferAccessDataBytes(blockBufferRef, i * bytesPerSample, _channelCount, channels, &sampleRef);
samples = (SInt16 *)sampleRef;
for (int channel = 0; channel < _channelCount; channel++)
totals[channel] += samples[channel];
sampleTally++;
}
CMSampleBufferInvalidate(sampleBufferRef);
CFRelease(sampleBufferRef);
}
}
for (int i = 0; i < _channelCount; i++){
cTotal = abs(totals[i] / sampleTally);
if (cTotal > _normalizedMax) _normalizedMax = cTotal;
[_sampledData appendBytes:&cTotal length:sizeof(cTotal)];
totals[i] = 0;
}
sampleTally = 0;
timeRange.start = CMTimeMake((intervalBetweenSamples * (i + 1)) - sampleSize, assetTimeScale); //Take the sample just before the interval
}
}
_assetNeedsProcessing = NO;
}
答案 0 :(得分:1)
我终于找到了原因。显然,您可以为AVAssetReader的timeRange指定某种“最小”持续时间。我不确定究竟是什么最小值,大约在1,000以上但小于5,000。最小的变化可能与资产的持续时间有关......老实说,我不确定。相反,我保持持续时间(无穷大)相同,只是改变了开始时间。我只复制一个缓冲区块,然后再进行处理,而不是处理整个样本。我仍然遇到代码问题,但如果我无法解决这个问题,我会将其作为另一个问题发布。