我需要从麦克风流式传输,处理声音并立即播放。
我想使用 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();
}