简体   繁体   English

转换Swift UnsafePointer <AudioStreamBasicDescription> 到字典?

[英]Converting a Swift UnsafePointer<AudioStreamBasicDescription> to a Dictionary?

I want to know how to create a dictionary of [String:AnyObject] from an UnsafePointer<AudioStreamBasicDescription> 我想知道如何从UnsafePointer<AudioStreamBasicDescription>创建[String:AnyObject]的字典

I guess I don't understand how to work with an UnsafePointer<T> in Swift . 我想我不明白如何在Swift使用UnsafePointer<T> Here's where I'm starting from - The AVAudioFile class has a fileFormat property which is of AVAudioFormat Type. 这就是我的出发地AVAudioFile类具有fileFormat属性,该属性具有AVAudioFormat类型。 AVAudioFormat has a streamDescription property which returns an UnsafePointer<AudioStreamBasicDescription> as a read-only property. AVAudioFormat具有streamDescription属性,该属性返回UnsafePointer<AudioStreamBasicDescription>作为只读属性。

I'd like to see what the values are in this struct and converting to a Dictionary seems like it might be reasonable goal. 我想看看此结构中的值是什么,并将其转换为Dictionary似乎是合理的目标。 In fact, there already seems to be a "settings" property on the AVAudioFormat Class that does this but I'm interested in understanding the "right way" to access the values stored in the UnsafePointer myself. 实际上, AVAudioFormat类上似乎已经存在执行此操作的“设置”属性,但我有兴趣了解“正确的方法”以访问自己存储在UnsafePointer的值。

From the docs Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html 从文档“ Discussion: Returns the AudioStreamBasicDescription (ASBD) struct, for use with lower-level audio APIs https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioFormat_Class/index.html

Is there a way to do an unsafe conversion after checking if the struct is not nil? 检查结构是否不为零后,有没有办法进行不安全的转换? Would I use an unsafeBitCast here? 我会在这里使用unsafeBitCast吗? I'm hesitant to jump into this too much as I've read that it's "extremely dangerous"... 我很犹豫地跳入这个话题,因为我已经知道这是“极其危险的” ...

I realize I can access the underlying memory with the following: 我意识到我可以通过以下方式访问基础内存:

let audioFileURL:NSURL = NSBundle.mainBundle().URLForResource("FILENAME", with Extension: "mp3")
var file:AVAudioFile?
do {
  file = try AVAudioFile(forReading: audioFileURL)
  guard let file = file else {
    fatalError("file must not be nil")
  }
}catch {
  print(error)
}

let format = file.processingFormat
let asbd:AudioStreamBasicDescription = format.streamDescription.memory

Is that dangerous and would I need to dealloc for some reason after creating the asbd constant? 那很危险吗,在创建asbd常量后我是否需要出于某种原因进行释放?

I've tried to follow along with the post here http://sitepoint.com/using-legacy-c-apis-swift but I'm just not getting it... Looking for any direction on best practices here. 我尝试按照http://sitepoint.com/using-legacy-c-apis-swift上的帖子进行操作,但我只是听不懂...在此处寻找最佳做法的任何指导。

Update: 更新:

Doing a bit more research, it seems that it might be possible to create the dictionary using reflect based off this post: http://freecake.angelodipaolo.org/simple-reflection-in-swift/ 做更多的研究,似乎有可能使用基于本文的反射来创建字典: http : //freecake.angelodipaolo.org/simple-reflection-in-swift/

Here's what I have so far: 这是我到目前为止的内容:

let asbdMirror = reflect(asbd)
var asbdDict = [String: Any]()
for i in 0..<asbdMirror.count {
  let (propertyName, childMirror) = asbdMirror[i]
  asbdDict[propertyName] = childMirror.value
} 

Any reason this is a bad idea? 任何理由这是一个坏主意吗?

You are doing everything correctly, you can access all the values in the description with asbd.mSampleRate and such. 您可以正确执行所有操作,可以使用asbd.mSampleRate等访问描述中的所有值。 It wouldn't make sense to convert it to a dictionary because that's just not what it is, there are no keys for the values. 将其转换为字典是没有意义的,因为那不是它,没有值的键。

You also don't have to dealloc anything when working with pointers like this unless you allocate one yourself (when using malloc or alloc ) 除非您自己分配一个指针(使用mallocalloc ),否则在使用此类指针时也不必释放任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM