2using System.Collections.Generic;
5using System.Runtime.InteropServices;
7using System.Threading.Tasks;
21 [StructLayout(LayoutKind.Sequential)]
72 [StructLayout(LayoutKind.Sequential)]
108 Dictionary<string, List<string>> m_rgInfo =
new Dictionary<string, List<string>>();
111 List<double[]> m_rgrgSamples;
127 get {
return m_format; }
135 get {
return m_rgrgSamples; }
143 get {
return m_rgInfo; }
159 if (!readAudioContent())
173 private bool readAudioContent()
175 m_stream.Seek(m_lDataPos, SeekOrigin.Begin);
179 m_rgrgSamples =
new List<double[]>();
180 for (
int i = 0; i < m_format.
nChannels; i++)
182 m_rgrgSamples.Add(
new double[nSamples]);
185 for (
int s = 0; s < nSamples; s++)
187 for (
int ch = 0; ch < m_format.
nChannels; ch++)
198 dfSample = (v / (double)0x80);
206 int v = ((0xFFFF * (b2 >> 7)) << 16) | (b2 << 8) | b1;
207 dfSample = (v / (double)0x8000);
216 int v = ((0xFF * (b3 >> 7)) << 24) | (b3 << 16) | (b2 << 8) | b1;
217 dfSample = (v / (double)0x800000);
227 int v = (b4 << 24) | (b3 << 16) | (b2 << 8) | b1;
228 dfSample = (v / (double)0x80000000);
233 throw new NotImplementedException(
"The bits per sample of " + m_format.
wBitsPerSample.ToString() +
" is not supported.");
236 m_rgrgSamples[ch][s] = (float)dfSample;
243 private bool readContent()
248 private bool readRiff()
250 string strRiff = readID();
251 int nSize = ReadInt32();
252 string strType = readID();
254 if (strRiff !=
"RIFF" || strType !=
"WAVE")
257 bool bEof = readChunk();
266 private string readID()
270 while (c1 < (
int)
' ' || c1 > 127)
272 if (m_stream.Position == m_stream.Length)
275 c1 = (int)m_stream.ReadByte();
282 str += (char)m_stream.ReadByte();
283 str += (char)m_stream.ReadByte();
284 str += (char)m_stream.ReadByte();
289 private bool readChunk()
291 if (m_stream.Position == m_stream.Length)
294 if (m_stream.Length < m_stream.Position)
297 string strID = readID();
301 int nSize = ReadInt32();
302 long lPos = m_stream.Position;
304 if (lPos + nSize > m_stream.Length)
305 nSize = (int)(m_stream.Length - lPos);
314 string strType = readID();
315 if (strType ==
"INFO")
325 m_stream.Seek(lPos + nSize, SeekOrigin.Begin);
332 private void readFmt(
int nSize)
334 int nStructSize = Marshal.SizeOf(m_format);
336 if (nSize >= nStructSize)
338 byte[] rgData = ReadBytes(nStructSize);
339 m_format = ByteArrayToStructure<WaveFormat>(rgData);
343 private void readListInfo(
int nSize)
345 long lPos = m_stream.Position;
347 while (m_stream.Position - lPos < nSize - 4)
349 string strField = readID();
350 if (strField ==
null)
353 int nFieldSize = ReadInt32();
356 byte[] rgData = ReadBytes(nFieldSize);
357 string strVal = Encoding.UTF8.GetString(rgData).Trim();
358 int nIdx = strVal.IndexOf((
char)0);
360 strVal = strVal.Substring(0, nIdx);
362 if (!m_rgInfo.ContainsKey(strField))
363 m_rgInfo.Add(strField,
new List<string>());
365 m_rgInfo[strField].Add(strVal);
378 GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
379 T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
The WAVReader is an extension of the BinaryReader and is used to read WAV files.
static T ByteArrayToStructure< T >(byte[] bytes)
Converts a byte array to a structure.
Dictionary< string, List< string > > ExtraInformation
Returns the WAV file header information in a key=value format.
int SampleCount
Returns the number of samples.
WaveFormat Format
Returns the WAV file header information.
bool ReadToEnd(bool bReadHeaderOnly=false)
Reads the WAV file data.
List< double[]> Samples
Returns the frequency samples of the WAV file.
WAVReader(Stream stream)
The constructor.
The MyCaffe.db.stream namespace contains all data streaming related classes.