generate.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* (c) 2004 James Robson, http://www.arbingersys.com
  2. **
  3. ** This program is free software; you can redistribute it and/or modify
  4. ** it under the terms of the GNU General Public License as published by
  5. ** the Free Software Foundation; either version 2 of the License, or
  6. ** (at your option) any later version.
  7. **
  8. ** This program is distributed in the hope that it will be useful,
  9. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ** GNU General Public License for more details.
  12. **
  13. ** You should have received a copy of the GNU General Public License
  14. ** along with this program; if not, write to the Free Software
  15. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. **
  17. ** ****************************
  18. **
  19. ** How to use:
  20. ** - libsndfile.dll must have already been compiled and be in this
  21. ** application's search path
  22. **
  23. ** - You must edit this file to point to the file you want to convert. Set
  24. ** the following line of code (found in the Main() function further below)
  25. ** to the name of a .WAV file that exists on your system.
  26. ** 186: string sfn = "input.wav";
  27. **
  28. ** - From a command prompt type
  29. ** csc generate.cs
  30. **
  31. ** - Run the resulting executable 'generate.exe'
  32. **
  33. **
  34. ** Note: You will obviously need the csc compiler and the .NET runtime. I think
  35. ** these are freely available for download from Microsoft's website
  36. ** (part of the .NET SDK?).
  37. */
  38. using System;
  39. using System.Runtime.InteropServices;
  40. using sf_count_t = System.Int64; //alias; see SF_INFO struct
  41. #if PLATFORM_64
  42. using size_t = System.UInt64;
  43. #else
  44. using size_t = System.UInt32;
  45. #endif
  46. class lsndf_example {
  47. //sound file formats
  48. public enum lsndf_frmts {
  49. SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */
  50. SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */
  51. SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */
  52. SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */
  53. SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */
  54. SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */
  55. SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */
  56. SF_FORMAT_VOC = 0x080000, /* VOC files. */
  57. SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */
  58. SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */
  59. SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */
  60. SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */
  61. SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */
  62. SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */
  63. SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */
  64. SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */
  65. /* Subtypes from here on. */
  66. SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */
  67. SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */
  68. SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */
  69. SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */
  70. SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */
  71. SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */
  72. SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */
  73. SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */
  74. SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */
  75. SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */
  76. SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */
  77. SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */
  78. SF_FORMAT_VOX_ADPCM = 0x0021, /* OKI / Dialogix ADPCM */
  79. SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */
  80. SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */
  81. SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */
  82. SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */
  83. SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */
  84. SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */
  85. SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */
  86. SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */
  87. SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */
  88. /* Endian-ness options. */
  89. SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */
  90. SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */
  91. SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */
  92. SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */
  93. SF_FORMAT_SUBMASK = 0x0000FFFF,
  94. SF_FORMAT_TYPEMASK = 0x0FFF0000,
  95. SF_FORMAT_ENDMASK = 0x30000000
  96. }
  97. //modes and other
  98. public enum lsndf_tf
  99. { /* True and false */
  100. SF_FALSE = 0,
  101. SF_TRUE = 1,
  102. /* Modes for opening files. */
  103. SFM_READ = 0x10,
  104. SFM_WRITE = 0x20,
  105. SFM_RDWR = 0x30
  106. }
  107. //important SF_INFO structure
  108. [StructLayout(LayoutKind.Sequential)]
  109. public struct SF_INFO
  110. {
  111. public sf_count_t frames ; // Used to be called samples. Changed to avoid confusion.
  112. public int samplerate ;
  113. public int channels ;
  114. public int format ;
  115. public int sections ;
  116. public int seekable ;
  117. };
  118. //function declarations
  119. //Note: Not all functions have been prototyped here. Only the ones necessary to
  120. // make this application work. The below code should give some clues as to
  121. // how to add the rest since they have a lot of parameter and return type
  122. // similarities.
  123. [DllImport("libsndfile.dll")]
  124. public static extern IntPtr sf_open ([MarshalAs(UnmanagedType.LPStr)] string path, int mode, ref SF_INFO sfinfo);
  125. [DllImport("libsndfile.dll")]
  126. static extern int sf_error (IntPtr sndfile);
  127. [DllImport("libsndfile.dll")]
  128. static extern IntPtr sf_strerror (IntPtr sndfile);
  129. [DllImport("libsndfile.dll")]
  130. static extern int sf_format_check (ref SF_INFO info);
  131. [DllImport("libsndfile.dll")]
  132. static extern sf_count_t sf_read_float (IntPtr sndfile, float[] ptr, sf_count_t items);
  133. [DllImport("libsndfile.dll")]
  134. static extern sf_count_t sf_write_float (IntPtr sndfile, float[] ptr, sf_count_t items);
  135. [DllImport("libsndfile.dll")]
  136. static extern int sf_close (IntPtr sndfile);
  137. public const sf_count_t BUFFER_LEN = 4096;
  138. //program entry
  139. static void Main( ) {
  140. //declarations
  141. SF_INFO sfinfo = new SF_INFO();
  142. float[] buffer = new float[BUFFER_LEN];
  143. sf_count_t rcnt;
  144. //set the input file
  145. string sfn = "input.wav"; //set to a file on YOUR system
  146. //string sfn = "noexist.wav"; //test with non-existent file
  147. //set the output file
  148. string ofn = "output.wav";
  149. //read in sound file to convert
  150. IntPtr infile = sf_open (sfn, (int)lsndf_tf.SFM_READ, ref sfinfo);
  151. //exit if error was thrown
  152. if ( (int)infile == 0 ) {
  153. Console.WriteLine("Error opening " + sfn);
  154. Console.WriteLine("Error #" + sf_error(infile));
  155. return;
  156. }
  157. //set the file type for the output file
  158. //uncomment one and only one of the statements below to change the output
  159. //file encoding.
  160. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_U8);
  161. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_16);
  162. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_MS_ADPCM);
  163. sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_IMA_ADPCM);
  164. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_GSM610);
  165. /* Soundforge W64. */
  166. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_U8);
  167. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_16);
  168. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_MS_ADPCM);
  169. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_IMA_ADPCM);
  170. //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_GSM610);
  171. //check that SF_INFO is valid
  172. if ( sf_format_check(ref sfinfo) == 0 ) {
  173. Console.WriteLine("sf_format_check failed. Invalid encoding");
  174. return;
  175. }
  176. //open output file
  177. IntPtr outfile = sf_open (ofn, (int)lsndf_tf.SFM_WRITE, ref sfinfo);
  178. //exit if error was thrown
  179. if ( (int)outfile == 0 ) {
  180. Console.WriteLine("Error opening " + ofn);
  181. Console.WriteLine("Error #" + sf_error(outfile));
  182. return;
  183. }
  184. //infile -> outfile
  185. Console.Write(sfn + " -> " + ofn);
  186. while ( (rcnt = sf_read_float (infile, buffer, BUFFER_LEN)) > 0) {
  187. Console.Write(".");
  188. sf_write_float (outfile, buffer, BUFFER_LEN);
  189. }
  190. Console.WriteLine("done.");
  191. //close up shop
  192. sf_close(infile);
  193. sf_close(outfile);
  194. } //main()
  195. } //class lsndf_example {}