Forked from akramhussein/GoogleSpeechToTextStreamingTap.swift
Created
February 4, 2021 05:08
-
-
Save KTRosenberg/e4dc888dd148b8e82c135b6130e0a1d6 to your computer and use it in GitHub Desktop.
AudioKit Tap to convert microphone data for Google Speech-to-Text API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class GoogleSpeechToTextStreamingTap { | |
internal var converter: AVAudioConverter! | |
@objc public init(_ input: AKNode?, sampleRate: Double = 16000.0) { | |
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16, sampleRate: sampleRate, channels: 1, interleaved: false)! | |
self.converter = AVAudioConverter(from: AudioKit.format, to: format) | |
self.converter?.sampleRateConverterAlgorithm = AVSampleRateConverterAlgorithm_Normal | |
self.converter?.sampleRateConverterQuality = .max | |
let sampleRateRatio = AKSettings.sampleRate / sampleRate | |
let sampleLength = 0.1 // 100ms | |
let inputSampleRate = AudioKit.format.sampleRate | |
let inputBufferSize = sampleLength * inputSampleRate // i.e 100ms of 44.1K = 4410 samples. | |
input?.avAudioNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount(inputBufferSize), format: nil) { buffer, time in | |
let capacity = Int(Double(buffer.frameCapacity) / sampleRateRatio) | |
let bufferPCM16 = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(capacity))! | |
var error: NSError? = nil | |
self.converter?.convert(to: bufferPCM16, error: &error) { inNumPackets, outStatus in | |
outStatus.pointee = AVAudioConverterInputStatus.haveData | |
return buffer | |
} | |
let channel = UnsafeBufferPointer(start: bufferPCM16.int16ChannelData!, count: 1) | |
let data = Data(bytes: channel[0], count: capacity * 2) | |
SpeechRecognitionService | |
.sharedInstance | |
.streamAudioData(data, | |
completion: { response, error in | |
if let error = error { | |
print("ERROR: \(error.localizedDescription)") | |
SpeechRecognitionService.sharedInstance.stopStreaming() | |
} else if let response = response { | |
print(response) | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment