Generate AVAudioPCMBuffer with AVAudioRecorder

2020-07-27 03:30发布

问题:

Along with iOS 10, apple released a new framework which recognizes speech. Data can be passed to this framework either by appending AVAudioPCMBuffers or giving a URL to a m4a. Currently, speech recognition works using the former but this is only possible after somebody has finished and is not in real time. Here is the code for that:

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)
        }
    })

}

I am trying to convert this but I cannot find where to get a AVAudioPCMBuffer.

Thanks,

回答1:

good topic.

Hi B Person

here is topic with solution Tap Mic Input Using AVAudioEngine in Swift

see lecture Wwdc 2014 502 - AVAudioEngine in Practice capture microphone => in 20 min create buffer with tap code => in 21 .50

here is swift 3 code

@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()

}

to stop audio

func stopAudio()
    {

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

    }


标签: swift ios10