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

Commit a95c6779 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Apply gain to the audio track stream" am: a9e79798 am: 17db1530 am:...

Merge "Apply gain to the audio track stream" am: a9e79798 am: 17db1530 am: b31222e5 am: c5b79783

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2530269



Change-Id: If04f125411b4199474e1533a1e21663789534f84
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 2e75a4fd c5b79783
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -735,7 +735,7 @@ static void btif_a2dp_sink_set_focus_state_event(
}
}


void btif_a2dp_sink_set_audio_track_gain(float gain) {
void btif_a2dp_sink_set_audio_track_gain(float gain) {
  LOG_INFO("%s: set gain to %f", __func__, gain);
  LOG_DEBUG("%s: set gain to %f", __func__, gain);
  LockGuard lock(g_mutex);
  LockGuard lock(g_mutex);


#ifndef OS_GENERIC
#ifndef OS_GENERIC
+25 −4
Original line number Original line Diff line number Diff line
@@ -23,6 +23,8 @@
#include <base/logging.h>
#include <base/logging.h>
#include <utils/StrongPointer.h>
#include <utils/StrongPointer.h>


#include <algorithm>

#include "bt_target.h"
#include "bt_target.h"
#include "osi/include/log.h"
#include "osi/include/log.h"


@@ -34,6 +36,7 @@ typedef struct {
  int channelCount;
  int channelCount;
  float* buffer;
  float* buffer;
  size_t bufferLength;
  size_t bufferLength;
  float gain;
} BtifAvrcpAudioTrack;
} BtifAvrcpAudioTrack;


#if (DUMP_PCM_DATA == TRUE)
#if (DUMP_PCM_DATA == TRUE)
@@ -41,6 +44,11 @@ FILE* outputPcmSampleFile;
char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm";
char outputFilename[50] = "/data/misc/bluedroid/output_sample.pcm";
#endif
#endif


// Maximum track gain that can be set.
constexpr float kMaxTrackGain = 1.0f;
// Minimum track gain that can be set.
constexpr float kMinTrackGain = 0.0f;

void* BtifAvrcpAudioTrackCreate(int trackFreq, int bitsPerSample,
void* BtifAvrcpAudioTrackCreate(int trackFreq, int bitsPerSample,
                                int channelCount) {
                                int channelCount) {
  LOG_VERBOSE("%s Track.cpp: btCreateTrack freq %d bps %d channel %d ",
  LOG_VERBOSE("%s Track.cpp: btCreateTrack freq %d bps %d channel %d ",
@@ -66,6 +74,7 @@ void* BtifAvrcpAudioTrackCreate(int trackFreq, int bitsPerSample,
  trackHolder->channelCount = channelCount;
  trackHolder->channelCount = channelCount;
  trackHolder->bufferLength =
  trackHolder->bufferLength =
      trackHolder->channelCount * AAudioStream_getBufferSizeInFrames(stream);
      trackHolder->channelCount * AAudioStream_getBufferSizeInFrames(stream);
  trackHolder->gain = kMaxTrackGain;
  trackHolder->buffer = new float[trackHolder->bufferLength]();
  trackHolder->buffer = new float[trackHolder->bufferLength]();


#if (DUMP_PCM_DATA == TRUE)
#if (DUMP_PCM_DATA == TRUE)
@@ -137,7 +146,16 @@ void BtifAvrcpSetAudioTrackGain(void* handle, float gain) {
    LOG_INFO("%s handle is null.", __func__);
    LOG_INFO("%s handle is null.", __func__);
    return;
    return;
  }
  }
  // Does nothing right now
  BtifAvrcpAudioTrack* trackHolder = static_cast<BtifAvrcpAudioTrack*>(handle);
  if (trackHolder != NULL) {
    const float clampedGain = std::clamp(gain, kMinTrackGain, kMaxTrackGain);
    if (clampedGain != gain) {
      LOG_WARN("Out of bounds gain set. Clamping the gain from :%f to %f", gain,
               clampedGain);
    }
    trackHolder->gain = clampedGain;
    LOG_INFO("Avrcp audio track gain is set to %f", trackHolder->gain);
  }
}
}


constexpr float kScaleQ15ToFloat = 1.0f / 32768.0f;
constexpr float kScaleQ15ToFloat = 1.0f / 32768.0f;
@@ -152,8 +170,9 @@ static size_t transcodeQ15ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  size_t i = 0;
  const float scaledGain = trackHolder->gain * kScaleQ15ToFloat;
  for (; i <= length / sampleSize; i++) {
  for (; i <= length / sampleSize; i++) {
    trackHolder->buffer[i] = ((int16_t*)buffer)[i] * kScaleQ15ToFloat;
    trackHolder->buffer[i] = ((int16_t*)buffer)[i] * scaledGain;
  }
  }
  return i * sampleSize;
  return i * sampleSize;
}
}
@@ -162,10 +181,11 @@ static size_t transcodeQ23ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  size_t i = 0;
  const float scaledGain = trackHolder->gain * kScaleQ23ToFloat;
  for (; i <= length / sampleSize; i++) {
  for (; i <= length / sampleSize; i++) {
    size_t offset = i * sampleSize;
    size_t offset = i * sampleSize;
    int32_t sample = *((int32_t*)(buffer + offset - 1)) & 0x00FFFFFF;
    int32_t sample = *((int32_t*)(buffer + offset - 1)) & 0x00FFFFFF;
    trackHolder->buffer[i] = sample * kScaleQ23ToFloat;
    trackHolder->buffer[i] = sample * scaledGain;
  }
  }
  return i * sampleSize;
  return i * sampleSize;
}
}
@@ -174,8 +194,9 @@ static size_t transcodeQ31ToFloat(uint8_t* buffer, size_t length,
                                  BtifAvrcpAudioTrack* trackHolder) {
                                  BtifAvrcpAudioTrack* trackHolder) {
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t sampleSize = sampleSizeFor(trackHolder);
  size_t i = 0;
  size_t i = 0;
  const float scaledGain = trackHolder->gain * kScaleQ31ToFloat;
  for (; i <= length / sampleSize; i++) {
  for (; i <= length / sampleSize; i++) {
    trackHolder->buffer[i] = ((int32_t*)buffer)[i] * kScaleQ31ToFloat;
    trackHolder->buffer[i] = ((int32_t*)buffer)[i] * scaledGain;
  }
  }
  return i * sampleSize;
  return i * sampleSize;
}
}