AvAssetReader获取的音频样本数量不正确

时间:2014-04-30 23:03:39

标签: ios audio avfoundation avassetreader

我正在尝试从视频中抓取音轨作为原始pcm数据。该计划将此数据传递到libpd中的float *表数组中。我设法获得了一些样本数据,但报告的样本数量太少了。例如,对于29秒的剪辑,我收到3968个样本报告。我期待的是每个样本的幅度。对于音频长度为29秒的音轨,我预计会有一个1278900的阵列(44.1khz)。

以下是我根据其他例子汇总的内容:

- (void)audioCapture {

NSLog(@"capturing");
NSError *error;

AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:self.videoAsset error:&error];

NSArray *audioTracks = [self.videoAsset tracksWithMediaType:AVMediaTypeAudio];

AVAssetTrack *audioTrack = nil;

if ([audioTracks count] > 0)
    audioTrack = [audioTracks objectAtIndex:0];

// Decompress to Linear PCM with the asset reader
NSDictionary *decompressionAudioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithUnsignedInt:kAudioFormatLinearPCM], AVFormatIDKey,
                                            nil];

AVAssetReaderOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:decompressionAudioSettings];
[reader  addOutput:output];


[reader startReading];

CMSampleBufferRef sample = [output copyNextSampleBuffer];

//array for our sample amp values
SInt16* samples = NULL;

//sample count;
CMItemCount numSamplesInBuffer = 0;

//sample buffer
CMBlockBufferRef buffer;

while( sample != NULL )
{
    sample = [output copyNextSampleBuffer];

    if( sample == NULL )
        continue;

    buffer = CMSampleBufferGetDataBuffer( sample );

    size_t lengthAtOffset;
    size_t totalLength;
    char* data;

    if( CMBlockBufferGetDataPointer( buffer, 0, &lengthAtOffset, &totalLength, &data ) != noErr )
    {
        NSLog( @"error!" );
        break;
    }

    numSamplesInBuffer = CMSampleBufferGetNumSamples(sample);

    AudioBufferList audioBufferList;

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
                                                            sample,
                                                            NULL,
                                                            &audioBufferList,
                                                            sizeof(audioBufferList),
                                                            NULL,
                                                            NULL,
                                                            kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
                                                            &buffer
                                                            );

    for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
        samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;


        NSLog(@"idx %f",(double)samples[bufferCount]);
    }
}
NSLog(@"num samps in buf %ld",numSamplesInBuffer);

//[PdBase copyArray:(float *)samples 
     //toArrayNamed:@"DestTable" withOffset:0    
            //count:pdTableSize];

}

0 个答案:

没有答案