From patchwork Fri May 5 07:38:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 13232226 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8A506C77B7C for ; Fri, 5 May 2023 07:40:20 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id DFF522BD7; Fri, 5 May 2023 09:39:27 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz DFF522BD7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1683272418; bh=CcmUj3S6fUHVn3H6deaWxEM6NfxNQhvKx3YqTs3Ksc0=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=JPCT/oJzCoQy4oa1vImgSQl6aDUGE2EKT5qw1xB58UgAS55QBt/aasic5QAdoP0SH W5pi2RL8gmnU5YEjqfJ4SfH0I5RReqLT3CUHu9HqvIrEQHbyAEDoD3W4RSgzITceKD KiIE2n3IDi87bDiYDNYznP0AKrJDw79t/5xlFpH8= Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 03704F8053D; Fri, 5 May 2023 09:38:42 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id A0267F8053D; Fri, 5 May 2023 09:38:39 +0200 (CEST) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 87016F80529 for ; Fri, 5 May 2023 09:38:35 +0200 (CEST) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id D294511E2; Fri, 5 May 2023 09:38:34 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz D294511E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683272314; bh=70l2F+aiW+P/HA/WXJwk90uEDcdWm5hb9BZF0NXWD/0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mHuF74WgJ7OYHNmcssRTw8DXn96h1gHwhLUSX3WuiH2RG0Lmn0hRoy4CAu2maqa9Y N4dgC2k1Ud36C3V/O6BwJH9a933Vf3NOUcoGycWOQNAkh/n6254OhEcpuhKT8PqKT4 9wreQb5k/ArK3vWNyUs60nYj3zrzwqjLYYmBRzgE= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Fri, 5 May 2023 09:38:29 +0200 (CEST) From: Jaroslav Kysela To: ALSA development Subject: [PATCH 1/5] ALSA: pcm: Revert "ALSA: pcm: rewrite snd_pcm_playback_silence()" Date: Fri, 5 May 2023 09:38:09 +0200 Message-Id: <20230505073813.1219175-2-perex@perex.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230505073813.1219175-1-perex@perex.cz> References: <20230505073813.1219175-1-perex@perex.cz> MIME-Version: 1.0 Message-ID-Hash: FLWJ3QDXGE5GNSHMDGTRGIOVI32UNOKW X-Message-ID-Hash: FLWJ3QDXGE5GNSHMDGTRGIOVI32UNOKW X-MailFrom: perex@perex.cz X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Takashi Iwai , Oswald Buddenhagen , Jeff Chua X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This reverts commit 9f656705c5faa18afb26d922cfc64f9fd103c38d. There is a regression (the top-up mode). Unfortunately, the patch provided from the author of this commit is not easy for the review. Keep the update and new comments in headers. Signed-off-by: Jaroslav Kysela --- sound/core/pcm_lib.c | 86 ++++++++++++++++++++++++----------------- sound/core/pcm_local.h | 3 +- sound/core/pcm_native.c | 6 +-- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index d21c73944efd..af1eb136feb0 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -42,56 +42,70 @@ static int fill_silence_frames(struct snd_pcm_substream *substream, * * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately */ -void snd_pcm_playback_silence(struct snd_pcm_substream *substream) +void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) { struct snd_pcm_runtime *runtime = substream->runtime; - snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); - snd_pcm_sframes_t added, hw_avail, frames; - snd_pcm_uframes_t noise_dist, ofs, transfer; + snd_pcm_uframes_t frames, ofs, transfer; int err; - added = appl_ptr - runtime->silence_start; - if (added) { - if (added < 0) - added += runtime->boundary; - if (added < runtime->silence_filled) - runtime->silence_filled -= added; - else - runtime->silence_filled = 0; - runtime->silence_start = appl_ptr; - } - - // This will "legitimately" turn negative on underrun, and will be mangled - // into a huge number by the boundary crossing handling. The initial state - // might also be not quite sane. The code below MUST account for these cases. - hw_avail = appl_ptr - runtime->status->hw_ptr; - if (hw_avail < 0) - hw_avail += runtime->boundary; - - noise_dist = hw_avail + runtime->silence_filled; if (runtime->silence_size < runtime->boundary) { - frames = runtime->silence_threshold - noise_dist; - if (frames <= 0) + snd_pcm_sframes_t noise_dist, n; + snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); + if (runtime->silence_start != appl_ptr) { + n = appl_ptr - runtime->silence_start; + if (n < 0) + n += runtime->boundary; + if ((snd_pcm_uframes_t)n < runtime->silence_filled) + runtime->silence_filled -= n; + else + runtime->silence_filled = 0; + runtime->silence_start = appl_ptr; + } + if (runtime->silence_filled >= runtime->buffer_size) + return; + noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; + if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) return; + frames = runtime->silence_threshold - noise_dist; if (frames > runtime->silence_size) frames = runtime->silence_size; } else { - frames = runtime->buffer_size - noise_dist; - if (frames <= 0) - return; + if (new_hw_ptr == ULONG_MAX) { /* initialization */ + snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); + if (avail > runtime->buffer_size) + avail = runtime->buffer_size; + runtime->silence_filled = avail > 0 ? avail : 0; + runtime->silence_start = (runtime->status->hw_ptr + + runtime->silence_filled) % + runtime->boundary; + } else { + ofs = runtime->status->hw_ptr; + frames = new_hw_ptr - ofs; + if ((snd_pcm_sframes_t)frames < 0) + frames += runtime->boundary; + runtime->silence_filled -= frames; + if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { + runtime->silence_filled = 0; + runtime->silence_start = new_hw_ptr; + } else { + runtime->silence_start = ofs; + } + } + frames = runtime->buffer_size - runtime->silence_filled; } - if (snd_BUG_ON(frames > runtime->buffer_size)) return; - ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; - do { + if (frames == 0) + return; + ofs = runtime->silence_start % runtime->buffer_size; + while (frames > 0) { transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; err = fill_silence_frames(substream, ofs, transfer); snd_BUG_ON(err < 0); runtime->silence_filled += transfer; frames -= transfer; ofs = 0; - } while (frames > 0); + } snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); } @@ -425,6 +439,10 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, return 0; } + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && + runtime->silence_size > 0) + snd_pcm_playback_silence(substream, new_hw_ptr); + if (in_interrupt) { delta = new_hw_ptr - runtime->hw_ptr_interrupt; if (delta < 0) @@ -442,10 +460,6 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, runtime->hw_ptr_wrap += runtime->boundary; } - if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && - runtime->silence_size > 0) - snd_pcm_playback_silence(substream); - update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); return snd_pcm_update_state(substream, runtime); diff --git a/sound/core/pcm_local.h b/sound/core/pcm_local.h index 42fe3a4e9154..ecb21697ae3a 100644 --- a/sound/core/pcm_local.h +++ b/sound/core/pcm_local.h @@ -29,7 +29,8 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream, struct snd_pcm_runtime *runtime); int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream); -void snd_pcm_playback_silence(struct snd_pcm_substream *substream); +void snd_pcm_playback_silence(struct snd_pcm_substream *substream, + snd_pcm_uframes_t new_hw_ptr); static inline snd_pcm_uframes_t snd_pcm_avail(struct snd_pcm_substream *substream) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 91c87cdb786e..39a65d1415ab 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -958,7 +958,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream, if (snd_pcm_running(substream)) { if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size > 0) - snd_pcm_playback_silence(substream); + snd_pcm_playback_silence(substream, ULONG_MAX); err = snd_pcm_update_state(substream, runtime); } snd_pcm_stream_unlock_irq(substream); @@ -1455,7 +1455,7 @@ static void snd_pcm_post_start(struct snd_pcm_substream *substream, __snd_pcm_set_state(runtime, state); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size > 0) - snd_pcm_playback_silence(substream); + snd_pcm_playback_silence(substream, ULONG_MAX); snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART); } @@ -1916,7 +1916,7 @@ static void snd_pcm_post_reset(struct snd_pcm_substream *substream, runtime->control->appl_ptr = runtime->status->hw_ptr; if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size > 0) - snd_pcm_playback_silence(substream); + snd_pcm_playback_silence(substream, ULONG_MAX); snd_pcm_stream_unlock_irq(substream); } From patchwork Fri May 5 07:38:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 13232227 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D9F27C77B7C for ; Fri, 5 May 2023 07:40:25 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 53DFA2BE4; Fri, 5 May 2023 09:39:33 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 53DFA2BE4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1683272423; bh=HIDVLCFxnleF6LhxCZtuHxZ1LiMM2aKCKP3WXUXdklk=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=hLyr3v/Hk36/iusoxiPpdTT0/vOethyOEMh3rfuxnUAPzFzD5cUn8w+BamuhoItB6 fPWRN5l7/8wXGiL6NJnUfoO3hKwD9Ry9m7KRZ9YADkX0YN3ngmup173178/enCIilk i+jFjVrLqRffd1xiQ85qNaVjPIVYMU8Ie/+86usE= Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id BD548F80542; Fri, 5 May 2023 09:38:46 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 51DA5F80549; Fri, 5 May 2023 09:38:43 +0200 (CEST) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 4A18FF80544 for ; Fri, 5 May 2023 09:38:41 +0200 (CEST) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 4EDC011E2; Fri, 5 May 2023 09:38:40 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 4EDC011E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683272320; bh=aL+Xd2IgKAFk6ghPerTDwNWIpiBvD0b91+EYJyTACpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y6vgnXhnZODgjAgb29lR2K15cNNNPNyR60SfmcTxhgzkK79fNZ+9iNlZenS4vlslt Chx1Q6ZR5ZD/DL0vnyaoPLPSSvACG+MIHQZfkfrwMoaRtF+Fph7T1pbSeCD3O2qC13 Zq8PaexALNnyx1vgDhuXP9WKuusCY75XGVGDkZ6A= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Fri, 5 May 2023 09:38:35 +0200 (CEST) From: Jaroslav Kysela To: ALSA development Subject: [PATCH 2/5] ALSA: pcm: fix playback silence - use the actual new_hw_ptr for the threshold mode Date: Fri, 5 May 2023 09:38:10 +0200 Message-Id: <20230505073813.1219175-3-perex@perex.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230505073813.1219175-1-perex@perex.cz> References: <20230505073813.1219175-1-perex@perex.cz> MIME-Version: 1.0 Message-ID-Hash: EINKIZEU7SKSXO5AM7ALREMCD4H7PACS X-Message-ID-Hash: EINKIZEU7SKSXO5AM7ALREMCD4H7PACS X-MailFrom: perex@perex.cz X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Takashi Iwai , Oswald Buddenhagen , Jeff Chua X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The snd_pcm_playback_hw_avail() function uses runtime->status->hw_ptr. Unfortunately, in case when we call this function from snd_pcm_update_hw_ptr0(), this variable contains the previous hardware pointer. Use the new_hw_ptr argument to calculate hw_avail (filled samples by the user space) to correct the threshold comparison. The new_hw_ptr argument may also be set to ULONG_MAX which means the initialization phase. In this case, use runtime->status->hw_ptr. Suggested-by: Oswald Buddenhagen Signed-off-by: Jaroslav Kysela --- sound/core/pcm_lib.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index af1eb136feb0..8a01aeda2213 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -63,7 +63,15 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram } if (runtime->silence_filled >= runtime->buffer_size) return; - noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled; + /* initialization outside pointer updates */ + if (new_hw_ptr == ULONG_MAX) + new_hw_ptr = runtime->status->hw_ptr; + /* get hw_avail with the boundary crossing */ + noise_dist = appl_ptr - new_hw_ptr; + if (noise_dist < 0) + noise_dist += runtime->boundary; + /* total noise distance */ + noise_dist += runtime->silence_filled; if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) return; frames = runtime->silence_threshold - noise_dist; From patchwork Fri May 5 07:38:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 13232228 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 99674C77B7D for ; Fri, 5 May 2023 07:40:45 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id AA1052BC9; Fri, 5 May 2023 09:39:52 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz AA1052BC9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1683272442; bh=pX9TVGELB608dTydkuG6zsPxrK/u+voxwTLOkaMHsGE=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=kVImEtyfUQ+1dovK8dxOfW9zOJgicUvCV2e7a18cj1fVuTeKAaKoq4fg7qPEwJREX xQzMzo57eZdoMM5qR4a75DGvt2fFpOdoWdCzWLJUDEu0c+SWo0S0c53jn0qfwpRpEe bOkii0CJ5RFwtSKS/L45BkRK5g/dTROj/SVrvdvI= Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 4DF08F80563; Fri, 5 May 2023 09:38:52 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id B4A19F80563; Fri, 5 May 2023 09:38:48 +0200 (CEST) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id D2181F80558 for ; Fri, 5 May 2023 09:38:46 +0200 (CEST) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 1C0A911E2; Fri, 5 May 2023 09:38:46 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 1C0A911E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683272326; bh=khBaiEbQjYgBsk+V5IP5Uykr5pvU7TMLWyxD0GCkLmI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sLRijrgn9G4rbutTMkbM2sgdy9xDByPdfxLPpFtaZtiiPfYpciCxMu7J9FRkwsZbT fAn0mFUSgi7vwY9+3s4SCwOYNOzhRKCn2ZMrCPgAYupJ5PfrNG8pOnEEdb5xKKUc0e nqrtj3SvrK6h2ppEn63zRktXYPc7wH23pf9DbUtA= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Fri, 5 May 2023 09:38:41 +0200 (CEST) From: Jaroslav Kysela To: ALSA development Subject: [PATCH 3/5] ALSA: pcm: fix playback silence - correct the incremental silencing Date: Fri, 5 May 2023 09:38:11 +0200 Message-Id: <20230505073813.1219175-4-perex@perex.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230505073813.1219175-1-perex@perex.cz> References: <20230505073813.1219175-1-perex@perex.cz> MIME-Version: 1.0 Message-ID-Hash: P6QILYAD2CIZRECC22G6HIK67ZY7INSK X-Message-ID-Hash: P6QILYAD2CIZRECC22G6HIK67ZY7INSK X-MailFrom: perex@perex.cz X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Takashi Iwai , Oswald Buddenhagen , Jeff Chua X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The incremental silencing was broken with the threshold mode. The silenced area was smaller than expected in some cases. The updated area starts at runtime->silence_start + runtime->silence_filled position not only at runtime->silence_start in this mode. Unify the runtime->silence_start use for all cases (threshold and top-up). Suggested-by: Oswald Buddenhagen Signed-off-by: Jaroslav Kysela --- sound/core/pcm_lib.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index 8a01aeda2213..cfdb4aa5f6fa 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -78,26 +78,24 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram if (frames > runtime->silence_size) frames = runtime->silence_size; } else { - if (new_hw_ptr == ULONG_MAX) { /* initialization */ + if (new_hw_ptr == ULONG_MAX) { + /* initialization, fill silence to whole unused buffer */ snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime); if (avail > runtime->buffer_size) avail = runtime->buffer_size; runtime->silence_filled = avail > 0 ? avail : 0; - runtime->silence_start = (runtime->status->hw_ptr + - runtime->silence_filled) % - runtime->boundary; + runtime->silence_start = runtime->status->hw_ptr; } else { - ofs = runtime->status->hw_ptr; - frames = new_hw_ptr - ofs; + /* top-up mode (appl_ptr is not required) */ + /* silence the played area immediately */ + frames = new_hw_ptr - runtime->status->hw_ptr; if ((snd_pcm_sframes_t)frames < 0) frames += runtime->boundary; - runtime->silence_filled -= frames; - if ((snd_pcm_sframes_t)runtime->silence_filled < 0) { + if ((snd_pcm_uframes_t)frames < runtime->silence_filled) + runtime->silence_filled -= frames; + else runtime->silence_filled = 0; - runtime->silence_start = new_hw_ptr; - } else { - runtime->silence_start = ofs; - } + runtime->silence_start = new_hw_ptr; } frames = runtime->buffer_size - runtime->silence_filled; } @@ -105,7 +103,7 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram return; if (frames == 0) return; - ofs = runtime->silence_start % runtime->buffer_size; + ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; while (frames > 0) { transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; err = fill_silence_frames(substream, ofs, transfer); From patchwork Fri May 5 07:38:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 13232229 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D69DCC77B7C for ; Fri, 5 May 2023 07:41:09 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 9B2622BCA; Fri, 5 May 2023 09:40:17 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 9B2622BCA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1683272467; bh=R88cO1861f6w+/4ZfPvDViXcrR/ueLQB9Ijq6Z2MgX8=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=mHB3xyTz709Nj0RHKEnuXXuQVDuGzI9vCtkn6jnLaNZwBpxAg+IekllXDa9lctkIo vOP9Nfsk5mkdiZlQGXpNLHu2zcuFldOf9pUAZd5kWeGyjEto1g4baGu4U+suPPbC0K twRklO24HC438SMNGumBwRvfnenv/iFPdVX89yTM= Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id B2051F8056F; Fri, 5 May 2023 09:38:57 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 18598F8056F; Fri, 5 May 2023 09:38:54 +0200 (CEST) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 45C97F8055C for ; Fri, 5 May 2023 09:38:52 +0200 (CEST) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 9331D11E2; Fri, 5 May 2023 09:38:51 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 9331D11E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683272331; bh=gp37Rx7Hq4VD9UfxfAihkr+ycljvRjXneQqHQ1WMjcs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0E3s/ljK8+3EKtkjiSwUoyHyBt6yPvaHymD9ich9wI1pTzsiAeE7/kncrl46fLvHE m3fWYulkADhQ4gaxAa1uhpcbkx2gSLgeAr2ahMliVbcfYgu6GMq3H5ZTVKFPfcFwfG 8qOtm3wr7H513nOCSLgi4yk0NvVXp5i0pGB4NlPM= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Fri, 5 May 2023 09:38:46 +0200 (CEST) From: Jaroslav Kysela To: ALSA development Subject: [PATCH 4/5] ALSA: pcm: playback silence - remove extra code Date: Fri, 5 May 2023 09:38:12 +0200 Message-Id: <20230505073813.1219175-5-perex@perex.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230505073813.1219175-1-perex@perex.cz> References: <20230505073813.1219175-1-perex@perex.cz> MIME-Version: 1.0 Message-ID-Hash: QKLVIBRSMTCOD2URKGP75AVC55VQCP7I X-Message-ID-Hash: QKLVIBRSMTCOD2URKGP75AVC55VQCP7I X-MailFrom: perex@perex.cz X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Takashi Iwai , Oswald Buddenhagen , Jeff Chua X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The removed condition handles de facto only one situation where runtime->silence_filled variable is equal to runtime->buffer_size, because this variable cannot go over the buffer size. This case is catched also with the next condition for the noise distance and required threshold comparison. Suggested-by: Oswald Buddenhagen Signed-off-by: Jaroslav Kysela --- sound/core/pcm_lib.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index cfdb4aa5f6fa..952f0d807124 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -61,8 +61,6 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram runtime->silence_filled = 0; runtime->silence_start = appl_ptr; } - if (runtime->silence_filled >= runtime->buffer_size) - return; /* initialization outside pointer updates */ if (new_hw_ptr == ULONG_MAX) new_hw_ptr = runtime->status->hw_ptr; From patchwork Fri May 5 07:38:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 13232230 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6024AC77B7D for ; Fri, 5 May 2023 07:41:16 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 29D202BEA; Fri, 5 May 2023 09:40:24 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 29D202BEA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1683272474; bh=/t3AQOl3bcfttdZtUZYZuHBFAPWJaA7KrBDtJB1p4gI=; h=From:To:Subject:Date:In-Reply-To:References:CC:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=vQzWjich5UjJ5JyneG05Vxlv3JsX5yacmcXQ3048LkbLz1+9zoA3vbN0TNxeZo4Ni 9pQXepGzUQ2hXozhJaaB+BCLiNd99BpYvZKPXKbUL5nSNcQ8L+08lIe6VQkMyeX0wV C66EDumVtAR3mizrvMnr717LWRQQPFdVaEY1xdJ8= Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 28FA2F80570; Fri, 5 May 2023 09:39:03 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 96B5EF80579; Fri, 5 May 2023 09:38:59 +0200 (CEST) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id B5DB9F80570 for ; Fri, 5 May 2023 09:38:57 +0200 (CEST) Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 0C23111E2; Fri, 5 May 2023 09:38:57 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 0C23111E2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1683272337; bh=JHSZHacJQAS5oo8RIrsJbAK+cQk3HLXFzjg6ru/0zmM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RrCl1BX84EYFY/3szQAX9pSp9BaAiOUvs5Fn9aoqHgibUb9lqz6B2HqwLEGr1Jbe0 aXzk6ThNynNUtvbr8x0jCAQZE2Av/yBA4R6+dPtKjsS/gjHac3bQ12cVQLJ/zNx6vm MWw/jQCK4l9jCl41RAUCbmJAplCAJGfQ3aYhNwm0= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Fri, 5 May 2023 09:38:52 +0200 (CEST) From: Jaroslav Kysela To: ALSA development Subject: [PATCH 5/5] ALSA: pcm: playback silence - move silence variables updates to separate function Date: Fri, 5 May 2023 09:38:13 +0200 Message-Id: <20230505073813.1219175-6-perex@perex.cz> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230505073813.1219175-1-perex@perex.cz> References: <20230505073813.1219175-1-perex@perex.cz> MIME-Version: 1.0 Message-ID-Hash: 7735EI25FZJFMXUHVLBFN7KJYPDT74SP X-Message-ID-Hash: 7735EI25FZJFMXUHVLBFN7KJYPDT74SP X-MailFrom: perex@perex.cz X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Takashi Iwai , Oswald Buddenhagen , Jeff Chua X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: There is a common code in the threshold and top-up mode tracking the added (already silenced) samples. Move this code to one place to enhance the readability. Signed-off-by: Jaroslav Kysela --- sound/core/pcm_lib.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index 952f0d807124..6b0601fec832 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -33,6 +33,25 @@ static int fill_silence_frames(struct snd_pcm_substream *substream, snd_pcm_uframes_t off, snd_pcm_uframes_t frames); + +static inline void silence_update(struct snd_pcm_runtime *runtime, + snd_pcm_uframes_t ptr, + snd_pcm_uframes_t new_ptr) +{ + snd_pcm_sframes_t n; + + if (ptr == new_ptr) + return; + n = new_ptr - ptr; + if (n < 0) + n += runtime->boundary; + if ((snd_pcm_uframes_t)n < runtime->silence_filled) + runtime->silence_filled -= n; + else + runtime->silence_filled = 0; + runtime->silence_start = new_ptr; +} + /* * fill ring buffer with silence * 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 int err; if (runtime->silence_size < runtime->boundary) { - snd_pcm_sframes_t noise_dist, n; + snd_pcm_sframes_t noise_dist; snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); - if (runtime->silence_start != appl_ptr) { - n = appl_ptr - runtime->silence_start; - if (n < 0) - n += runtime->boundary; - if ((snd_pcm_uframes_t)n < runtime->silence_filled) - runtime->silence_filled -= n; - else - runtime->silence_filled = 0; - runtime->silence_start = appl_ptr; - } + silence_update(runtime, runtime->silence_start, appl_ptr); /* initialization outside pointer updates */ if (new_hw_ptr == ULONG_MAX) new_hw_ptr = runtime->status->hw_ptr; @@ -86,14 +96,7 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_ufram } else { /* top-up mode (appl_ptr is not required) */ /* silence the played area immediately */ - frames = new_hw_ptr - runtime->status->hw_ptr; - if ((snd_pcm_sframes_t)frames < 0) - frames += runtime->boundary; - if ((snd_pcm_uframes_t)frames < runtime->silence_filled) - runtime->silence_filled -= frames; - else - runtime->silence_filled = 0; - runtime->silence_start = new_hw_ptr; + silence_update(runtime, runtime->status->hw_ptr, new_hw_ptr); } frames = runtime->buffer_size - runtime->silence_filled; }