|
Packit |
4a16fb |
/*
|
|
Packit |
4a16fb |
* This extra small demo sends a random samples to your speakers.
|
|
Packit |
4a16fb |
*/
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
#include "../include/asoundlib.h"
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
static char *device = "default"; /* playback device */
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
snd_output_t *output = NULL;
|
|
Packit |
4a16fb |
unsigned char buffer[16*1024]; /* some random data */
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
int main(void)
|
|
Packit |
4a16fb |
{
|
|
Packit |
4a16fb |
int err;
|
|
Packit |
4a16fb |
unsigned int i;
|
|
Packit |
4a16fb |
snd_pcm_t *handle;
|
|
Packit |
4a16fb |
snd_pcm_sframes_t frames;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
for (i = 0; i < sizeof(buffer); i++)
|
|
Packit |
4a16fb |
buffer[i] = random() & 0xff;
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
|
|
Packit |
4a16fb |
printf("Playback open error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if ((err = snd_pcm_set_params(handle,
|
|
Packit |
4a16fb |
SND_PCM_FORMAT_U8,
|
|
Packit |
4a16fb |
SND_PCM_ACCESS_RW_INTERLEAVED,
|
|
Packit |
4a16fb |
1,
|
|
Packit |
4a16fb |
48000,
|
|
Packit |
4a16fb |
1,
|
|
Packit |
4a16fb |
500000)) < 0) { /* 0.5sec */
|
|
Packit |
4a16fb |
printf("Playback open error: %s\n", snd_strerror(err));
|
|
Packit |
4a16fb |
exit(EXIT_FAILURE);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
for (i = 0; i < 16; i++) {
|
|
Packit |
4a16fb |
frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
|
|
Packit |
4a16fb |
if (frames < 0)
|
|
Packit |
4a16fb |
frames = snd_pcm_recover(handle, frames, 0);
|
|
Packit |
4a16fb |
if (frames < 0) {
|
|
Packit |
4a16fb |
printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
|
|
Packit |
4a16fb |
break;
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
if (frames > 0 && frames < (long)sizeof(buffer))
|
|
Packit |
4a16fb |
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
|
|
Packit |
4a16fb |
}
|
|
Packit |
4a16fb |
|
|
Packit |
4a16fb |
snd_pcm_close(handle);
|
|
Packit |
4a16fb |
return 0;
|
|
Packit |
4a16fb |
}
|