IBM Watson Text to Speech - Spring Boot WEBSERVICE returning audio to HTML5
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i have a chatbot solution and i'm trying to implement audio text-to-speech feature. After the chatbot return the answer from a question, i call a spring api that process the text converting it in a "audio/wav" file returned by Watson Text-to-Speech service.
The audio data returned by watson is ok, when i save in my computer, i can play without problem, but i'm having trouble returning this data to the javascript client. At the end of all process, i can play a audio file full of noise, but i dont know why. i have no experience with audio data and maybe for more experience developer, this can be a common mistake.
spring boot webservice
@PostMapping("/text-to-speech")
@Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Consumes(value = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte> textToSpeech(@RequestBody ConversationBean bean, HttpServletRequest request) {
try {
SpeechModel speechModel = new SpeechModelFactory().create(EPlataforma.IBM_BLUEMIX.getCodigo());
InputStream audio = speechModel.textToSpeech(bean.getInput());
byte arraySaida = IOUtils.toByteArray(audio);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("audio/wav"));
responseHeaders.set("Content-disposition", "attachment; filename=filename.wav");
return new ResponseEntity<byte>(arraySaida, responseHeaders, HttpStatus.OK);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
javascript call to spring boot service
function textToSpeech(text) {
console.log("textToSpeech: " + text);
var data = {
input: text
};
$.ajax({
url: urlTextToSpeech,
beforeSend: function (request) {
request.setRequestHeader("Authorization", tokenAuth);
},
cache: false,
timeout: 600000,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function (data, textStatus, request) {
console.log("textToSpeech return: ", data);
playByteArray(data);
},
error: function (request, error) {
console.log("error: " + JSON.stringify(request));
}
});
}
javascript receive and playing audio
function playByteArray(bytes) {
var array = getByteArray(bytes);
var isWAV = array[0] === 82 && array[1] === 73 && array[2] === 70 && array[3] === 70 &&
array[8] === 87 && array[9] === 65 && array[10] === 86 && array[11] === 69;
console.log("Is a valid WAV File? : " + isWAV);
/* TODO: replace the blob content with your byte */
var blob = new Blob(array, {type: "application/octet-stream"});
var fileName = "c:\temp\boomcake3.wav";
saveAs(blob, fileName);
var arrayBuffer = new ArrayBuffer(array.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < array.length; i++) {
bufferView[i] = array[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play();
});
}
// Play the loaded file
function play() {
console.log("audio decodificado, playing audio ...");
// Create a source node from the buffer
var source = context.createBufferSource();
console.log("step 2: ", source);
source.buffer = buf;
console.log("step 3: ", source.buffer);
// Connect to the final output node (the speakers)
source.connect(context.destination);
console.log("step 4 ", context.destination);
// Play immediately
source.start(0);
console.log("step 5 ", context.destination);
}
function getByteArray(dataStr) {
var array = new Uint8Array(dataStr.length);
for (var i = 0; i < dataStr.length; i++) {
array[i] = dataStr.charCodeAt(i);
}
return array;
}
spring-boot web-audio-api audiocontext watson-text-to-speech
add a comment |
i have a chatbot solution and i'm trying to implement audio text-to-speech feature. After the chatbot return the answer from a question, i call a spring api that process the text converting it in a "audio/wav" file returned by Watson Text-to-Speech service.
The audio data returned by watson is ok, when i save in my computer, i can play without problem, but i'm having trouble returning this data to the javascript client. At the end of all process, i can play a audio file full of noise, but i dont know why. i have no experience with audio data and maybe for more experience developer, this can be a common mistake.
spring boot webservice
@PostMapping("/text-to-speech")
@Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Consumes(value = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte> textToSpeech(@RequestBody ConversationBean bean, HttpServletRequest request) {
try {
SpeechModel speechModel = new SpeechModelFactory().create(EPlataforma.IBM_BLUEMIX.getCodigo());
InputStream audio = speechModel.textToSpeech(bean.getInput());
byte arraySaida = IOUtils.toByteArray(audio);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("audio/wav"));
responseHeaders.set("Content-disposition", "attachment; filename=filename.wav");
return new ResponseEntity<byte>(arraySaida, responseHeaders, HttpStatus.OK);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
javascript call to spring boot service
function textToSpeech(text) {
console.log("textToSpeech: " + text);
var data = {
input: text
};
$.ajax({
url: urlTextToSpeech,
beforeSend: function (request) {
request.setRequestHeader("Authorization", tokenAuth);
},
cache: false,
timeout: 600000,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function (data, textStatus, request) {
console.log("textToSpeech return: ", data);
playByteArray(data);
},
error: function (request, error) {
console.log("error: " + JSON.stringify(request));
}
});
}
javascript receive and playing audio
function playByteArray(bytes) {
var array = getByteArray(bytes);
var isWAV = array[0] === 82 && array[1] === 73 && array[2] === 70 && array[3] === 70 &&
array[8] === 87 && array[9] === 65 && array[10] === 86 && array[11] === 69;
console.log("Is a valid WAV File? : " + isWAV);
/* TODO: replace the blob content with your byte */
var blob = new Blob(array, {type: "application/octet-stream"});
var fileName = "c:\temp\boomcake3.wav";
saveAs(blob, fileName);
var arrayBuffer = new ArrayBuffer(array.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < array.length; i++) {
bufferView[i] = array[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play();
});
}
// Play the loaded file
function play() {
console.log("audio decodificado, playing audio ...");
// Create a source node from the buffer
var source = context.createBufferSource();
console.log("step 2: ", source);
source.buffer = buf;
console.log("step 3: ", source.buffer);
// Connect to the final output node (the speakers)
source.connect(context.destination);
console.log("step 4 ", context.destination);
// Play immediately
source.start(0);
console.log("step 5 ", context.destination);
}
function getByteArray(dataStr) {
var array = new Uint8Array(dataStr.length);
for (var i = 0; i < dataStr.length; i++) {
array[i] = dataStr.charCodeAt(i);
}
return array;
}
spring-boot web-audio-api audiocontext watson-text-to-speech
add a comment |
i have a chatbot solution and i'm trying to implement audio text-to-speech feature. After the chatbot return the answer from a question, i call a spring api that process the text converting it in a "audio/wav" file returned by Watson Text-to-Speech service.
The audio data returned by watson is ok, when i save in my computer, i can play without problem, but i'm having trouble returning this data to the javascript client. At the end of all process, i can play a audio file full of noise, but i dont know why. i have no experience with audio data and maybe for more experience developer, this can be a common mistake.
spring boot webservice
@PostMapping("/text-to-speech")
@Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Consumes(value = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte> textToSpeech(@RequestBody ConversationBean bean, HttpServletRequest request) {
try {
SpeechModel speechModel = new SpeechModelFactory().create(EPlataforma.IBM_BLUEMIX.getCodigo());
InputStream audio = speechModel.textToSpeech(bean.getInput());
byte arraySaida = IOUtils.toByteArray(audio);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("audio/wav"));
responseHeaders.set("Content-disposition", "attachment; filename=filename.wav");
return new ResponseEntity<byte>(arraySaida, responseHeaders, HttpStatus.OK);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
javascript call to spring boot service
function textToSpeech(text) {
console.log("textToSpeech: " + text);
var data = {
input: text
};
$.ajax({
url: urlTextToSpeech,
beforeSend: function (request) {
request.setRequestHeader("Authorization", tokenAuth);
},
cache: false,
timeout: 600000,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function (data, textStatus, request) {
console.log("textToSpeech return: ", data);
playByteArray(data);
},
error: function (request, error) {
console.log("error: " + JSON.stringify(request));
}
});
}
javascript receive and playing audio
function playByteArray(bytes) {
var array = getByteArray(bytes);
var isWAV = array[0] === 82 && array[1] === 73 && array[2] === 70 && array[3] === 70 &&
array[8] === 87 && array[9] === 65 && array[10] === 86 && array[11] === 69;
console.log("Is a valid WAV File? : " + isWAV);
/* TODO: replace the blob content with your byte */
var blob = new Blob(array, {type: "application/octet-stream"});
var fileName = "c:\temp\boomcake3.wav";
saveAs(blob, fileName);
var arrayBuffer = new ArrayBuffer(array.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < array.length; i++) {
bufferView[i] = array[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play();
});
}
// Play the loaded file
function play() {
console.log("audio decodificado, playing audio ...");
// Create a source node from the buffer
var source = context.createBufferSource();
console.log("step 2: ", source);
source.buffer = buf;
console.log("step 3: ", source.buffer);
// Connect to the final output node (the speakers)
source.connect(context.destination);
console.log("step 4 ", context.destination);
// Play immediately
source.start(0);
console.log("step 5 ", context.destination);
}
function getByteArray(dataStr) {
var array = new Uint8Array(dataStr.length);
for (var i = 0; i < dataStr.length; i++) {
array[i] = dataStr.charCodeAt(i);
}
return array;
}
spring-boot web-audio-api audiocontext watson-text-to-speech
i have a chatbot solution and i'm trying to implement audio text-to-speech feature. After the chatbot return the answer from a question, i call a spring api that process the text converting it in a "audio/wav" file returned by Watson Text-to-Speech service.
The audio data returned by watson is ok, when i save in my computer, i can play without problem, but i'm having trouble returning this data to the javascript client. At the end of all process, i can play a audio file full of noise, but i dont know why. i have no experience with audio data and maybe for more experience developer, this can be a common mistake.
spring boot webservice
@PostMapping("/text-to-speech")
@Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Consumes(value = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte> textToSpeech(@RequestBody ConversationBean bean, HttpServletRequest request) {
try {
SpeechModel speechModel = new SpeechModelFactory().create(EPlataforma.IBM_BLUEMIX.getCodigo());
InputStream audio = speechModel.textToSpeech(bean.getInput());
byte arraySaida = IOUtils.toByteArray(audio);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("audio/wav"));
responseHeaders.set("Content-disposition", "attachment; filename=filename.wav");
return new ResponseEntity<byte>(arraySaida, responseHeaders, HttpStatus.OK);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
javascript call to spring boot service
function textToSpeech(text) {
console.log("textToSpeech: " + text);
var data = {
input: text
};
$.ajax({
url: urlTextToSpeech,
beforeSend: function (request) {
request.setRequestHeader("Authorization", tokenAuth);
},
cache: false,
timeout: 600000,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function (data, textStatus, request) {
console.log("textToSpeech return: ", data);
playByteArray(data);
},
error: function (request, error) {
console.log("error: " + JSON.stringify(request));
}
});
}
javascript receive and playing audio
function playByteArray(bytes) {
var array = getByteArray(bytes);
var isWAV = array[0] === 82 && array[1] === 73 && array[2] === 70 && array[3] === 70 &&
array[8] === 87 && array[9] === 65 && array[10] === 86 && array[11] === 69;
console.log("Is a valid WAV File? : " + isWAV);
/* TODO: replace the blob content with your byte */
var blob = new Blob(array, {type: "application/octet-stream"});
var fileName = "c:\temp\boomcake3.wav";
saveAs(blob, fileName);
var arrayBuffer = new ArrayBuffer(array.length);
var bufferView = new Uint8Array(arrayBuffer);
for (i = 0; i < array.length; i++) {
bufferView[i] = array[i];
}
context.decodeAudioData(arrayBuffer, function(buffer) {
buf = buffer;
play();
});
}
// Play the loaded file
function play() {
console.log("audio decodificado, playing audio ...");
// Create a source node from the buffer
var source = context.createBufferSource();
console.log("step 2: ", source);
source.buffer = buf;
console.log("step 3: ", source.buffer);
// Connect to the final output node (the speakers)
source.connect(context.destination);
console.log("step 4 ", context.destination);
// Play immediately
source.start(0);
console.log("step 5 ", context.destination);
}
function getByteArray(dataStr) {
var array = new Uint8Array(dataStr.length);
for (var i = 0; i < dataStr.length; i++) {
array[i] = dataStr.charCodeAt(i);
}
return array;
}
spring-boot web-audio-api audiocontext watson-text-to-speech
spring-boot web-audio-api audiocontext watson-text-to-speech
asked Nov 23 '18 at 14:12
Cateno ViglioCateno Viglio
169524
169524
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448262%2fibm-watson-text-to-speech-spring-boot-webservice-returning-audio-to-html5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448262%2fibm-watson-text-to-speech-spring-boot-webservice-returning-audio-to-html5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown