Skip to content

Commit

Permalink
fix(cpp): str params should not be released early
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Dec 9, 2023
1 parent 3a78b10 commit 31e09aa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ struct whisper_full_params createFullParams(JNIEnv *env, jobject options) {
jstring prompt = readablemap::getString(env, options, "prompt", nullptr);
if (prompt != nullptr) {
params.initial_prompt = env->GetStringUTFChars(prompt, nullptr);
env->ReleaseStringUTFChars(prompt, params.initial_prompt);
env->DeleteLocalRef(prompt);
}
jstring language = readablemap::getString(env, options, "language", nullptr);
if (language != nullptr) {
params.language = env->GetStringUTFChars(language, nullptr);
env->ReleaseStringUTFChars(language, params.language);
env->DeleteLocalRef(language);
}
return params;
}
Expand Down Expand Up @@ -330,10 +330,10 @@ Java_com_rnwhisper_WhisperContext_createRealtimeTranscribeJob(
vad.freq_thold = readablemap::getFloat(env, options, "vadFreqThold", 100.0f);

jstring audio_output_path = readablemap::getString(env, options, "audioOutputPath", nullptr);
std::string *audio_output_path_str = nullptr;
const char* audio_output_path_str = nullptr;
if (audio_output_path != nullptr) {
audio_output_path_str = new std::string(env->GetStringUTFChars(audio_output_path, nullptr));
env->ReleaseStringUTFChars(audio_output_path, audio_output_path_str->c_str());
audio_output_path_str = env->GetStringUTFChars(audio_output_path, nullptr);
env->DeleteLocalRef(audio_output_path);
}
job->set_realtime_params(
vad,
Expand All @@ -357,6 +357,7 @@ Java_com_rnwhisper_WhisperContext_finishRealtimeTranscribeJob(

rnwhisper::job *job = rnwhisper::job_get(job_id);
if (job->audio_output_path != nullptr) {
RNWHISPER_LOG_INFO("job->params.language: %s\n", job->params.language);
std::vector<int> slice_n_samples_vec;
jint *slice_n_samples_arr = env->GetIntArrayElements(slice_n_samples, nullptr);
slice_n_samples_vec = std::vector<int>(slice_n_samples_arr, slice_n_samples_arr + env->GetArrayLength(slice_n_samples));
Expand All @@ -365,7 +366,7 @@ Java_com_rnwhisper_WhisperContext_finishRealtimeTranscribeJob(
// TODO: Append in real time so we don't need to keep all slices & also reduce memory usage
rnaudioutils::save_wav_file(
rnaudioutils::concat_short_buffers(job->pcm_slices, slice_n_samples_vec),
*job->audio_output_path
job->audio_output_path
);
}
rnwhisper::job_remove(job_id);
Expand Down
2 changes: 2 additions & 0 deletions cpp/rn-audioutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void save_wav_file(const std::vector<uint8_t>& raw, const std::string& file) {
output.write(reinterpret_cast<const char*>(data.data()), data.size());

output.close();

RNWHISPER_LOG_INFO("Saved audio file: %s\n", file.c_str());
}

} // namespace rnaudioutils
2 changes: 1 addition & 1 deletion cpp/rn-whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void job::set_realtime_params(
vad_params params,
int sec,
int slice_sec,
std::string* output_path
const char* output_path
) {
vad = params;
if (vad.vad_ms < 2000) vad.vad_ms = 2000;
Expand Down
4 changes: 2 additions & 2 deletions cpp/rn-whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ struct job {
vad_params vad;
int audio_sec = 0;
int audio_slice_sec = 0;
std::string* audio_output_path = nullptr;
const char* audio_output_path = nullptr;
std::vector<short *> pcm_slices;
void set_realtime_params(vad_params vad, int audio_sec, int audio_slice_sec, std::string* audio_output_path);
void set_realtime_params(vad_params vad, int sec, int slice_sec, const char* output_path);
bool vad_simple(int slice_index, int n_samples, int n);
void put_pcm_data(short* pcm, int slice_index, int n_samples, int n);
float* pcm_slice_to_f32(int slice_index, int size);
Expand Down
5 changes: 2 additions & 3 deletions ios/RNWhisperContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ - (void)prepareRealtime:(int)jobId options:(NSDictionary *)options {
self->recordState.sliceNSamples.push_back(0);

self->recordState.job = rnwhisper::job_new(jobId, [self createParams:options jobId:jobId]);
std::string audio_output_path = options[@"audioOutputPath"] != nil ? [options[@"audioOutputPath"] UTF8String] : "";
self->recordState.job->set_realtime_params(
{
.use_vad = options[@"useVad"] != nil ? [options[@"useVad"] boolValue] : false,
Expand All @@ -129,7 +128,7 @@ - (void)prepareRealtime:(int)jobId options:(NSDictionary *)options {
},
options[@"realtimeAudioSec"] != nil ? [options[@"realtimeAudioSec"] intValue] : 0,
options[@"realtimeAudioSliceSec"] != nil ? [options[@"realtimeAudioSliceSec"] intValue] : 0,
options[@"audioOutputPath"] != nil ? &audio_output_path : nullptr
options[@"audioOutputPath"] != nil ? [options[@"audioOutputPath"] UTF8String] : nullptr
);
self->recordState.isUseSlices = self->recordState.job->audio_slice_sec < self->recordState.job->audio_sec;

Expand Down Expand Up @@ -227,7 +226,7 @@ - (void)finishRealtimeTranscribe:(RNWhisperContextRecordState*) state result:(NS
// TODO: Append in real time so we don't need to keep all slices & also reduce memory usage
rnaudioutils::save_wav_file(
rnaudioutils::concat_short_buffers(state->job->pcm_slices, state->sliceNSamples),
*state->job->audio_output_path
state->job->audio_output_path
);
}
state->transcribeHandler(state->job->job_id, @"end", result);
Expand Down

0 comments on commit 31e09aa

Please sign in to comment.