-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,407 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,6 @@ lib/ | |
# React Native Codegen | ||
ios/generated | ||
android/generated | ||
|
||
# SPM | ||
.spm.pods/ |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#define DR_WAV_IMPLEMENTATION | ||
#include "utils.h" | ||
#include "dr_wav.h" | ||
|
||
|
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,11 @@ | ||
#ifdef __cplusplus | ||
#import "bark.h" | ||
#endif | ||
|
||
@interface BarkContext : NSObject | ||
|
||
+ (BarkContext *)initWithModelPath:(NSString *)model_path params:(NSDictionary *)ns_params; | ||
- (void)dealloc; | ||
- (NSDictionary *)generate:(NSString *)text out_path:(NSString *)out_path threads:(NSInteger)threads sample_rate:(NSInteger)sample_rate; | ||
|
||
@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,64 @@ | ||
#import "BarkContext.h" | ||
#import "Convert.h" | ||
|
||
#include "utils.h" | ||
#include <vector> | ||
#include <thread> | ||
|
||
@interface BarkContext () | ||
|
||
@property (nonatomic, assign) bark_context *context; | ||
@property (nonatomic, assign) int sample_rate; | ||
@property (nonatomic, assign) int n_threads; | ||
|
||
@end | ||
|
||
@implementation BarkContext | ||
|
||
+ (BarkContext *)initWithModelPath:(NSString *)model_path params:(NSDictionary *)ns_params { | ||
self = [super init]; | ||
if (self) { | ||
int seed = 0; | ||
if (ns_params && ns_params[@"seed"]) seed = [ns_params[@"seed"] intValue]; | ||
self.sample_rate = 24000; | ||
if (ns_params && ns_params[@"sample_rate"]) self.sample_rate = [ns_params[@"sample_rate"] intValue]; | ||
self.n_threads = -1; | ||
if (ns_params && ns_params[@"n_threads"]) self.n_threads = [ns_params[@"n_threads"] intValue]; | ||
if (self.n_threads < 0) self.n_threads = std::thread::hardware_concurrency() << 1; | ||
if (self.n_threads == 0) self.n_threads = 1; | ||
bark_context_params params = [Convert convert_params:ns_params]; | ||
try { | ||
self.context = bark_load_model(model_path, params, seed); | ||
} catch (const std::exception &e) { | ||
@throw [NSException exceptionWithName:@"BarkContext" reason:[NSString stringWithUTF8String:e.what()] userInfo:nil]; | ||
} | ||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
bark_free(self.context); | ||
self.context = NULL; | ||
[super dealloc]; | ||
} | ||
|
||
- (NSDictionary *)generate:(NSString *)text out_path:(NSString *)out_path { | ||
try { | ||
bool success = bark_generate_audio(self.context, text, self.n_threads); | ||
} catch (const std::exception &e) { | ||
@throw [NSException exceptionWithName:@"BarkContext" reason:[NSString stringWithUTF8String:e.what()] userInfo:nil]; | ||
} | ||
if (success) { | ||
int audio_samples = bark_get_audio_data_size(self.context); | ||
const float *audio_data = bark_get_audio_data(self.context); | ||
std::vector<float> audio_data_vec(audio_data, audio_data + audio_samples); | ||
barkrn::pcmToWav(audio_data_vec, self.sample_rate, out_path); | ||
} | ||
return @{ | ||
@"success": @(success), | ||
@"load_time": @(bark_get_load_time(self.context)), | ||
@"eval_time": @(bark_get_eval_time(self.context)) | ||
}; | ||
} | ||
|
||
@end |
Oops, something went wrong.