Skip to content

Instantly share code, notes, and snippets.

View Jake-Prickett's full-sized avatar

Jake Prickett Jake-Prickett

  • Grand Rapids, MI
View GitHub Profile
class ArticleView: UIView {
@TitleLabel(text: "Property Wrappers are fun!")
var titleLabel: UILabel
@ErrorLabel(text: "Something bad happened, rut roh!")
var errorLabel: UILabel
// ...
}
@propertyWrapper
public class ErrorLabel {
public var wrappedValue: UILabel
public init(text: String) {
self.wrappedValue = UILabel()
wrappedValue.text = text
configureLabel()
}
@Jake-Prickett
Jake-Prickett / TitleLabel.swift
Last active May 28, 2020 00:14
propertyWrapper for creating a TitleLabel
@propertyWrapper
public class TitleLabel {
public var wrappedValue: UILabel
public init(text: String) {
self.wrappedValue = UILabel()
wrappedValue.text = text
configureLabel()
}
func processAudio(_ data: Data) {
audioData.append(data)
let chunkSize: Int = Int(0.1 * Constants.kSampleRate * 2)
if audioData.count > chunkSize {
speechService.stream(audioData) { [weak self] response in
guard let self = self else { return }
DispatchQueue.main.async {
func stream(_ data: Data,
completion: ((Google_Cloud_Speech_V1_StreamingRecognizeResponse) -> Void)? = nil) {
if !isStreaming {
call = client.streamingRecognize { (response) in
completion?(response)
}
isStreaming = true
let config = Google_Cloud_Speech_V1_RecognitionConfig.with {
client = Google_Cloud_Speech_V1_SpeechClient(channel: channel,
defaultCallOptions: callOptions)
var callOptions = CallOptions()
callOptions.customMetadata.add(name: "X-Goog-Api-Key",
value: Constants.kAPIKey)
callOptions.customMetadata.add(name: "X-Ios-Bundle-Identifier",
value: "com.SpeechToText-gRPC-iOS")
let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
let channel = ClientConnection
.secure(group: group)
.connect(host: "speech.googleapis.com", port: 443)
/// Usage: instantiate Google_Cloud_Speech_V1_SpeechClient, then call methods of this protocol to make API calls.
public protocol Google_Cloud_Speech_V1_SpeechClientProtocol {
func recognize(_ request: Google_Cloud_Speech_V1_RecognizeRequest, callOptions: CallOptions?) -> UnaryCall<Google_Cloud_Speech_V1_RecognizeRequest, Google_Cloud_Speech_V1_RecognizeResponse>
func longRunningRecognize(_ request: Google_Cloud_Speech_V1_LongRunningRecognizeRequest, callOptions: CallOptions?) -> UnaryCall<Google_Cloud_Speech_V1_LongRunningRecognizeRequest, Google_Longrunning_Operation>
func streamingRecognize(callOptions: CallOptions?, handler: @escaping (Google_Cloud_Speech_V1_StreamingRecognizeResponse) -> Void) -> BidirectionalStreamingCall<Google_Cloud_Speech_V1_StreamingRecognizeRequest, Google_Cloud_Speech_V1_StreamingRecognizeResponse>
}
public final class Google_Cloud_Speech_V1_SpeechClient: GRPCClient, Google_Cloud_Speech_V1_SpeechClientProtocol {
public let channel: GRPCChannel
public var defaultCallOpti
public struct Google_Cloud_Speech_V1_StreamingRecognizeResponse {
/// If set, returns a [google.rpc.Status][google.rpc.Status] message that
/// specifies the error for the operation.
public var error: Google_Rpc_Status {
get {return _error ?? Google_Rpc_Status()}
set {_error = newValue}
}
/// Returns true if `error` has been explicitly set.
public var hasError: Bool {return self._error != nil}