2015-07-26 21 views
6

Görselleştirmeler için ses verilerini almama yardımcı olan Swift koduna dönüştürmek için çalışıyorum. Ben iyi çalışıyor Obj C, içinde çalışıyorum koddur:Ses veri için Swift'de unSafeMutablePointer Int16'dan değer alma

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

, ben yazmaya çalışıyorum bu parçasıdır: Ben println zaman Ancak

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. 

() sampleValue sadece konsolda (Opak Değer) çıkar. SampleValue’nın gerçekten nasıl okunacağını anlayamıyorum.

Görselleştirme amacıyla ses verilerini okumaya çalışırken yeniyim. Çalışmak için ses verilerinin bir tamponu alma konusunda herhangi bir yardım yardımcı olacaktır. Teşekkür ederim.

cevap

0

Adımı kullanın?

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 
}