简体   繁体   English

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

[英]How to get PCM 16-bit data from TarsosDSP AudioDispatcher?

I want to get PCM 16-bit data from TarsosDSP AudioDispatcher.我想从 TarsosDSP AudioDispatcher 获取 PCM 16 位数据。 I followed this link for the pitch analysis of a real-time audio stream.我按照此链接对实时音频 stream 进行音高分析。 I am getting the desired results but I also want to get the PCM data from AudioDispatcher.我得到了想要的结果,但我也想从 AudioDispatcher 获取 PCM 数据。

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

How can I get the required PCM data from AudioDispatcher or any other technique to pass my data from the android AudioRecord to AudioDispatcher?如何从 AudioDispatcher 或任何其他技术获取所需的 PCM 数据,以将我的数据从 android AudioRecord 传递到 AudioDispatcher?

You can pass your audioRecorder object to AudioDispatcherFactory by simply creating your own AudioDispatcherFactory.java class with extra argument of AudioRecorder您可以通过简单地创建自己的AudioDispatcherFactory.java class和 AudioRecorder 的额外参数将您的 audioRecorder object 传递给AudioDispatcherFactory

Like this:像这样:

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);
}
}

And in your Activity do some this like this在你的活动中做一些这样的事情

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM