I'm currently learning to develop using alsa API (libasound). I want to send PCM sound to my usb sound card.
I run this code :
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include "helloPi.h"
int main(int argc, char *argv[]) {
int i;
int err;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_open(&playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK,
0)) < 0) {
fprintf(stderr, "cannot open audio device %s (%s)\n", argv[1],
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) {
fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_any(playback_handle, hw_params)) < 0){
fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params,
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params,
SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err));
exit(1);
}
unsigned int freq = 44100;
if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params,
&freq, 0)) < 0) {
fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2))
< 0) {
fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err));
exit(1);
}
if ((err = snd_pcm_hw_params(playback_handle, hw_params)) < 0) {
fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err));
exit(1);
}
snd_pcm_hw_params_free(hw_params);
if ((err = snd_pcm_prepare(playback_handle)) < 0) {
fprintf(stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror(err));
exit(1);
}
for (i = 0; i < 100; ++i) {
if ((err = snd_pcm_writei(playback_handle, buf, 2048)) < 0) {
fprintf(stderr, "write to audio interface failed (%s)\n",
snd_strerror(err));
exit(1);
}
}
snd_pcm_close(playback_handle);
exit(0);
}
Nota : buf is an array declared in HelloPi.h containing a sound wave.
When I use snd_pcm_open(&playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK,0) with hw:0,0, is work fine with the internal sound card of my laptop. But, if I use hw:1,0, to use the usb sound card, nothing happened (not even an error !).
If I run in a terminal :
aplay -l
I have the following result :
carte 0: PCH [HDA Intel PCH], périphérique 0: ALC283 Analog [ALC283 Analog]
Sous-périphériques: 1/1
Sous-périphérique #0: subdevice #0
carte 0: PCH [HDA Intel PCH], périphérique 3: HDMI 0 [HDMI 0]
Sous-périphériques: 1/1
Sous-périphérique #0: subdevice #0
carte 1: Set [C-Media USB Headphone Set], périphérique 0: USB Audio [USB Audio]
Sous-périphériques: 1/1
Sous-périphérique #0: subdevice #0
Hello
I know that the usb card is OK because I have sound with :
speaker-test -Dhw:1,0 -c2 -twav
I don't know why my code is note producing sound with hw:1,0...
I hope some of you will help me ! Thanks,
Maxime.