public void setActive(boolean active) {
    this.active = active;
    Visualizer visualizer = getVizualiser();
    if (visualizer == null) {
      return;
    }

    int captureRate = Math.min(PREFERRED_CAPTURE_RATE_MILLIHERTZ, Visualizer.getMaxCaptureRate());
    if (active) {
      visualizer.setDataCaptureListener(
          new Visualizer.OnDataCaptureListener() {
            @Override
            public void onWaveFormDataCapture(
                Visualizer visualizer, byte[] waveform, int samplingRate) {
              updateVisualizer(waveform);
            }

            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {}
          },
          captureRate,
          true,
          false);
    } else {
      visualizer.setDataCaptureListener(null, captureRate, false, false);
    }

    visualizer.setEnabled(active);
    invalidate();
  }
  public VisualizerGLSurfaceView(Context context, int mediaPlayerSessionId) {
    super(context);

    mWaveformData = null;

    mVisualizer = new Visualizer(mediaPlayerSessionId);
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), true, false);

    mVisualizer.setEnabled(true);

    setEGLContextClientVersion(1);
    setRenderer(new VisualizerGLRenderer(this));
  }
  private void setupVisualizerUI() {
    // Create the Visualizer object and attach it to our media player.
    mVisualizerView = (VisualizerView) mView.findViewById(R.id.myvisualizerview);
    mVisualizer = new Visualizer(mPlayer.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(
        new Visualizer.OnDataCaptureListener() {
          public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
            mVisualizerView.updateVisualizer(bytes);
          }

          public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
        },
        Visualizer.getMaxCaptureRate() / 2,
        true,
        false);
  }
    /**
     * Links the visualizer to a player
     *
     * @param player - MediaPlayer instance to link to
     */
    public void link(MediaPlayer player) {
      if (player == null) {
        throw new NullPointerException("Cannot link to null MediaPlayer");
      }

      // Create the Visualizer object and attach it to our media player.
      mVisualizer = new Visualizer(player.getAudioSessionId());
      mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

      // Pass through Visualizer data to VisualizerView
      Visualizer.OnDataCaptureListener captureListener =
          new Visualizer.OnDataCaptureListener() {
            @Override
            public void onWaveFormDataCapture(
                Visualizer visualizer, byte[] bytes, int samplingRate) {
              updateVisualizer(bytes);
            }

            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
              updateVisualizerFFT(bytes);
            }
          };

      mVisualizer.setDataCaptureListener(
          captureListener, Visualizer.getMaxCaptureRate() / 2, true, true);

      // Enabled Visualizer and disable when we're done with the stream
      mVisualizer.setEnabled(true);
      player.setOnCompletionListener(
          new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
              mVisualizer.setEnabled(false);
            }
          });
    }
    @Override
    protected Void doInBackground(Void... arg0) {

      if (MainActivity.analyzeMode) {

        // RECORD FROM MICROPHONE
        try {

          // SET UP AUDIORECORDER
          int bufferSize =
              AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
          AudioRecord audioRecord =
              new AudioRecord(
                  MediaRecorder.AudioSource.VOICE_RECOGNITION,
                  frequency,
                  channelConfiguration,
                  audioEncoding,
                  bufferSize);
          short[] buffer = new short[blockSize];
          double[] toTransform = new double[blockSize];
          audioRecord.startRecording();

          // RECORDS AUDIO & PERFORMS FFT
          while (started) {
            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);
            for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
              toTransform[i] = (double) buffer[i] / 32768.0; // / 32768.0
            }

            transformer.ft(toTransform);

            // getAvg(toTransform);
            getFrequency(toTransform, 0, 1, 2);
            // getFrequency(toTransform, 1, 0, 2);
            // getFrequency(toTransform, 2, 1, 0);
            publishProgress(toTransform);
          }

          audioRecord.stop();

        } catch (Throwable t) {
          t.printStackTrace();
          Log.e("AudioRecord", "Recording Failed");
        }
      } else { // RECORD FROM SOUND MIX

        // SETS UP VISUALIZER
        visualizer = new Visualizer(0);
        visualizer.setEnabled(false);
        int capRate = Visualizer.getMaxCaptureRate();
        visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

        // USES VISUALIZER TO RETURN AUDIO & THEN PERFORMS FFT
        Visualizer.OnDataCaptureListener captureListener =
            new Visualizer.OnDataCaptureListener() {
              public void onWaveFormDataCapture(
                  Visualizer visualizer, byte[] bytes, int samplingRate) {
                double[] toTransform = new double[blockSize];
                for (int i = 0; i < bytes.length; i++) {
                  toTransform[i] = (double) (bytes[i]) / 8192.0; // 32768.0
                }
                transformer.ft(toTransform);
                // getAvg(toTransform);
                getFrequency(toTransform, 0, 1, 2);
                publishProgress(toTransform);
              }

              public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
            };

        int status =
            visualizer.setDataCaptureListener(
                captureListener, capRate, true /*wave*/, false /*no fft needed*/);
        visualizer.setScalingMode(Visualizer.SCALING_MODE_AS_PLAYED);
        visualizer.setEnabled(true);
      }

      return null;
    }