26 struct __attribute__((packed)) Frame {
30 Frame(
int v = 0) { channel1 = channel2 = v; }
32 Frame(
int ch1,
int ch2) {
49 virtual void update_audio_data(uint8_t* data, uint16_t byteCount) {
50 update_audio_data((Frame*)data, byteCount / 4);
53 virtual void update_audio_data(Frame* data, uint16_t frameCount) {
54 if (data !=
nullptr && frameCount > 0 && (mono_downmix || is_volume_used)) {
55 ESP_LOGD(
"VolumeControl",
"update_audio_data");
56 for (
int i = 0; i < frameCount; i++) {
57 int32_t pcmLeft = data[i].channel1;
58 int32_t pcmRight = data[i].channel2;
61 pcmRight = pcmLeft = (pcmLeft + pcmRight) / 2;
65 pcmLeft = clip(pcmLeft * volumeFactor / volumeFactorMax);
66 pcmRight = clip(pcmRight * volumeFactor / volumeFactorMax);
68 data[i].channel1 = pcmLeft;
69 data[i].channel2 = pcmRight;
75 int32_t get_volume_factor() {
return volumeFactor; }
78 int32_t get_volume_factor_max() {
return volumeFactorMax; }
80 void set_enabled(
bool enabled) { is_volume_used = enabled; }
82 void set_mono_downmix(
bool enabled) { mono_downmix = enabled; }
84 virtual void set_volume(uint8_t volume) = 0;
87 bool is_volume_used =
false;
88 bool mono_downmix =
false;
90 int32_t volumeFactorMax;
92 int32_t clip(int32_t value) {
93 int32_t result = value;
94 if (value < -32768) result = -32768;
95 if (value > 32767) result = 32767;
107 void set_volume(uint8_t volume)
override {
108 constexpr
float base = 1.4f;
109 constexpr
float bits = 12.0f;
110 constexpr
float zero_ofs = pow(base, -bits);
111 constexpr
float scale = pow(2.0f, bits);
112 float volumeFactorFloat =
113 (pow(base, volume * bits / 127.0f - bits) - zero_ofs) * scale /
115 volumeFactor = volumeFactorFloat;
116 if (volumeFactor > 0xfff) {
117 volumeFactor = 0xfff;
128 void set_volume(uint8_t volume)
override {
129 float volumeFactorFloat = volume;
130 volumeFactorFloat = pow(2.0f, volumeFactorFloat * 12.0f / 127.0f);
131 volumeFactor = volumeFactorFloat - 1.0f;
132 if (volumeFactor > 0xfff) {
133 volumeFactor = 0xfff;
148 void set_volume(uint8_t volume)
override { volumeFactor = volume; }
158 void update_audio_data(Frame* data, uint16_t frameCount)
override {}
159 void set_volume(uint8_t volume)
override {}
Default implementation for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:105
The simplest possible implementation of a VolumeControl.
Definition: A2DPVolumeControl.h:143
Keeps the audio data as is -> no volume control!
Definition: A2DPVolumeControl.h:156
Exponentional volume control.
Definition: A2DPVolumeControl.h:126
Abstract class for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:45