Skip to content

Commit 6d8d56d

Browse files
perexgtiwai
authored andcommitted
ALSA: pcm: playback silence - move silence variable updates to separate function
The code tracking the added samples in thresholded mode and the code tracking the just played samples in top-up mode are semantically identical, so factor it out to a common function to enhance readability. Co-developed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> Signed-off-by: Jaroslav Kysela <perex@perex.cz> Link: https://lore.kernel.org/r/20230505155244.2312199-5-oswald.buddenhagen@gmx.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 190cb66 commit 6d8d56d

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

sound/core/pcm_lib.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333
static int fill_silence_frames(struct snd_pcm_substream *substream,
3434
snd_pcm_uframes_t off, snd_pcm_uframes_t frames);
3535

36+
37+
static inline void update_silence_vars(struct snd_pcm_runtime *runtime,
38+
snd_pcm_uframes_t ptr,
39+
snd_pcm_uframes_t new_ptr)
40+
{
41+
snd_pcm_sframes_t delta;
42+
43+
delta = new_ptr - ptr;
44+
if (delta == 0)
45+
return;
46+
if (delta < 0)
47+
delta += runtime->boundary;
48+
if ((snd_pcm_uframes_t)delta < runtime->silence_filled)
49+
runtime->silence_filled -= delta;
50+
else
51+
runtime->silence_filled = 0;
52+
runtime->silence_start = new_ptr;
53+
}
54+
3655
/*
3756
* fill ring buffer with silence
3857
* runtime->silence_start: starting pointer to silence area
@@ -49,18 +68,9 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram
4968
int err;
5069

5170
if (runtime->silence_size < runtime->boundary) {
52-
snd_pcm_sframes_t noise_dist, n;
71+
snd_pcm_sframes_t noise_dist;
5372
snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr);
54-
if (runtime->silence_start != appl_ptr) {
55-
n = appl_ptr - runtime->silence_start;
56-
if (n < 0)
57-
n += runtime->boundary;
58-
if ((snd_pcm_uframes_t)n < runtime->silence_filled)
59-
runtime->silence_filled -= n;
60-
else
61-
runtime->silence_filled = 0;
62-
runtime->silence_start = appl_ptr;
63-
}
73+
update_silence_vars(runtime, runtime->silence_start, appl_ptr);
6474
/* initialization outside pointer updates */
6575
if (new_hw_ptr == ULONG_MAX)
6676
new_hw_ptr = runtime->status->hw_ptr;
@@ -87,15 +97,7 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram
8797
runtime->silence_filled = avail > 0 ? avail : 0;
8898
runtime->silence_start = runtime->status->hw_ptr;
8999
} else {
90-
ofs = runtime->status->hw_ptr;
91-
frames = new_hw_ptr - ofs;
92-
if ((snd_pcm_sframes_t)frames < 0)
93-
frames += runtime->boundary;
94-
runtime->silence_filled -= frames;
95-
if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
96-
runtime->silence_filled = 0;
97-
}
98-
runtime->silence_start = new_hw_ptr;
100+
update_silence_vars(runtime, runtime->status->hw_ptr, new_hw_ptr);
99101
}
100102
frames = runtime->buffer_size - runtime->silence_filled;
101103
}

0 commit comments

Comments
 (0)