Hi,
I have modified an example programm of the alsa website, in fact
to test reading a wave file.
The behaviour of my program is very strange !
It works fine, the sample is played until the end, except when
I change virtual screen in Window Maker !! When i do it,
the program exit without error before the end of the sample.
Very strange ....
Is there something wrong in the program ?
This is the source code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define TAILLE 4000000
snd_pcm_t *sound_handle;
snd_output_t *errlog;
snd_pcm_hw_params_t *hwparams;
char *buffer;
int desc;
int n;
int main(void)
{
int err;
// Connect error reporting to stderr
snd_output_stdio_attach(&errlog, stderr, 0);
if (snd_pcm_open(&sound_handle, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0)
< 0)
{
fprintf(stderr, "AIE ! Error opening hw:0,0\n");
snd_output_close(errlog);
return 1;
}
// Set up the hardware device for 16-bit, 44KHZ, stereo
// First initialize the hwparams struct using the sound_handle
// we got for our sound hardware
snd_pcm_hw_params_alloca(&hwparams);
err = snd_pcm_hw_params_any(sound_handle, hwparams);
if (err < 0)
goto _alsa_error;
// Now request the desired parameters one by one
// Access method, interleaved or non-interleaved
err = snd_pcm_hw_params_set_access(sound_handle, hwparams,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0)
goto _alsa_error;
// The sample format, signed 16-bit little endian
err = snd_pcm_hw_params_set_format(sound_handle, hwparams,
SND_PCM_FORMAT_S16_LE);
if (err < 0)
goto _alsa_error;
// The sample rate
err = snd_pcm_hw_params_set_rate(sound_handle, hwparams,
44100, 0);
if (err < 0)
goto _alsa_error;
// Number of channels we want, stereo = 2
err = snd_pcm_hw_params_set_channels(sound_handle, hwparams, 2);
if (err < 0)
goto _alsa_error;
// The period size. For all practical purposes this is synonymous
// to OSS/Free's fragment size.
// Note that this in frames (frame = nr_channels * sample_width)
// So a value of 1024 means 4096 bytes (1024 x 2 x 16-bits)
err = snd_pcm_hw_params_set_period_size(sound_handle, hwparams,
1024, 0);
if (err < 0)
goto _alsa_error;
// The number of periods we want to allocate, 4 is reasonable
err = snd_pcm_hw_params_set_periods(sound_handle, hwparams, 4, 0);
if (err < 0)
goto _alsa_error;
// Finally setup our hardware with the selected values
err = snd_pcm_hw_params(sound_handle, hwparams);
if (err < 0) {
fprintf(stderr, "Unable to set hardware parameter:\n");
snd_pcm_hw_params_dump(hwparams, errlog);
return 2;
}
fprintf(stdout, "Success!\n");
buffer = malloc(TAILLE);
desc = open("dj.wav", O_RDONLY);
read(desc, buffer, TAILLE);
printf("Debut de la lecture\n");
n = snd_pcm_writei(sound_handle, buffer, TAILLE);
printf("Retour : %d\n", n);
printf("Fin de la lecture\n");
snd_pcm_close(sound_handle);
snd_output_close(errlog);
return 0;
_alsa_error:
fprintf(stderr, "Invalid hardware parameter for device:\n");
snd_pcm_hw_params_dump(hwparams, errlog);
snd_pcm_close(sound_handle);
snd_output_close(errlog);
return 1;
}
Thanks
______________________________________________________________________________
ifrance.com, l'email gratuit le plus complet de l'Internet !
vos emails depuis un navigateur, en POP3, sur Minitel, sur le WAP...
http://www.ifrance.com/_reloc/email.emailif
_______________________________________________
Alsa-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/alsa-devel