-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): add option to saving recorded audio as wav on startRealtim…
…eTranscribe (#132) * feat(ios): add option to saving recorded audio as wav on startRealtimeTranscribe * feat(example): add audioOutputPath example & play button * feat(ios): use sliceNSamples to concatShortBuffers * feat: add todo
- Loading branch information
Showing
10 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
@interface RNWhisperAudioUtils : NSObject | ||
|
||
+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceNSamples:(NSMutableArray<NSNumber *> *)sliceNSamples; | ||
+ (void)saveWavFile:(NSData *)rawData audioOutputFile:(NSString *)audioOutputFile; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#import "RNWhisperAudioUtils.h" | ||
#import "whisper.h" | ||
|
||
@implementation RNWhisperAudioUtils | ||
|
||
+ (NSData *)concatShortBuffers:(NSMutableArray<NSValue *> *)buffers sliceNSamples:(NSMutableArray<NSNumber *> *)sliceNSamples { | ||
NSMutableData *outputData = [NSMutableData data]; | ||
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)]; | ||
} | ||
return outputData; | ||
} | ||
|
||
+ (void)saveWavFile:(NSData *)rawData audioOutputFile:(NSString *)audioOutputFile { | ||
NSMutableData *outputData = [NSMutableData data]; | ||
|
||
// WAVE header | ||
[outputData appendData:[@"RIFF" dataUsingEncoding:NSUTF8StringEncoding]]; // chunk id | ||
int chunkSize = CFSwapInt32HostToLittle(36 + rawData.length); | ||
[outputData appendBytes:&chunkSize length:sizeof(chunkSize)]; | ||
[outputData appendData:[@"WAVE" dataUsingEncoding:NSUTF8StringEncoding]]; // format | ||
[outputData appendData:[@"fmt " dataUsingEncoding:NSUTF8StringEncoding]]; // subchunk 1 id | ||
|
||
int subchunk1Size = CFSwapInt32HostToLittle(16); | ||
[outputData appendBytes:&subchunk1Size length:sizeof(subchunk1Size)]; | ||
|
||
short audioFormat = CFSwapInt16HostToLittle(1); // PCM | ||
[outputData appendBytes:&audioFormat length:sizeof(audioFormat)]; | ||
|
||
short numChannels = CFSwapInt16HostToLittle(1); // mono | ||
[outputData appendBytes:&numChannels length:sizeof(numChannels)]; | ||
|
||
int sampleRate = CFSwapInt32HostToLittle(WHISPER_SAMPLE_RATE); | ||
[outputData appendBytes:&sampleRate length:sizeof(sampleRate)]; | ||
|
||
// (bitDepth * sampleRate * channels) >> 3 | ||
int byteRate = CFSwapInt32HostToLittle(WHISPER_SAMPLE_RATE * 1 * 16 / 8); | ||
[outputData appendBytes:&byteRate length:sizeof(byteRate)]; | ||
|
||
// (bitDepth * channels) >> 3 | ||
short blockAlign = CFSwapInt16HostToLittle(16 / 8); | ||
[outputData appendBytes:&blockAlign length:sizeof(blockAlign)]; | ||
|
||
// bitDepth | ||
short bitsPerSample = CFSwapInt16HostToLittle(16); | ||
[outputData appendBytes:&bitsPerSample length:sizeof(bitsPerSample)]; | ||
|
||
[outputData appendData:[@"data" dataUsingEncoding:NSUTF8StringEncoding]]; // subchunk 2 id | ||
int subchunk2Size = CFSwapInt32HostToLittle((int)rawData.length); | ||
[outputData appendBytes:&subchunk2Size length:sizeof(subchunk2Size)]; | ||
|
||
// Audio data | ||
[outputData appendData:rawData]; | ||
|
||
// Save to file | ||
[outputData writeToFile:audioOutputFile atomically:YES]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters