using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX;
using Buffer = Microsoft.DirectX.DirectSound.Buffer;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
namespace ARAMC
{
public partial class ATISRecordPanel : Form
{
string tempFile = "C:\\Program Files\\ARAMC\\Temp.WAV";
private Capture MyCapture = new Capture();
private CaptureBuffer MySndBuf;
/// <summary>
/// Sets a default format capture buffer with no effects; 22KHz 8bit/sample, mono
/// </summary>
public ATISRecordPanel()
{
InitializeComponent();
CaptureBufferDescription MySBufDesc = new CaptureBufferDescription();
//setting the default wave capture format for use
//by the buffer descriptor
WaveFormat DefaultFormat = new WaveFormat();
DefaultFormat.SamplesPerSecond = 22000; //default freq 22khz
DefaultFormat.Channels = 1;
DefaultFormat.BitsPerSample = 8;
DefaultFormat.AverageBytesPerSecond = 22000;
DefaultFormat.BlockAlign = 1;
//setting the buffer descriptor to tell the capture buffer object how the
//buffer should perform
MySBufDesc.Format = DefaultFormat;
MySBufDesc.BufferBytes = 100000;
MySBufDesc.ControlEffects = false;
MySBufDesc.WaveMapped = true;
MySndBuf = new CaptureBuffer(MySBufDesc, MyCapture);
}
/// <summary>
/// Constructor that sets format and buffersize, as well as enables
/// echo cancellation and noise suppression effects
/// </summary>
/// <param name="MyFormat"></param>
/// <param name="bufsize"></param>
public void SoundRec(WaveFormat MyFormat, int bufsize)
{
CaptureBufferDescription MySBufDesc = new CaptureBufferDescription();
//Format has been defined in MainForm
MySBufDesc.Format = MyFormat;
MySBufDesc.BufferBytes = bufsize;
MySBufDesc.ControlEffects = true;
MySBufDesc.WaveMapped = true;
// CaptureAcousticEchoCancellationEffect AECEffect;
//
// MySBufDesc.CaptureEffectDescription = new CaptureEffectDescription[1];
// MySBufDesc.CaptureEffectDescription[0].LocateInSoftware = true;
// MySBufDesc.CaptureEffectDescription[0].GuidEffectsClass = DSoundHelper.CaptureEffectsMsAcousticEchoCancellation;
// MySBufDesc.CaptureEffectDescription[0].GuidEffectsInstance = DSoundHelper.InterfaceCaptureEffectsAcousticEchoCancellation;
// MySBufDesc.CaptureEffectDescription[1].LocateInSoftware = true;
// MySBufDesc.CaptureEffectDescription[1].GuidEffectsClass = DSoundHelper.CaptureEffectsMsNoiseSuppression;
try
{
//Create the CaptureBuffer
MySndBuf = new CaptureBuffer(MySBufDesc,MyCapture);
}
catch (SoundException se)
{
MessageBox.Show( "There is a " + se.ErrorString +
" sound exception", "DirectSound Error");
}
}
/// <summary>
/// Starts the capture from the capture device
/// </summary>
public void StartRecord()
{
MySndBuf.Start(true);
}
/// <summary>
/// Stops the recording of sound.
/// </summary>
public void StopRecord()
{
MySndBuf.Stop();
}
/// <summary>
/// Saves the data in the capture buffer into a wave file
/// </summary>
public void ReadData()
{
int readposition, writeposition;
ArrayList byteArrayList = new ArrayList();
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
//Create a new wav file to store the capture buffer data.
//if already exists will overwrite filename is test.wav
string path =Application.StartupPath + "\\test.wav";
Stream MemStream = new MemoryStream();
MySndBuf.GetCurrentPosition(out writeposition, out readposition);
MySndBuf.Read(0, MemStream, writeposition, LockFlag.None);
Stream MyStream = new FileStream(tempFile, FileMode.Create);
//begin to write the wave file header. for more details
//Search google.com for "wave formats"
//RIFF header
try
{
byteArrayList.AddRange(ascii.GetBytes("RIFF"));
byteArrayList.AddRange( ToBytes(36 + (int)MemStream.Length, 4));
byteArrayList.AddRange(ascii.GetBytes("WAVE"));
//fmt chunk
byteArrayList.AddRange(ascii.GetBytes("fmt "));
//length of fmt chunk (never changes)
byteArrayList.AddRange( ToBytes(16, 4));
//"1" for pcm encoding
byteArrayList.AddRange( ToBytes(1, 2));
byteArrayList.AddRange( ToBytes(MySndBuf.Format.Channels, 2));
byteArrayList.AddRange( ToBytes(MySndBuf.Format.SamplesPerSecond, 4));
byteArrayList.AddRange( ToBytes(MySndBuf.Format.AverageBytesPerSecond, 4));
byteArrayList.AddRange( ToBytes(MySndBuf.Format.BlockAlign, 2));
byteArrayList.AddRange( ToBytes(MySndBuf.Format.BitsPerSample, 2));
//the data chunk
byteArrayList.AddRange(ascii.GetBytes("data"));
byteArrayList.AddRange( ToBytes((int)MemStream.Length, 4));
byte []buffer = new byte[MemStream.Length];
MemStream.Read(buffer, 0, (int)MemStream.Length);
byteArrayList.AddRange(buffer);
buffer = new byte[byteArrayList.Count];
byteArrayList.CopyTo(buffer);
MyStream.Write(buffer, 0, buffer.Length);
}
catch(ArgumentException ae)
{
MessageBox.Show("Argument Exception with Message:\n\t" + ae.Message);
}
finally
{
MemStream.Close();
MyStream.Close();
}
}
/// <summary>
/// returns capture status (boolean)
/// </summary>
/// <returns></returns>
public bool Capturing()
{
return MySndBuf.Capturing;
}
/// <summary>
/// Recursive method that returns a target number in the form
/// of an ArrayList of bytes with designated number of bytes. If not enough
/// bytes to hold the targetnumber, will throw argumentexception.
/// Should be used in a try-catch clause
/// </summary>
/// <param name="targetnumber">the value intended to convert</param>
/// <param name="numofbytes">number of bytes needed</param>
/// <returns></returns>
private ArrayList ToBytes(int targetnumber, short numofbytes)
{
int remainder, result;
ArrayList returningarray;
ArgumentException wrongnumofbytes =
new ArgumentException("Not enough bytes to represent number",
"numofbytes");
result = Math.DivRem(targetnumber, 256, out remainder);
//if not enough bytes specified to represent target number
if (targetnumber >= Math.Pow(256, (double)numofbytes))
{
throw wrongnumofbytes;
}
//if there are higher significant hexadecima, run a recursion
if (result >= 1)
{
returningarray = ToBytes(result, (short)(numofbytes - 1));
returningarray.Insert(0, Convert.ToByte(remainder));
return returningarray;
}
else //if (result < 1) recursion terminating condition
{
returningarray = new ArrayList(numofbytes);
returningarray.Add(Convert.ToByte(targetnumber));
for (int i = 0; i < numofbytes - 1; i++)
{
returningarray.Add(Convert.ToByte(0));//fill up most significant hexadecima with 0's
}
return returningarray;
}
}
public void RecordATIS(string ATIS)
{
textBox1.Text = ATIS;
}
private void RecordButton_CheckedChanged(object sender, EventArgs e)
{
if (RecordButton.Checked == true)
{
StartRecord();
}
else
{
StopRecord();
}
}
private void SaveButton_CheckedChanged(object sender, EventArgs e)
{
SaveButton.Checked = false;
ReadData();
}
private void ListenButton_CheckedChanged(object sender, EventArgs e)
{
if (ListenButton.Checked == true)
{
PlaySound(tempFile, IntPtr.Zero,
SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC);
}
else
{
PlaySound(null, IntPtr.Zero, SoundFlags.SND_PURGE);
}
}
[DllImport("winmm.dll", SetLastError = true,
CallingConvention = CallingConvention.Winapi)]
static extern bool PlaySound(
string pszSound,
IntPtr hMod,
SoundFlags sf);
// Flags for playing sounds. For this example, we are reading
// the sound from a filename, so we need only specify
// SND_FILENAME | SND_ASYNC
[Flags]
public enum SoundFlags : int
{
SND_SYNC = 0x0000, // play synchronously (default)
SND_ASYNC = 0x0001, // play asynchronously
SND_NODEFAULT = 0x0002, // silence (!default) if sound not found
SND_MEMORY = 0x0004, // pszSound points to a memory file
SND_LOOP = 0x0008, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x0010, // don't stop any currently playing sound
SND_NOWAIT = 0x00002000, // don't wait if the driver is busy
SND_ALIAS = 0x00010000, // name is a registry alias
SND_ALIAS_ID = 0x00110000, // alias is a predefined ID
SND_FILENAME = 0x00020000, // name is file name
SND_RESOURCE = 0x00040004, // name is resource name or atom
SND_PURGE = 0x0040,
SND_APPLICATION = 0x0080
}
}
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
by: newyuppiePosted on 2009-08-17 at 08:52:39ID: 25115659
It could be a problem with your DirectX installation. How about you switch method?
m/resource s/4967-How -record-vo ice- from-m icrophone. aspx
http://www.dotnetspider.co