Reputation: 67
I am using the Microsoft azure Speech to text REST API. According to the docs, Ogg and Wav formats are supported in REST API. But, when I am sending the request for OGG audio file getting 400- Bad request error.
I am using following code to prepare request, And this is working for WAV audio format:
String url= "https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=simple";
private void connect(String extension) throws IOException {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (extension.equalsIgnoreCase(Constants.WAV))
connection.setRequestProperty("Content-type", "audio/wav; codecs=\"audio/pcm\"; samplerate=16000");
else if (extension.equalsIgnoreCase(Constants.OGG))
connection.setRequestProperty("Content-type", "audio/ogg; codecs=\"audio/opus\"");
connection.setRequestProperty("Accept", "application/json;text/xml");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setRequestProperty("Transfer-Encoding", "chunked");
connection.setRequestProperty("Expect", "100-continue");
connection.setChunkedStreamingMode(0); // 0 == default chunk size
connection.connect();
}
Using this for uploading file:
private void upload(InputStream inputStream) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
}}
Upvotes: 1
Views: 389
Reputation: 36
API is working fine for both WAV and OGG format. The request is also looking fine. The problem may be with the audio file sending in the request. If you want to use OGG audio file format then the audio file must have the following properties: OGG (Codec: Opus, Bitrate: 16-bit, Sample rate: 16 kHz, Chanel: mono)
Otherwise, you will get 400 bad request error. Make sure that codec is Opus because most of the OGG files have Vorbis codec which is not supported by the API.
You can use this website to convert an audio file in the required format.
Upvotes: 2