2using System.Collections.Generic;
5using System.Runtime.InteropServices;
7using System.Threading.Tasks;
22 Dictionary<string, List<string>> m_rgInfo =
new Dictionary<string, List<string>>();
24 List<double[]> m_rgrgSamples;
25 int m_nSampleStep = 1;
41 get {
return m_format; }
42 set { m_format = value; }
50 get {
return m_rgrgSamples; }
51 set { m_rgrgSamples = value; }
61 if (m_rgrgSamples ==
null)
62 throw new Exception(
"You must set the audio data first!");
65 throw new Exception(
"You must set the format first!");
67 if (nNewSampleRate > 0)
77 if (!writeAudioContent())
80 int nSize = (int)(m_stream.Position - m_lSizePos) - 4;
81 m_stream.Seek(m_lSizePos, SeekOrigin.Begin);
87 private bool writeAudioContent()
89 int nSamples = m_rgrgSamples[0].Length;
90 int nChannels = m_rgrgSamples.Count;
92 for (
int s = 0; s < nSamples; s++)
94 for (
int ch = 0; ch < nChannels; ch++)
96 if (s % m_nSampleStep == 0)
98 double dfSample = m_rgrgSamples[ch][s];
104 int v = (int)(dfSample * (
double)0x80000000);
105 byte b1 = (byte)(0x000000FF & v);
106 byte b2 = (byte)((0x0000FF00 & v) >> 8);
107 byte b3 = (byte)((0x00FF0000 & v) >> 16);
108 byte b4 = (byte)((0xFF000000 & v) >> 24);
117 throw new NotImplementedException(
"The bits per sample of " + m_format.
wBitsPerSample.ToString() +
" is not supported.");
126 private bool writeContent()
131 private bool writeRiff()
133 if (!writeID(
"RIFF"))
136 m_lSizePos = m_stream.Position;
140 if (!writeID(
"WAVE"))
143 if (!writeChunk(
"fmt "))
146 if (!writeChunk(
"data"))
152 private bool writeID(
string strID)
154 if (strID.Length != 4)
155 throw new Exception(
"The ID must have a length of 4.");
157 byte b1 = (byte)strID[0];
158 byte b2 = (byte)strID[1];
159 byte b3 = (byte)strID[2];
160 byte b4 = (byte)strID[3];
170 private bool writeChunk(
string strType)
172 if (!writeID(strType))
184 throw new NotImplementedException(
"The format '" + strType +
"' is not supported.");
188 private bool writeFmt()
190 int nStructSize = Marshal.SizeOf(m_format);
191 byte[] rgData = StructureToByteArray<WaveFormat>(m_format);
193 if (nStructSize != rgData.Length)
194 throw new Exception(
"Invalid byte sizing.");
201 private bool writeData()
206 for (
int i = 0; i < m_rgrgSamples.Count; i++)
208 nSize += nByteCount * m_rgrgSamples[i].Length;
224 GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
225 T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
239 int size = Marshal.SizeOf(val);
240 byte[] arr =
new byte[size];
242 IntPtr ptr = Marshal.AllocHGlobal(size);
243 Marshal.StructureToPtr(val, ptr,
true);
244 Marshal.Copy(ptr, arr, 0, size);
245 Marshal.FreeHGlobal(ptr);
The WAVWriter is a special BinaryWriter used to write WAV files.
bool WriteAll(int nNewSampleRate=0)
The WriteAll method writes all WAV file data to the file.
static T ByteArrayToStructure< T >(byte[] bytes)
Converts a byte array into a structure.
WaveFormat Format
Get/set the WaveFormat.
static byte[] StructureToByteArray< T >(T val)
Converts a structure into a byte array.
WAVWriter(Stream stream)
The constructor.
List< double[]> Samples
Get/set the frequency samples.
The MyCaffe.db.stream namespace contains all data streaming related classes.