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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
//SlimDX
using SlimDX.DirectSound;
using SlimDX.Multimedia;
namespace gdrm.Recorder
{
public class SoundRecord
{
//**********************************
//********** Proprieté *************
//**********************************
private string fileName;
private List<byte[]> rawSoundData;
private bool record = false;
private DirectSoundCapture soundCapture;
private WaveFormat waveFormat;
private CaptureBufferDescription bufferDescription;
private CaptureBuffer buffer;
private FileStream fileStream;
//**********************************
//********** Méthodes **************
//**********************************
// --- Constructeur ---
public SoundRecord(string fileName, DirectSoundCapture soundCapture)
{
this.fileName = fileName;
this.waveFormat = new WaveFormat();
this.waveFormat.FormatTag = WaveFormatTag.Pcm;
this.waveFormat.BitsPerSample = 16;
this.waveFormat.BlockAlignment = (short)(waveFormat.BitsPerSample / 8);
this.waveFormat.Channels = 1;
this.waveFormat.SamplesPerSecond = 44100;
this.waveFormat.AverageBytesPerSecond = waveFormat.SamplesPerSecond * waveFormat.BlockAlignment * waveFormat.Channels;
this.bufferDescription = new CaptureBufferDescription();
this.bufferDescription.BufferBytes = this.waveFormat.AverageBytesPerSecond;
this.bufferDescription.ControlEffects = false;
this.bufferDescription.Format = this.waveFormat;
this.bufferDescription.WaveMapped = true;
this.rawSoundData = new List<byte[]>();
this.soundCapture = soundCapture;
this.buffer = new CaptureBuffer(this.soundCapture, this.bufferDescription);
}
//--- Start Sound Record ---
public void StartSoundRecord()
{
this.record = true;
this.buffer.Start(true);
while (this.record)
{
this.rawSoundData.Add(BitConverter.GetBytes(this.buffer.CurrentCapturePosition));
}
}
//--- Stop sound Record ---
public string StopSoundRecord()
{
this.record = false;
if (!this.record)
{
this.buffer.Stop();
this.buffer.Dispose();
List<byte> data = new List<byte>();
for (int a = 0; a < rawSoundData.Count; a++)
{
for (int b = 0; b < rawSoundData[a].Length; b++)
{
data.Add(rawSoundData[a].ElementAt<byte>(b));
}
}
this.rawSoundData.Clear();
if (this.WriteSoundToWaveFile(data.ToArray()))
{
return "ok";
}
else
{
return "Nook";
}
}
return "Nook";
}
//--- Enregistrement du son ---
private bool WriteSoundToWaveFile(byte[] data)
{
int dataLength = data.Length;
byte[] header = new byte[44];
MemoryStream ms = new MemoryStream(header, 0, 44, true);
UTF8Encoding utf8 = new UTF8Encoding();
ms.Write(utf8.GetBytes("RIFF"), 0, 4);
ms.Write(BitConverter.GetBytes(dataLength + 36), 0, 4);
ms.Write(utf8.GetBytes("WAVE"), 0, 4);
ms.Write(utf8.GetBytes("fmt "), 0, 4);
ms.Write(BitConverter.GetBytes(16), 0, 4);
ms.Write(BitConverter.GetBytes(1), 0, 2);
ms.Write(BitConverter.GetBytes(this.waveFormat.Channels), 0, 2);
ms.Write(BitConverter.GetBytes(this.waveFormat.SamplesPerSecond), 0, 4);
ms.Write(BitConverter.GetBytes(this.waveFormat.AverageBytesPerSecond), 0, 4);
ms.Write(BitConverter.GetBytes(this.waveFormat.BlockAlignment), 0, 2);
ms.Write(BitConverter.GetBytes(this.waveFormat.BitsPerSample), 0, 2);
ms.Write(utf8.GetBytes("data"), 0, 4);
ms.Write(BitConverter.GetBytes(dataLength), 0, 4);
try
{
this.fileStream = new FileStream(this.fileName, FileMode.Create);
this.fileStream.Write(header, 0, header.Length);
this.fileStream.Write(data, 0, data.Length);
this.fileStream.Close();
this.fileStream.Dispose();
ms.Close();
ms.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
}
} |