From 1967edea3569376e23b6fd83b1b2f8f02f5d0fad Mon Sep 17 00:00:00 2001 From: Jhen Date: Sat, 23 Sep 2023 14:53:14 +0800 Subject: [PATCH] feat(ios): use sliceNSamples to concatShortBuffers --- ios/RNWhisperAudioUtils.h | 2 +- ios/RNWhisperAudioUtils.m | 10 ++++------ ios/RNWhisperContext.mm | 3 +-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ios/RNWhisperAudioUtils.h b/ios/RNWhisperAudioUtils.h index dffe2e5..3749daf 100644 --- a/ios/RNWhisperAudioUtils.h +++ b/ios/RNWhisperAudioUtils.h @@ -2,7 +2,7 @@ @interface RNWhisperAudioUtils : NSObject -+ (NSData *)concatShortBuffers:(NSMutableArray *)buffers sliceSize:(int)sliceSize lastSliceSize:(int)lastSliceSize; ++ (NSData *)concatShortBuffers:(NSMutableArray *)buffers sliceNSamples:(NSMutableArray *)sliceNSamples; + (void)saveWavFile:(NSData *)rawData audioOutputFile:(NSString *)audioOutputFile; @end diff --git a/ios/RNWhisperAudioUtils.m b/ios/RNWhisperAudioUtils.m index bd7db33..bd8d443 100644 --- a/ios/RNWhisperAudioUtils.m +++ b/ios/RNWhisperAudioUtils.m @@ -3,13 +3,11 @@ @implementation RNWhisperAudioUtils -+ (NSData *)concatShortBuffers:(NSMutableArray *)buffers sliceSize:(int)sliceSize lastSliceSize:(int)lastSliceSize { ++ (NSData *)concatShortBuffers:(NSMutableArray *)buffers sliceNSamples:(NSMutableArray *)sliceNSamples { NSMutableData *outputData = [NSMutableData data]; - for (NSValue *buffer in buffers) { - int size = sliceSize; - if (buffer == buffers.lastObject) { - size = lastSliceSize; - } + for (int i = 0; i < buffers.count; i++) { + int size = [sliceNSamples objectAtIndex:i].intValue; + NSValue *buffer = [buffers objectAtIndex:i]; short *bufferPtr = buffer.pointerValue; [outputData appendBytes:bufferPtr length:size * sizeof(short)]; } diff --git a/ios/RNWhisperContext.mm b/ios/RNWhisperContext.mm index f50226b..6acea4a 100644 --- a/ios/RNWhisperContext.mm +++ b/ios/RNWhisperContext.mm @@ -251,8 +251,7 @@ - (void)fullTranscribeSamples:(RNWhisperContextRecordState*) state { if (state->options[@"audioOutputPath"] != nil) { [RNWhisperAudioUtils saveWavFile:[RNWhisperAudioUtils concatShortBuffers:state->shortBufferSlices - sliceSize:state->audioSliceSec * WHISPER_SAMPLE_RATE - lastSliceSize:nSamplesOfIndex] + sliceNSamples:state->sliceNSamples] audioOutputFile:state->options[@"audioOutputPath"] ]; }