title: How to Set Sample Rate on Text to Speech - Android
body: <p>In the output of my text to Speech, I need to set Sampling rate about to 32000 Hz with Pitch - 1 and SpeechRate - 0.2 (which I already did). But I can't set Sample Rate.</p>
  
  <pre><code>tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
          @Override
          public void onInit(int status) {
              if(status != TextToSpeech.ERROR) {
                  tts.setLanguage(Locale.US);
                  tts.setSpeechRate((float) 0.2);
                  tts.setPitch((float) 1);
              }
          }
      }, TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS);
  </code></pre>
  
  <p>I used AudioTrack to set Sample Rate but it took lots of time because I have to first TTS synthesizeToFile then I play it in AudioTrack.</p>
  
  <pre><code>HashMap&lt;String, String&gt; myHasRead = new HashMap&lt;String, String&gt;();
  myHasRead.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, outPutS);
  String StorePath = Environment.getExternalStorageDirectory().getAbsolutePath();
  File myF = new File(StorePath+"/tempAudio.wav");
                              try {
                                  myF.createNewFile();
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                              tts.setOnUtteranceProgressListener(new TtsUtteranceListener());
                              tts.synthesizeToFile("Bla Bla bla",myHasRead, StorePath+"/tempAudio.wav");
  
  ....
  
  private class TtsUtteranceListener extends UtteranceProgressListener {
          @Override
          public void onStart(String utteranceId) {
  
          }
  
          @Override
          public void onDone(String utteranceId) {
              playWav();
          }
  
          @Override
          public void onError(String utteranceId) {
  
          }
      }
  
      public void playWav(){
          int minBufferSize = AudioTrack.getMinBufferSize(32000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
          int bufferSize = 512;
          AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 32000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
          String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
  
          int i = 0;
          byte[] s = new byte[bufferSize];
          try {
              FileInputStream fin = new FileInputStream(filepath + "/tempAudio.wav");
              DataInputStream dis = new DataInputStream(fin);
  
              at.play();
              while((i = dis.read(s, 0, bufferSize)) &gt; -1){
                  at.write(s, 0, i);
              }
              at.stop();
              at.release();
              dis.close();
              fin.close();
  
          } catch (FileNotFoundException e) {
              // TODO
              e.printStackTrace();
          } catch (IOException e) {
              // TODO
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>There is any way to set sample rate Direct to TTS like <code>tts.setSampleRate(32000);</code> or get Stream from TTS to AudioTrack like <code>DataInputStream dis = new DataInputStream(tts.speak("bla bla bla").getDataInputStream);</code> . <strong>In Short I need Chipmunk's Text to Speech for Android but without synthesizeToFile or direct stream TTS voice Data in AudioTrack without saving output of TTS.</strong></p>