Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This extra small demo sends a random samples to your speakers.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include "../include/alsa/asoundlib.h"
- static char *device = "playback_0"; /* playback device */
- snd_output_t *output = NULL;
- //unsigned char buffer[16*1024]; /* some random data */
- unsigned char buffer[1024 * 16]; /* some random data */
- static const char *help="usage: ./program wav_file.wav\n";
- int main(int argc, char *argv[])
- {
- int err;
- unsigned int i;
- snd_pcm_t *handle;
- snd_pcm_sframes_t frames;
- int rret;
- static snd_output_t *output = NULL;
- FILE *file;
- if (argc < 2) {
- printf("%s", help);
- return -1;
- }
- 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,
- 2,
- 44100,
- 1,
- 500000)) < 0) { /* 0.5sec */
- printf("Playback open error: %s\n", snd_strerror(err));
- exit(EXIT_FAILURE);
- }
- if ( (file = fopen(argv[1], "rb")) == NULL) {
- printf("Can not open the wav file !");
- return -1;
- }
- err = snd_output_stdio_attach(&output, stdout, 0);
- if (err < 0) {
- printf("Output failed: %s\n", snd_strerror(err));
- return 0;
- }
- snd_pcm_dump(handle, output);
- while ( (rret = fread(buffer, 1024 * 2, 4, file))) {
- printf("rret = %d\n", rret);
- frames = snd_pcm_writei(handle, buffer, 1024 * 2);
- printf("snd_pcm_writei:frames: %d\n", frames);
- 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);
- // sleep needed to see that the frame is repeated.
- usleep(5000000);
- }
- snd_pcm_close(handle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement