Getting value from unSafeMutablePointer Int16 in Swift for audio data purposes
Asked Answered
T

1

8

I'm working to convert to Swift this code which helps get me get audio data for visualizations. The code I'm working with in Obj C, which works well, is:

    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);
   }
}

In Swift, I'm trying to write is this part:

 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.

However, when I println() sampleValue is just comes out (Opaque Value) in the console. I can't figure out how to actually read the sampleValue.

I'm new at trying to read audio data for visualization purposes. Any help on getting a buffer of audio data to work with would be helpful. Thank you.

Tabbitha answered 26/7, 2015 at 22:14 Comment(0)
W
0

Use stride?

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
}
Wellread answered 7/10, 2017 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.