diff --git a/native_client/client.cc b/native_client/client.cc index f108419b..1f7f78eb 100644 --- a/native_client/client.cc +++ b/native_client/client.cc @@ -45,7 +45,7 @@ struct meta_word { }; char* -CandidateTranscriptToString(CandidateTranscript* transcript) +CandidateTranscriptToString(const CandidateTranscript* transcript) { std::string retval = ""; for (int i = 0; i < transcript->num_tokens; i++) { @@ -56,7 +56,7 @@ CandidateTranscriptToString(CandidateTranscript* transcript) } std::vector -CandidateTranscriptToWords(CandidateTranscript* transcript) +CandidateTranscriptToWords(const CandidateTranscript* transcript) { std::vector word_list; @@ -101,7 +101,7 @@ CandidateTranscriptToWords(CandidateTranscript* transcript) } std::string -CandidateTranscriptToJSON(CandidateTranscript *transcript) +CandidateTranscriptToJSON(const CandidateTranscript *transcript) { std::ostringstream out_string; @@ -130,7 +130,7 @@ MetadataToJSON(Metadata* result) out_string << "{\n"; for (int j=0; j < result->num_transcripts; ++j) { - CandidateTranscript *transcript = &result->transcripts[j]; + const CandidateTranscript *transcript = &result->transcripts[j]; if (j == 0) { out_string << CandidateTranscriptToJSON(transcript); diff --git a/native_client/deepspeech.cc b/native_client/deepspeech.cc index d284a319..96989e04 100644 --- a/native_client/deepspeech.cc +++ b/native_client/deepspeech.cc @@ -478,14 +478,14 @@ DS_FreeMetadata(Metadata* m) if (m) { for (int i = 0; i < m->num_transcripts; ++i) { for (int j = 0; j < m->transcripts[i].num_tokens; ++j) { - free(m->transcripts[i].tokens[j].text); + free((void*)m->transcripts[i].tokens[j].text); } - delete[] m->transcripts[i].tokens; + free((void*)m->transcripts[i].tokens); } - delete[] m->transcripts; - delete m; + free((void*)m->transcripts); + free(m); } } diff --git a/native_client/deepspeech.h b/native_client/deepspeech.h index 6fb9645c..a8c29c93 100644 --- a/native_client/deepspeech.h +++ b/native_client/deepspeech.h @@ -24,13 +24,13 @@ typedef struct StreamingState StreamingState; */ typedef struct TokenMetadata { /** The text corresponding to this token */ - char* text; + const char* const text; /** Position of the token in units of 20ms */ - unsigned int timestep; + const unsigned int timestep; /** Position of the token in seconds */ - float start_time; + const float start_time; } TokenMetadata; /** @@ -39,14 +39,14 @@ typedef struct TokenMetadata { */ typedef struct CandidateTranscript { /** Array of TokenMetadata objects */ - TokenMetadata* tokens; + const TokenMetadata* const tokens; /** Size of the tokens array */ - unsigned int num_tokens; + const unsigned int num_tokens; /** Approximated confidence value for this transcript. This is roughly the * sum of the acoustic model logit values for each timestep/character that * contributed to the creation of this transcript. */ - double confidence; + const double confidence; } CandidateTranscript; /** @@ -54,9 +54,9 @@ typedef struct CandidateTranscript { */ typedef struct Metadata { /** Array of CandidateTranscript objects */ - CandidateTranscript* transcripts; + const CandidateTranscript* const transcripts; /** Size of the transcripts array */ - unsigned int num_transcripts; + const unsigned int num_transcripts; } Metadata; enum DeepSpeech_Error_Codes diff --git a/native_client/javascript/deepspeech.i b/native_client/javascript/deepspeech.i index 6b0151a4..cb3968c2 100644 --- a/native_client/javascript/deepspeech.i +++ b/native_client/javascript/deepspeech.i @@ -85,10 +85,6 @@ using namespace node; %ignore Metadata::num_transcripts; %ignore CandidateTranscript::num_tokens; -%immutable Metadata::transcripts; -%immutable CandidateTranscripts::tokens; -%immutable TokenMetadata::text; - %nodefaultctor Metadata; %nodefaultdtor Metadata; %nodefaultctor CandidateTranscript; diff --git a/native_client/modelstate.cc b/native_client/modelstate.cc index d4f16636..3cb06ac2 100644 --- a/native_client/modelstate.cc +++ b/native_client/modelstate.cc @@ -41,33 +41,35 @@ ModelState::decode_metadata(const DecoderState& state, size_t num_results) { vector out = state.decode(num_results); - size_t num_returned = out.size(); + unsigned int num_returned = out.size(); - std::unique_ptr metadata(new Metadata); - metadata->num_transcripts = num_returned; - - std::unique_ptr transcripts(new CandidateTranscript[num_returned]); + CandidateTranscript* transcripts = (CandidateTranscript*)malloc(sizeof(CandidateTranscript)*num_returned); for (int i = 0; i < num_returned; ++i) { - transcripts[i].num_tokens = out[i].tokens.size(); - transcripts[i].confidence = out[i].confidence; + TokenMetadata* tokens = (TokenMetadata*)malloc(sizeof(TokenMetadata)*out[i].tokens.size()); - std::unique_ptr tokens(new TokenMetadata[transcripts[i].num_tokens]); - - // Loop through each token for (int j = 0; j < out[i].tokens.size(); ++j) { - tokens[j].text = strdup(alphabet_.StringFromLabel(out[i].tokens[j]).c_str()); - tokens[j].timestep = out[i].timesteps[j]; - tokens[j].start_time = out[i].timesteps[j] * ((float)audio_win_step_ / sample_rate_); - - if (tokens[j].start_time < 0) { - tokens[j].start_time = 0; - } + TokenMetadata token { + strdup(alphabet_.StringFromLabel(out[i].tokens[j]).c_str()), // text + static_cast(out[i].timesteps[j]), // timestep + out[i].timesteps[j] * ((float)audio_win_step_ / sample_rate_), // start_time + }; + memcpy(&tokens[j], &token, sizeof(TokenMetadata)); } - transcripts[i].tokens = tokens.release(); + CandidateTranscript transcript { + tokens, // tokens + static_cast(out[i].tokens.size()), // num_tokens + out[i].confidence, // confidence + }; + memcpy(&transcripts[i], &transcript, sizeof(CandidateTranscript)); } - metadata->transcripts = transcripts.release(); - return metadata.release(); + Metadata* ret = (Metadata*)malloc(sizeof(Metadata)); + Metadata metadata { + transcripts, // transcripts + num_returned, // num_transcripts + }; + memcpy(ret, &metadata, sizeof(Metadata)); + return ret; } diff --git a/native_client/python/impl.i b/native_client/python/impl.i index 001a6165..259a5b5d 100644 --- a/native_client/python/impl.i +++ b/native_client/python/impl.i @@ -108,10 +108,6 @@ static PyObject *parent_reference() { } } -%immutable Metadata::transcripts; -%immutable CandidateTranscript::tokens; -%immutable TokenMetadata::text; - %nodefaultctor Metadata; %nodefaultdtor Metadata; %nodefaultctor CandidateTranscript;