[alsa-devel] using ALSA one call
Hi Developers.
My name is Alf and i new in this list . I physics student , and i write some sound programs under linux. Big sorry for my english , in school i learn only deutsch.
So far i used old /dev/dsp open funntion also under ALSA.
/*--OSD--*/#define RATE 44100 /* the sampling rate */ /*--OSD--*/#define SIZE 16 /* sample size: 8 or 16 bits */ /*--OSD--*/#define CHANNELS 2 /* 1 = mono 2 = stereo */ /*--OSD--*/void open_sound_device_wr(void) /*--OSD--*/{// fdo = open("/dev/dsp", O_RDWR ); /*--OSD--*/ fdo = open("/dev/dsp", O_WRONLY ); /*--OSD--*/ if (fdo < 0) { perror("open of /dev/dsp failed"); exit(1); } /*--OSD--*/ /*--OSD--*/ arg = SIZE; //// sample size /*--OSD--*/ status = ioctl(fdo, SOUND_PCM_WRITE_BITS, &arg); /*--OSD--*/ if (status == -1) perror("SOUND_PCM_WRITE_BITS ioctl failed"); /*--OSD--*/ if (arg != SIZE) perror("unable to set sample size"); /*--OSD--*/ /*--OSD--*/ arg = CHANNELS; //// mono or stereo /*--OSD--*/ status = ioctl(fdo, SOUND_PCM_WRITE_CHANNELS, &arg); /*--OSD--*/ if (status == -1) perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); /*--OSD--*/ if (arg != CHANNELS) perror("unable to set number of channels"); /*--OSD--*/ /*--OSD--*/ arg = RATE; //// sampling rate /*--OSD--*/ status = ioctl(fdo, SOUND_PCM_WRITE_RATE, &arg); /*--OSD--*/ if (status == -1) perror("SOUND_PCM_WRITE_WRITE ioctl failed"); /*--OSD--*/} ----
Now i wana try da same with ALSA , for example all parameters put in one struckture and then simple call one universal alsa_open funktion. Is it posssible with ALSA ? Is here copy-paste example/function for fast, comfortable and easy ALSA use ?
for example so : ==== #include "alsa/asoundlib.h" ... alsa_parm_struckt.device = "default"; // "hw:0,0"; "hw:1,0"; alsa_parm_struckt.play_capt = SND_PCM_STREAM_PLAYBACK; alsa_parm_struckt.format = SND_PCM_FORMAT_S16_LE; alsa_parm_struckt.access = SND_PCM_ACCESS_RW_INTERLEAVED; //alsa_parm_struckt.samplerate = 48000; alsa_parm_struckt.samplerate = 44100; alsa_parm_struckt.channelz =2; alsa_parm_struckt.bytes_per_sample = 2; alsa_parm_struckt.latency = 0; // ?? alsa_parm_struckt.nonblock = 1; // ?? ... other parameters set ... ... alsa_open_function( & handle , & alsa_parm_struckt [ , ...] );
and after this write data blocks frames_2_write = ... for(;;) { ... frames = snd_pcm_writei(handle, buffer, frames_2_write ); ... } ====
OK , this example works http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm__min_8c-example.ht... but only if samplerate = 48000; if samplerate = 44100 - then not :( 44k file play in 48k mode :D it sounds funnee :D
I remixed this program so that it read data from wave file.
gcc pcm_min01.c -lasound -O2 -o pcm_min01 ./pcm_min01 '/mnt/hda2/Booty Luv - Shine/Booty Luv - Shine (Ian Carey remix).wav' i gotta this : ALSA lib pcm.c:7160:(snd_pcm_set_params) Rate doesn't match (requested 44100Hz, get 0Hz) Playback open error: Invalid argument
With aplay no problems, it is played in da correct speed. -- aplay '/mnt/hda2/Booty Luv - Shine/Booty Luv - Shine (Ian Carey remix).wav' Playing WAVE '/mnt/hda2/Booty Luv - Shine/Booty Luv - Shine (Ian Carey remix).wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo --
Any ideas/examples/... welcome Tnx in advance
Alf
====
----
kallipygos@inbox.lv wrote:
OK , this example works http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm__min_8c-example.ht... but only if samplerate = 48000; if samplerate = 44100 - then not :( 44k file play in 48k mode :D it sounds funnee :D
I remixed this program so that it read data from wave file.
gcc pcm_min01.c -lasound -O2 -o pcm_min01 ./pcm_min01 '/mnt/hda2/Booty Luv - Shine/Booty Luv - Shine (Ian Carey remix).wav' i gotta this : ALSA lib pcm.c:7160:(snd_pcm_set_params) Rate doesn't match (requested 44100Hz, get 0Hz)
Please show the source code of your program.
Best regards, Clemens
Moin Meister
Quoting Clemens Ladisch clemens@ladisch.de:
but only if samplerate = 48000; if samplerate = 44100 - then not :( 44k file play in 48k mode :D it sounds funnee :D
i gotta this : ALSA lib pcm.c:7160:(snd_pcm_set_params) Rate doesn't match (requested
44100Hz, get 0Hz)
Please show the source code of your program.
see below
I have on my notebook 2 sound cards * built in $ lspci ... 00:1b.0 Audio device: Intel Corporation 82801G (ICH7 Family) High Definition Audio Controller (rev 02) ... and second on USB wire - Edirol by Roland ... I have switch em to 44,1k
As given here http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#g6aa164ed37... latency required overall latency in us (0 = optimum latency for players) i have try to set optimum latency = 0;
snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, channelz, samplerate , 0, 0)
./pcm_min01 '/mnt/hda2/Booty Luv - Shine/Booty Luv - Boogie 2nite (DJ Teddy-o remix).wav' Now playing to USB device "hw:1,0" gotta this : Short write (expected 4096, wrote 360) Short write (expected 4096, wrote 360) Short write (expected 4096, wrote 2427) wery fragmentary sound
with latency = 500000 us USB play is OK.
But why i can not play my wave on notebook sound device "hw:0,0" ??
Tnx in advance
Alf
/* * This extra small demo sends a random samples to your speakers. */
#include "alsa/asoundlib.h"
#include <stdio.h> #include <fcntl.h> // #include <unistd.h> // #include <sys/types.h> //
//static char *device = "default"; /* playback device */ //static char *device = "hw:0,0"; static char *device = "hw:1,0";
snd_output_t *output = NULL; //unsigned char buffer[16*1024]; /* some random data */ /*unsigned*/ short buffer[/*4*1024*/8192];
int main( int argc, char *argv[] ) { int err; unsigned int i; int channelz, bytes_per_sample, samplerate; snd_pcm_t *handle; snd_pcm_sframes_t frames , frames_2_write; int einfd ; int rdstatus ;
// samplerate = 48000; samplerate = 44100; channelz =2; bytes_per_sample = 2; frames_2_write = sizeof(buffer) / (channelz * bytes_per_sample); //for (i = 0; i < (4*1024) ; i++){ buffer[i] = random() & 0xffff; } einfd = open( argv[1] , O_RDONLY ); lseek(einfd, 48L ,SEEK_SET);
if ((err = 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 = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, channelz, samplerate , 0, 500000)) < 0) { /* 500000 0.5sec */ printf("Playback open error: %s\n", snd_strerror(err)); exit(EXIT_FAILURE); }
for (i = 0; i < 128; i++) { rdstatus = read(einfd,&buffer[0],sizeof(buffer)); frames = snd_pcm_writei(handle, buffer, frames_2_write ); if (frames < 0) frames = snd_pcm_recover(handle, frames, 1); if (frames < 0) { printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); /*break;*/ goto yuck_off; } if (frames > 0 && frames < (long) frames_2_write) printf("Short write (expected %li, wrote %li)\n", (long) frames_2_write, frames); }
yuck_off: close(einfd); snd_pcm_close(handle); return 0; }
----
Alfs Kurmis wrote:
As given here http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#g6aa164ed37... latency required overall latency in us (0 = optimum latency for players) i have try to set optimum latency = 0;
That documentation is wrong; 0 is not allowed. Just use 0.5 s, or some larger value.
But why i can not play my wave on notebook sound device "hw:0,0" ??
Because the "hw" device goes straight to the hardware and disallows any conversion of sample rate/format.
Use "default", or something like "default:0" to select a specific card.
snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, channelz, samplerate , 0, 500000)
... and setting soft_resample to 0 disallows resampling in any case.
HTH Clemens
participants (3)
-
Alfs Kurmis
-
Clemens Ladisch
-
kallipygos@inbox.lv