2015-07-26 27 views
6

Sto lavorando per convertire in Swift questo codice che mi aiuta a ottenere i dati audio per le visualizzazioni. Il codice con cui sto lavorando in Obj C, che funziona bene, è:Ottenere valore da unSafeMutablePointer Int16 in Swift per scopi dati audio

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, sto cercando di scrivere è questa parte:

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. 

Tuttavia, quando ho println () sampleValue è appena uscito (Opaque Value) nella console. Non riesco a capire come effettivamente leggere il sampleValue.

Sono nuovo nel tentativo di leggere i dati audio per scopi di visualizzazione. Sarebbe utile qualsiasi aiuto per ottenere un buffer di dati audio con cui lavorare. Grazie.

risposta

0

Utilizzare il passo?

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 
}