summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/recorder/peakmeter.c13
-rw-r--r--uisimulator/sdl/sound.c65
2 files changed, 71 insertions, 7 deletions
diff --git a/apps/recorder/peakmeter.c b/apps/recorder/peakmeter.c
index 0142a9d093..f70e9ae709 100644
--- a/apps/recorder/peakmeter.c
+++ b/apps/recorder/peakmeter.c
@@ -44,9 +44,7 @@
44#include "pcm_record.h" 44#include "pcm_record.h"
45#endif 45#endif
46 46
47#ifndef SIMULATOR /* this is not used in the sim */
48static bool pm_playback = true; /* selects between playback and recording peaks */ 47static bool pm_playback = true; /* selects between playback and recording peaks */
49#endif
50 48
51#endif 49#endif
52 50
@@ -539,11 +537,7 @@ void peak_meter_peek(void)
539{ 537{
540 int left, right; 538 int left, right;
541 /* read current values */ 539 /* read current values */
542#ifdef SIMULATOR 540#if CONFIG_CODEC == SWCODEC
543 pm_cur_left = left = 8000;
544 pm_cur_right = right = 9000;
545#elif CONFIG_CODEC == SWCODEC
546
547 if (pm_playback) 541 if (pm_playback)
548 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right); 542 pcm_calculate_peaks(&pm_cur_left, &pm_cur_right);
549#ifdef HAVE_RECORDING 543#ifdef HAVE_RECORDING
@@ -555,8 +549,13 @@ void peak_meter_peek(void)
555 left = pm_cur_left; 549 left = pm_cur_left;
556 right = pm_cur_right; 550 right = pm_cur_right;
557#else 551#else
552#ifndef SIMULATOR
558 pm_cur_left = left = mas_codec_readreg(pm_src_left); 553 pm_cur_left = left = mas_codec_readreg(pm_src_left);
559 pm_cur_right = right = mas_codec_readreg(pm_src_right); 554 pm_cur_right = right = mas_codec_readreg(pm_src_right);
555#else
556 pm_cur_left = left = 8000;
557 pm_cur_right = right = 9000;
558#endif
560#endif 559#endif
561 560
562 /* check for clips 561 /* check for clips
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
158void 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
145Uint8 overflow[8192]; 210Uint8 overflow[8192];
146Uint32 overflow_amount = 0; 211Uint32 overflow_amount = 0;
147 212