Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /**
  2. * Trying to set some parameters for ALSA.
  3. *
  4. * Code from: http://equalarea.com/paul/alsa-audio.html
  5. *
  6. */
  7.  
  8. #include <alsa/asoundlib.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <math.h>
  16.  
  17. #define PCM_DEVICE "default"
  18.  
  19. int main(int argc, char **argv)
  20. {
  21. unsigned int pcm;
  22. snd_pcm_t *pcm_handle;
  23. snd_pcm_hw_params_t *params;
  24.  
  25. char test[5];
  26.  
  27. if(pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
  28. printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
  29.  
  30. return 1;
  31. }
  32.  
  33. snd_pcm_hw_params_alloca(&params);
  34.  
  35. snd_pcm_hw_params_any(pcm_handle, params);
  36.  
  37. if(pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
  38. printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
  39.  
  40. return 1;
  41. }
  42.  
  43. if(pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_FLOAT) < 0) {
  44. printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
  45.  
  46. return 1;
  47. }
  48.  
  49. int channels = 2;
  50. if(pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0) {
  51. printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
  52.  
  53. return 1;
  54. }
  55.  
  56. int rate = 44100;
  57. if(pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0) {
  58. printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
  59.  
  60. return 1;
  61. }
  62.  
  63. printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
  64. printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
  65.  
  66. int ch;
  67. snd_pcm_hw_params_get_channels(params, &ch);
  68.  
  69. if(ch > 1) {
  70. printf("Channels: %i, stereo\n", ch);
  71. }
  72. else if(ch == 1) {
  73. printf("Channels: %i, mono\n", ch);
  74. }
  75.  
  76. snd_pcm_hw_params_get_rate(params, &ch, 0);
  77. printf("Rate: %d bps\n", ch);
  78.  
  79. int dir;
  80. snd_pcm_uframes_t frames = 1024;
  81.  
  82. printf("Attempting to set frames to %d\n", frames);
  83.  
  84. snd_pcm_hw_params_set_period_size_near(pcm_handle, params, &frames, &dir);
  85.  
  86. snd_pcm_hw_params_get_period_size(params, &frames, 0);
  87.  
  88. printf("Frames is now: %zd\n", frames);
  89.  
  90. snd_pcm_hw_params_get_period_time(params, &ch, NULL);
  91.  
  92. int buffer_size = 4096;
  93.  
  94. printf("Attempting to set the buffer size to: %d\n", buffer_size);
  95.  
  96. snd_pcm_hw_params_set_buffer_size(pcm_handle, params, buffer_size);
  97.  
  98. if(pcm = snd_pcm_hw_params(pcm_handle, params) < 0) {
  99. printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
  100.  
  101. return 1;
  102. }
  103.  
  104. snd_pcm_uframes_t temp;
  105.  
  106. snd_pcm_hw_params_get_buffer_size(params, &temp);
  107.  
  108. printf("Buffer size is now: %d\n", temp);
  109.  
  110. snd_pcm_drain(pcm_handle);
  111. snd_pcm_close(pcm_handle);
  112.  
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement