मैं बॉब मैकक्यून की लर्निंग एवीफ़ाउंडेशन पुस्तक से एक उदाहरण बदलने और AVAssetReader और NSInputStream का उपयोग करके कुछ समस्याएं बदलने की कोशिश कर रहा हूं। ग्राफ शुद्ध साइन लहर होना चाहिए लेकिन मूल्य किसी भी तरह एक्स-अक्ष पर दिखाई देता है।स्विफ्ट 2: AVAssetReader और NSInputStream ऑडियो ग्राफ
मैंने बाइट स्वैपिंग के हर पुनरावृत्ति की कोशिश की है जिसे मैं सोच सकता हूं और यह काम नहीं करता है। https://github.com/justinlevi/AVAssetReader
//: Playground - noun: a place where people can play
import UIKit
import AVFoundation
import XCPlayground
func plotArrayInPlayground<T>(arrayToPlot:Array<T>, title:String) {
for currentValue in arrayToPlot {
XCPCaptureValue(title, value: currentValue)
}
}
class SSSampleDataFilter {
var sampleData:NSData?
init(data:NSData) {
sampleData = data
}
func filteredSamplesForSize(size:CGSize) -> [Int]{
var filterSamples = [UInt16]()
if let sampleData = sampleData {
let sampleCount = sampleData.length
let binSize = CGFloat(sampleCount)/size.width
let stream = NSInputStream(data: sampleData)
stream.open()
var readBuffer = Array<UInt8>(count: 16 * 1024, repeatedValue: 0)
var totalBytesRead = 0
let size = sizeof(UInt16)
while (totalBytesRead < sampleData.length) {
let numberOfBytesRead = stream.read(&readBuffer, maxLength: size)
let u16: UInt16 = UnsafePointer<UInt16>(readBuffer).memory
var sampleBin = [UInt16]()
for _ in 0..<Int(binSize) {
sampleBin.append(u16)
}
filterSamples.append(sampleBin.maxElement()!)
totalBytesRead += numberOfBytesRead
}
//plotArrayInPlayground(filterSamples, title: "Samples")
}
return [0]
}
}
let sineURL = NSBundle.mainBundle().URLForResource("440.0-sine", withExtension: "aif")!
let asset = AVAsset(URL: sineURL)
var assetReader:AVAssetReader
do{
assetReader = try AVAssetReader(asset: asset)
}catch{
fatalError("Unable to read Asset: \(error) : \(__FUNCTION__).")
}
let track = asset.tracksWithMediaType(AVMediaTypeAudio).first
let outputSettings: [String:Int] =
[ AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVLinearPCMIsBigEndianKey: 0,
AVLinearPCMIsFloatKey: 0,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsNonInterleaved: 0]
let trackOutput = AVAssetReaderTrackOutput(track: track!, outputSettings: outputSettings)
assetReader.addOutput(trackOutput)
assetReader.startReading()
var sampleData = NSMutableData()
while assetReader.status == AVAssetReaderStatus.Reading {
if let sampleBufferRef = trackOutput.copyNextSampleBuffer() {
if let blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef) {
let bufferLength = CMBlockBufferGetDataLength(blockBufferRef)
var data = NSMutableData(length: bufferLength)
CMBlockBufferCopyDataBytes(blockBufferRef, 0, bufferLength, data!.mutableBytes)
var samples = UnsafeMutablePointer<Int16>(data!.mutableBytes)
sampleData.appendBytes(samples, length: bufferLength)
CMSampleBufferInvalidate(sampleBufferRef)
}
}
}
let view = UIView(frame: CGRectMake(0, 0, 375.0, 667.0))
//view.backgroundColor = UIColor.lightGrayColor()
if assetReader.status == AVAssetReaderStatus.Completed {
print("complete")
let filter = SSSampleDataFilter(data: sampleData)
let filteredSamples = filter.filteredSamplesForSize(view.bounds.size)
}
//XCPShowView("Bezier Path", view: view)
XCPSetExecutionShouldContinueIndefinitely(true)
यहाँ ग्राफ खेल का मैदान
में कैसा दिखता है कैसा दिखना चाहिए:
खेल का मैदान यहाँ GitHub पर पोस्ट किए गए
विचित्र है कि यह आपके लिए Xcode7b5 में प्रस्तुत नहीं किया गया है। क्या आपने जिथब से संस्करण को पकड़ लिया था जिसमें एआईएफएफ ऑडियो फाइल शामिल थी? निश्चित रूप से, जैसे ही मैंने 'UInt16' के सभी उदाहरणों को 'Int16' में बदल दिया, सबकुछ काम करता प्रतीत होता है। –
कल घंटों के लिए देखा। धन्यवाद –
हालांकि प्रश्न का पालन करें - कोई विचार क्यों एआईएफ, कैफ, एम 4 ए सभी ठीक काम करते हैं लेकिन एक ही ऑडियो का एमपी 3 संस्करण नहीं है? मुझे दस्तावेज़ों पर वापस जाने की जरूरत है और देखें कि मैं समझ सकता हूं कि क्यों, लेकिन मैं AVASetReader ने एमपी 3 संपीड़न की देखभाल की है, साथ ही –