首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从TarsosDSP AudioDispatcher获取PCM 16位数据?

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

Stack Overflow用户
提问于 2019-11-18 10:57:13
回答 1查看 416关注 0票数 2

我想从TarsosDSP AudioDispatcher获得PCM 16位数据。我遵循这个link的音高分析实时音频流.我正在获得所需的结果,但我也希望从AudioDispatcher获得PCM数据。

代码语言:javascript
复制
AudioDispatcher adp = AudioDispatcherFactory.fromDefaultMicrophone(2048, 0);

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-18 13:56:07

您可以将您的AudioDispatcherFactory对象传递给AudioDispatcherFactory.java,只需使用 audioRecorder 的额外参数创建您自己的类。

如下所示:

代码语言:javascript
复制
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);
}
}

在你的活动中,像这样做

代码语言:javascript
复制
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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58912983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档