aaudio - Stereo output

47 Views Asked by At

following this great tutorial Android Making Waves I'm finally able to facing with cpp dsp programming using AAUDIO lib for low latency audio.

this tutorial explain how to archive monophonic sound, by chanching AAudioStreamBuilder_setChannelCount to "2" the sound becomes stereo but distorted,

AAudioStreamBuilder *streamBuilder;
    AAudio_createStreamBuilder(&streamBuilder);
    AAudioStreamBuilder_setFormat(streamBuilder, AAUDIO_FORMAT_PCM_FLOAT);
    AAudioStreamBuilder_setChannelCount(streamBuilder, 1);
    AAudioStreamBuilder_setPerformanceMode(streamBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
    AAudioStreamBuilder_setDataCallback(streamBuilder, ::dataCallback, &oscillator_);
    AAudioStreamBuilder_setErrorCallback(streamBuilder, ::errorCallback, this);

based on this tutorial do you know how to make the dsp stereo? practically split the signal and then be able to apply an amplitude control on each channel to archive the PAN effect

1

There are 1 best solutions below

0
CL7 On

Here's how to do it:

  1. Enable 2 channels for your stream
    AAudioStreamBuilder_setChannelCount(streamBuilder, 2);
  1. When passing the audioData reserve i for the left channel and i+1 for the right audio channel:
    void Oscillator::render(float *audioData, int32_t numFrames) {

        // If the wave has been switched off then reset the phase to zero. Starting at a non-zero value
        // could result in an unwanted audible 'click'
        if (!isWaveOn_.load()) phase_ = 0;

        for (int i = 0; i < numFrames; i+=2) {

            if (isWaveOn_.load()) {

                // Calculates the next sample value for the sine wave.
                audioData[i] = (float) (sin(phase_) * AMPLITUDE)*leftVolume; //left
                audioData[i+1] = (float) (sin(phase_) * AMPLITUDE)*rightVolume; //right


                // Increments the phase, handling wrap around.
                phase_ += phaseIncrement_;
                if (phase_ > TWO_PI) phase_ -= TWO_PI;


            } else {
                // Outputs silence by setting sample value to zero.
                audioData[i] = 0;
                audioData[i+1] = 0;

            }
        }
    }