Skip to content

Commit

Permalink
It help to do the math right.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael McCulloch committed Dec 18, 2022
1 parent 87e6cd7 commit fcca98c
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
Pair<Optional<float[]>, Long> byteBuffer = RustLib.endRec();

if (byteBuffer.first.isPresent()) {
// draw(byteBuffer.first.get());
draw(byteBuffer.first.get());
// signalTxt.setText(new String(byteBuffer.second / 1000_000 + " ms"));
Pair<String, Long> transcribeAudio = transcribeAudio(byteBuffer.first.get());
String transcribed = transcribeAudio.first.trim() + " ";
Expand Down Expand Up @@ -210,7 +210,7 @@ private void draw(float[] floats) {
}
//write the bitmap to file
try {
FileOutputStream out = new FileOutputStream(getFilesDir().getAbsolutePath() + "/spectrogram3.png");
FileOutputStream out = new FileOutputStream(getFilesDir().getAbsolutePath() + "/spectrogram" + System.nanoTime() + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (IOException e) {
Expand Down
Binary file modified app/src/main/jniLibs/arm64-v8a/librust.so
Binary file not shown.
Binary file modified app/src/main/jniLibs/x86_64/librust.so
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/rust/src/lina.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn dot_product(left: &[f32], right: &[f32]) -> f32 {
}
#[cfg(target_arch = "aarch64")]
// #[cfg(target_arch = "aarch64")]
/// I tried to beat the compiler. I lost. Badly. I suspect the compiler detects both the loop above and the looped function calls correctly and does a proper matrix multiplication
/// I tried to beat the compiler. I lost. Badly. This is because the latency cost for the vector instruction outweighs the benefit to using them, UNLESS, we allow the compiler to hide the latency behind many such calls at once:loop unrolling.
/// Calculates the dot product of two slices of `f32` values.
///
/// # Parameters
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/rust/src/spectrogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,18 @@ fn compute_power(fft_work_buffer: &[Complex32], n_fft: usize) -> Vec<f32> {
power_spectrum[0..n_fft].to_vec()
}

/// Append the log mel spectrogram to the mel spectrogram columns buffer.
fn append(table: &mut Vec<Vec<f32>>, column: &mut Vec<f32>) {}

/// Normalize the mel spectrogram columns buffer.
fn normalize(mel_spectrogram_columns: &mut [f32]) {
// Compute the maximum value of the mel spectrogram columns buffer.
mel_spectrogram_columns
.iter_mut()
.for_each(|x| *x = (*x).max(1e-10).log10());

let maximum_value = mel_spectrogram_columns
.iter()
.fold(-1e20f32, |acc, f| f.max(acc));

mel_spectrogram_columns
.iter_mut()
.for_each(|x| *x = ((*x).max(1e-10).log10().max(maximum_value - 8.0) + 4.0) / 4.0);
.for_each(|x| *x = ((*x).max(maximum_value - 8.0) + 4.0) / 4.0);
}

0 comments on commit fcca98c

Please sign in to comment.