mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-10-29 05:40:25 -04:00
Added pcm_min test & example - minimalistic pcm output
This commit is contained in:
parent
23f7e58fa1
commit
4e1452ac07
2 changed files with 58 additions and 1 deletions
56
test/pcm_min.c
Normal file
56
test/pcm_min.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* This extra small demo sends a raw file to your speakers.
|
||||
*/
|
||||
|
||||
#include "../include/asoundlib.h"
|
||||
|
||||
static char *device = "default"; /* playback device */
|
||||
|
||||
snd_output_t *output = NULL;
|
||||
unsigned char buffer[16*1024]; /* some random data */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err;
|
||||
unsigned int i;
|
||||
snd_pcm_t *handle;
|
||||
snd_pcm_sframes_t frames;
|
||||
|
||||
for (i = 0; i < sizeof(buffer); i++)
|
||||
buffer[i] = random() & 0xff;
|
||||
|
||||
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_U8,
|
||||
SND_PCM_ACCESS_RW_INTERLEAVED,
|
||||
1,
|
||||
48000,
|
||||
1,
|
||||
500000)) < 0) { /* 0.5sec */
|
||||
printf("Playback open error: %s\n", snd_strerror(err));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((err = snd_pcm_nonblock(handle, 0)) < 0) {
|
||||
printf("Set to blocking mode failed: %s\n", snd_strerror(err));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
|
||||
if (frames < 0)
|
||||
frames = snd_pcm_recover(handle, frames, 0);
|
||||
if (frames < 0) {
|
||||
printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
|
||||
break;
|
||||
}
|
||||
if (frames > 0 && frames < (long)sizeof(buffer))
|
||||
printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
|
||||
}
|
||||
|
||||
snd_pcm_close(handle);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue