0

我需要从麦克风流式传输,处理声音并立即播放。

我想使用 Tarsos,但我不知道如何让 Tarsos 的 AudioPlayer 立即播放结果。到目前为止,我可以从 mic + process + 另存为 .pcm 文件流式传输。我需要在进程和保存之间添加“立即播放”。

这是代码(不包括方法 processPitch):

public void record() throws FileNotFoundException {
    PitchDetectionHandler pdh = new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult result, AudioEvent e) {
            final float pitchInHz = result.getPitch();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    processPitch(pitchInHz);
                    frequency.setText("" + pitchInHz);
                }
            });
        }
    };
    dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(SAMPLE_RATE, 1024, 0);
    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    isRecording = true;

    // Output
    filePath = "/sdcard/recording_test.pcm";
    RandomAccessFile outputFile = new RandomAccessFile(filePath, "rw");
    final TarsosDSPAudioFormat outputFormat = new TarsosDSPAudioFormat(SAMPLE_RATE, 16, 1, true, false);
    WriterProcessor writer = new WriterProcessor(outputFormat, outputFile);
    dispatcher.addAudioProcessor(writer);
    recordingThread = new Thread(dispatcher, "Audio Dispatcher)");
    recordingThread.start();
}
4

1 回答 1

0

-已解决-确实很简单。只需将一个新播放器作为 AudioProcessor 添加到调度程序(这在文档中有所提及,但我没有找到正确的语法来表达它):

dispatcher.addAudioProcessor(new AndroidAudioPlayer(outputFormat, intSize, AudioManager.STREAM_MUSIC));
于 2019-08-06T01:30:45.810 回答