Skip to content

Commit

Permalink
Merge pull request coqui-ai#2269 from iarahealth/node-model-buffer
Browse files Browse the repository at this point in the history
Allow loading model from buffer in nodejs
  • Loading branch information
reuben authored Aug 9, 2022
2 parents 90331e4 + 1ee66c4 commit 7784fca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions native_client/javascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,18 @@ export class Model {
_impl: any;

/**
* @param aModelPath The path to the frozen model graph.
* @param aModelData Either the path to the frozen model graph or the frozen model's bytes.
* @param loadFromBytes Wheter to load the model from bytes or from a file.
*
* @throws on error
*/
constructor(aModelPath: string) {
constructor(aModelData: string | Uint8Array, loadFromBytes: boolean = false) {
this._impl = null;

const [status, impl] = binding.CreateModel(aModelPath);
let status, impl;
if (loadFromBytes) [status, impl] = binding.CreateModelFromBuffer(aModelData);
else [status, impl] = binding.CreateModel(aModelData);

if (status !== 0) {
throw `CreateModel failed: ${binding.ErrorCodeToErrorMessage(status)} (0x${status.toString(16)})`;
}
Expand Down
3 changes: 2 additions & 1 deletion native_client/javascript/stt.i
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ using namespace node;

// apply to STT_FeedAudioContent and STT_SpeechToText
%apply (short* IN_ARRAY1, int DIM1) {(const short* aBuffer, unsigned int aBufferSize)};

// apply the buffer typemap to STT_CreateModelFromBuffer
%apply (short* IN_ARRAY1, int DIM1) {(const char *aModelBuffer, unsigned int aBufferSize)};

// make sure the string returned by SpeechToText is freed
%typemap(newfree) char* "STT_FreeString($1);";
Expand Down

0 comments on commit 7784fca

Please sign in to comment.