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 6D3C0C48BF6 for ; Thu, 29 Feb 2024 06:26:47 +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 2821C828; Thu, 29 Feb 2024 07:26:30 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 2821C828 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1709188000; bh=+4kvN9h2RfOejwcb+pLVDu3z+Gb9Tp0Zs//i5jXrbxs=; h=From:To:In-Reply-To:References:Subject:Date:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From; b=cmol4OK6+Z8pAtbgXs4Quu+i2AVbuG/HgEUThxgs67ACBnwf9inm0TYQM5y0s+N/n 4MiFWjnSsMrIFnf+EjRXPMI9MVOWaY8uOwzI9zOaH52olRe2l+xlYL6UJzY2j46ijT +s8Iei2xzd53kHm3Y0vC2DoD4SbHd7N6LUjOPURw= Received: by alsa1.perex.cz (Postfix, from userid 50401) id BF8E8F8057D; Thu, 29 Feb 2024 07:26:07 +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 CEC54F805A8; Thu, 29 Feb 2024 07:26:06 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id DABACF8024C; Thu, 29 Feb 2024 07:24:35 +0100 (CET) Received: from webhooks-bot.alsa-project.org (vmi242170.contaboserver.net [207.180.221.201]) by alsa1.perex.cz (Postfix) with ESMTP id 82F4CF8013D for ; Thu, 29 Feb 2024 07:24:31 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 82F4CF8013D MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: GitHub issues - opened To: alsa-devel@alsa-project.org In-Reply-To: <1709187869858871222-webhooks-bot@alsa-project.org> References: <1709187869858871222-webhooks-bot@alsa-project.org> Subject: alsa cant initialize on raspberry pi 5 Message-Id: <20240229062435.DABACF8024C@alsa1.perex.cz> Date: Thu, 29 Feb 2024 07:24:35 +0100 (CET) Message-ID-Hash: 3KOAAJHPDSVUVIYL7JS2IF7TCJIS7EAH X-Message-ID-Hash: 3KOAAJHPDSVUVIYL7JS2IF7TCJIS7EAH X-MailFrom: github@alsa-project.org 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: alsa-project/alsa-lib issue #386 was opened from sy-project: I can use alsa command ``` # aplay -l **** List of PLAYBACK Hardware Devices **** card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplusadcpro], device 0: HiFiBerry DAC+ADC Pro HiFi multicodec-0 [HiFiBerry DAC+ADC Pro HiFi multicodec-0] Subdevices: 1/1 Subdevice #0: subdevice #0 # arecord -l **** List of CAPTURE Hardware Devices **** card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplusadcpro], device 0: HiFiBerry DAC+ADC Pro HiFi multicodec-0 [HiFiBerry DAC+ADC Pro HiFi multicodec-0] Subdevices: 1/1 Subdevice #0: subdevice #0 ``` and I can aplay to use wav file. ``` # aplay -D hw:0,0 /root/C_Code/audiotest/test1.wav Playing WAVE '/root/C_Code/audiotest/test1.wav' : Signed 32 bit Little Endian, Rate 48000 Hz, Stereo ``` but I cant initialize on c code. my code is here. ```c printf("alsa start\n"); int err; snd_pcm_t *capture_handle, *playback_handle; snd_pcm_hw_params_t *hw_params; unsigned char *buffer; int sample_rate = SAMPLE_RATE; int dir; snd_pcm_uframes_t frames = 32; snd_pcm_uframes_t bufferSize, periodSize; double x_prev = 0.0; double y_prev = 0.0; double b0 = 1.0; double b1 = -0.8; double a1 = 0.0; printf("alsa open capture handle\n"); err = snd_pcm_open(&capture_handle, "default", SND_PCM_STREAM_CAPTURE, 0); if(err < 0) { fprintf(stderr, "Capture open error: %s\n", snd_strerror(err)); exit(1); } printf("alsa open playback handle\n"); err = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if(err < 0) { fprintf(stderr, "Playback open error: %s\n", snd_strerror(err)); exit(1); } printf("alsa pcm hw param alloc\n"); snd_pcm_hw_params_alloca(&hw_params); printf("alsa capture_handle set start\n"); err = snd_pcm_hw_params_any(capture_handle, hw_params); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_any cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_access cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_S16_LE); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_format cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, NUM_CHANNELS); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_channels cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &sample_rate, &dir); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_rate_near cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_buffer_size_near(capture_handle, hw_params, &bufferSize); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_buffer_size_near cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params_set_period_size_near(capture_handle, hw_params, &periodSize, &dir); if (err < 0) { fprintf(stderr, "snd_pcm_hw_params_set_period_size_near cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } err = snd_pcm_hw_params(capture_handle, hw_params); if (err < 0) { fprintf(stderr, "alsa cant set hw params. [%s]\n", snd_strerror(err)); exit(1); } snd_pcm_hw_params_get_buffer_size(hw_params, &bufferSize); snd_pcm_hw_params_get_period_size(hw_params, &periodSize, &dir); printf("Buffer size: %lu frames\n", bufferSize); printf("Period size: %lu frames\n", periodSize); printf("alsa playback_handle set start\n"); snd_pcm_hw_params_any(playback_handle, hw_params); snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_channels(playback_handle, hw_params, NUM_CHANNELS); snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &sample_rate, 0); snd_pcm_hw_params(playback_handle, hw_params); printf("alsa playback_handle set success\n"); printf("alsa buf malloc\n"); buffer = (unsigned char*)malloc(BUFFER_SIZE*NUM_CHANNELS*2); while(1) { printf("main loop start\n"); err = snd_pcm_readi(capture_handle, buffer, BUFFER_SIZE); if(err<0) { fprintf(stderr, "Capture read error[%d]: %s\n", err, snd_strerror(err)); break; } for(int i = 0; i < BUFFER_SIZE * NUM_CHANNELS * 2; i += 2) { short sample = ((short)buffer[i + 1] << 8) | buffer[i]; double x = (double)sample / 32768.0; double y = b0 * x + b1 * x_prev + a1 * y_prev; x_prev = x; y_prev = y; short filter_sample = (short)(y * 32768.0); buffer[i] = (unsigned char)(filter_sample & 0xFF); buffer[i+1] = (unsigned char)((filter_sample >> 8) & 0xFF); } err = snd_pcm_writei(playback_handle, buffer, BUFFER_SIZE); if(err < 0) { fprintf(stderr, "Playback write error: %s\n", snd_strerror(err)); break; } } free(buffer); snd_pcm_close(capture_handle); snd_pcm_close(playback_handle); exit(0); ``` This code said ``` alsa start alsa open capture handle alsa open playback handle alsa pcm hw param alloc alsa capture_handle set start snd_pcm_hw_params_set_buffer_size_near cant set hw params. [Invalid argument] ``` what should I do? Issue URL : https://github.com/alsa-project/alsa-lib/issues/386 Repository URL: https://github.com/alsa-project/alsa-lib