之前写过一篇Swift与硬件打交道封装的方法,现在又开始做硬件开发了,主要方向是音频(AudioUnit
)视频的采集、播放、合成、以及蓝牙、socket通讯,这里继续记录下开发中封装的一些方法,以便以后再次使用。
获取AudioBuffer
中音频数据的平均分贝值
这里是AudioBuffer
,且采样位数为16bit,如果是Data类型,也可以按照下文的方法将Data转为UInt16数组再计算(如果采样位数为8bit,需要用Int8计算)
(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分贝以下认为是静音,97分贝认为是最大
//如果需要转换为0-1
let db = min(volume - 42, 97-42)
var value = 0.0
if db > 0 {
value = db/(97-42)
}
print(value)
}
附DBFS计算(电平表显示)
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)" != "nan" else { return }
self.delegate?.audioRecorder(recorder: self, didUpdate: volume)
}
}
根据PCM数据长度生成WAV音频的头
//此处是AudioUnit采集音频的参数,根据项目自身需求修改
struct AudioConst {
static let SampleRate: Int = 44100
static let Channels: UInt32 = 1
static let InputBus: AudioUnitElement = 1
static let OutputBus: AudioUnitElement = 0
static let BufferDuration: Int = 20
static let mDataByteSize: Int = 4096
static let mBitsPerChannel: UInt32 = 16
}
private var wavHeader: Data?
func getWavHeader(pcmDataLen: Int) -> Data{
if wavHeader == nil {
wavHeader = initWavHeader()
}
wavHeader![4...7] = withUnsafeBytes(of: UInt32(littleEndian: UInt32(pcmDataLen+44-8))) { Data($0) }
wavHeader![40...43] = withUnsafeBytes(of: UInt32(littleEndian: UInt32(pcmDataLen))) { Data($0) }
return wavHeader!
}
private func initWavHeader() -> 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)!
//过滤字节 00000010
wavHeader[16...19] = withUnsafeBytes(of: UInt32(littleEndian: 16)) { Data($0) }
//格式种类(值为1时,表示数据为线性pcm编码)
wavHeader[20] = 1
wavHeader[21] = 0
//chanel
wavHeader[22] = UInt8(AudioConst.Channels)
wavHeader[23] = 0
let samplerate = UInt32(littleEndian: UInt32(AudioConst.SampleRate))
wavHeader[24...27] = withUnsafeBytes(of: samplerate) { Data($0) }
let bitRate = UInt32(littleEndian: UInt32(AudioConst.SampleRate)*AudioConst.Channels*AudioConst.mBitsPerChannel/8)
wavHeader[28...31] = withUnsafeBytes(of: bitRate) { Data($0) }
let sampleBit = UInt16(littleEndian: UInt16(AudioConst.Channels*AudioConst.mBitsPerChannel/8))
wavHeader[32...33] = withUnsafeBytes(of: sampleBit) { Data($0) }
wavHeader[34] = UInt8(AudioConst.mBitsPerChannel)
wavHeader[35] = 0
//data
wavHeader[36...39] = "data".data(using: .ascii)!
//40-43 PCM数据大小,暂时不填
return wavHeader
}
使用场景,例如需要实时保存PCM数据(这里的情况,不需要在音频采集完后再更新head,不知道什么时候采集结束的情况)
检测是否有耳机设备
监听设备变化的通知名字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
类型互转
这里举例Double类型,其他类型可以以此类推
let value = Date().timeIntervalSince1970
//Double转data
let data = withUnsafeBytes(of: value) { Data($0) }
//data转Double
var value2 = 0.0
let _ = withUnsafeMutableBytes(of: &value2, { data.copyBytes(to: $0)} )
print(value2)
Data
转Int16
数组
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)
}
十六进制数据
与Data
互转 以及大小端模式
在转大小端时,需要先确定转出的数据需要占几个字节,例如需要占2字节则使用UInt16、需要占4字节用Uint32,以此类推,例如我要将0x06
转为2字节的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
网友评论