gpt4 book ai didi

swift - 用 AVAudioRecorder 生成 AVAudioPCMBuffer

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:34 29 4
gpt4 key购买 nike

与 iOS 10 一起,Apple 发布了一个识别语音的新框架。可以通过附加 AVAudioPCMBuffers 或为 m4a 提供 URL 将数据传递到此框架。目前,语音识别使用前者进行工作,但这只有在有人完成并且不是实时的情况下才有可能。这是相关代码:

let audioSession = AVAudioSession.sharedInstance()
var audioRecorder:AVAudioRecorder!;
var soundURLGlobal:URL!;

function setUp(){
let recordSettings = [AVSampleRateKey : NSNumber(value: Float(44100.0)),
AVFormatIDKey : NSNumber(value: Int32(kAudioFormatMPEG4AAC)),
AVNumberOfChannelsKey : NSNumber(value: 1),
AVEncoderAudioQualityKey : NSNumber(value: Int32(AVAudioQuality.medium.rawValue))]

let fileManager = FileManager.default()
let urls = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)
let documentDirectory = urls[0] as NSURL
let soundURL = documentDirectory.appendingPathComponent("sound.m4a")
soundURLGlobal=soundURL;


do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioRecorder = AVAudioRecorder(url: soundURL!, settings: recordSettings)
audioRecorder.prepareToRecord()
} catch {}
}

function start(){
do {
try audioSession.setActive(true)
audioRecorder.record()
} catch {}
}

function stop(){
audioRecorder.stop()
let request=SFSpeechURLRecognitionRequest(url: soundURLGlobal!)
let recognizer=SFSpeechRecognizer();
recognizer?.recognitionTask(with: request, resultHandler: { (result, error) in
if(result!.isFinal){
print(result?.bestTranscription.formattedString)
}
})

}

我正在尝试转换它,但找不到从哪里获得 AVAudioPCMBuffer。

谢谢,

最佳答案

好话题。

你好 B 人

这里是主题和解决方案 Tap Mic Input Using AVAudioEngine in Swift

参见 Wwdc 2014 讲座502 - AVAudioEngine 实践捕获麦克风 => 在 20 分钟内使用点击代码创建缓冲区 => 在 21 .50

这里是swift 3代码

@IBAction func button01Pressed(_ sender: Any) {

let inputNode = audioEngine.inputNode
let bus = 0
inputNode?.installTap(onBus: bus, bufferSize: 2048, format: inputNode?.inputFormat(forBus: bus)) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

var theLength = Int(buffer.frameLength)
print("theLength = \(theLength)")

var samplesAsDoubles:[Double] = []
for i in 0 ..< Int(buffer.frameLength)
{
var theSample = Double((buffer.floatChannelData?.pointee[i])!)
samplesAsDoubles.append( theSample )
}

print("samplesAsDoubles.count = \(samplesAsDoubles.count)")

}

audioEngine.prepare()
try! audioEngine.start()

}

停止播放

func stopAudio()
{

let inputNode = audioEngine.inputNode
let bus = 0
inputNode?.removeTap(onBus: bus)
self.audioEngine.stop()

}

关于swift - 用 AVAudioRecorder 生成 AVAudioPCMBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954593/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com