I need to stream from mic, process sound and play it immediately back.
I thought to use Tarsos, but I can't figure out how to have Tarsos' AudioPlayer to play the result back immediately. So far I can stream from mic + process + save as .pcm file. I need to add "play back immediately" between process and save.
This is the code (method processPitch not included):
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();
}