How to add stereo,treble options in audio equalize

2019-02-07 00:29发布

问题:

I am trying to a small audio songs equalizer. I want to add in it options of treble,stereo like it is in Poweramp player.

I implemented equlizer with 5 bands successfully like this:-

public class FragmentEqualizer extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);

        equalizer = new EQ(getActivity(), new Equalizer(0,com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.mp.getAudioSessionId()));
       for(Bar bar : eqBars)
            bar.setActiveEQ();
       maximum= EQ.getEqualizer().getBandLevelRange()[1];
       minimum= EQ.getEqualizer().getBandLevelRange()[0];
    }


public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);


                   lvforprest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                                    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                                btnformenu.setText(gtuforpresets.get(position).gtumcaFirstName);
                                if(position!=0 && position <=10)
                                {
                                    try
                                    {
                                 EQ.getEqualizer().usePreset((short) (position-1));
                          EQ.getEqualizer().setBandLevel((short)0,  EQ.getEqualizer().getBandLevel((short) 0));
                          EQ.getEqualizer().setBandLevel((short)1,  EQ.getEqualizer().getBandLevel((short) 1));
                          EQ.getEqualizer().setBandLevel((short)2, EQ.getEqualizer().getBandLevel((short) 2));
                          EQ.getEqualizer().setBandLevel((short)3,  EQ.getEqualizer().getBandLevel((short) 3));
                          EQ.getEqualizer().setBandLevel((short)4,  EQ.getEqualizer().getBandLevel((short) 4));

                          eqBars.get(0).setEQPosition(EQ.getEqualizer().getBandLevel((short) 0));
                          eqBars.get(1).setEQPosition(EQ.getEqualizer().getBandLevel((short) 1));
                          eqBars.get(2).setEQPosition(EQ.getEqualizer().getBandLevel((short) 2));
                          eqBars.get(3).setEQPosition(EQ.getEqualizer().getBandLevel((short) 3));
                          eqBars.get(4).setEQPosition(EQ.getEqualizer().getBandLevel((short) 4));
                          seekbar1katop.setText(EQ.getEqualizer().getBandLevel((short) 0)+"mB");
                          seekbar2katop.setText(EQ.getEqualizer().getBandLevel((short) 1)+"mB");
                          seekbar3katop.setText(EQ.getEqualizer().getBandLevel((short) 2)+"mB");
                          seekbar4katop.setText(EQ.getEqualizer().getBandLevel((short) 3)+"mB");
                          seekbar5katop.setText(EQ.getEqualizer().getBandLevel((short) 4)+"mB");


                                 }
                                    catch(IllegalStateException e)
                                    {
                                         Toast.makeText(getActivity(),"Unable",Toast.LENGTH_SHORT).show();

                                    }
                                    catch(IllegalArgumentException e)
                                {
                                         Toast.makeText(getActivity(),"Unable",Toast.LENGTH_SHORT).show();

                                    }
                                    catch(UnsupportedOperationException e)
                                    {
                                      Toast.makeText(getActivity(),"Unable",Toast.LENGTH_SHORT).show();

                                    }
                                }

                            //    Toast.makeText(getApplicationContext(),"You Clicked : " + mEqualizer.getEnabled(),Toast.LENGTH_SHORT).show();
                             }
                        });

}
}

Above code is just a short brief of my equalizer code.it wont work just as a example i posted here . .

I too want to add treble, stereo, mono effects in my equalizer.

I already implemented bass boost like this:

public static void setBassBoost(BassBoost bassBoost, int percent) {
        try{
            bassBoost.setStrength((short) ((short) 1000 / 100 * percent));
            bassBoost.setEnabled(true);
        }catch (Exception e){

        }
    }

    public static void setBassBoostOff(BassBoost bassBoost) {
        bassBoost.setEnabled(false);
    }

I used an inbulilt class for bass boost.

How can I add treble and stereo/mono effects to my app?

回答1:

In order to change the bass, mid, treble there's no need to use the AudioTrack object (even because with this object you could only playback non-compressed PCM data).

You just need to adjust the proper frequency bands level using your Equalizer object. To get the number of available bands, just call:

myEqualizer.getNumberOfBands()

Considering the number of available bands, you can now set the level for each band using the following method:

myEqualizer.setBandLevel(band, level);

where:

band: frequency band that will have the new gain. The numbering of the bands starts from 0 and ends at (number of bands - 1).

level: new gain in millibels that will be set to the given band. getBandLevelRange() will define the maximum and minimum values.

The meaning of each bands, from left to right, is summarized in the following image:

UPDATE

To implement a trivial balance effect, just differentiate the left/right volume on your player (MediaPlayer, SoundPool,...):

mediaPlayer.setVolume(left, right)

To obtain a mono effect you can consider using a Virtualizer, which provides a stereo widening effect. You can set the strength of the virtualization effect using the method:

virtualizer.setStrength(1000); //range is [0..1000]

You need to read the documentation carefully in order to check if the current configuration of your virtualizer is really supported by the underlying system.


Anyway, this is not a real mono output and I think you won't be able to obtain a mono ouput on stereo speaker without using low level API such as AudioTrack (actually Poweramp relies on native JNI libraries for its audio pipeline). If you want to use an AudioTrack for playback you need to consider that it only supports PCM data (WAV) as input: this means you won't be able to play compressed audio file (like MP3, flac, ...) directly since you need to manually decode the compressed audio file first.

[Compressed File (MP3)] ===> decode() ===> [PCM data] ===> customEffect() ===> AudioTrack playback()

Thus, in order to play a compressed audio using and AudioTrack (and eventually create a custom effect) the following steps are required:

  1. decode the compressed file using a decoder (NO PUBLIC SYSTEM API available for this, you need to do it manually!!!).
  2. if necessary, transform uncompressed data in a PCM format which is compatible with AudioTrack
  3. (eventually) apply your transformation on the PCM data stream (e.g. you can merge both L/R channels and create a mono effect)
  4. play the PCM stream using an AudioTrack

I suggest you to skip this effect ;)


Regarding the bass-boost effect, you need to check if your current configuration is supported by the the running device (like the virtualizer). Take a look here for more info on this.