Waveform data capture

As mentioned earlier, the Android Visualizer class lets us define callbacks to capture audio data. This data comes in two formats: waveform and FFT. We'll add just the waveform data to the VisualizerBox class now.

First, define the variables that we'll use for the captured audio data, as follows:

    Visualizer visualizer;
    public static int captureSize;
    public static byte[] audioBytes;

Using the API, we can determine the minimum capture size available, and then use that as our capture sample size.

Then, initialize them in the constructor as follows. First, instantiate an Android Visualizer. Then set the capture size to use, and allocate our buffers:

    public VisualizerBox(final CardboardView cardboardView){
        visualizer = new Visualizer(0);
        captureSize = Visualizer.getCaptureSizeRange()[0];
        visualizer.setCaptureSize(captureSize);
        // capture audio data
        // Visualizer.OnDataCaptureListener captureListener = ...
        visualizer.setDataCaptureListener(captureListener, Visualizer.getMaxCaptureRate(), true, true);
        visualizer.setEnabled(true);
    }

We want to use the minimum size for a variety of reasons. Firstly, it will be faster, and in VR, speed is paramount. Secondly, it organizes our FFT samples (as discussed later) into fewer buckets. This is helpful because each bucket catches more activity over a broader range of frequencies.

Note

Note that we left a comment where we'll define the capture listener, and then set it in the visualizer. Make sure that you enable the visualizer as always listening.

Let's first write the captureListener object for waveform data only. We define and instantiate a new anonymous class that implements Visualizer.OnDataCaptureListener, and provide it with a function named onWaveFormDataCapture, which receives the wave form bytes and stores them for our Visualization code (forthcoming):

        // capture audio data
        Visualizer.OnDataCaptureListener captureListener = new Visualizer.OnDataCaptureListener() {
            @Override
            public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
                audioBytes = bytes;
            }
            @Override
            public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
            }
        };

The interface still requires that we provide an onFftDataCapture method, but we're leaving it empty for the time being.

Now we're ready to add some graphics to this baby.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset