MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
WAVProcessor.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace MyCaffe.db.stream
10{
14 public class WAVProcessor
15 {
16 WaveFormat m_fmt;
17 List<double[]> m_rgrgSamples;
18 Log m_log;
19 CancelEvent m_evtCancel;
20
28 public WAVProcessor(WaveFormat fmt, List<double[]> rgrgSamples, Log log, CancelEvent evtCancel)
29 {
30 m_fmt = fmt;
31 m_rgrgSamples = rgrgSamples;
32 m_log = log;
33 m_evtCancel = evtCancel;
34 }
35
40 {
41 get { return m_fmt; }
42 }
43
47 public List<double[]> Samples
48 {
49 get { return m_rgrgSamples; }
50 }
51
57 public WAVProcessor DownSample(int nNewSamplesPerSecond)
58 {
59 if (m_fmt.nSamplesPerSec < nNewSamplesPerSecond)
60 throw new Exception("The new sample rate (" + nNewSamplesPerSecond.ToString() + ") must be less than the old sample rate (" + m_fmt.nSamplesPerSec.ToString() + ").");
61
62 if (m_fmt.nSamplesPerSec % nNewSamplesPerSecond != 0)
63 throw new Exception("The new sample rate (" + nNewSamplesPerSecond.ToString() + ") must be a factor of the old sample rate (" + m_fmt.nSamplesPerSec.ToString() + ").");
64
65 int nStep = (int)m_fmt.nSamplesPerSec / nNewSamplesPerSecond;
66 List<double[]> rgrgNewSamples = new List<double[]>();
67 List<double> rgSamples = new List<double>();
68 int nIdx = 0;
69 int nTotal = m_rgrgSamples.Count * m_rgrgSamples[0].Length;
70 Stopwatch sw = new Stopwatch();
71
72 sw.Start();
73
74 for (int i = 0; i < m_rgrgSamples.Count; i++)
75 {
76 for (int j = 0; j < m_rgrgSamples[i].Length; j++)
77 {
78 if (j % nStep == 0)
79 {
80 double fVal = m_rgrgSamples[i][j];
81 rgSamples.Add(fVal);
82
83 if (m_evtCancel.WaitOne(0))
84 return null;
85
86 if (sw.Elapsed.TotalMilliseconds > 1000)
87 {
88 double dfPct = (double)nIdx / (double)nTotal;
89 m_log.WriteLine("Downsampling at " + dfPct.ToString("P") + "...");
90 sw.Restart();
91 }
92 }
93
94 nIdx++;
95 }
96
97 rgrgNewSamples.Add(rgSamples.ToArray());
98 rgSamples = new List<double>();
99 }
100
101 WaveFormat fmt = m_fmt;
102 fmt.nSamplesPerSec = (uint)nNewSamplesPerSecond;
103 fmt.nAvgBytesPerSec = (uint)(nNewSamplesPerSecond * fmt.wBitsPerSample);
104
105 return new WAVProcessor(fmt, rgrgNewSamples, m_log, m_evtCancel);
106 }
107 }
108}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
The WAVProcessor is used to process WAV files and perform tasks such as downsampling.
Definition: WAVProcessor.cs:15
WAVProcessor DownSample(int nNewSamplesPerSecond)
The DownSample method reduces the number of samples per second in the resulting sample set.
Definition: WAVProcessor.cs:57
WaveFormat Format
Returns the WaveFormat.
Definition: WAVProcessor.cs:40
List< double[]> Samples
Returns the WAV frequency samples.
Definition: WAVProcessor.cs:48
WAVProcessor(WaveFormat fmt, List< double[]> rgrgSamples, Log log, CancelEvent evtCancel)
The constructor.
Definition: WAVProcessor.cs:28
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.
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
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 wBitsPerSample
Specifies the number of bits per sample (8, 16, 32, etc.)
Definition: WAVReader.cs:98