Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.

My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
diff --git a/firmware/pcm.c b/firmware/pcm.c
index 94b0d6e..9e2b900 100644
--- a/firmware/pcm.c
+++ b/firmware/pcm.c
@@ -258,6 +258,21 @@ bool pcm_is_initialized(void)
     return pcm_is_ready;
 }
 
+static inline int32_t FRACMUL(int32_t x, int32_t y)
+{
+    return (int32_t) (((int64_t)x * y) >> 31);
+}
+
+static inline void pcm_apply_gain(const void *addr, size_t s)
+{
+    int16_t *pcm = (int16_t *)addr; /* cast away constness, yay */
+    int32_t gain = 0x7fffffff>>5;
+    for (unsigned i = 0; i < s/2; i++)
+    {
+        pcm[i] = FRACMUL(pcm[i], gain);
+    }
+}
+
 /* Common code to pcm_play_data and pcm_play_pause */
 static void pcm_play_data_start(const void *addr, size_t size)
 {
@@ -281,6 +296,7 @@ static void pcm_play_data_start(const void *addr, size_t size)
     {
         logf(" pcm_play_dma_start");
         pcm_apply_settings();
+        pcm_apply_gain(addr, size);
         pcm_play_dma_start(addr, size);
         pcm_playing = true;
         pcm_paused = false;
@@ -327,9 +343,11 @@ bool pcm_play_dma_complete_callback(enum pcm_dma_status status,
         /* Call registered callback to obtain next buffer */
         get_more(addr, size);
         ALIGN_AUDIOBUF(*addr, *size);
-
         if (*addr && *size)
+        {
+            pcm_apply_gain(*addr, *size);
             return true;
+        }
     } 
 
     /* Error, callback missing or no more DMA to do */