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 39 40 using System; 41 using System.Runtime.InteropServices; 42 using sf_count_t = System.Int64; //alias; see SF_INFO struct 43 44 #if PLATFORM_64 45 using size_t = System.UInt64; 46 #else 47 using size_t = System.UInt32; 48 #endif 49 50 51 class lsndf_example { 52 53 54 //sound file formats 55 public enum lsndf_frmts { 56 SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */ 57 SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */ 58 SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */ 59 SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */ 60 SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */ 61 SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */ 62 SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */ 63 SF_FORMAT_VOC = 0x080000, /* VOC files. */ 64 SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */ 65 SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */ 66 SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */ 67 SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */ 68 SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */ 69 SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */ 70 SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */ 71 SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */ 72 73 /* Subtypes from here on. */ 74 75 SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */ 76 SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */ 77 SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */ 78 SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */ 79 80 SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */ 81 82 SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */ 83 SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */ 84 85 SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */ 86 SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */ 87 SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */ 88 SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */ 89 90 SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */ 91 SF_FORMAT_VOX_ADPCM = 0x0021, /* OKI / Dialogix ADPCM */ 92 93 SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */ 94 SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */ 95 SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */ 96 97 SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */ 98 SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */ 99 SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */ 100 SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */ 101 102 SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */ 103 SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */ 104 105 106 /* Endian-ness options. */ 107 108 SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */ 109 SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */ 110 SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */ 111 SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */ 112 113 SF_FORMAT_SUBMASK = 0x0000FFFF, 114 SF_FORMAT_TYPEMASK = 0x0FFF0000, 115 SF_FORMAT_ENDMASK = 0x30000000 116 } 117 118 119 //modes and other 120 public enum lsndf_tf 121 { /* True and false */ 122 SF_FALSE = 0, 123 SF_TRUE = 1, 124 125 /* Modes for opening files. */ 126 SFM_READ = 0x10, 127 SFM_WRITE = 0x20, 128 SFM_RDWR = 0x30 129 } 130 131 132 //important SF_INFO structure 133 [StructLayout(LayoutKind.Sequential)] 134 public struct SF_INFO 135 { 136 public sf_count_t frames ; // Used to be called samples. Changed to avoid confusion. 137 public int samplerate ; 138 public int channels ; 139 public int format ; 140 public int sections ; 141 public int seekable ; 142 }; 143 144 145 //function declarations 146 //Note: Not all functions have been prototyped here. Only the ones necessary to 147 // make this application work. The below code should give some clues as to 148 // how to add the rest since they have a lot of parameter and return type 149 // similarities. 150 [DllImport("libsndfile.dll")] sf_open([MarshalAs(UnmanagedType.LPStr)] string path, int mode, ref SF_INFO sfinfo)151 public static extern IntPtr sf_open ([MarshalAs(UnmanagedType.LPStr)] string path, int mode, ref SF_INFO sfinfo); 152 153 [DllImport("libsndfile.dll")] sf_error(IntPtr sndfile)154 static extern int sf_error (IntPtr sndfile); 155 156 [DllImport("libsndfile.dll")] sf_strerror(IntPtr sndfile)157 static extern IntPtr sf_strerror (IntPtr sndfile); 158 159 [DllImport("libsndfile.dll")] sf_format_check(ref SF_INFO info)160 static extern int sf_format_check (ref SF_INFO info); 161 162 [DllImport("libsndfile.dll")] sf_read_float(IntPtr sndfile, float[] ptr, sf_count_t items)163 static extern sf_count_t sf_read_float (IntPtr sndfile, float[] ptr, sf_count_t items); 164 165 [DllImport("libsndfile.dll")] sf_write_float(IntPtr sndfile, float[] ptr, sf_count_t items)166 static extern sf_count_t sf_write_float (IntPtr sndfile, float[] ptr, sf_count_t items); 167 168 [DllImport("libsndfile.dll")] sf_close(IntPtr sndfile)169 static extern int sf_close (IntPtr sndfile); 170 171 172 public const sf_count_t BUFFER_LEN = 4096; 173 174 175 //program entry Main( )176 static void Main( ) { 177 178 179 //declarations 180 SF_INFO sfinfo = new SF_INFO(); 181 float[] buffer = new float[BUFFER_LEN]; 182 sf_count_t rcnt; 183 184 //set the input file 185 string sfn = "input.wav"; //set to a file on YOUR system 186 //string sfn = "noexist.wav"; //test with non-existent file 187 188 //set the output file 189 string ofn = "output.wav"; 190 191 //read in sound file to convert 192 IntPtr infile = sf_open (sfn, (int)lsndf_tf.SFM_READ, ref sfinfo); 193 194 //exit if error was thrown 195 if ( (int)infile == 0 ) { 196 Console.WriteLine("Error opening " + sfn); 197 Console.WriteLine("Error #" + sf_error(infile)); 198 return; 199 } 200 201 //set the file type for the output file 202 //uncomment one and only one of the statements below to change the output 203 //file encoding. 204 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_U8); 205 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_16); 206 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_MS_ADPCM); 207 sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_IMA_ADPCM); 208 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_GSM610); 209 /* Soundforge W64. */ 210 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_U8); 211 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_16); 212 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_MS_ADPCM); 213 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_IMA_ADPCM); 214 //sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_GSM610); 215 216 217 //check that SF_INFO is valid 218 if ( sf_format_check(ref sfinfo) == 0 ) { 219 Console.WriteLine("sf_format_check failed. Invalid encoding"); 220 return; 221 } 222 223 //open output file 224 IntPtr outfile = sf_open (ofn, (int)lsndf_tf.SFM_WRITE, ref sfinfo); 225 226 //exit if error was thrown 227 if ( (int)outfile == 0 ) { 228 Console.WriteLine("Error opening " + ofn); 229 Console.WriteLine("Error #" + sf_error(outfile)); 230 return; 231 } 232 233 //infile -> outfile 234 Console.Write(sfn + " -> " + ofn); 235 while ( (rcnt = sf_read_float (infile, buffer, BUFFER_LEN)) > 0) { 236 Console.Write("."); 237 sf_write_float (outfile, buffer, BUFFER_LEN); 238 } 239 Console.WriteLine("done."); 240 241 //close up shop 242 sf_close(infile); 243 sf_close(outfile); 244 245 246 } //main() 247 248 249 } //class lsndf_example {} 250 251