Skip to content

Commit

Permalink
feat(ios): use sliceNSamples to concatShortBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Sep 23, 2023
1 parent 5705f4f commit 1967ede
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ios/RNWhisperAudioUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@interface RNWhisperAudioUtils : NSObject

+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceSize:(int)sliceSize lastSliceSize:(int)lastSliceSize;
+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceNSamples:(NSMutableArray<NSNumber *> *)sliceNSamples;
+ (void)saveWavFile:(NSData *)rawData audioOutputFile:(NSString *)audioOutputFile;

@end
10 changes: 4 additions & 6 deletions ios/RNWhisperAudioUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

@implementation RNWhisperAudioUtils

+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceSize:(int)sliceSize lastSliceSize:(int)lastSliceSize {
+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceNSamples:(NSMutableArray<NSNumber *> *)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)];
}
Expand Down
3 changes: 1 addition & 2 deletions ios/RNWhisperContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
];
}
Expand Down

0 comments on commit 1967ede

Please sign in to comment.