android environmentalreverb doesn't do anything

删除回忆录丶 提交于 2019-12-12 17:46:34

问题


I'm trying to apply a reverb effect to a sine wave that I'm generating using AudioTrack. I tried presetReverb and applied it to audiosession 0 like the docs said, since doing the getAudioSessionId() method brought an error, but that didn't apply a reverb at all. So I tried EnvironmentalReverb and tried audiosession 0, and also using getAudioSessionId(), both of which didn't create a reverb.

Here's my code:

t = new Thread()
    {
        public void run()
        {
            setPriority(Thread.MAX_PRIORITY);
            int buffsize = AudioTrack.getMinBufferSize(sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

            AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffsize, AudioTrack.MODE_STREAM);

            EnvironmentalReverb reverb = new EnvironmentalReverb(1,0);

            audioTrack.attachAuxEffect(reverb.getId());
            reverb.setDiffusion((short) 1000);
            reverb.setReverbLevel((short) 1000);
            reverb.setDecayTime(10000);
            reverb.setReverbDelay(100);
            reverb.setDensity((short) 1000);
            audioTrack.setAuxEffectSendLevel(1.0f);
            reverb.setEnabled(true);

            short samples[] = new short[buffsize];
            int amp = 32767;
            double twopi = 2*Math.PI;
            double fr = 262.f;
            double ph = 0.0;

            audioTrack.play();

            while(isRunning)
            {
                fr = 262;

                for(int i=0; i < buffsize; i++)
                {
                    samples[i] = (short) (amp*Math.sin(ph));
                    ph += twopi*fr/sr;
                }
                audioTrack.write(samples, 0, buffsize);
            }
            audioTrack.stop();
            audioTrack.release();

        }
    };
    t.start();

I have the modify audio settings permission in my manifest, so why doesn't this create the reverb effect?


回答1:


Two things: One, make sure you set up your effects (and attaching) BEFORE your "write" or "play". Two, if you want PresetReverb to work, you need to do this and then attach it to your AudioTrack (or MediaPlayer):

PresetReverb reverb = new PresetReverb(1, 0);

See the example code mid-way through this and my response at the bottom: Android MediaPlayer with AudioEffect : Getting Error (-22,0)



来源:https://stackoverflow.com/questions/18191740/android-environmentalreverb-doesnt-do-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!