mar-kim

tiny-tinyalsa-test

Oct 23rd, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. /*
  2.  *  This tiny demo sends random samples to your speakers.
  3.  */
  4. #include <tinyalsa/asoundlib.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #define RATE 44100
  9. struct pcm *dev;
  10. struct pcm_config cfg;
  11. unsigned char buffer[1024]; /* some random data */
  12.  
  13. int main(void)
  14. {
  15.         int err;
  16.         unsigned int i;
  17.         for (i = 0; i < sizeof(buffer); i++) buffer[i] = rand() & 0xff;
  18.  
  19.         // Set config values, drawing info from:
  20.         // pactl list short sinks
  21.         // 0    alsa_output.pci-0000_00_03.0.hdmi-stereo    module-alsa-card.c  s16le 2ch 44100Hz   SUSPENDED
  22.         // 1    alsa_output.pci-0000_00_1b.0.analog-stereo  module-alsa-card.c  s16le 2ch 44100Hz   SUSPENDED
  23.         cfg.channels = 2;
  24.         cfg.rate = RATE;
  25.         cfg.period_size = 4;
  26.         cfg.period_count = RATE; // 1 sec
  27.         cfg.format = PCM_FORMAT_S16_LE;
  28.         cfg.start_threshold = 0;
  29.         cfg.stop_threshold = 0;
  30.         cfg.silence_threshold = 0;
  31.        
  32.         printf("errno %d\n", errno); // 0
  33.         dev = pcm_open(1, 0, PCM_OUT, &cfg);
  34.         printf("errno %d\n", errno); // 22
  35.         err = pcm_start(dev);
  36.         printf("errno %d\n", errno); // 9
  37.         err = pcm_write(dev, &buffer, 1024);
  38.         printf("errno %d\n", errno); // 9
  39.         err = pcm_close(dev);
  40.         printf("errno %d\n", errno); // 9
  41.         return 0;
  42. }
Add Comment
Please, Sign In to add comment