-
Notifications
You must be signed in to change notification settings - Fork 39
/
SpeechToTextModule.m
312 lines (261 loc) · 10.8 KB
/
SpeechToTextModule.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// VoiceAddModule.m
// AstridiPhone
//
// Created by Sam Bosley on 10/7/11.
// Copyright (c) 2011 Todoroo. All rights reserved.
//
#import "SpeechToTextModule.h"
#import "SineWaveViewController.h"
#define FRAME_SIZE 110
@interface SpeechToTextModule ()
- (void)reset;
- (void)postByteData:(NSData *)data;
- (void)cleanUpProcessingThread;
@end
@implementation SpeechToTextModule
@synthesize delegate;
static void HandleInputBuffer (void *aqData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime, UInt32 inNumPackets,
const AudioStreamPacketDescription *inPacketDesc) {
AQRecorderState *pAqData = (AQRecorderState *) aqData;
if (inNumPackets == 0 && pAqData->mDataFormat.mBytesPerPacket != 0)
inNumPackets = inBuffer->mAudioDataByteSize / pAqData->mDataFormat.mBytesPerPacket;
// process speex
int packets_per_frame = pAqData->speex_samples_per_frame;
char cbits[FRAME_SIZE + 1];
for (int i = 0; i < inNumPackets; i+= packets_per_frame) {
speex_bits_reset(&(pAqData->speex_bits));
speex_encode_int(pAqData->speex_enc_state, ((spx_int16_t*)inBuffer->mAudioData) + i, &(pAqData->speex_bits));
int nbBytes = speex_bits_write(&(pAqData->speex_bits), cbits + 1, FRAME_SIZE);
cbits[0] = nbBytes;
[pAqData->encodedSpeexData appendBytes:cbits length:nbBytes + 1];
}
pAqData->mCurrentPacket += inNumPackets;
if (!pAqData->mIsRunning)
return;
AudioQueueEnqueueBuffer(pAqData->mQueue, inBuffer, 0, NULL);
}
static void DeriveBufferSize (AudioQueueRef audioQueue, AudioStreamBasicDescription *ASBDescription, Float64 seconds, UInt32 *outBufferSize) {
static const int maxBufferSize = 0x50000;
int maxPacketSize = ASBDescription->mBytesPerPacket;
if (maxPacketSize == 0) {
UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
AudioQueueGetProperty (audioQueue, kAudioQueueProperty_MaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize);
}
Float64 numBytesForTime = ASBDescription->mSampleRate * maxPacketSize * seconds;
*outBufferSize = (UInt32)(numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize);
}
- (id)init {
if ((self = [self initWithCustomDisplay:nil])) {
//
}
return self;
}
- (id)initWithCustomDisplay:(NSString *)nibName {
if ((self = [super init])) {
aqData.mDataFormat.mFormatID = kAudioFormatLinearPCM;
aqData.mDataFormat.mSampleRate = 16000.0;
aqData.mDataFormat.mChannelsPerFrame = 1;
aqData.mDataFormat.mBitsPerChannel = 16;
aqData.mDataFormat.mBytesPerPacket =
aqData.mDataFormat.mBytesPerFrame =
aqData.mDataFormat.mChannelsPerFrame * sizeof (SInt16);
aqData.mDataFormat.mFramesPerPacket = 1;
aqData.mDataFormat.mFormatFlags =
kLinearPCMFormatFlagIsSignedInteger
| kLinearPCMFormatFlagIsPacked;
memset(&(aqData.speex_bits), 0, sizeof(SpeexBits));
speex_bits_init(&(aqData.speex_bits));
aqData.speex_enc_state = speex_encoder_init(&speex_wb_mode);
int quality = 8;
speex_encoder_ctl(aqData.speex_enc_state, SPEEX_SET_QUALITY, &quality);
int vbr = 1;
speex_encoder_ctl(aqData.speex_enc_state, SPEEX_SET_VBR, &vbr);
speex_encoder_ctl(aqData.speex_enc_state, SPEEX_GET_FRAME_SIZE, &(aqData.speex_samples_per_frame));
aqData.mQueue = NULL;
if (nibName) {
sineWave = [[SineWaveViewController alloc] initWithNibName:nibName bundle:nil];
sineWave.delegate = self;
}
[self reset];
aqData.selfRef = self;
}
return self;
}
- (void)dealloc {
[processingThread cancel];
if (processing) {
[self cleanUpProcessingThread];
}
self.delegate = nil;
status.delegate = nil;
[status release];
sineWave.delegate = nil;
[sineWave release];
speex_bits_destroy(&(aqData.speex_bits));
speex_encoder_destroy(aqData.speex_enc_state);
[aqData.encodedSpeexData release];
AudioQueueDispose(aqData.mQueue, true);
[volumeDataPoints release];
[super dealloc];
}
- (BOOL)recording {
return aqData.mIsRunning;
}
- (void)reset {
if (aqData.mQueue != NULL)
AudioQueueDispose(aqData.mQueue, true);
UInt32 enableLevelMetering = 1;
AudioQueueNewInput(&(aqData.mDataFormat), HandleInputBuffer, &aqData, NULL, kCFRunLoopCommonModes, 0, &(aqData.mQueue));
AudioQueueSetProperty(aqData.mQueue, kAudioQueueProperty_EnableLevelMetering, &enableLevelMetering, sizeof(UInt32));
DeriveBufferSize(aqData.mQueue, &(aqData.mDataFormat), 0.5, &(aqData.bufferByteSize));
for (int i = 0; i < kNumberBuffers; i++) {
AudioQueueAllocateBuffer(aqData.mQueue, aqData.bufferByteSize, &(aqData.mBuffers[i]));
AudioQueueEnqueueBuffer(aqData.mQueue, aqData.mBuffers[i], 0, NULL);
}
[aqData.encodedSpeexData release];
aqData.encodedSpeexData = [[NSMutableData alloc] init];
[meterTimer invalidate];
[meterTimer release];
samplesBelowSilence = 0;
detectedSpeech = NO;
[volumeDataPoints release];
volumeDataPoints = [[NSMutableArray alloc] initWithCapacity:kNumVolumeSamples];
for (int i = 0; i < kNumVolumeSamples; i++) {
[volumeDataPoints addObject:[NSNumber numberWithFloat:kMinVolumeSampleValue]];
}
sineWave.dataPoints = volumeDataPoints;
}
- (void)beginRecording {
@synchronized(self) {
if (!self.recording && !processing) {
aqData.mCurrentPacket = 0;
aqData.mIsRunning = true;
[self reset];
AudioQueueStart(aqData.mQueue, NULL);
if (sineWave && [delegate respondsToSelector:@selector(showSineWaveView:)]) {
[delegate showSineWaveView:sineWave];
} else {
status = [[UIAlertView alloc] initWithTitle:@"Speak now!" message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
[status show];
}
meterTimer = [[NSTimer scheduledTimerWithTimeInterval:kVolumeSamplingInterval target:self selector:@selector(checkMeter) userInfo:nil repeats:YES] retain];
}
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (self.recording && buttonIndex == 0) {
[self stopRecording:YES];
}
}
- (void)sineWaveDoneAction {
if (self.recording)
[self stopRecording:YES];
else if ([delegate respondsToSelector:@selector(dismissSineWaveView:cancelled:)]) {
[delegate dismissSineWaveView:sineWave cancelled:NO];
}
}
- (void)cleanUpProcessingThread {
@synchronized(self) {
[processingThread release];
processingThread = nil;
processing = NO;
}
}
- (void)sineWaveCancelAction {
if (self.recording) {
[self stopRecording:NO];
} else {
if (processing) {
[processingThread cancel];
processing = NO;
}
if ([delegate respondsToSelector:@selector(dismissSineWaveView:cancelled:)]) {
[delegate dismissSineWaveView:sineWave cancelled:YES];
}
}
}
- (void)stopRecording:(BOOL)startProcessing {
@synchronized(self) {
if (self.recording) {
[status dismissWithClickedButtonIndex:-1 animated:YES];
[status release];
status = nil;
if ([delegate respondsToSelector:@selector(dismissSineWaveView:cancelled:)])
[delegate dismissSineWaveView:sineWave cancelled:!startProcessing];
AudioQueueStop(aqData.mQueue, true);
aqData.mIsRunning = false;
[meterTimer invalidate];
[meterTimer release];
meterTimer = nil;
if (startProcessing) {
[self cleanUpProcessingThread];
processing = YES;
processingThread = [[NSThread alloc] initWithTarget:self selector:@selector(postByteData:) object:aqData.encodedSpeexData];
[processingThread start];
if ([delegate respondsToSelector:@selector(showLoadingView)])
[delegate showLoadingView];
}
}
}
}
- (void)checkMeter {
AudioQueueLevelMeterState meterState;
AudioQueueLevelMeterState meterStateDB;
UInt32 ioDataSize = sizeof(AudioQueueLevelMeterState);
AudioQueueGetProperty(aqData.mQueue, kAudioQueueProperty_CurrentLevelMeter, &meterState, &ioDataSize);
AudioQueueGetProperty(aqData.mQueue, kAudioQueueProperty_CurrentLevelMeterDB, &meterStateDB, &ioDataSize);
[volumeDataPoints removeObjectAtIndex:0];
float dataPoint;
if (meterStateDB.mAveragePower > kSilenceThresholdDB) {
detectedSpeech = YES;
dataPoint = MIN(kMaxVolumeSampleValue, meterState.mPeakPower);
} else {
dataPoint = MAX(kMinVolumeSampleValue, meterState.mPeakPower);
}
[volumeDataPoints addObject:[NSNumber numberWithFloat:dataPoint]];
[sineWave updateWaveDisplay];
if (detectedSpeech) {
if (meterStateDB.mAveragePower < kSilenceThresholdDB) {
samplesBelowSilence++;
if (samplesBelowSilence > kSilenceThresholdNumSamples)
[self stopRecording:YES];
} else {
samplesBelowSilence = 0;
}
}
}
- (void)postByteData:(NSData *)byteData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:byteData];
[request addValue:@"audio/x-speex-with-header-byte; rate=16000" forHTTPHeaderField:@"Content-Type"];
[request setURL:url];
[request setTimeoutInterval:15];
NSURLResponse *response;
NSError *error = nil;
if ([processingThread isCancelled]) {
//NSLog(@"Caught cancel");
[self cleanUpProcessingThread];
[request release];
[pool drain];
return;
}
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[request release];
if ([processingThread isCancelled]) {
[self cleanUpProcessingThread];
[pool drain];
return;
}
[self performSelectorOnMainThread:@selector(gotResponse:) withObject:data waitUntilDone:NO];
[pool drain];
}
- (void)gotResponse:(NSData *)jsonData {
[self cleanUpProcessingThread];
[delegate didReceiveVoiceResponse:jsonData];
}
@end