java - 用 AudioRecord Android 替换 RandomAccessFile
问题描述
我正在使用 deepspeech 的 tflite 模型并使用 wav 文件对文本进行语音,因为他们的文档用于RandomAccessFile
读取 wav 文件:
private void doInference(String audioFile) {
long inferenceExecTime = 0;
this._startInference.setEnabled(false);
this.newModel(this._tfliteModel.getText().toString());
this._tfliteStatus.setText("Extracting audio features ...");
try {
RandomAccessFile wave = new RandomAccessFile(audioFile, "r");
wave.seek(20); char audioFormat = this.readLEChar(wave);
assert (audioFormat == 1); // 1 is PCM
// tv_audioFormat.setText("audioFormat=" + (audioFormat == 1 ? "PCM" : "!PCM"));
wave.seek(22); char numChannels = this.readLEChar(wave);
assert (numChannels == 1); // MONO
// tv_numChannels.setText("numChannels=" + (numChannels == 1 ? "MONO" : "!MONO"));
wave.seek(24); int sampleRate = this.readLEInt(wave);
assert (sampleRate == this._m.sampleRate()); // desired sample rate
// tv_sampleRate.setText("sampleRate=" + (sampleRate == 16000 ? "16kHz" : "!16kHz"));
wave.seek(34); char bitsPerSample = this.readLEChar(wave);
assert (bitsPerSample == 16); // 16 bits per sample
// tv_bitsPerSample.setText("bitsPerSample=" + (bitsPerSample == 16 ? "16-bits" : "!16-bits" ));
wave.seek(40); int bufferSize = this.readLEInt(wave);
assert (bufferSize > 0);
// tv_bufferSize.setText("bufferSize=" + bufferSize);
wave.seek(44);
byte[] bytes = new byte[bufferSize];
wave.readFully(bytes);
short[] shorts = new short[bytes.length/2];
// to turn bytes to shorts as either big endian or little endian.
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
this._tfliteStatus.setText("Running inference ...");
long inferenceStartTime = System.currentTimeMillis();
// sphinx-doc: java_ref_inference_start
String decoded = this._m.stt(shorts, shorts.length);
// sphinx-doc: java_ref_inference_stop
inferenceExecTime = System.currentTimeMillis() - inferenceStartTime;
this._decodedString.setText(decoded);
我想处理来自麦克风的数据,我该怎么做AudioRecord
解决方案
推荐阅读
- python - 单例的哪种用法最合适/pythonic?
- c# - 文件流读取数组到行 C#
- python - Python numpy 方法/属性比 numpy 函数快吗?
- python - 如何使用 Selenium 更新 INITIAL_REDUX_STATE?
- javascript - 从 img 标签中检索值
- javascript - 父范围没有触发 React 中的子重新渲染
- spring - 从数据库动态更新权限(spring security)
- python - 如何使用 DLL 依赖项打包 Python 项目
- linux - Logitech MX 键的 Azerty 键盘布局错误 Linux 中的 Azerty 版本
- javascript - 如何通过在javascript中升序对我的数组进行排序