Skip to content

Commit

Permalink
Fix building errors on Android (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Mar 20, 2024
1 parent 29c55d4 commit 8a4623e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(kaldi-native-fbank CXX C)

set(KALDI_NATIVE_FBANK_VERSION "1.19.0")
set(KALDI_NATIVE_FBANK_VERSION "1.19.1")

# Disable warning about
#
Expand Down
1 change: 1 addition & 0 deletions kaldi-native-fbank/csrc/CPPLINT.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude_files=fftsg.cc
13 changes: 9 additions & 4 deletions kaldi-native-fbank/csrc/feature-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
// This file is copied/modified from kaldi/src/feat/feature-window.cc

#include "kaldi-native-fbank/csrc/feature-window.h"
#include "kaldi-native-fbank/csrc/kaldi-math.h"

#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>

#include "kaldi-native-fbank/csrc/kaldi-math.h"

namespace knf {

std::ostream &operator<<(std::ostream &os, const FrameExtractionOptions &opts) {
Expand Down Expand Up @@ -203,11 +204,14 @@ float InnerProduct(const float *a, const float *b, int32_t n) {
}

void Dither(float *d, int32_t n, float dither_value) {
if (dither_value == 0.0)
if (dither_value == 0.0) {
return;
}

RandomState rstate;
for (int32_t i = 0; i < n; i++)
for (int32_t i = 0; i < n; ++i) {
d[i] += RandGauss(&rstate) * dither_value;
}
}

static void Preemphasize(float *d, int32_t n, float preemph_coeff) {
Expand All @@ -228,8 +232,9 @@ void ProcessWindow(const FrameExtractionOptions &opts,
float *log_energy_pre_window /*= nullptr*/) {
int32_t frame_length = opts.WindowSize();

if (opts.dither != 0.0)
if (opts.dither != 0.0) {
Dither(window, frame_length, opts.dither);
}

if (opts.remove_dc_offset) {
RemoveDcOffset(window, frame_length);
Expand Down
10 changes: 6 additions & 4 deletions kaldi-native-fbank/csrc/feature-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ struct FrameExtractionOptions {
float samp_freq = 16000;
float frame_shift_ms = 10.0f; // in milliseconds.
float frame_length_ms = 25.0f; // in milliseconds.
float dither = 0.00003f; // Amount of dithering, 0.0 means no dither.
// Value 0.00003f is equivalent to 1.0 in kaldi.
float preemph_coeff = 0.97f; // Preemphasis coefficient.
bool remove_dc_offset = true; // Subtract mean of wave before FFT.

float dither = 0.00003f; // Amount of dithering, 0.0 means no dither.
// Value 0.00003f is equivalent to 1.0 in kaldi.

float preemph_coeff = 0.97f; // Preemphasis coefficient.
bool remove_dc_offset = true; // Subtract mean of wave before FFT.
std::string window_type = "povey"; // e.g. Hamming window
// May be "hamming", "rectangular", "povey", "hanning", "hann", "sine",
// "blackman".
Expand Down
9 changes: 6 additions & 3 deletions kaldi-native-fbank/csrc/kaldi-math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
//
// Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely)

// This file is an excerpt from kaldi/src/feat/kaldi-math.cc
// This file is an excerpt from kaldi/src/base/kaldi-math.cc

#include "kaldi-math.h"
#include "kaldi-native-fbank/csrc/kaldi-math.h"

#include <mutex> // NOLINT

namespace knf {

int Rand(struct RandomState* state) {
int Rand(struct RandomState *state) {
#if !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
// On Windows and Cygwin, just call Rand()
return rand();
#else
if (state) {
return rand_r(&(state->seed));
} else {
static std::mutex _RandMutex;
std::lock_guard<std::mutex> lock(_RandMutex);
return rand();
}
Expand Down
20 changes: 11 additions & 9 deletions kaldi-native-fbank/csrc/kaldi-math.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
//
// Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely)

// This file is an excerpt from kaldi/src/feat/kaldi-math.h
// This file is an excerpt from kaldi/src/base/kaldi-math.h

#pragma once
#ifndef KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_
#define KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_

#include <cmath> // logf, sqrtf, cosf
#include <cmath> // logf, sqrtf, cosf
#include <cstdlib> // RAND_MAX

#ifndef M_PI
Expand All @@ -22,7 +23,7 @@ namespace knf {
inline float Log(float x) { return logf(x); }

// Returns a random integer between 0 and RAND_MAX, inclusive
int Rand(struct RandomState* state = NULL);
int Rand(struct RandomState *state = NULL);

// State for thread-safe random number generator
struct RandomState {
Expand All @@ -31,13 +32,14 @@ struct RandomState {
};

/// Returns a random number strictly between 0 and 1.
inline float RandUniform(struct RandomState* state = NULL) {
return static_cast<float>((Rand(state) + 1.0) / (RAND_MAX+2.0));
inline float RandUniform(struct RandomState *state = NULL) {
return static_cast<float>((Rand(state) + 1.0) / (RAND_MAX + 2.0));
}

inline float RandGauss(struct RandomState* state = NULL) {
return static_cast<float>(sqrtf (-2 * Log(RandUniform(state)))
* cosf(2*M_PI*RandUniform(state)));
inline float RandGauss(struct RandomState *state = NULL) {
return static_cast<float>(sqrtf(-2 * Log(RandUniform(state))) *
cosf(2 * M_PI * RandUniform(state)));
}

} // namespace knf
#endif // KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_

0 comments on commit 8a4623e

Please sign in to comment.