BackgroundAudio 1.3.3
Loading...
Searching...
No Matches
BackgroundAudioGain.h
1/*
2 BackgroundAudio
3 Plays an audio file using IRQ driven decompression. Main loop() writes
4 data to the buffer but isn't blocked while playing
5
6 Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#pragma once
23#include <stdint.h>
24
32static inline void ApplyGain(int16_t *samples, size_t count, int32_t gain) {
33 // Default case, don't do anything
34 if (gain == 1 << 16) {
35 return;
36 }
37
38 if (gain > 1 << 16) { // May clip
39 for (size_t i = 0; i < count; i++) {
40 int32_t x = samples[i];
41 x *= gain;
42 x >>= 16;
43 if (x < -32767) {
44 x = -32767;
45 } else if (x > 32767) {
46 x = 32767;
47 }
48 samples[i] = x;
49 }
50 } else { // Can't clip, no need to check
51 for (size_t i = 0; i < count; i++) {
52 int32_t x = samples[i];
53 x *= gain;
54 x >>= 16;
55 samples[i] = x;
56 }
57 }
58}