Swift 3 AVAudioEngine 设置麦克风输入格式
Swift 3 AVAudioEngine set microphone input format
我想在 iOS 上使用 Swift 3 处理从麦克风读取的字节。我目前使用 AVAudioEngine.
print(inputNode.inputFormat(forBus: bus).settings)
print(inputNode.inputFormat(forBus: bus).formatDescription)
这给了我以下输出:
["AVNumberOfChannelsKey": 1, "AVLinearPCMBitDepthKey": 32, "AVSampleRateKey": 16000, "AVLinearPCMIsNonInterleaved": 1, "AVLinearPCMIsBigEndianKey": 0, "AVFormatIDKey": 1819304813, "AVLinearPCMIsFloatKey": 1]
<CMAudioFormatDescription 0x14d5bbb0 [0x3a5fb7d8]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 16000.000000
mFormatID: 'lpcm'
mFormatFlags: 0x29
mBytesPerPacket: 4
mFramesPerPacket: 1
mBytesPerFrame: 4
mChannelsPerFrame: 1
mBitsPerChannel: 32 }
cookie: {(null)}
ACL: {(null)}
FormatList Array: {(null)}
}
extensions: {(null)}
}
问题是我要将数据发送到的服务器不期望 32 位浮点数而是 16 位无符号整数。我想我必须更改 mFormatFlags。有人知道我该怎么做吗?正确的值是多少?
生成的字节流应该等同于我在 android 使用
获得的字节流
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLES_PER_SECOND,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
recordSegmentSizeBytes);
我试过这个:
let cfmt = AVAudioCommonFormat.pcmFormatInt16
inputNode.inputFormat(forBus: bus) = AVAudioFormat(commonFormat: cfmt, sampleRate: 16000.0, channels: 1, interleaved: false)
但是遇到这个错误
Cannot assign to value: function call returns immutable value
有什么想法吗?
天哪,我想我明白了。我太瞎了,没看到你可以指定 installTap 回调的格式。这似乎有效
let audioEngine = AVAudioEngine()
func startRecording() {
let inputNode = audioEngine.inputNode!
let bus = 0
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: false)
inputNode.installTap(onBus: bus, bufferSize: 2048, format: format) { // inputNode.inputFormat(forBus: bus)
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
let values = UnsafeBufferPointer(start: buffer.int16ChannelData![0], count: Int(buffer.frameLength))
let arr = Array(values)
print(arr)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("Error info: \(error)")
}
}
我想在 iOS 上使用 Swift 3 处理从麦克风读取的字节。我目前使用 AVAudioEngine.
print(inputNode.inputFormat(forBus: bus).settings)
print(inputNode.inputFormat(forBus: bus).formatDescription)
这给了我以下输出:
["AVNumberOfChannelsKey": 1, "AVLinearPCMBitDepthKey": 32, "AVSampleRateKey": 16000, "AVLinearPCMIsNonInterleaved": 1, "AVLinearPCMIsBigEndianKey": 0, "AVFormatIDKey": 1819304813, "AVLinearPCMIsFloatKey": 1]
<CMAudioFormatDescription 0x14d5bbb0 [0x3a5fb7d8]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 16000.000000
mFormatID: 'lpcm'
mFormatFlags: 0x29
mBytesPerPacket: 4
mFramesPerPacket: 1
mBytesPerFrame: 4
mChannelsPerFrame: 1
mBitsPerChannel: 32 }
cookie: {(null)}
ACL: {(null)}
FormatList Array: {(null)}
}
extensions: {(null)}
}
问题是我要将数据发送到的服务器不期望 32 位浮点数而是 16 位无符号整数。我想我必须更改 mFormatFlags。有人知道我该怎么做吗?正确的值是多少?
生成的字节流应该等同于我在 android 使用
获得的字节流AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLES_PER_SECOND,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
recordSegmentSizeBytes);
我试过这个:
let cfmt = AVAudioCommonFormat.pcmFormatInt16
inputNode.inputFormat(forBus: bus) = AVAudioFormat(commonFormat: cfmt, sampleRate: 16000.0, channels: 1, interleaved: false)
但是遇到这个错误
Cannot assign to value: function call returns immutable value
有什么想法吗?
天哪,我想我明白了。我太瞎了,没看到你可以指定 installTap 回调的格式。这似乎有效
let audioEngine = AVAudioEngine()
func startRecording() {
let inputNode = audioEngine.inputNode!
let bus = 0
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: 16000.0, channels: 1, interleaved: false)
inputNode.installTap(onBus: bus, bufferSize: 2048, format: format) { // inputNode.inputFormat(forBus: bus)
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
let values = UnsafeBufferPointer(start: buffer.int16ChannelData![0], count: Int(buffer.frameLength))
let arr = Array(values)
print(arr)
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch {
print("Error info: \(error)")
}
}