Guest User

Untitled

a guest
Feb 26th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #ifdef HAVE_CONFIG_H
  2. #include <config.h>
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10.  
  11. #include <pulse/simple.h>
  12. #include <pulse/error.h>
  13. #include <pulse/gccmacro.h>
  14. #include <sndfile.h>
  15.  
  16. #define BUFSIZE 1024
  17.  
  18.  
  19. int main(int argc, char*argv[]) {
  20.  
  21. /* The Sample format to use */
  22. static const pa_sample_spec ss = {
  23. .format = PA_SAMPLE_S16LE,
  24. // .rate = 44100,
  25. .rate = 44100,
  26. .channels = 2
  27. };
  28. pa_simple *s = NULL;
  29. int ret = 1;
  30. int error;
  31.  
  32. if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) {
  33. fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
  34. goto finish;
  35. }
  36. SF_INFO sinfo;
  37. sinfo.format = 0; //prepare
  38.  
  39. SNDFILE* sf = sf_open( argv[1], SFM_READ, &sinfo );
  40. uint8_t buf[BUFSIZE];
  41. ssize_t r;
  42. int i;
  43.  
  44. for (;;) {
  45.  
  46. /* Read some data ... */
  47. if ((r = sf_read_raw(sf, (void*)buf, BUFSIZE)) <= 0){
  48. if (r == 0) /* EOF */
  49. break;
  50.  
  51. fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
  52. goto finish;
  53. }
  54.  
  55. /* ... and play it */
  56. if (pa_simple_write(s, buf, (size_t) r, &error) < 0) {
  57. fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
  58. goto finish;
  59. }
  60. /* for(i=0;i<r;i++){ */
  61. /* fprintf(stdout, "%d,", buf[i]); */
  62. /* } */
  63. }
  64.  
  65. /* Make sure that every single sample was played */
  66. if (pa_simple_drain(s, &error) < 0) {
  67. fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
  68. goto finish;
  69. }
  70.  
  71. ret = 0;
  72.  
  73. finish:
  74.  
  75. if (s)
  76. pa_simple_free(s);
  77.  
  78. return ret;
  79. }
Add Comment
Please, Sign In to add comment