List of usage examples for javax.sound.sampled SourceDataLine isOpen
boolean isOpen();
From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java
public void synthesise(String utterance) throws Exception { try {// w ww . j av a2 s . co m log.fine("calling Nuance server to synthesise utterance \"" + utterance + "\""); HttpPost httppost = new HttpPost(ttsURI); httppost.addHeader("Content-Type", "text/plain"); httppost.addHeader("Accept", "audio/x-wav;codec=pcm;bit=16;rate=16000"); HttpEntity entity = new StringEntity(utterance); //HttpEntity entity = new ByteArrayEntity(utterance.getBytes("UTF-8")); httppost.setEntity(entity); HttpResponse response = ttsClient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (resEntity == null || response.getStatusLine().getStatusCode() != 200) { System.out.println("Response status: " + response.getStatusLine()); throw new Exception("Response status: " + response.getStatusLine()); } format = new AudioFormat(16000, 16, 1, true, false); System.out.println(response.getStatusLine().getStatusCode()); data = new byte[0]; write(resEntity.getContent()); httppost.releaseConnection(); //Get the file path String basepath = System.getProperty("user.home"); basepath = basepath + "/wav/" + LANGUAGE + "/" + VOICE; File dir = new File(basepath); if (!dir.exists()) { // attempt to create the directory here boolean successful = dir.mkdirs(); if (successful) { // creating the directory succeeded System.out.println("directory was created successfully"); } else { // creating the directory failed log.severe("failed trying to create the directory"); throw new Exception("failed trying to create the directory"); } return; } String fullpath = basepath + "/" + utterance.toLowerCase() + ".wav"; //Record the sound generateFile(data, new File(fullpath)); //Play the received sound SourceDataLine line = AudioSystem.getSourceDataLine(format); line.open(format); line.start(); rewind(); int nBytesRead = 0; byte[] abData = new byte[512 * 16]; while (nBytesRead != -1) { nBytesRead = read(abData, 0, abData.length); if (nBytesRead >= 0) { line.write(abData, 0, nBytesRead); } } line.drain(); if (line.isOpen()) { line.close(); } } catch (LineUnavailableException e) { log.warning("Audio line is unavailable: " + e); throw e; } catch (Exception e) { throw e; } }