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 8A71BC47422 for ; Sun, 21 Jan 2024 11:48: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 ED58F83B; Sun, 21 Jan 2024 12:47:56 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz ED58F83B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1705837687; bh=ZT0AUqTtPT6wkkXqAKwHnyohCWe+XlnaDjzDGqpRN60=; h=Subject:From:To:Date:List-Id:List-Archive:List-Help:List-Owner: List-Post:List-Subscribe:List-Unsubscribe:From; b=Az7fLk1zgg7bYdrlpLPmfkRZDE37eCc4BDtV+vAs8ZF2a1gUqOZY39HhmD59L/igk l+jO4988dy+zakryy9hhxoudKSO9Gdp/Epd6u8h1iq7kwxRFk+oRMKKBiFGPbrCT46 MppK+jBFvjlHQGoOH7+r2ufbP3lFzfSjh2Mbxrvw= Received: by alsa1.perex.cz (Postfix, from userid 50401) id 727BCF80588; Sun, 21 Jan 2024 12:47:35 +0100 (CET) Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 19901F80580; Sun, 21 Jan 2024 12:47:35 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 65225F8025F; Sun, 21 Jan 2024 12:47:26 +0100 (CET) Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) (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 89330F8003A for ; Sun, 21 Jan 2024 12:47:18 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 89330F8003A Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key, unprotected) header.d=posteo.de header.i=@posteo.de header.a=rsa-sha256 header.s=2017 header.b=e3DmSUMl Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id A93A8240101 for ; Sun, 21 Jan 2024 12:47:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1705837637; bh=ZT0AUqTtPT6wkkXqAKwHnyohCWe+XlnaDjzDGqpRN60=; h=Message-ID:Subject:From:To:Date:Autocrypt:Content-Type: Content-Transfer-Encoding:MIME-Version:From; b=e3DmSUMlV7uFtlRptSPI+IFZ7RBT+KRQe7W0mn5CRrtthJLRcf0G2eVhqJXigCGz9 hPH2ZsknZqbrHcqeD9ppL8hq2hvxy3P2GCLgwy1BZCt6zGLv6aBpLJklADGNHbje4E STE6uLMZ9f9uLN0GyKdfFim6WWNU0bXGQJl7J8WIlNPOJwdn/EMmo284W8MjqpXIK/ pzKGCV7EkmCxAfwoJc8cNubhyXI6qyu3F9xJE4U+pDjmCDjJUux9ndoXrrGe6dg7u3 dAK7feJ1VYN7mIx1QWkmSbjN8qDPaxCb483KqjTtxzq2QwCoS+1CMDBYSs6593NsVp ACS8iBu7FefDQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4THs5x0ncYz6twL for ; Sun, 21 Jan 2024 12:47:16 +0100 (CET) Message-ID: Subject: How to configure a pcm to write to it periodically? From: Philipp Stanner To: alsa-devel@alsa-project.org Date: Sun, 21 Jan 2024 11:47:16 +0000 Autocrypt: addr=stanner@posteo.de; prefer-encrypt=mutual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bKgQ5CjiPBvNzbXHFoUzkDK1nM97cfpIOzXJi6U2EcRpJSd/uYgLfGpYpVARlAtfZvYhHwJKOkgMzZShz6TMkbm9TnofIQARAQABiQI8BBgBCAAmFiEEKhdtQ6gsUuxNg+0TCD/rNRUJyDUFAlui1z8CGwwFCQ8JnAAACgkQCD/rNRUJyDWe3g/+L5bOyije1iAl2muvHFkUWHTXPkgZhA1fbcLOZYJ+cCxWzl42cwCgXAwCBvDXrg0VeWKlauf0Fin/Qx2YY+fbraDTWYAhaT9iyhPkZcOIL0vXK+COyI2Iw6sOZ8/Lwbb+xTNJ0ahr2HPsmLKrKhp6PGj0gkVaTDLcyxZiS/oB/d5qCowKfEULXW2dK3qvSUMFegOEN8q8X+e4etf2PsYbADViFMdI8iXflouUdP/1dFJlzad+lPDxA6pOqBZ8eLs+x2B86V5m/ZjSgyl99TxPwREb4rJR2MKFbrhWNiUPisoQKQLxa1rjQWqX2PuP0G7KXeAuBgawIjvSAFkg/DfSGiTZvdckh6D83bXW/9JHVWz8Um0pSYK4p6xjc/L+8a0kH8WO0up6f3bHYkcruNjIGs+AXZkKCZwcQkTYQpcHX1Z1BE4GBidrgsWpF9mbTsAvdc7GzTljKGSVBqdGKYhnH9bq0PPZoWX0maV2LEyAPe0GWaJ/SInshgH0KRKZ0rIeuV0AYGt0DBJCzWExh3PgH39Xz06I/R0OHTgDy3zy38tylUdNNmCO/BNZmEF89eUc5PezqgH4dzDScyvqE96n7xQwJTSRxQqMFsL3Vgl/CP8J79ai+GameHWHcP8ImyK9jHE F7QIFgS2GY4HlfvXRAAgut8lvmafqET/zlEMAp68= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Message-ID-Hash: CGV7J3Q62HXG7GXXRZ7PXX36GPT2BUJN X-Message-ID-Hash: CGV7J3Q62HXG7GXXRZ7PXX36GPT2BUJN X-MailFrom: stanner@posteo.de 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 X-Mailman-Version: 3.3.9 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: Hi all, hope this is the right list. [1] stated that it's also intended for "application developers", so I ask here. I've got a program that uses timers to periodically read samples from a microphone and write to a sound device. The timer's period is, for example, 20ms. So I have a sample rate of r =3D 16000, frame sizes of 20ms, a bit depth of 16 and therefore 320 samples per 20ms. I try to write those 320 samples timer-controled every 20ms to the pcm device. That works fine when reading samples. When writing, however, it seems that snd_pcm_writei() blocks longer than the 20ms. Which it shouldn't do, as far as I understand snd_pcm_set_params() delay-parameter. I'll attach the example below. So, I'd have a number of questions: 1. Am I even using ALSA as it's intended? As far as I saw in the API, there's the possiblity to get poll()able file descriptors from the API. So should I, instead of using my own timers, configure the ALSA interface to become readable every 20ms? 2. Why does my example not work as I intended? What does the "latency" parameter in snd_pcm_set_params() actually do? 3. Is the 'size' parameter in the write functions [2] the size in bytes or in samples? So if I configured SND_PCM_S16_LE and pass size =3D 10, will it read 10 bytes or 160 bit? It seems to be that it's the number of samples, whose type has been set with snd_pcm_set_params(). The docu isn't super clear to me, though. It seems that the size parameter has changed a bit. At least the examples here [3] are faulting when I build them with -fsanitize=3Daddress Thx a lot! Philipp [1] https://www.alsa-project.org/wiki/Mailing-lists [2] https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#gabc7= 48a500743713eafa960c7d104ca6f [3] http://equalarea.com/paul/alsa-audio.html Example: /* * This is a mini-example of the code I took for my draft ofa broader appl= ication. * The main difference is that I call snd_pcm_writei() when my timer fires. */ #include static char *device =3D "default"; /* playback= device */ unsigned char buffer[16*1024]; /* some random data */ int main(v= oid) { int err; unsigned int i; snd_pcm_t *handle; snd_pcm_sframes_t frames= ; for (i =3D 0; i < sizeof(buffer); i++) buffer[i] =3D random() & 0xff; if = ((err =3D snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {= printf("Playback open error: %s\n", snd_strerror(err)); exit(EXIT_FAILURE)= ; } if ((err =3D snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_= ACCESS_RW_INTERLEAVED, 1, 16000, 1, 20000)) < 0) { printf("Playback open er= ror: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); } for (i =3D 0; i < 16;= i++) { frames =3D snd_pcm_writei(handle, buffer, sizeof(buffer) / sizeof(s= hort)); if (frames < 0) frames =3D snd_pcm_recover(handle, frames, 0); if = (frames < 0) { printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));= break; } if (frames > 0 && frames < (long)(sizeof(buffer) / sizeof(short))= ) printf("Short write (expected %li, wrote %li)\n", (long)(sizeof(buffer) /= sizeof(short)), frames); } /* pass the remaining samples, otherwise they'r= e dropped in close */ err =3D snd_pcm_drain(handle); if (err < 0) printf("s= nd_pcm_drain failed: %s\n", snd_strerror(err)); snd_pcm_close(handle); retu= rn 0; }