如何从CMSampleBufferRef中获取样本以进行音高变换?

时间:2011-07-25 07:48:07

标签: objective-c

我试图将样本从CMSampleBufferRef中取出并放入浮点数组中以改变音高。我在文档中找到了一个方法CMSampleBufferCallForEachSample,但不知道如何使用它,并且没有示例或解释。我已经和这个东西一起乱跑了一段时间,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我正在解决类似的问题。我需要从一个SampleBuffer中分离AAC样本。

    ...
    CMSampleBufferCallForEachSample(sampleBuffer, &sampler, NULL);
    ...

static OSStatus sampler(CMSampleBufferRef sampleBuffer, CMItemCount index, void *refcon) {
    CMTime presentationTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
    CMBlockBufferRef buff = CMSampleBufferGetDataBuffer(sampleBuffer);
    UInt32 timeStamp = (1000*presentationTimeStamp.value) / presentationTimeStamp.timescale;
    size_t size = CMBlockBufferGetDataLength(buff);

    void *sampleData = malloc(size);
    CMBlockBufferCopyDataBytes(buff, 0, size, sampleData);
    NSData * data = [NSData dataWithBytes:sampleData length:size];
    NSLog(@"Audio %ldx samples of size %lu has been transfered at time %ld", CMSampleBufferGetNumSamples(sampleBuffer), size, timeStamp); 
}

但是每个sampleBuffer中的第一个样本只调用一次回调,即使有76个样本的数组,CMSampleBufferGetNumSamples返回这个数字

我找到了出色的解决方案here(方法fillMovieSampleBuffer第283行)

有关CoreMedia框架check this URL

的文档
@function   CMSampleBufferCallForEachSample
@abstract   Calls a function for every individual sample in a sample buffer.
@discussion Temporary sample buffers will be created for individual samples,
referring to the sample data and containing its timing, size and attachments.
The callback function may retain these sample buffers if desired.
If the callback function returns an error, iteration will stop immediately
and the error will be returned.

If there are no sample sizes in the provided sample buffer, kCMSampleBufferError_CannotSubdivide will be returned.
This will happen, for example, if the samples in the buffer are non-contiguous (eg. non-interleaved audio, where
the channel values for a single sample are scattered through the buffer).