diff options
Diffstat (limited to 'uisimulator/sdl')
-rw-r--r-- | uisimulator/sdl/sound.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/uisimulator/sdl/sound.c b/uisimulator/sdl/sound.c index 23d8718411..ae9d5f9053 100644 --- a/uisimulator/sdl/sound.c +++ b/uisimulator/sdl/sound.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | #include <stdbool.h> | 25 | #include <stdbool.h> |
26 | #include <memory.h> | 26 | #include <memory.h> |
27 | #include "kernel.h" | ||
27 | #include "sound.h" | 28 | #include "sound.h" |
28 | #include "SDL.h" | 29 | #include "SDL.h" |
29 | 30 | ||
@@ -142,6 +143,70 @@ bool pcm_is_playing(void) | |||
142 | return pcm_playing; | 143 | return pcm_playing; |
143 | } | 144 | } |
144 | 145 | ||
146 | /* | ||
147 | * This function goes directly into the DMA buffer to calculate the left and | ||
148 | * right peak values. To avoid missing peaks it tries to look forward two full | ||
149 | * peek periods (2/HZ sec, 100% overlap), although it's always possible that | ||
150 | * the entire period will not be visible. To reduce CPU load it only looks at | ||
151 | * every third sample, and this can be reduced even further if needed (even | ||
152 | * every tenth sample would still be pretty accurate). | ||
153 | */ | ||
154 | |||
155 | #define PEAK_SAMPLES (44100*2/HZ) /* 44100 samples * 2 / 100 Hz tick */ | ||
156 | #define PEAK_STRIDE 3 /* every 3rd sample is plenty... */ | ||
157 | |||
158 | void pcm_calculate_peaks(int *left, int *right) | ||
159 | { | ||
160 | long samples = pcm_data_size / 4; | ||
161 | short *addr = pcm_data; | ||
162 | |||
163 | if (samples > PEAK_SAMPLES) | ||
164 | samples = PEAK_SAMPLES; | ||
165 | |||
166 | samples /= PEAK_STRIDE; | ||
167 | |||
168 | if (left && right) { | ||
169 | int left_peak = 0, right_peak = 0, value; | ||
170 | |||
171 | while (samples--) { | ||
172 | if ((value = addr [0]) > left_peak) | ||
173 | left_peak = value; | ||
174 | else if (-value > left_peak) | ||
175 | left_peak = -value; | ||
176 | |||
177 | if ((value = addr [PEAK_STRIDE | 1]) > right_peak) | ||
178 | right_peak = value; | ||
179 | else if (-value > right_peak) | ||
180 | right_peak = -value; | ||
181 | |||
182 | addr += PEAK_STRIDE * 2; | ||
183 | } | ||
184 | |||
185 | *left = left_peak; | ||
186 | *right = right_peak; | ||
187 | } | ||
188 | else if (left || right) { | ||
189 | int peak_value = 0, value; | ||
190 | |||
191 | if (right) | ||
192 | addr += (PEAK_STRIDE | 1); | ||
193 | |||
194 | while (samples--) { | ||
195 | if ((value = addr [0]) > peak_value) | ||
196 | peak_value = value; | ||
197 | else if (-value > peak_value) | ||
198 | peak_value = -value; | ||
199 | |||
200 | addr += PEAK_STRIDE * 2; | ||
201 | } | ||
202 | |||
203 | if (left) | ||
204 | *left = peak_value; | ||
205 | else | ||
206 | *right = peak_value; | ||
207 | } | ||
208 | } | ||
209 | |||
145 | Uint8 overflow[8192]; | 210 | Uint8 overflow[8192]; |
146 | Uint32 overflow_amount = 0; | 211 | Uint32 overflow_amount = 0; |
147 | 212 | ||