首页 > 解决方案 > 用 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

标签: javaaudio-recordingrandomaccessfile

解决方案


推荐阅读