-
Notifications
You must be signed in to change notification settings - Fork 435
Add Speech-to-Text sample to illustrate one way to call a bidirectional streaming API. #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
This is example is for the non-NIO implementation; do we really want to add new samples for that? (Also, using bidi APIs should be much more straightforward with the NIO version.) |
|
@MrMage I got some questions about how to make streaming calls with the original APIs. I plan to add a NIO version when we have Cocoapods support for that. |
MrMage
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few stylistic suggestions. If it were framework code, I'd probably request some more edits, but as an example it is sufficient I guess.
| static var sharedInstance = AudioController() | ||
|
|
||
| deinit { | ||
| AudioComponentInstanceDispose(remoteIOUnit!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove the semicolons (also below)
| } | ||
|
|
||
| func prepare(specifiedSampleRate: Int) -> OSStatus { | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra newline
| return status | ||
| } | ||
|
|
||
| let bus1 : AudioUnitElement = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please remove the space before the colon (also below)
| } | ||
|
|
||
| func recordingCallback( | ||
| inRefCon:UnsafeMutableRawPointer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please add spaces after the colons
| inBusNumber:UInt32, | ||
| inNumberFrames:UInt32, | ||
| ioData:UnsafeMutablePointer<AudioBufferList>?) -> OSStatus { | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra newlines
|
|
||
| static let sharedInstance = SpeechRecognitionService() | ||
|
|
||
| func streamAudioData(_ audioData: NSData, completion: @escaping SpeechRecognitionCompletionHandler) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use Data instead of NSData?
| print("update send error > \(error)") | ||
| } | ||
| } | ||
| // self.signal(UIColor.green); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this?
| self.button.setTitle("LISTENING (tap to stop)", for: .normal) | ||
| let audioSession = AVAudioSession.sharedInstance() | ||
| do { | ||
| try audioSession.setCategory(AVAudioSession.Category.record) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try! instead?
| @IBOutlet weak var textView: UITextView! | ||
| @IBOutlet weak var button: UIButton! | ||
|
|
||
| var audioData: NSMutableData! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use Data?
|
|
||
| if (audioData.length > chunkSize) { | ||
| SpeechRecognitionService.sharedInstance.streamAudioData(audioData, | ||
| completion: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove completion: and just pass the closure as a trailing closure instead?
glbrntt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really cool example Tim!
|
|
||
| ## Quickstart | ||
| - Clone this repo and `cd` into this directory. | ||
| - Run `./COMPILE-PROTOS.sh` to generate the Protocol Buffer and gRPC support files. Note that this requires protoc and the protoc-gen-swift and protoc-gen-swiftgrpc plugins. You can get the plugins by running `swift build` in the root of the grpc-swift repository. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add protoc to the prerequisites
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the plugins need to be statically linked? i.e. make plugins instead of swift build?
|
|
||
| var window: UIWindow? | ||
|
|
||
| func application |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: formatting looks off here
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
return true
}| import Foundation | ||
| import SwiftGRPC | ||
|
|
||
| let API_KEY = "YOUR_API_KEY" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW: you can add a compiler warning (/error) here so that it's easier to find: #warning("You need to set your API Key")
Do we need to do the NIO example with Cocoapods? The Xcode 11 support for SwiftPM would make this easier. Also I'd be happy to put that example together. |
I guess we could be fine without it :-) |
|
@glbrntt We don't need this to use Cocoapods - I think it would be fine/great to replace all Cocoapods examples with SwiftPM/Xcode 11 ones. |
| return | ||
| } | ||
| self.button.setTitle("LISTENING (tap to stop)", for: .normal) | ||
| let audioSession = AVAudioSession.sharedInstance() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these two lines necessary, they're also done in AudioController.prepare?
| } | ||
|
|
||
| class AudioController { | ||
| var remoteIOUnit: AudioComponentInstance? // optional to allow it to be an inout argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, this is the microphone, right? microphoneIOUnit would be clearer if so.
| AudioComponentInstanceDispose(remoteIOUnit!) | ||
| } | ||
|
|
||
| func prepare(specifiedSampleRate: Int) -> OSStatus { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is an example and the following isn't really the subject of the example but it would be useful to provide more of a description as to what each part of this is doing. Breaking it up into a couple of functions would also help readability!
| // Set format for mic input (bus 1) on RemoteIO's output scope | ||
| var asbd = AudioStreamBasicDescription() | ||
| asbd.mSampleRate = sampleRate | ||
| asbd.mFormatID = kAudioFormatLinearPCM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth adding a comment saying that the initial request config is linked to this config.
|
Closing this due to inactivity for now; please let us know if you would like to pick it up again. |
No description provided.