Android Audio Streaming - Getting Static Noise on AudioTrack

Asy*_*sym 4 streaming android pcm audiotrack

I have a streaming server running on localhost. When I try to stream audio from it from my Android application. I'm getting static noise most of the time (The kind you get on radio). Sometimes the complete audio is static noise, sometimes a part of it, and sometimes the audio plays just fine, so I'm not sure what's going wrong.

Here's the streaming code from my android application:

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();
Run Code Online (Sandbox Code Playgroud)

Please help me to fix the code to solve the problem. I'm not able to hear the sound properly as I described before.

Also, the server response is fine since I'm able to save it to a file using Python code. The saved file is being played just fine.

>>> 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)
...
Run Code Online (Sandbox Code Playgroud)

Update: Writing the inputstream to file and then playing from file to audiotrack works just fine...

Asy*_*sym 6

最后,我解决了这个问题。事实证明,AudioTrack这不喜欢将不一致数量的数据写入其中,并因此而导致静态噪声。这是正在写入的字节数序列AudioTrack之前,这是造成问题的原因1248,3439,5152,5152,3834,..., 823(不一致)。因此,我研究了使用的readFully方法DataInputStream并解决了静态噪声问题。字节计数顺序现在看起来5152,5152,5152,..., 5152(一致)。但是现在的问题是读取由于跳过的剩余字节EOFException。因此,我必须实现自己的方法来解决此问题。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我的最终代码如下:

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();
Run Code Online (Sandbox Code Playgroud)

现在,我的音频播放良好,没有任何静噪。希望这可以帮助其他与我有同样问题的人。