Guest User

Untitled

a guest
Oct 20th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alsa/asoundlib.h>
  4. #include <stddef.h>
  5. main (int argc, char *argv[])
  6. {
  7. int i;
  8. int err;
  9. char *buffer;
  10. int buffer_frames = 128;
  11. unsigned int rate = 44100;
  12. snd_pcm_t *capture_handle;
  13. snd_pcm_hw_params_t *hw_params;
  14. snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
  15.  
  16. if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  17. fprintf (stderr, "cannot open audio device %s (%s)\n",
  18. argv[1],
  19. snd_strerror (err));
  20. exit (1);
  21. }
  22.  
  23. fprintf(stdout, "audio interface opened\n");
  24.  
  25. if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
  26. fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
  27. snd_strerror (err));
  28. exit (1);
  29. }
  30.  
  31. fprintf(stdout, "hw_params allocated\n");
  32.  
  33. if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
  34. fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
  35. snd_strerror (err));
  36. exit (1);
  37. }
  38.  
  39. fprintf(stdout, "hw_params initialized\n");
  40.  
  41. if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  42. fprintf (stderr, "cannot set access type (%s)\n",
  43. snd_strerror (err));
  44. exit (1);
  45. }
  46.  
  47. fprintf(stdout, "hw_params access setted\n");
  48.  
  49. if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
  50. fprintf (stderr, "cannot set sample format (%s)\n",
  51. snd_strerror (err));
  52. exit (1);
  53. }
  54.  
  55. fprintf(stdout, "hw_params format setted\n");
  56.  
  57. if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
  58. fprintf (stderr, "cannot set sample rate (%s)\n",
  59. snd_strerror (err));
  60. exit (1);
  61. }
  62.  
  63. fprintf(stdout, "hw_params rate setted\n");
  64.  
  65. if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
  66. fprintf (stderr, "cannot set channel count (%s)\n",
  67. snd_strerror (err));
  68. exit (1);
  69. }
  70.  
  71. fprintf(stdout, "hw_params channels setted\n");
  72.  
  73. if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
  74. fprintf (stderr, "cannot set parameters (%s)\n",
  75. snd_strerror (err));
  76. exit (1);
  77. }
  78.  
  79. fprintf(stdout, "hw_params setted\n");
  80.  
  81. snd_pcm_hw_params_free (hw_params);
  82.  
  83. fprintf(stdout, "hw_params freed\n");
  84.  
  85. if ((err = snd_pcm_prepare (capture_handle)) < 0) {
  86. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  87. snd_strerror (err));
  88. exit (1);
  89. }
  90.  
  91. fprintf(stdout, "audio interface prepared\n");
  92.  
  93. buffer = malloc(128 * snd_pcm_format_width(format) / 8 * 2);
  94.  
  95. fprintf(stdout, "buffer allocated\n");
  96.  
  97. for (i = 0; i < 10; ++i) {
  98. if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
  99. fprintf (stderr, "read from audio interface failed (%s)\n",
  100. err, snd_strerror (err));
  101. exit (1);
  102. }
  103. fprintf(stdout, "read %d done\n", i);
  104. }
  105.  
  106. free(buffer);
  107.  
  108. fprintf(stdout, "buffer freed\n");
  109.  
  110. snd_pcm_close (capture_handle);
  111. fprintf(stdout, "audio interface closed\n");
  112.  
  113. exit (0);
  114. }
Add Comment
Please, Sign In to add comment