1
This commit is contained in:
@@ -20,12 +20,26 @@
|
||||
|
||||
@implementation KBVoiceToTextManager
|
||||
|
||||
- (void)setDeepgramEnabled:(BOOL)deepgramEnabled {
|
||||
if (_deepgramEnabled == deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
_deepgramEnabled = deepgramEnabled;
|
||||
if (!deepgramEnabled) {
|
||||
[self.deepgramManager cancel];
|
||||
[self resetTranscript];
|
||||
} else {
|
||||
[self.deepgramManager prepareConnection];
|
||||
}
|
||||
}
|
||||
|
||||
- (instancetype)initWithInputBar:(KBVoiceInputBar *)inputBar {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_inputBar = inputBar;
|
||||
_inputBar.delegate = self;
|
||||
_fullText = [[NSMutableString alloc] init];
|
||||
_deepgramEnabled = YES;
|
||||
[self setupDeepgram];
|
||||
}
|
||||
return self;
|
||||
@@ -38,10 +52,16 @@
|
||||
#pragma mark - Public Methods
|
||||
|
||||
- (void)prepareConnection {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
[self.deepgramManager prepareConnection];
|
||||
}
|
||||
|
||||
- (void)disconnect {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
[self.deepgramManager disconnect];
|
||||
}
|
||||
|
||||
@@ -70,8 +90,12 @@
|
||||
|
||||
- (void)voiceInputBarDidBeginRecording:(KBVoiceInputBar *)inputBar {
|
||||
[self resetTranscript];
|
||||
inputBar.statusText = @"正在连接...";
|
||||
[self.deepgramManager start];
|
||||
if (self.deepgramEnabled) {
|
||||
inputBar.statusText = @"正在连接...";
|
||||
[self.deepgramManager start];
|
||||
} else {
|
||||
inputBar.statusText = @"正在录音...";
|
||||
}
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector
|
||||
(voiceToTextManagerDidBeginRecording:)]) {
|
||||
@@ -80,8 +104,12 @@
|
||||
}
|
||||
|
||||
- (void)voiceInputBarDidEndRecording:(KBVoiceInputBar *)inputBar {
|
||||
inputBar.statusText = @"正在识别...";
|
||||
[self.deepgramManager stopAndFinalize];
|
||||
if (self.deepgramEnabled) {
|
||||
inputBar.statusText = @"正在识别...";
|
||||
[self.deepgramManager stopAndFinalize];
|
||||
} else {
|
||||
inputBar.statusText = @"录音结束";
|
||||
}
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector
|
||||
(voiceToTextManagerDidEndRecording:)]) {
|
||||
@@ -92,7 +120,9 @@
|
||||
- (void)voiceInputBarDidCancelRecording:(KBVoiceInputBar *)inputBar {
|
||||
inputBar.statusText = @"已取消";
|
||||
[self resetTranscript];
|
||||
[self.deepgramManager cancel];
|
||||
if (self.deepgramEnabled) {
|
||||
[self.deepgramManager cancel];
|
||||
}
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector
|
||||
(voiceToTextManagerDidCancelRecording:)]) {
|
||||
@@ -103,10 +133,16 @@
|
||||
#pragma mark - DeepgramStreamingManagerDelegate
|
||||
|
||||
- (void)deepgramStreamingManagerDidConnect {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
self.inputBar.statusText = @"正在聆听...";
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidDisconnect:(NSError *_Nullable)error {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
@@ -119,10 +155,16 @@
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidUpdateRMS:(float)rms {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
[self.inputBar updateVolumeRMS:rms];
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidReceiveInterimTranscript:(NSString *)text {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
NSString *displayText = text ?: @"";
|
||||
if (self.fullText.length > 0 && displayText.length > 0) {
|
||||
displayText =
|
||||
@@ -141,6 +183,9 @@
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidReceiveFinalTranscript:(NSString *)text {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
if (text.length > 0) {
|
||||
if (self.fullText.length > 0) {
|
||||
[self.fullText appendString:@" "];
|
||||
@@ -160,6 +205,9 @@
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidFail:(NSError *)error {
|
||||
if (!self.deepgramEnabled) {
|
||||
return;
|
||||
}
|
||||
self.inputBar.statusText = @"识别失败";
|
||||
if ([self.delegate respondsToSelector:@selector
|
||||
(voiceToTextManager:didFailWithError:)]) {
|
||||
|
||||
Reference in New Issue
Block a user