2017-04-24 45 views
3

在我的文本到語音的輸出中,我需要設置採樣率約爲32000赫茲,使用Pitch-1和SpeechRate-0.2(我已經這樣做了)。但我無法設置採樣率。如何在文本上設置採樣率到語音 - Android

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); 

我以前AudioTrack設置採樣率,但花了大量的時間,因爲我得先TTS synthesizeToFile然後我在AudioTrack播放。

HashMap<String, String> myHasRead = new HashMap<String, String>(); 
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)) > -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(); 
     } 
    } 

有什麼辦法來設置採樣率直接到TTS像tts.setSampleRate(32000);或TTS獲得流至AudioTrack像DataInputStream dis = new DataInputStream(tts.speak("bla bla bla").getDataInputStream);簡而言之,我需要Chipmunk的Text to Speech for Android,但不需要合成文件或者直接在AudioTrack中傳輸TTS語音數據,而不需要保存TTS的輸出。

回答

-1

你不能直接設置TTS採樣率:

我做了這樣的事情在一個項目(力使用TTS)

這可以幫助你,

播放記錄具有不同的語音類型: -

waveSampling = 90000; (Chipmunk)

waveSampling = 24200; ( 「慢鏡頭」)

waveSampling = 30000;( 「BANE」)/蝙蝠俠字符

waveSampling = 18000;(鬼)

waveSampling = 70000;(蜜蜂)

waveSampling = 60000;(Woman)

waveSampling = 37000; (普通)

void playRecord() throws IOException { 




      int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); 
      int bufferSize = 512; 
       at = new AudioTrack(AudioManager.STREAM_MUSIC, waveSampling, 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(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/temp/"+filename+".wav"); 
       DataInputStream dis = new DataInputStream(fin); 

       at.play(); 
       while((i = dis.read(s, 0, bufferSize)) > -1){ 
        at.write(s, 0, i); 

       } 
       at.stop(); 
       at.release(); 
       dis.close(); 
       fin.close(); 

        openmenu(); 


      } catch (FileNotFoundException e) { 
       // TODO 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO 
       e.printStackTrace(); 
      } 



    } 

保存音頻: -

public void save() throws IOException { 
     Random r = new Random(); 
     final int i1 = r.nextInt(80 - 65) + 65; 
     File tempfile2=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/temp/"+i1+filename+".wav"); 

     savedfile=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/"+"VOICE CHANGER"+i1+filename+".mp3"; 






     Toast.makeText(this, "File Saved", Toast.LENGTH_SHORT).show(); 



     rawToWave(tempfile,tempfile2); 

     File wavFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/temp/"+i1+filename+".wav"); 
     IConvertCallback callback = new IConvertCallback() { 
      @Override 
      public void onSuccess(File convertedFile) { 

       File newfile=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/"+"VOICE CHANGER"+i1+filename+".mp3"); 
       File savedmp3=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voice Changer/temp/"+i1+filename+".mp3"); 
       Toast.makeText(MainActivity.this, "SUCCESS: " + newfile.getPath(), Toast.LENGTH_LONG).show(); 

       try { 
        copyit(savedmp3,newfile); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      @Override 
      public void onFailure(Exception error) { 
       Toast.makeText(MainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show(); 


      } 
     }; 
     Toast.makeText(this, "Converting audio file...", Toast.LENGTH_SHORT).show(); 
     AndroidAudioConverter.with(this) 
       .setFile(wavFile) 
       .setFormat(cafe.adriel.androidaudioconverter.model.AudioFormat.MP3) 
       .setCallback(callback) 
       .convert(); 





    } 

輸出將是一個.MP3文件。如果你想快速輸出,你可以使用.wav格式。

+0

我想要更改TTS語音輸出的採樣率而不合成文件,或者要在AudioTrack中直接傳輸數據的TTS語音而不保存TTS的輸出。 –

相關問題