android - 如何设置文本到语音的采样率 - Android

标签 android text-to-speech audiotrack

在我的文本到语音的输出中,我需要将采样率设置为大约 32000 Hz,Pitch - 1 和 SpeechRate - 0.2(我已经这样做了)。但我无法设置采样率。

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 获取 Stream 到 AudioTrack,如 DataInputStream dis = new DataInputStream(tts.speak( "bla bla bla").getDataInputStream);简而言之,我需要 Chipmunk 的 Android 文本转语音,但没有 synthesizeToFile 或在 AudioTrack 中直接流式传输 TTS 语音数据而不保存 TTS 输出。

最佳答案

不能直接设置 TTS 采样率:

我在一个项目中做了这样的事情(Dint使用TTS)

这可能对你有帮助,

播放不同语音类型的录音:-

waveSampling=90000; (花栗鼠)

waveSampling=24200; (“慢动作”)

waveSampling=30000;("BANE")/ bat 侠角色

waveSampling=18000;(幽灵)

waveSampling=70000;(蜜蜂)

waveSampling=60000;(女性)

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 格式。

关于android - 如何设置文本到语音的采样率 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43579867/

相关文章:

android - TextToSpeech.isSpeaking() 在什么情况下返回 true?

c++ - Festival TTS API/CodeBlocks 构建问题!

android - 通过耳机播放音乐时通过扬声器播放声音

android - 非常简单的Android地理围栏示例

android - GreenDAO 如何获得最高记录?

c++ - 文字转语音传字符串说话

Android:如何获取当前 Activity 的 Audio Session ID

audio - android AudioTrack播放短数组(16bit)

android - 不使用 WRITE_EXTERNAL_STORAGE 共享图像?

android - Android OutOfMemoryError崩溃