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();
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)
...
from Android Audio Streaming - Getting Static Noise on AudioTrack
No comments:
Post a Comment