MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
StandardQueryWAVFile.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace MyCaffe.db.stream
10{
15 {
16 string m_strPath;
17 string[] m_rgstrFiles;
18 int m_nFileIdx = 0;
19 Dictionary<string, float> m_rgInfo = new Dictionary<string, float>();
20
25 public StandardQueryWAVFile(string strParam = null)
26 {
27 if (strParam != null)
28 {
29 strParam = ParamPacker.UnPack(strParam);
30 PropertySet ps = new PropertySet(strParam);
31 m_strPath = ps.GetProperty("FilePath");
32 }
33 }
34
39 {
40 get { return CUSTOM_QUERY_TYPE.REAL_DOUBLE; }
41 }
42
46 public string Name
47 {
48 get { return "StdWAVFileQuery"; }
49 }
50
54 public int FieldCount
55 {
56 get { return 1; } // data
57 }
58
64 public IXCustomQuery Clone(string strParam)
65 {
66 return new StandardQueryWAVFile(strParam);
67 }
68
72 public void Close()
73 {
74 m_rgstrFiles = null;
75 m_nFileIdx = 0;
76 }
77
81 public void Open()
82 {
83 string[] rgstrFiles = Directory.GetFiles(m_strPath);
84 m_nFileIdx = 0;
85
86 List<string> rgstr = new List<string>();
87
88 for (int i = 0; i < rgstrFiles.Length; i++)
89 {
90 FileInfo fi = new FileInfo(rgstrFiles[i]);
91
92 if (fi.Extension.ToLower() == ".wav")
93 rgstr.Add(rgstrFiles[i]);
94 }
95
96 m_rgstrFiles = rgstr.ToArray();
97
98 if (m_rgstrFiles.Length == 0)
99 throw new Exception("The CustomTextQuery could not find any audio WAV (*.wav) files to load.");
100 }
101
109 public double[] QueryByTime(DateTime dt, TimeSpan ts, int nCount)
110 {
111 throw new NotImplementedException();
112 }
113
118 public byte[] QueryBytes()
119 {
120 throw new NotImplementedException();
121 }
122
127 public List<double[]> QueryRealD()
128 {
129 if (m_nFileIdx == m_rgstrFiles.Length)
130 return null;
131
132 using (FileStream fs = File.OpenRead(m_rgstrFiles[m_nFileIdx]))
133 using (WAVReader wav = new WAVReader(fs))
134 {
135 m_nFileIdx++;
136 wav.ReadToEnd();
137
138 m_rgInfo = new Dictionary<string, float>();
139 m_rgInfo.Add("AveBytesPerSec", wav.Format.nAvgBytesPerSec);
140 m_rgInfo.Add("BlockAlign", wav.Format.nBlockAlign);
141 m_rgInfo.Add("Channels", wav.Format.nChannels);
142 m_rgInfo.Add("SamplesPerSec", wav.Format.nSamplesPerSec);
143 m_rgInfo.Add("BitsPerSample", wav.Format.wBitsPerSample);
144 m_rgInfo.Add("FormatTag", wav.Format.wFormatTag);
145
146 List<double[]> rgData = new List<double[]>();
147
148 for (int i = 0; i < wav.Samples[0].Length; i++)
149 {
150 rgData.Add(new double[] { wav.Samples[0][i], wav.Samples[1][i] });
151 }
152
153 return rgData;
154 }
155 }
156
161 public List<float[]> QueryRealF()
162 {
163 throw new NotImplementedException();
164 }
165
170 public Dictionary<string, float> QueryInfo()
171 {
172 if (m_rgInfo != null && m_rgInfo.Count > 0)
173 return m_rgInfo;
174
175 if (m_nFileIdx == m_rgstrFiles.Length)
176 throw new Exception("Invalid field index.");
177
178 using (FileStream fs = File.OpenRead(m_rgstrFiles[m_nFileIdx]))
179 using (WAVReader wav = new WAVReader(fs))
180 {
181 wav.ReadToEnd(true);
182
183 m_rgInfo = new Dictionary<string, float>();
184 m_rgInfo.Add("AveBytesPerSec", wav.Format.nAvgBytesPerSec);
185 m_rgInfo.Add("BlockAlign", wav.Format.nBlockAlign);
186 m_rgInfo.Add("Channels", wav.Format.nChannels);
187 m_rgInfo.Add("SamplesPerSec", wav.Format.nSamplesPerSec);
188 m_rgInfo.Add("BitsPerSample", wav.Format.wBitsPerSample);
189 m_rgInfo.Add("FormatTag", wav.Format.wFormatTag);
190
191 return m_rgInfo;
192 }
193 }
194
201 public static byte[] PackBytes(WaveFormat fmt, List<float[]> rgSamples)
202 {
203 using (MemoryStream ms = new MemoryStream())
204 using (BinaryWriter bw = new BinaryWriter(ms))
205 {
206 byte[] rgHeader = WAVWriter.StructureToByteArray<WaveFormat>(fmt);
207 List<float[]> rgData = rgSamples;
208
209 bw.Write(rgHeader.Length);
210 bw.Write(rgHeader);
211 bw.Write(rgData.Count);
212 bw.Write(rgData[0].Length);
213
214 for (int i = 0; i < rgData[0].Length; i++)
215 {
216 for (int j = 0; j < rgData.Count; j++)
217 {
218 bw.Write(rgData[j][i]);
219 }
220 }
221
222 return ms.ToArray();
223 }
224 }
225
232 public static List<double[]> UnPackBytes(byte[] rg, out WaveFormat fmt)
233 {
234 using (MemoryStream ms = new MemoryStream(rg))
235 using (BinaryReader br = new BinaryReader(ms))
236 {
237 int nLen = br.ReadInt32();
238 byte[] rgFmt = br.ReadBytes(nLen);
239 fmt = WAVWriter.ByteArrayToStructure<WaveFormat>(rgFmt);
240 int nCh = br.ReadInt32();
241 int nS = br.ReadInt32();
242
243 List<double[]> rgData = new List<double[]>();
244 for (int i = 0; i < nCh; i++)
245 {
246 rgData.Add(new double[nS]);
247 }
248
249 for (int i = 0; i < nS; i++)
250 {
251 for (int j = 0; j < nCh; j++)
252 {
253 float fVal = br.ReadSingle();
254 rgData[j][i] = fVal;
255 }
256 }
257
258 return rgData;
259 }
260 }
261
266 public List<int> GetQuerySize()
267 {
268 List<int> rgSize = new List<int>();
269
270 using (FileStream fs = File.OpenRead(m_rgstrFiles[m_nFileIdx]))
271 using (WAVReader wav = new WAVReader(fs))
272 {
273 wav.ReadToEnd(true);
274 rgSize.Add(1);
275 rgSize.Add((int)wav.Format.nChannels);
276 rgSize.Add(wav.SampleCount);
277
278 return rgSize;
279 }
280 }
281
285 public void Reset()
286 {
287 m_nFileIdx = 0;
288 }
289
296 public byte[] ConvertOutput(float[] rg, out string strType)
297 {
298 strType = "WAV";
299 Dictionary<string, float> rgInfo = QueryInfo();
300 WaveFormat fmt = new WaveFormat();
301
302 fmt.nAvgBytesPerSec = (uint)rgInfo["AveBytesPerSec"];
303 fmt.nBlockAlign = (ushort)rgInfo["BlockAlign"];
304 fmt.nChannels = (ushort)rgInfo["Channels"];
305 fmt.nSamplesPerSec = (uint)rgInfo["SamplesPerSec"];
306 fmt.wBitsPerSample = (ushort)rgInfo["BitsPerSample"];
307 fmt.wFormatTag = (ushort)rgInfo["FormatTag"];
308
309 List<float[]> rgrgSamples = new List<float[]>();
310 for (int i = 0; i < fmt.nChannels; i++)
311 {
312 rgrgSamples.Add(rg);
313 }
314
315 return PackBytes(fmt, rgrgSamples);
316 }
317 }
318}
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
string GetProperty(string strName, bool bThrowExceptions=true)
Returns a property as a string value.
Definition: PropertySet.cs:146
The ParamPacker is use to pack and unpack parameters sent to each custom query.
Definition: Interfaces.cs:246
static string UnPack(string str)
Unpack the custom query parameters.
Definition: Interfaces.cs:263
The StandardQueryWAVFile provides queries that read sound frequencies from (*.WAV) files residing in ...
double[] QueryByTime(DateTime dt, TimeSpan ts, int nCount)
The QueryByTime method is not implemented.
void Reset()
Reset the file index to the first file.
byte[] ConvertOutput(float[] rg, out string strType)
Converts the output values into the native type used by the CustomQuery.
List< float[]> QueryRealF()
The QueryRealF method returns all samples of the next file in the directory.
static byte[] PackBytes(WaveFormat fmt, List< float[]> rgSamples)
The PackBytes method packs the wav file information into a byte stream.
IXCustomQuery Clone(string strParam)
Clone the custom query returning a new copy.
CUSTOM_QUERY_TYPE QueryType
Returns the QUERY_TYPE of REAL_FLOAT.
void Close()
Close the custom query.
Dictionary< string, float > QueryInfo()
The Query information returns information about the data queried such as header information.
string Name
Returns the custom query name 'StdWAVFileQuery'.
StandardQueryWAVFile(string strParam=null)
The constructor.
List< int > GetQuerySize()
The GetQuerySize method returns the size of the query as {1,1,filesize}.
List< double[]> QueryRealD()
The QueryRealD method returns all samples of the next file in the directory.
byte[] QueryBytes()
The QueryBytes method is not implemented.
int FieldCount
Returns the field count of 1.
static List< double[]> UnPackBytes(byte[] rg, out WaveFormat fmt)
The UnPackBytes method is used to unpack a byte stream into the Wav information.
void Open()
Open the custom query. The query must be opened before calling QueryBytes.
The WAVReader is an extension of the BinaryReader and is used to read WAV files.
Definition: WAVReader.cs:105
The WAVWriter is a special BinaryWriter used to write WAV files.
Definition: WAVWriter.cs:19
The custom query interface defines the functions implemented by each Custom Query object used to spec...
Definition: Interfaces.cs:168
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.stream namespace contains all data streaming related classes.
CUSTOM_QUERY_TYPE
Defines the custom query type to use.
Definition: Interfaces.cs:134
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12
The WaveFormat structure describes the header information of a WAV file.
Definition: WAVReader.cs:74
ushort nBlockAlign
Specifies the block alignment (Channels * BitsPerSample / 8)
Definition: WAVReader.cs:94
uint nAvgBytesPerSec
Specifies the average byte rate per second (nSamplesPerSec * Channels * BitsPerSample / 8)
Definition: WAVReader.cs:90
uint nSamplesPerSec
Specifies the sample rate (e.g. 8000, 44100, etc.)
Definition: WAVReader.cs:86
ushort nChannels
Specifies the number of channels in the data where Mono = 1 and Stero = 2.
Definition: WAVReader.cs:82
ushort wBitsPerSample
Specifies the number of bits per sample (8, 16, 32, etc.)
Definition: WAVReader.cs:98
ushort wFormatTag
Specifies the AudioFormat where PCM = 1 for Linear quantization.
Definition: WAVReader.cs:78