RIFF Wave 形式のファイルから音声データを読み出す。
Inheritance: IDisposable
Exemplo n.º 1
0
		static void _Main(string[] args)
		{
			const int N = 1024;
			double[] x = new double[N];

			IFilter filter = new ShelvingEqualizer(Common.Normalize(1000), 1);

			// インパルス応答取得
			filter.Clear();
			/*
			filter.GetValue(1);
			for(int i=0; i<16; ++i)
				filter.GetValue(0);
				*/
			filter.GetValue(1);
			for(int i=1; i<N; ++i)
				x[i] = filter.GetValue(0);

#if TIME_SEQUENCE
			int n = N;
			double[] t = new double[n];
			for(int i=0; i<n; ++i) t[i] = i;

			Graph.GraphForm gf = new Graph.GraphForm();
			gf.Graph.AddEntry(t, x, new Pen(Color.Crimson));
			gf.Graph.SetXAxis(0, n, 5, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Graph.SetYAxis(0, 2, 5, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Size = new Size(640, 480);
			gf.ShowDialog();
#elif PHASE
			Spectrum spec = Spectrum.FromTimeSequence(x, 1);

			int n = spec.Count;
			double[] t = new double[n];
			for(int i=0; i<n; ++i) t[i] = i;

			double[] y = spec.GetPhase();
			Spectrum.Unwrap(y);

			Graph.GraphForm gf = new Graph.GraphForm();
			gf.Graph.AddEntry(t, y, new Pen(Color.Crimson));
			gf.Graph.SetXAxis(0, n, 4, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Graph.SetYAxis(-50, 0, 4, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Size = new Size(640, 480);
			gf.ShowDialog();
#elif GAIN
			Spectrum spec = Spectrum.FromTimeSequence(x, 1);

			int n = spec.Count;
			double[] t = new double[n];
			for(int i=0; i<n; ++i) t[i] = i;

			double[] y = spec.GetAmplitude();

			Graph.GraphForm gf = new Graph.GraphForm();
			gf.Graph.AddEntry(t, y, new Pen(Color.Crimson));
			gf.Graph.SetXAxis(0, n, 4, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Graph.SetYAxis(0, 4, 4, new Font(FontFamily.GenericSerif, 12), new SolidBrush(Color.Black));
			gf.Size = new Size(640, 480);
			gf.ShowDialog();
#endif

#if false
			const int N = 512;
			double[] x = new double[N];
			double[] x0 = new double[N];
			double[] y0 = new double[N];
/*
			Random rnd = new Random();
			x0[0] = 1;
			x0[1] = 1;
			y0[0] = 0;
			y0[1] = 0;
			for(int i=1; i<N/2; ++i)
			{
				x0[2*i]   = rnd.NextDouble();
				x0[2*i+1] = rnd.NextDouble();
				y0[2*i]   = -x0[2*i+1];
				y0[2*i+1] = x0[2*i];
			}
			Fft fft = new Fft(N);
			fft.Invert(x0);
			fft.Invert(y0);
			for(int i=0; i<N; ++i)
				x[i] = x0[i];
//*/
//*
			for(int i=0; i<N; ++i)
			{
				x[i] = 100;
				for(int k=1; k<200; ++k)
					x[i] += short.MaxValue / 4 / k * Math.Cos(Math.PI / 512 * 32 * i * k);
				x0[i] = x[i];
				y0[i] = 0;
				for(int k=1; k<200; ++k)
					y0[i] += short.MaxValue / 4 / k * Math.Sin(Math.PI / 512 * 32 * i * k);
			}
//*/

			double[] y = Spectrum.HilbertTransform(x);

			using(StreamWriter writer = new StreamWriter("z.csv", false, Encoding.Default))
			{
/*
				for(int i=0; i<x.Length; ++i)
					writer.Write("{0},{1},{2},{3},{4}\n", i, x0[i], x[i], y0[i], y[i]);
//*/					
//*
				Spectrum a0 = Spectrum.FromTimeSequence(x0);
				Spectrum a  = Spectrum.FromTimeSequence(x);
				Spectrum b0 = Spectrum.FromTimeSequence(y0);
				Spectrum b  = Spectrum.FromTimeSequence(y);

				for(int i=0; i<a.Count; ++i)
				{
					writer.Write("{0},{1},{2},{3},{4}\n", i, a0[i].Abs, a[i].Abs, b0[i].Abs, b[i].Abs);
//					writer.Write("{0},{1},{2},{3},{4}\n", i, a0[i].Arg, a[i].Arg, b0[i].Arg, b[i].Arg);
				}
//*/
			}

#elif false
			const int N = 1024;

			double[] lp = new double[N];
			IFilter lpf = new LowPassFir(16, Common.Normalize(2000));
			lp[0] = lpf.GetValue(1);
			for(int i=1; i<N; ++i) lp[i] = lpf.GetValue(0);

			double[] bp = new double[N];
			IFilter bpf = new BandPassFir(16, Common.Normalize(2000), Common.Normalize(8000));
			lp[0] = bpf.GetValue(1);
			for(int i=1; i<N; ++i) bp[i] = bpf.GetValue(0);

			double[] hp = new double[N];
			IFilter hpf = new HighPassFir(16, Common.Normalize(8000));
			lp[0] = hpf.GetValue(1);
			for(int i=1; i<N; ++i) hp[i] = hpf.GetValue(0);

			Fft fft = new Fft(N);
			fft.Transform(1, lp);
			fft.Transform(1, bp);
			fft.Transform(1, hp);

			using(StreamWriter writer = new StreamWriter("z.csv", false, Encoding.Default))
			{
				double la = Common.Amp(lp[0]);
				double ba = Common.Amp(bp[0]);
				double ha = Common.Amp(hp[0]);
				writer.Write("{0},{1},{2},{3}\n", 0, la, ba, ha);

				for(int i=1; i<N/2; ++i)
				{
					la = Common.Amp(lp[2*i] * lp[2*i] + lp[2*i+1] * lp[2*i+1]);
					ba = Common.Amp(bp[2*i] * bp[2*i] + bp[2*i+1] * bp[2*i+1]);
					ha = Common.Amp(hp[2*i] * hp[2*i] + hp[2*i+1] * hp[2*i+1]);

					writer.Write("{0},{1},{2},{3}\n", i, la, ba, ha);
				}

				la = Common.Amp(lp[1]);
				ba = Common.Amp(bp[1]);
				ha = Common.Amp(hp[1]);
				writer.Write("{0},{1},{2},{3}\n", N/2, la, ba, ha);
			}
#elif false
			uint length;
			double[] l;
			double[] r;
			FormatHeader header;

			using(WaveReader reader = new WaveReader(@"TD100V00H030.wav"))
			{
				header = reader.Header;
				length = reader.Length;
				int tmp = reader.Read(length, out l, out r);
			}

			IFilter lpf = new LowPassFir(16, Common.Normalize(4500));
			IFilter bpf = new BandPassFir(16, Common.Normalize(4500), Common.Normalize(10000));
			IFilter hpf = new HighPassFir(16, Common.Normalize(10000));

			for(int i=0; i<l.Length; ++i)
			{
				double lp = lpf.GetValue(l[i]);
				double bp = bpf.GetValue(l[i]);
				double hp = hpf.GetValue(l[i]);
				l[i] = lp + bp + hp;
			}
			if(r != null)
				for(int i=0; i<l.Length; ++i)
				{
					double lp = lpf.GetValue(r[i]);
					double bp = bpf.GetValue(r[i]);
					double hp = hpf.GetValue(r[i]);
					r[i] = lp + bp + hp;
				}

			using(WaveWriter writer = new WaveWriter("z.wav", header))
			{
				writer.Write(l, r);
			}

#elif false
			using(StreamWriter writer = new StreamWriter("z.csv", false, Encoding.Default))
			{
				for(int i=0; i<l.Length; ++i)
				{
					writer.Write("{0},{1},{2}\n", i, l[i], r[i]);
				}
			}
#endif
		}
Exemplo n.º 2
0
		}//Read

		/// <summary>
		/// ファイルから Wave データを読み出し。
		/// </summary>
		/// <param name="filename">ファイル名</param>
		/// <param name="length">読み込む長さ</param>
		/// <param name="skip">ファイルの先頭を読み飛ばす長さ</param>
		/// <param name="threshold">閾値。この値以下の音は無音とみなす</param>
		/// <param name="relative">閾値にピーク値からみた相対値を使う</param>
		/// <returns>読み出したデータ</returns>
		static WaveData Read(
			string filename, int skip, int length,
			double threshold ,bool relative)
		{
			WaveReader reader = null;
			FormatHeader header;
			double[] l;
			double[] r;

			using(reader = new WaveReader(filename))
			{
				header = reader.Header;
				reader.Skip(0);
				reader.Read(reader.Length, out l, out r);
			}

			if(relative) threshold *= Math.Max(GetPeekValue(l), GetPeekValue(r));
			skip += Math.Min(GetSilentLength(l, threshold), GetSilentLength(r, threshold));
			if(skip < 0 || skip + length >= l.Length) skip = 0;

			return new WaveTime(header, CopyArray(l, skip, length), CopyArray(r, skip, length));
		}//Read
Exemplo n.º 3
0
		}//Read

		/// <summary>
		/// ファイルから Wave データを読み出し。
		/// </summary>
		/// <param name="filename">ファイル名</param>
		/// <param name="length">読み込む長さ</param>
		/// <param name="skip">ファイルの先頭を読み飛ばす長さ</param>
		/// <param name="threshold">閾値。この値以下の音は無音とみなす</param>
		/// <param name="relative">閾値にピーク値からみた相対値を使う</param>
		/// <param name="type">リファレンスのタイプ</param>
		/// <returns>読み出したデータ</returns>
		static WaveData Read(
			string filename, int skip, int length,
			double threshold ,bool relative, ReferenceType type)
		{
			WaveReader reader = null;
			FormatHeader header;
			double[] l;
			double[] r;

			using(reader = new WaveReader(filename))
			{
				header = reader.Header;

				// R ch を使いたいのに wave ファイルがモノラルの場合
				if(type != ReferenceType.Left && reader.Header.ch == 1)
				{
					return new WaveTime();
				}

				reader.Skip(0);
				reader.Read(reader.Length, out l, out r);
			}

			if(type == ReferenceType.Left)
			{
				if(relative) threshold *= GetPeekValue(l);
				skip += GetSilentLength(l, threshold);
			}
			else if(type == ReferenceType.Right)
			{
				if(relative) threshold *= GetPeekValue(r);
				skip += GetSilentLength(r, threshold);
			}
			else
			{
				if(relative) threshold *= Math.Max(GetPeekValue(l), GetPeekValue(r));
				skip += Math.Min(GetSilentLength(l, threshold), GetSilentLength(r, threshold));
			}
			if(skip < 0 || skip + length >= l.Length) skip = 0;

			return new WaveTime(header, CopyArray(l, skip, length), CopyArray(r, skip, length));
		}//Read
Exemplo n.º 4
0
		/// <summary>
		/// ファイルから Wave データを読み出し。
		/// </summary>
		/// <param name="filename">ファイル名</param>
		/// <param name="length">読み込む長さ</param>
		/// <param name="skip">ファイルの先頭を読み飛ばす長さ</param>
		/// <returns>読み出したデータ</returns>
		static WaveData Read(string filename, int skip, int length)
		{
			WaveReader reader = null;
			FormatHeader header;
			double[] l;
			double[] r;

			using(reader = new WaveReader(filename))
			{
				header = reader.Header;
				reader.Skip(skip);
				reader.Read((uint)length, out l, out r);
			}

			return new WaveTime(header, l, r);
		}//Read