-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceSendRecognizer.m
112 lines (92 loc) · 3.08 KB
/
VoiceSendRecognizer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// VoiceRecognizer.m
// app
//
// Created by Remi Robert on 16/04/16.
// Copyright © 2016 Remi Robert. All rights reserved.
//
#import "VoiceSendRecognizer.h"
#import "SinVoicePlayer.h"
#include "ESPcmPlayer.h"
#import "MyPcmPlayerImp.h"
static const char* const CODE_BOOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_";
@interface VoiceSendRecognizer ()
@property (nonnull, nonatomic, assign) SinVoicePlayer *mSinVoicePlayer;
@property (nonatomic, copy) void (^sendCompletion)(void);
@end
ESVoid onSinVoicePlayerStart(ESVoid* cbParam) {
VoiceSendRecognizer * vc = (__bridge VoiceSendRecognizer *)cbParam;
[vc onPlayData:vc];
}
ESVoid onSinVoicePlayerStop(ESVoid* cbParam) {
VoiceSendRecognizer * vc = (__bridge VoiceSendRecognizer *)cbParam;
[vc completed];
}
SinVoicePlayerCallback gSinVoicePlayerCallback = {onSinVoicePlayerStart, onSinVoicePlayerStop};
@implementation VoiceSendRecognizer {
ESPcmPlayer mPcmPlayer;
}
- (instancetype)init {
self = [super init];
if (self) {
mPcmPlayer.create = MyPcmPlayerImp_create;
mPcmPlayer.start = MyPcmPlayerImp_start;
mPcmPlayer.stop = MyPcmPlayerImp_stop;
mPcmPlayer.setParam = MyPcmPlayerImp_setParam;
mPcmPlayer.destroy = MyPcmPlayerImp_destroy;
self.mSinVoicePlayer = SinVoicePlayer_create2("com.sinvoice.demo",
"SinVoiceDemo",
&gSinVoicePlayerCallback,
(__bridge ESVoid *)(self), &mPcmPlayer);
mMaxEncoderIndex = SinVoicePlayer_getMaxEncoderIndex(self.mSinVoicePlayer);
}
return self;
}
- (void)completed {
self.sendCompletion();
}
- (void)onPlayData:(VoiceSendRecognizer *)data {
char ch[100] = { 0 };
for ( int i = 0; i < mPlayCount; ++i ) {
ch[i] = (char)data->mRates[i];
}
}
- (void)startPlay:(nonnull NSString *)string completion:(void (^)(void))sendingCompletion {
self.sendCompletion = sendingCompletion;
int index = 0;
const char* str = [string cStringUsingEncoding:NSUTF8StringEncoding];
mPlayCount = (int)strlen(str);
if (mMaxEncoderIndex < 255) {
int lenCodeBook = (int)strlen(CODE_BOOK);
int isOK = 1;
while (index < mPlayCount) {
int i = 0;
for ( i = 0; i < lenCodeBook; ++i ) {
if ( str[index] == CODE_BOOK[i] ) {
mRates[index] = i;
break;
}
}
if ( i >= lenCodeBook ) {
isOK = 0;
break;
}
++index;
}
if (isOK) {
SinVoicePlayer_play(self.mSinVoicePlayer, mRates, mPlayCount);
}
}
else {
int index = 0;
while (index < mPlayCount) {
mRates[index] = str[index];
++index;
}
SinVoicePlayer_play(self.mSinVoicePlayer, mRates, mPlayCount);
}
}
- (void)stopPlay {
SinVoicePlayer_stop(self.mSinVoicePlayer);
}
@end