java - 如何从 TarsosDSP AudioDispatcher 获取 PCM 16 位数据?

标签 java android signal-processing tarsosdsp

我想从 TarsosDSP AudioDispatcher 获取 PCM 16 位数据。我跟着这个link用于实时音频流的音高分析。我得到了想要的结果,但我还想从 AudioDispatcher 获取 PCM 数据。

AudioDispatcher adp = AudioDispatcherFactory.fromDefaultMicrophone(2048, 0);

如何从 AudioDispatcher 或任何其他技术获取所需的 PCM 数据以将我的数据从 android AudioRecord 传递到 AudioDispatcher?

最佳答案

您可以将 audioRecorder 对象传递给 AudioDispatcherFactory,只需创建您自己的 AudioDispatcherFactory.java 类,并添加额外的 AudioRecorder

参数

像这样:

package com.example.revertback;

public class AudioDispatcherFactory {


public static AudioDispatcher fromDefaultMicrophone(final AudioRecord audioInputStream,final int sampleRate,
                                                    final int audioBufferSize, final int bufferOverlap) {
    int minAudioBufferSize = AudioRecord.getMinBufferSize(sampleRate,
            android.media.AudioFormat.CHANNEL_IN_MONO,
            android.media.AudioFormat.ENCODING_PCM_16BIT);
    int minAudioBufferSizeInSamples =  minAudioBufferSize/2;
    if(minAudioBufferSizeInSamples <= audioBufferSize ){

        TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(sampleRate, 16,1, true, false);

        TarsosDSPAudioInputStream audioStream = new AndroidAudioInputStream(audioInputStream, format);
        //start recording ! Opens the stream.
        return new AudioDispatcher(audioStream,audioBufferSize,bufferOverlap);
    }else{
        throw new IllegalArgumentException("Buffer size too small should be at least " + (minAudioBufferSize *2));
    }
}



public static AudioDispatcher fromPipe(final String source,final int targetSampleRate, final int audioBufferSize,final int bufferOverlap){
    PipedAudioStream f = new PipedAudioStream(source);
    TarsosDSPAudioInputStream audioStream = f.getMonoStream(targetSampleRate,0);
    return new AudioDispatcher(audioStream, audioBufferSize, bufferOverlap);
}
}

然后在你的 Activity 中做这样的事情

AudioRecorder recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();
  AudioDispatcher dispatcher = com.example.revertback.AudioDispatcherFactory.fromDefaultMicrophone(recorder,22050,1024,0);


    PitchDetectionHandler pdh = new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult result, AudioEvent e) {
            final float pitchInHz = result.getPitch();

        }
    };
    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    new Thread(dispatcher,"Audio Dispatcher").start();

关于java - 如何从 TarsosDSP AudioDispatcher 获取 PCM 16 位数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58912983/

相关文章:

java - WebLogicServer 不创建 DemoIdentity.jks

java - 导出和导入 SQLite 数据库

c - 寻找用 C 语言读取音频的基础知识

android - 悬停时检测scrollTop()(手持设备)

Android gradle 3.0.0 - ZLIB 输入流的意外结束

floating-point - 两个实函数的同时快速傅立叶逆变换

python - 在 python 中计算一致性

Java Socket BufferReader.readline 获取 null

java - Android 项目失败 (React Native) - 任务 ':app:packageDebug' 执行失败 APK META-INF/LICENSE 中复制的重复文件

java - 如何在 JFreechart 中获取点的菱形