Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5412766 authored by Eric Laurent's avatar Eric Laurent
Browse files

ToneGenerator: fix sampling rate selection.

The ToneGenerator sampling rate is used to configure the wave generator
oscillators and must match the AudioTrack sampling rate.
Previous sampling rate initialization assumed that the sampling rate
retrieved by AudioSystem::getOutputSamplingRate at contruction time
would always match the AudioTrack sampling rate but it is not always the case.

Bug: 325575809
Flag: EXEMPT bug fix
Test: repro steps in bug
Change-Id: Ie2235c66f7f5f03f64e0afb66309d451f7a3a4e2
parent 20d2ea8e
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -1033,17 +1033,11 @@ ToneGenerator::ToneGenerator(audio_stream_type_t streamType, float volume, bool

    mState = TONE_IDLE;

    if (AudioSystem::getOutputSamplingRate(&mSamplingRate, streamType) != NO_ERROR) {
        ALOGE("Unable to marshal AudioFlinger");
        return;
    }
    mThreadCanCallJava = threadCanCallJava;
    mStreamType = streamType;
    mVolume = volume;
    mpToneDesc = NULL;
    mpNewToneDesc = NULL;
    // Generate tone by chunks of 20 ms to keep cadencing precision
    mProcessSize = (mSamplingRate * 20) / 1000;

    char value[PROPERTY_VALUE_MAX];
    if (property_get("gsm.operator.iso-country", value, "") == 0) {
@@ -1321,6 +1315,7 @@ bool ToneGenerator::initAudioTrack() {
    mpAudioTrack = new AudioTrack(attributionSource);
    ALOGV("AudioTrack(%p) created", mpAudioTrack.get());


    audio_attributes_t attr;
    audio_stream_type_t streamType = mStreamType;
    if (mStreamType == AUDIO_STREAM_VOICE_CALL || mStreamType == AUDIO_STREAM_BLUETOOTH_SCO) {
@@ -1329,13 +1324,12 @@ bool ToneGenerator::initAudioTrack() {
    attr = AudioSystem::streamTypeToAttributes(streamType);
    attr.flags = static_cast<audio_flags_mask_t>(attr.flags | AUDIO_FLAG_LOW_LATENCY);

    const size_t frameCount = mProcessSize;
    status_t status = mpAudioTrack->set(
            AUDIO_STREAM_DEFAULT,
            0,    // sampleRate
            AUDIO_FORMAT_PCM_16_BIT,
            AUDIO_CHANNEL_OUT_MONO,
            frameCount,
            0,    // frameCount
            AUDIO_OUTPUT_FLAG_NONE,
            wp<AudioTrack::IAudioTrackCallback>::fromExisting(this),
            0,    // notificationFrames
@@ -1355,6 +1349,10 @@ bool ToneGenerator::initAudioTrack() {
        return false;
    }

    mSamplingRate = mpAudioTrack->getSampleRate();
    // Generate tone by chunks of 20 ms to keep cadencing precision
    mProcessSize = (mSamplingRate * 20) / 1000;

    mpAudioTrack->setVolume(mVolume);
    mState = TONE_INIT;
    return true;