sfprocess.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. ** Copyright (C) 2001-2013 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. /* Include this header file to use functions from libsndfile. */
  35. #include <sndfile.h>
  36. /* This will be the length of the buffer used to hold.frames while
  37. ** we process them.
  38. */
  39. #define BUFFER_LEN 1024
  40. /* libsndfile can handle more than 6 channels but we'll restrict it to 6. */
  41. #define MAX_CHANNELS 6
  42. /* Function prototype. */
  43. static void process_data (double *data, int count, int channels) ;
  44. int
  45. main (void)
  46. { /* This is a buffer of double precision floating point values
  47. ** which will hold our data while we process it.
  48. */
  49. static double data [BUFFER_LEN] ;
  50. /* A SNDFILE is very much like a FILE in the Standard C library. The
  51. ** sf_open function return an SNDFILE* pointer when they sucessfully
  52. ** open the specified file.
  53. */
  54. SNDFILE *infile, *outfile ;
  55. /* A pointer to an SF_INFO struct is passed to sf_open.
  56. ** On read, the library fills this struct with information about the file.
  57. ** On write, the struct must be filled in before calling sf_open.
  58. */
  59. SF_INFO sfinfo ;
  60. int readcount ;
  61. const char *infilename = "input.wav" ;
  62. const char *outfilename = "output.wav" ;
  63. /* The SF_INFO struct must be initialized before using it.
  64. */
  65. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  66. /* Here's where we open the input file. We pass sf_open the file name and
  67. ** a pointer to an SF_INFO struct.
  68. ** On successful open, sf_open returns a SNDFILE* pointer which is used
  69. ** for all subsequent operations on that file.
  70. ** If an error occurs during sf_open, the function returns a NULL pointer.
  71. **
  72. ** If you are trying to open a raw headerless file you will need to set the
  73. ** format and channels fields of sfinfo before calling sf_open(). For
  74. ** instance to open a raw 16 bit stereo PCM file you would need the following
  75. ** two lines:
  76. **
  77. ** sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
  78. ** sfinfo.channels = 2 ;
  79. */
  80. if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
  81. { /* Open failed so print an error message. */
  82. printf ("Not able to open input file %s.\n", infilename) ;
  83. /* Print the error message from libsndfile. */
  84. puts (sf_strerror (NULL)) ;
  85. return 1 ;
  86. } ;
  87. if (sfinfo.channels > MAX_CHANNELS)
  88. { printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ;
  89. sf_close (infile) ;
  90. return 1 ;
  91. } ;
  92. /* Open the output file. */
  93. if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
  94. { printf ("Not able to open output file %s.\n", outfilename) ;
  95. puts (sf_strerror (NULL)) ;
  96. sf_close (infile) ;
  97. return 1 ;
  98. } ;
  99. /* While there are.frames in the input file, read them, process
  100. ** them and write them to the output file.
  101. */
  102. while ((readcount = (int) sf_read_double (infile, data, BUFFER_LEN)))
  103. { process_data (data, readcount, sfinfo.channels) ;
  104. sf_write_double (outfile, data, readcount) ;
  105. } ;
  106. /* Close input and output files. */
  107. sf_close (infile) ;
  108. sf_close (outfile) ;
  109. return 0 ;
  110. } /* main */
  111. static void
  112. process_data (double *data, int count, int channels)
  113. { double channel_gain [MAX_CHANNELS] = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 } ;
  114. int k, chan ;
  115. /* Process the data here.
  116. ** If the soundfile contains more then 1 channel you need to take care of
  117. ** the data interleaving youself.
  118. ** Current we just apply a channel dependant gain.
  119. */
  120. for (chan = 0 ; chan < channels ; chan ++)
  121. for (k = chan ; k < count ; k+= channels)
  122. data [k] *= channel_gain [chan] ;
  123. return ;
  124. } /* process_data */