之前寫過一篇Swift與硬件打交道封裝的方法惦费,現(xiàn)在又開始做硬件開發(fā)了,主要方向是音頻(AudioUnit
)視頻的采集隘世、播放栈雳、合成、以及藍(lán)牙蝉娜、socket通訊唱较,這里繼續(xù)記錄下開發(fā)中封裝的一些方法,以便以后再次使用召川。
獲取AudioBuffer
中音頻數(shù)據(jù)的平均分貝值
這里是AudioBuffer
南缓,且采樣位數(shù)為16bit,如果是Data類型荧呐,也可以按照下文的方法將Data轉(zhuǎn)為UInt16數(shù)組再計(jì)算(如果采樣位數(shù)為8bit汉形,需要用Int8計(jì)算)
(DBSPL)
private func getVolumeValue(buffer: AudioBuffer) {
var pcmAll: Int = 0
let bufferPoint = UnsafeMutableBufferPointer<Int16>.init(buffer)
let bufferArray = Array(bufferPoint)
let len = bufferArray.count
for index in 0..<len {
let value = bufferArray[index]
pcmAll += Int(value) * Int(value)
}
let mean: Double = Double(pcmAll) / Double(len)
let volume: Double = 10 * log10(mean)
guard "\(volume)" != "nan" else { return }
print(volume)
//0-42 42-97
//42分貝以下認(rèn)為是靜音,97分貝認(rèn)為是最大
//如果需要轉(zhuǎn)換為0-1
let db = min(volume - 42, 97-42)
var value = 0.0
if db > 0 {
value = db/(97-42)
}
print(value)
}
附DBFS計(jì)算(電平表顯示)
private func getVolumeValue(buffer: AudioBuffer) {
let bufferPoint = UnsafeMutableBufferPointer<Int16>.init(buffer)
let originalArray = Array(bufferPoint)
let dic = [
64: 8,
128: 16,
256: 24,
512: 32,
1024: 64,
2048: 128
]
let base = dic[originalArray.count] ?? 16
var index = base
var bufferArray = [Int16]()
while index < originalArray.count {
bufferArray.append(originalArray[index])
index += base
}
DispatchQueue.global().async {
let maxValue = bufferArray.max() ?? 0
let minValue = bufferArray.min() ?? 0
let value = max(maxValue, abs(minValue))
let volume: Double = 20 * log10(Double(value)/65535)
guard !volume.isNaN else { return }
self.delegate?.audioRecorder(recorder: self, didUpdate: volume)
}
}
根據(jù)PCM數(shù)據(jù)長度生成WAV音頻的頭
func getWavHeader(asbd: WSASBD, pcmDataLen: Int) -> Data {
var wavHeader = Data(count: 44)
// RIFF
wavHeader[0 ... 3] = "RIFF".data(using: .ascii)!
// 04~07 文件長度倍阐,暫時不填
// wave
wavHeader[8 ... 11] = "WAVE".data(using: .ascii)!
// fmt
wavHeader[12 ... 15] = "fmt ".data(using: .ascii)!
// 過濾字節(jié) 00000010
wavHeader[16 ... 19] = withUnsafeBytes(of: UInt32(littleEndian: 16)) { Data($0) }
// 格式種類(值為1時,表示數(shù)據(jù)為線性pcm編碼)
wavHeader[20] = 1
wavHeader[21] = 0
// chanel
wavHeader[22] = UInt8(asbd.channels)
wavHeader[23] = 0
let samplerate = UInt32(littleEndian: UInt32(asbd.sampleRate))
wavHeader[24 ... 27] = withUnsafeBytes(of: samplerate) { Data($0) }
let bitRate = UInt32(littleEndian: UInt32(asbd.sampleRate) * asbd.channels * asbd.mBitsPerChannel / 8)
wavHeader[28 ... 31] = withUnsafeBytes(of: bitRate) { Data($0) }
let sampleBit = UInt16(littleEndian: UInt16(asbd.channels * asbd.mBitsPerChannel / 8))
wavHeader[32 ... 33] = withUnsafeBytes(of: sampleBit) { Data($0) }
wavHeader[34] = UInt8(asbd.mBitsPerChannel)
wavHeader[35] = 0
// data
wavHeader[36 ... 39] = "data".data(using: .ascii)!
// 04~07 文件長度
wavHeader[4 ... 7] = withUnsafeBytes(of: UInt32(littleEndian: UInt32(pcmDataLen + 44 - 8))) { Data($0) }
// 40-43 PCM數(shù)據(jù)大小
wavHeader[40 ... 43] = withUnsafeBytes(of: UInt32(littleEndian: UInt32(pcmDataLen))) { Data($0) }
return wavHeader
}
struct WSASBD {
/// 聲道數(shù)
var channels: UInt32 = 1
/// 采樣率:
var sampleRate: Double = 48000
/// 位深
var mBitsPerChannel: UInt32 = 16
}
檢測是否有耳機(jī)設(shè)備
監(jiān)聽設(shè)備變化的通知名字AVAudioSession.routeChangeNotification
private func getHeadPhoneState(){
var state = false
for outPort in AVAudioSession.sharedInstance().currentRoute.outputs {
if outPort.portType == .headphones ||
outPort.portType == .bluetoothA2DP ||
outPort.portType == .bluetoothHFP
{
state = true
break
}
}
hasHeadPhone = state
}
Double
類型與 Data
類型互轉(zhuǎn)
這里舉例Double類型概疆,其他類型可以以此類推
let value = Date().timeIntervalSince1970
//Double轉(zhuǎn)data
let data = withUnsafeBytes(of: value) { Data($0) }
//data轉(zhuǎn)Double
var value2 = 0.0
let _ = withUnsafeMutableBytes(of: &value2, { data.copyBytes(to: $0)} )
print(value2)
Data
轉(zhuǎn)Int16
數(shù)組
let data = Data([0, 7, 3, 2, 1, 0, 0, 4])
let int16array = data.withUnsafeBytes {
Array($0.bindMemory(to: Int16.self)).map(Int16.init(bigEndian:))
}
print(int16array)
Data
截取及移除
extension Data{
func getSubData(start: Int, num: Int) -> Data?{
guard start >= 0 && num >= 0 else { return nil }
guard self.count >= start + num else {
return nil
}
let startIndex = self.index(self.startIndex, offsetBy: start)
let endIndex = self.index(self.startIndex, offsetBy: start + num)
let range = startIndex..<endIndex
return self[range]
}
//或者
func getSubData2(start: Int, num: Int) -> Data?{
guard start >= 0 && num >= 0 else { return nil }
guard self.count >= start + num else {
return nil
}
let byte = [UInt8](self)
return Data(byte[start...start+num])
}
}
private func removeSubData(data: inout Data, start: Int, num: Int){
guard start >= 0 && num >= 0 else { return }
guard data.count >= start + num else { return }
let startIndex = data.index(data.startIndex, offsetBy: start)
let endIndex = data.index(data.startIndex, offsetBy: start + num)
let range = startIndex..<endIndex
data.removeSubrange(range)
}
十六進(jìn)制數(shù)據(jù)
與Data
互轉(zhuǎn) 以及大小端模式
在轉(zhuǎn)大小端時,需要先確定轉(zhuǎn)出的數(shù)據(jù)需要占幾個字節(jié)峰搪,例如需要占2字節(jié)則使用UInt16岔冀、需要占4字節(jié)用Uint32,以此類推概耻,例如我要將0x06
轉(zhuǎn)為2字節(jié)的Data
let bigEndianData = withUnsafeBytes(of: UInt16(bigEndian: UInt16(0x06))) { Data($0) } //大端 0x0006
var lc: UInt16 = 0
(bigEndianData as NSData).getBytes(&lc, range: NSMakeRange(0, bigEndianData.count))
let bigEndianValue = Int(UInt16(bigEndian: lc)) //6
let littleEndianData = withUnsafeBytes(of: UInt16(littleEndian: UInt16(0x06))) { Data($0) } //小端 0x0600
(littleEndianData as NSData).getBytes(&lc, range: NSMakeRange(0, littleEndianData.count))
let littleEndianValue = Int(UInt16(littleEndian: lc)) //6
獲取ip地址使套、子網(wǎng)掩碼罐呼、廣播地址
en0: WiFi
bridge100:熱點(diǎn)開啟方
func getWiFiAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return nil }
guard let firstAddr = ifaddr else { return nil }
for ifptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
let interface = ifptr.pointee
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) {
let name = String(cString: interface.ifa_name)
if name == "en0" || name == "bridge100" || name == "en1" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.pointee.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
var broadcast = "error"
ifptr.pointee.ifa_dstaddr.withMemoryRebound(to: sockaddr_in.self, capacity: 1) {
sockaddr in
if let addressCString = inet_ntoa(sockaddr.pointee.sin_addr) {
broadcast = String(cString: addressCString)
}
}
var net = interface.ifa_netmask.pointee
var netmaskName = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(&net, socklen_t(net.sa_len), &netmaskName, socklen_t(netmaskName.count),
nil, socklen_t(0), NI_NUMERICHOST)
let netmask = String(cString: netmaskName)
print("address: \(address), name:\(name), 廣播地址: \(broadcast), 子網(wǎng)掩碼: \(netmask)")
}
}
}
freeifaddrs(ifaddr)
return address
}