在Swift中获取unSafeMutablePointer Int16的值,用于音频数据目的

8

我正在努力将以下ObjC代码转换为Swift,用于获取音频数据进行可视化。这段ObjC代码运行良好:

    while (reader.status == AVAssetReaderStatusReading) {
           AVAssetReaderTrackOutput *trackOutput = (AVAssetReaderTrackOutput *)[reader.outputs objectAtIndex:0];
            self.sampleBufferRef = [trackOutput copyNextSampleBuffer];
            if (self.sampleBufferRef) {

                CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(self.sampleBufferRef);
                size_t bufferLength = CMBlockBufferGetDataLength(blockBufferRef);
                void *data = malloc(bufferLength);
                CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data);

                SInt16 *samples = (SInt16 *)data;
                int sampleCount = bufferLength / bytesPerInputSample;


                for (int i=0; i<sampleCount; i+=100) {
                    Float32 sample = (Float32) *samples++;

                sample = decibel(sample);
                sample = minMaxX(sample,noiseFloor,0);
                tally += sample; 

                for (int j=1; j<channelCount; j++)
                    samples++;
                tallyCount++;

                if (tallyCount == downsampleFactor) {
                    sample = tally / tallyCount;
                    maximum = maximum > sample ? maximum : sample;
                    [fullSongData appendBytes:&sample length:sizeof(sample)];//tried dividing the sample by 2
                    tally = 0;
                    tallyCount = 0;
                    outSamples++;


                }
            }

        CMSampleBufferInvalidate(self.sampleBufferRef);
        CFRelease(self.sampleBufferRef);
        free(data);
   }
}

在Swift中,我正在尝试编写的部分是:
 while (reader.status == AVAssetReaderStatus.Reading) {
            var trackOutput = reader.outputs[0] as! AVAssetReaderTrackOutput
            self.sampleBufferRef = trackOutput.copyNextSampleBuffer()

            if (self.sampleBufferRef != nil) {

            let blockBufferRef = CMSampleBufferGetDataBuffer(self.sampleBufferRef)
            let bufferLength = CMBlockBufferGetDataLength(blockBufferRef)
            var data = NSMutableData(length: bufferLength)
            CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data!.mutableBytes)


            var samples = UnsafeMutablePointer<Int16>(data!.mutableBytes)

            var sampleCount = Int32(bufferLength)/bytesPerInputSample


            for var i = 0; i < Int(sampleCount); i++ {

                var sampleValue = CGFloat(samples[i]) etc. etc.

然而,当我使用println()时,控制台上只显示了(Opaque Value)的sampleValue。我不知道如何实际读取sampleValue。
我尝试读取音频数据以进行可视化处理,但是还不熟悉。如果能提供有关获取音频数据缓冲区的帮助,将不胜感激。谢谢。
1个回答

0

使用步幅?

let bytesPerInputSample = 4 // assumption ;)

var samplePtr = data.mutableBytes

for _ in stride(from: 0, to: data.length, by: bytesPerInputSample) {
    let currentSample = Data(bytes: samplePtr, count: bytesPerInputSample)
    // do whatever is needed with current sample

    //...

    // increase ptr by size of sample
    samplePtr = samplePtr + bytesPerInputSample
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接