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