Android 音频流 - 在 AudioTrack 上获取静态噪声

Android Audio Streaming - Getting Static Noise on AudioTrack

我在本地主机上有一个流媒体服务器 运行。当我尝试从我的 Android 应用程序流式传输音频时。大多数时候我都会收到静电噪声(收音机里听到的那种)。有时完整的音频是静态噪声,有时是其中的一部分,有时音频播放得很好,所以我不确定出了什么问题。

这是来自我的 android 应用程序的流代码:

new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URI uri = URI.create("http://192.168.1.6:5000/api/tts");
                            HttpURLConnection urlConnection = (HttpURLConnection) uri.toURL().openConnection();
                            urlConnection.setRequestMethod("POST");
                            urlConnection.setRequestProperty("Content-Type", "application/json");
                            urlConnection.setRequestProperty("x-access-token", credentials.getAccessToken());
                            urlConnection.setRequestProperty("Accept", "*");
                            urlConnection.setDoInput(true);
                            urlConnection.setDoOutput(true);
                            urlConnection.connect();
                            OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
                            String body = "{\"text\": \"" + text + "\", \"ttsLang\": \"" + language + "\"}";
                            Log.d("TTS_HTTP", body);
                            osw.write(body);
                            osw.flush();
                            osw.close();
                            Log.d("TTS_OUT", credentials.getAccessToken());
                            Log.d("TTS_OUT", urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage());

                            // define the buffer size for audio track
                            int SAMPLE_RATE = 16000;
                            int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT);
                            if (bufferSize == AudioTrack.ERROR || bufferSize == AudioTrack.ERROR_BAD_VALUE) {
                                bufferSize = SAMPLE_RATE * 2;
                            }
                            bufferSize *= 2;

                            AudioTrack audioTrack = new AudioTrack(
                                    AudioManager.STREAM_MUSIC,
                                    SAMPLE_RATE,
                                    AudioFormat.CHANNEL_OUT_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT,
                                    bufferSize*2,
                                    AudioTrack.MODE_STREAM);
                            byte[] buffer = new byte[bufferSize];
                            InputStream is = urlConnection.getInputStream();
                            int count;

                            audioTrack.play();
                            while ((count = is.read(buffer, 0, bufferSize)) > -1) {
                                Log.d("TTS_COUNT", count + "");
                                audioTrack.write(buffer, 0, count);
                            }
                            is.close();
                            audioTrack.stop();
                            audioTrack.release();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        ).start();

请帮我修改代码以解决问题。我无法像之前描述的那样正常听到声音。

此外,服务器响应很好,因为我可以使用 Python 代码将其保存到文件中。保存的文件正在正常播放。

>>> import requests
>>> import wave
>>> with wave.open("output.wav", "wb") as f:
...   f.setframerate(16000)  # 16khz
...   f.setnchannels(1)  # mono channel
...   f.setsampwidth(2)  # 16-bit audio
...   res = requests.post("http://192.168.1.6:5000/api/tts", headers={"x-access-token": token}, json={"text": "Hello, would you like to have some tea", "ttsLang": "en-us"}, stream=True)
...   for i in res.iter_content(chunk_size=16*1024):
...     f.writeframes(i)
...

更新:将输入流写入文件,然后从文件播放到音轨效果很好...

终于,我解决了这个问题。事实证明,AudioTrack 不喜欢向其写入不一致的数据量,并因此导致静态噪声。这是之前写入 AudioTrack 的字节计数序列,导致了问题 12483439515251523834, ... , 823 (不一致)。因此,我查看了 DataInputStreamreadFully 方法并使用了它并解决了静态噪声问题。字节计数序列现在看起来像 5152,5152,5152, ..., 5152(一致)。但现在的问题是读取由于 EOFException 而被跳过的 left-over 字节。所以我不得不实现自己的方法来解决这个问题。

public class TTSInputStream extends DataInputStream {
    public TTSInputStream(InputStream in) {
        super(in);
    }

    public final int readFullyUntilEof(byte b[]) throws IOException {
        return readFullyUntilEof(b, 0, b.length);
    }

    public final int readFullyUntilEof(byte b[], int off, int len) throws IOException {
        if (len < 0)
            throw new IndexOutOfBoundsException();
        int n = 0;
        while (n < len) {
            int count = in.read(b, off + n, len - n);
            if (count < 0)
                break;
            n += count;
        }
        return n;
    }
}

我的最终代码现在看起来像这样:

new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URI uri = URI.create("http://192.168.1.6:5000/api/tts");
                            HttpURLConnection urlConnection = (HttpURLConnection) uri.toURL().openConnection();
                            urlConnection.setRequestMethod("POST");
                            urlConnection.setRequestProperty("Content-Type", "application/json");
                            urlConnection.setRequestProperty("x-access-token", credentials.getAccessToken());
                            urlConnection.setRequestProperty("Accept", "*");
                            urlConnection.setChunkedStreamingMode(bufferSize);
                            urlConnection.setDoInput(true);
                            urlConnection.setDoOutput(true);
                            urlConnection.connect();
                            OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
                            String body = "{\"text\": \"" + text + "\", \"ttsLang\": \"" + language + "\"}";
                            Log.d("TTS_HTTP", body);
                            osw.write(body);
                            osw.flush();
                            osw.close();
                            Log.d("TTS_OUT", credentials.getAccessToken());
                            Log.d("TTS_OUT", urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage());

                            // define the buffer size for audio track
                            int SAMPLE_RATE = 16000;
                            int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT);
                            if (bufferSize == AudioTrack.ERROR || bufferSize == AudioTrack.ERROR_BAD_VALUE) {
                                bufferSize = SAMPLE_RATE * 2;
                            }
                            bufferSize *= 2;
                            TTSInputStream bis = new TTSInputStream(urlConnection.getInputStream());
                            AudioTrack audioTrack = new AudioTrack(
                                    AudioManager.STREAM_MUSIC,
                                    SAMPLE_RATE,
                                    AudioFormat.CHANNEL_OUT_MONO,
                                    AudioFormat.ENCODING_PCM_16BIT,
                                    bufferSize * 2,
                                    AudioTrack.MODE_STREAM);
                            byte[] buffer = new byte[bufferSize];
                            audioTrack.play();
                            int count;
                            while ((count = bis.readFullyUntilEof(buffer)) > 0) {
                                Log.d("TTS_COUNT", "Read " + count + " bytes.");
                                audioTrack.write(buffer, 0, buffer.length);
                            }
                            bis.close();
                            audioTrack.stop();
                            audioTrack.release();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
        ).start();

现在我的音频播放效果很好,没有任何静态噪音。希望这可以帮助遇到与我相同问题的其他人。