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 <alsa/asoundlib.h>
- #include <unistd.h>
- static char *device = "MEDIA_playback_0"; /* playback device */
- static snd_pcm_uframes_t last_size = 20; /* Will be modifed by gdb */
- snd_output_t *output = NULL;
- unsigned char buffer[2*8*1024]; /* some random data */
- unsigned char last_buff[4096*2];
- int main(void)
- {
- int err;
- unsigned int i;
- snd_pcm_t *handle;
- snd_pcm_sframes_t frames;
- static snd_output_t *output = NULL;
- long a, b, c;
- snd_pcm_hw_params_t *hwparams;
- snd_pcm_sw_params_t *swparams;
- err = snd_output_stdio_attach(&output, stdout, 0);
- if (err < 0) {
- printf("Output failed: %s\n", snd_strerror(err));
- return 0;
- }
- for (i = 0; i < sizeof(buffer); i++)
- buffer[i] = random() & 0xff;
- for (i = 0; i < sizeof(last_buff); i++)
- last_buff[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);
- }
- err = snd_pcm_hw_params_malloc(&hwparams);
- if (err < 0)
- goto exit;
- err = snd_pcm_hw_params_any(handle, hwparams);
- if (err < 0)
- goto exit;
- err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
- if (err < 0)
- goto exit;
- err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
- //err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_U8);
- if (err < 0)
- goto exit;
- err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
- if (err < 0)
- goto exit;
- unsigned int rate = 18900;
- err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, 0);
- if (err < 0)
- goto exit;
- snd_pcm_uframes_t size = 1024;
- err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &size, 0);
- if (err < 0)
- goto exit;
- snd_pcm_uframes_t buffer2 = 16384;
- err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer2);
- if (err < 0)
- goto exit;
- err = snd_pcm_hw_params(handle, hwparams);
- if (err < 0)
- goto exit;
- snd_pcm_hw_params_free(hwparams);
- err = snd_pcm_sw_params_malloc(&swparams);
- if (err < 0)
- goto exit;
- err = snd_pcm_sw_params_current(handle, swparams);
- if (err < 0)
- goto exit;
- err = snd_pcm_sw_params(handle, swparams);
- if (err < 0)
- goto exit;
- snd_pcm_sw_params_free(swparams);
- snd_pcm_dump(handle, output);
- // (3) PREPARE
- err = snd_pcm_prepare(handle);
- if (err < 0)
- goto exit;
- a = snd_pcm_avail(handle);
- printf("a: %ld\n", a);
- // (4) Start playback
- b = snd_pcm_avail_update(handle);
- printf("b: %ld\n", b);
- for (i = 0; i < 64; i++) {
- usleep(50000);
- b = snd_pcm_avail_update(handle);
- if( b > 1024 ) {
- frames = snd_pcm_writei(handle, buffer, size);
- if (frames < 0)
- frames = snd_pcm_recover(handle, frames, 0);
- if (frames < 0) {
- printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
- break;
- }
- if (frames > 0 && frames < (long)sizeof(buffer))
- printf("Short write (expected %li, wrote %li), c:%ld\n", (long)sizeof(buffer), frames, (a-b));
- }
- }
- // LAST WRITE - NOT COMPLETE
- printf("frames: %ld, sizeof(buffer): %d\n", (long int)frames, sizeof(last_buff));
- frames = snd_pcm_writei(handle, last_buff, last_size);
- if (frames < 0)
- frames = snd_pcm_recover(handle, frames, 0);
- if (frames < 0) {
- printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
- }
- if (frames > 0 && frames < (long)sizeof(last_buff))
- printf("Short write (expected %li, wrote %li)\n", (long)sizeof(last_buff), frames);
- // check buffer fill level every 100ms
- do {
- b = snd_pcm_avail(handle);
- c = a - b;
- printf("c:%ld\n", c);
- usleep(100000);
- } while(c > 4096);
- snd_pcm_drain(handle);
- snd_pcm_drop(handle);
- usleep(100000);
- snd_pcm_hw_free(handle);
- snd_pcm_close(handle);
- return 0;
- exit:
- printf("Error occured!\n");
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement