有没有办法将音频文件传递给文本到语音引擎?

时间:2017-09-07 01:10:25

标签: android text-to-speech

在我的应用程序中我想记录用户的语音,通过带通过滤器运行它,然后将生成的音频文件(PCM / WAV)传递给文本到语音引擎,以说出过滤后的结果,I让一切正常,除了找不到将音频文件传递给tts引擎的方法,我已经用Google搜索了很长时间(2周),没有运气。有没有解决方法来实现这一目标? 我尝试的是调用RecognizerIntent,然后通过录制启动带通滤波器,并且还通过首先启动带通方法然后调用识别器意图尝试相反的方式,但是无论哪种方式都杀死了tts实例,即使它也是如此。在一个单独的线程上运行。我还使用识别器意图中的普通tts程序以及识别器意图的网络搜索版本测试了这两个结果,如果我没有实现带通滤波器(请注意,记录线程已启动)在这个时候它工作正常,但是一旦我实现了带通滤波器,它就会失败,并在网络搜索模式中显示一条有用的消息,表示"谷歌不可用"这是我目前的代码:

RecognizerIntent,正常版本:

 public void getMic() {//bring up the speak now message window
        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    result = tts.setLanguage(Locale.US);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        l = new Intent();
                        l.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                        startActivity(l);
                    }
                }
            }
        });
        k = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        k.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        k.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        k.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
        try {
            startActivityForResult(k, 400);
        } catch (ActivityNotFoundException a) {
            Log.i("CrowdSpeech", "Your device doesn't support Speech Recognition");
        }
        if(crowdFilter && running==4){
        try {
            startRecording();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      }
    }

识别器意图网络搜索版本:

 public void getWeb() { //Search the web from voice input
  k = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
  k.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  k.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  k.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
  try {
    startActivityForResult(k, 400);
  } catch (ActivityNotFoundException a) {
    Log.i("CrowdSpeech", "Your device doesn't support Speech Recognition");
  }
  if (crowdFilter && running == 4) {
    try {
      startRecording();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}

应用带通滤波器的startRecording方法:

private void startRecording() throws FileNotFoundException {

  if (running == 4) { //start recording from mic, apply bandpass filter and save as wave file using TARSOS library
    dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(RECORDER_SAMPLERATE, bufferSize, 0);
    AudioProcessor p = new BandPass(freqChange, tollerance, RECORDER_SAMPLERATE);
    dispatcher.addAudioProcessor(p);
    isRecording = true;
    // Output
    File f = new File(myFilename.toString() + "/Filtered result.wav");
    RandomAccessFile outputFile = new RandomAccessFile(f, "rw");
    TarsosDSPAudioFormat outputFormat = new TarsosDSPAudioFormat(44100, 16, 1, true, true);
    WriterProcessor writer = new WriterProcessor(outputFormat, outputFile);
    dispatcher.addAudioProcessor(writer);
    recordingThread = new Thread(new Runnable() {
      @Override
      public void run() {
        dispatcher.run();
      }
    }, "Crowd_Speech Thread");
    recordingThread.start();
  }
}

我这样做的唯一原因是希望通过应用tts引擎将接收修改后的音频的过滤器,这也保存在文件中,因为最初我只想将文件传递给录音后要读取,有没有办法实现这个目标?

我想到的另一件事是我的项目中有任何可能的方法,我可以修改识别器意图引用的库中的源代码,以便我可以添加一个参数来从文件中获取音频吗?

0 个答案:

没有答案