Add online LSTM transducer model (#25)

This commit is contained in:
Fangjun Kuang
2023-02-18 21:35:15 +08:00
committed by GitHub
parent b2d96c1d9a
commit cb8f85ff83
28 changed files with 1315 additions and 984 deletions

View File

@@ -1,20 +1,6 @@
/**
* Copyright 2022 Xiaomi Corporation (authors: Fangjun Kuang)
*
* See LICENSE for clarification regarding multiple authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// sherpa/csrc/wave-reader.cc
//
// Copyright (c) 2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/wave-reader.h"
@@ -31,19 +17,44 @@ namespace {
// Note: We assume little endian here
// TODO(fangjun): Support big endian
struct WaveHeader {
void Validate() const {
// F F I R
assert(chunk_id == 0x46464952);
assert(chunk_size == 36 + subchunk2_size);
// E V A W
assert(format == 0x45564157);
assert(subchunk1_id == 0x20746d66);
assert(subchunk1_size == 16); // 16 for PCM
assert(audio_format == 1); // 1 for PCM
assert(num_channels == 1); // we support only single channel for now
assert(byte_rate == sample_rate * num_channels * bits_per_sample / 8);
assert(block_align == num_channels * bits_per_sample / 8);
assert(bits_per_sample == 16); // we support only 16 bits per sample
bool Validate() const {
// F F I R
if (chunk_id != 0x46464952) {
return false;
}
// E V A W
if (format != 0x45564157) {
return false;
}
if (subchunk1_id != 0x20746d66) {
return false;
}
if (subchunk1_size != 16) { // 16 for PCM
return false;
}
if (audio_format != 1) { // 1 for PCM
return false;
}
if (num_channels != 1) { // we support only single channel for now
return false;
}
if (byte_rate != (sample_rate * num_channels * bits_per_sample / 8)) {
return false;
}
if (block_align != (num_channels * bits_per_sample / 8)) {
return false;
}
if (bits_per_sample != 16) { // we support only 16 bits per sample
return false;
}
return true;
}
// See
@@ -52,7 +63,7 @@ struct WaveHeader {
// https://www.robotplanet.dk/audio/wav_meta_data/riff_mci.pdf
void SeekToDataChunk(std::istream &is) {
// a t a d
while (subchunk2_id != 0x61746164) {
while (is && subchunk2_id != 0x61746164) {
// const char *p = reinterpret_cast<const char *>(&subchunk2_id);
// printf("Skip chunk (%x): %c%c%c%c of size: %d\n", subchunk2_id, p[0],
// p[1], p[2], p[3], subchunk2_size);
@@ -80,44 +91,61 @@ static_assert(sizeof(WaveHeader) == 44, "");
// Read a wave file of mono-channel.
// Return its samples normalized to the range [-1, 1).
std::vector<float> ReadWaveImpl(std::istream &is, float *sample_rate) {
std::vector<float> ReadWaveImpl(std::istream &is, float expected_sample_rate,
bool *is_ok) {
WaveHeader header;
is.read(reinterpret_cast<char *>(&header), sizeof(header));
assert(static_cast<bool>(is));
header.Validate();
if (!is) {
*is_ok = false;
return {};
}
if (!header.Validate()) {
*is_ok = false;
return {};
}
header.SeekToDataChunk(is);
if (!is) {
*is_ok = false;
return {};
}
*sample_rate = header.sample_rate;
if (expected_sample_rate != header.sample_rate) {
*is_ok = false;
return {};
}
// header.subchunk2_size contains the number of bytes in the data.
// As we assume each sample contains two bytes, so it is divided by 2 here
std::vector<int16_t> samples(header.subchunk2_size / 2);
is.read(reinterpret_cast<char *>(samples.data()), header.subchunk2_size);
assert(static_cast<bool>(is));
if (!is) {
*is_ok = false;
return {};
}
std::vector<float> ans(samples.size());
for (int32_t i = 0; i != ans.size(); ++i) {
ans[i] = samples[i] / 32768.;
}
*is_ok = true;
return ans;
}
} // namespace
std::vector<float> ReadWave(const std::string &filename,
float expected_sample_rate) {
float expected_sample_rate, bool *is_ok) {
std::ifstream is(filename, std::ifstream::binary);
float sample_rate;
auto samples = ReadWaveImpl(is, &sample_rate);
if (expected_sample_rate != sample_rate) {
std::cerr << "Expected sample rate: " << expected_sample_rate
<< ". Given: " << sample_rate << ".\n";
exit(-1);
}
return ReadWave(is, expected_sample_rate, is_ok);
}
std::vector<float> ReadWave(std::istream &is, float expected_sample_rate,
bool *is_ok) {
auto samples = ReadWaveImpl(is, expected_sample_rate, is_ok);
return samples;
}