/// <summary> /// 全曲が表示順に並んでいる再生リストap.PcmDataListForPlayを作成。 /// </summary> public void CreateAllTracksPlayList() { m_pcmDataListForPlay = new PcmDataList(); for (int i = 0; i < m_pcmDataListForDisp.Count(); ++i) { var pcmData = new PcmData(); pcmData.CopyFrom(m_pcmDataListForDisp.At(i)); m_pcmDataListForPlay.Add(pcmData); } }
/// <summary> /// 1曲再生のプレイリストをap.PcmDataListForPlayに作成。 /// </summary> public void CreateOneTrackPlayList(int wavDataId) { var pcmData = new PcmData(); pcmData.CopyFrom(m_pcmDataListForDisp.FindById(wavDataId)); pcmData.GroupId = 0; m_pcmDataListForPlay = new PcmDataList(); m_pcmDataListForPlay.Add(pcmData); }
/// <summary> /// シャッフルした再生リストap.PcmDataListForPlayを作成する /// </summary> public void CreateShuffledPlayList() { // 適当にシャッフルされた番号が入っている配列pcmDataIdxArrayを作成。 var pcmDataIdxArray = new int[PcmDataListForDisp.Count()]; for (int i = 0; i < pcmDataIdxArray.Length; ++i) { pcmDataIdxArray[i] = i; } var gen = new RNGCryptoServiceProvider(); int N = pcmDataIdxArray.Length; for (int i = 0; i < N * 100; ++i) { var a = GetRandomNumber(gen, N); var b = GetRandomNumber(gen, N); if (a == b) { // 入れ替え元と入れ替え先が同じ。あんまり意味ないのでスキップする。 continue; } // a番目とb番目を入れ替える var tmp = pcmDataIdxArray[a]; pcmDataIdxArray[a] = pcmDataIdxArray[b]; pcmDataIdxArray[b] = tmp; } // ap.PcmDataListForPlayを作成。 m_pcmDataListForPlay = new PcmDataList(); for (int i = 0; i < pcmDataIdxArray.Length; ++i) { var idx = pcmDataIdxArray[i]; // 再生順番号Ordinalを付け直す // GroupIdをバラバラの番号にする(1曲ずつ読み込む) var pcmData = new PcmData(); pcmData.CopyFrom(m_pcmDataListForDisp.At(idx)); pcmData.Ordinal = i; pcmData.GroupId = i; m_pcmDataListForPlay.Add(pcmData); } }
// この関数は音量制限を行わない。呼び出し側で必要に応じて音量を制限する。 private bool PhaseRotationDo(FirWorkerArgs argsFWA, PcmData pcmDataIn, out PcmData pcmDataOutput) { PhaseRotationWorkerArgs args = argsFWA as PhaseRotationWorkerArgs; var conv = new WasapiPcmUtil.PcmFormatConverter(pcmDataIn.NumChannels); PcmData pcmDataReal = conv.Convert(pcmDataIn, Wasapi.WasapiCS.SampleFormatType.Sdouble, null); pcmDataOutput = new PcmData(); pcmDataOutput.CopyFrom(pcmDataIn); PcmData pcmDataImaginary = new PcmData(); pcmDataImaginary.CopyFrom(pcmDataReal); var dft = new WWDirectComputeCS.WWDftCpu(); var hilb = WWHilbert.HilbertFirCoeff(args.hilbertFilterType, args.firLength); System.Diagnostics.Debug.Assert(hilb.Length == args.firLength); // 窓関数 double [] window; if (args.windowFunc == WindowFuncType.Blackman) { WWWindowFunc.BlackmanWindow(args.firLength, out window); } else { WWWindowFunc.KaiserWindow(args.firLength, args.kaiserAlpha, out window); } // FIR coeffの個数は、window.Length個。 // ヒルベルト変換パラメータは未来から過去の方向に並んでいるので左右をひっくり返す。 double [] coeff = new double[args.firLength]; for (int i = 0; i < coeff.Length; ++i) { int pos = coeff.Length - i - 1; coeff[i] = hilb[pos] * window[i]; } for (int ch = 0; ch < pcmDataImaginary.NumChannels; ++ch) { var pcm1ch = new double[pcmDataImaginary.NumFrames]; for (long i = 0; i < pcm1ch.Length; ++i) { pcm1ch[i] = pcmDataImaginary.GetSampleValueInDouble(ch, i); } // 少しずつFIRする。 var fir = new WWFirCpu(); fir.Setup(coeff, pcm1ch); const int FIR_SAMPLE = 65536; for (int offs = 0; offs < pcm1ch.Length; offs += FIR_SAMPLE) { int nSample = FIR_SAMPLE; if (pcm1ch.Length < offs + nSample) { nSample = pcm1ch.Length - offs; } var pcmFir = new double[nSample]; fir.Do(offs - window.Length / 2, nSample, pcmFir); // 結果を出力に書き込む。 for (long i = 0; i < pcmFir.Length; ++i) { var re = pcmFir[i]; pcmDataImaginary.SetSampleValueInDouble(ch, i + offs, (float)(re)); } // 進捗Update。 int percentage = (int)( (100L * ch / pcmDataImaginary.NumChannels) + (100L * (offs + 1) / pcm1ch.Length / pcmDataImaginary.NumChannels)); mPRWorker.ReportProgress(percentage); } fir.Unsetup(); } // 音の位相を回転。 for (int ch = 0; ch < pcmDataReal.NumChannels; ++ch) { for (long pos = 0; pos < pcmDataReal.NumFrames; ++pos) { // 解析信号の各サンプル値を極座標表現に変換。オリジナルの長さと位相を得る。 // 長さをそのままに位相を回転し、回転後の実数成分を出力する。 double x = pcmDataReal.GetSampleValueInDouble(ch, pos); double y = pcmDataImaginary.GetSampleValueInDouble(ch, pos); double norm = Math.Sqrt(x * x + y * y); double theta = Math.Atan2(y, x); double re = norm * Math.Cos(theta + args.phaseRadian); pcmDataOutput.SetSampleValueInDouble(ch, pos, re); } } return(true); }
/// <summary> /// FLACファイルのヘッダ部分を読み込む。 /// </summary> /// <returns>読めたらtrue、失敗false</returns> private bool ReadFlacFileHeader(string path, ReadHeaderMode mode) { PcmDataLib.PcmData pcmData; List <WWFlacRWCS.FlacCuesheetTrack> ctiList; var fdif = new FlacDecodeIF(); int flacErcd = 0; flacErcd = fdif.ReadHeader(path, out pcmData, out ctiList); if (flacErcd != 0) { // FLACヘッダ部分読み込み失敗。 LoadErrorMessageAdd(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ReadFileFailed + " {2}: {1}{3}", "FLAC", path, FlacDecodeIF.ErrorCodeToStr(flacErcd), Environment.NewLine)); return(false); } // FLACヘッダ部分読み込み成功。 // PCMデータにDoPマーカーが付いているか調べ、pcmData.SampleDataTypeを確定する。 pcmData = FlacScanDopMarker(pcmData, fdif); fdif.ReadEnd(); if (ctiList.Count == 0 || mode == ReadHeaderMode.OnlyConcreteFile) { // FLAC埋め込みCUEシート情報を読まない。 CheckAddPcmData(path, pcmData, true); } else { // FLAC埋め込みCUEシート情報を読む。 PcmData pcmTrack = null; for (int trackId = 0; trackId < ctiList.Count; ++trackId) { var cti = ctiList[trackId]; if (cti.indices.Count == 0) { // インデックスが1つもないトラック。lead-outトラックの場合等。 if (pcmTrack != null) { pcmTrack.EndTick = (int)((cti.offsetSamples * 75) / pcmTrack.SampleRate); CheckAddPcmData(path, pcmTrack, false); pcmTrack = null; } } else { for (int indexId = 0; indexId < cti.indices.Count; ++indexId) { var indexInfo = cti.indices[indexId]; if (pcmTrack != null) { pcmTrack.EndTick = (int)(((cti.offsetSamples + indexInfo.offsetSamples) * 75) / pcmTrack.SampleRate); CheckAddPcmData(path, pcmTrack, false); pcmTrack = null; } pcmTrack = new PcmData(); pcmTrack.CopyFrom(pcmData); if (pcmTrack.DisplayName.Length == 0) { pcmTrack.DisplayName = System.IO.Path.GetFileName(path); } pcmTrack.DisplayName = string.Format(CultureInfo.InvariantCulture, "{0} (Track {1}, Index {2})", pcmTrack.DisplayName, cti.trackNr, indexInfo.indexNr); pcmTrack.CueSheetIndex = indexInfo.indexNr; pcmTrack.StartTick = (int)(((cti.offsetSamples + indexInfo.offsetSamples) * 75) / pcmTrack.SampleRate); } } } } return(true); }
private void m_USAQworker_DoWork(object sender, DoWorkEventArgs e) { // System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Lowest; USWorkerArgs args = (USWorkerArgs)e.Argument; PcmData pcmDataIn = ReadWavFile(args.inputPath); if (null == pcmDataIn) { e.Result = string.Format("WAVファイル 読み込み失敗: {0}", args.inputPath); return; } // ファイル読み込み完了。 if (args.addJitter) { // ジッター負荷の場合、サンプリング周波数は変更しない。 args.resampleFrequency = pcmDataIn.SampleRate; } if (args.resampleFrequency < pcmDataIn.SampleRate) { e.Result = string.Format("エラー: ダウンサンプルは対応していません {0} from={1} to={2}", args.inputPath, pcmDataIn.SampleRate, args.resampleFrequency); return; } if (0x7fff0000L < pcmDataIn.NumFrames * 4 * pcmDataIn.NumChannels * args.resampleFrequency / pcmDataIn.SampleRate) { e.Result = string.Format("エラー: リサンプル後のファイルサイズが2GBを超えそうなので中断しました {0}", args.inputPath); return; } m_USAQworker.ReportProgress(1); var conv = new WasapiPcmUtil.PcmFormatConverter(pcmDataIn.NumChannels); pcmDataIn = conv.Convert(pcmDataIn, WasapiCS.BitAndFormatToSampleFormatType(32, 32, WasapiCS.BitFormatType.SFloat), null); PcmData pcmDataOut = new PcmData(); pcmDataOut.CopyFrom(pcmDataIn); int sampleTotalTo = (int)(args.resampleFrequency * pcmDataIn.NumFrames / pcmDataIn.SampleRate); { // PcmDataOutのサンプルレートとサンプル数を更新する。 byte[] outSampleArray = new byte[(long)sampleTotalTo * pcmDataOut.NumChannels * 4]; pcmDataOut.SetSampleArray(sampleTotalTo, outSampleArray); pcmDataOut.SampleRate = args.resampleFrequency; outSampleArray = null; } // 再サンプルテーブル作成 args.resamplePosArray = null; args.fractionArray = null; if (args.addJitter) { // ジッター付加の場合、サンプルレートは変更しない。 args.resamplePosArray = new int[pcmDataIn.NumFrames]; args.fractionArray = new double[pcmDataIn.NumFrames]; /* * sampleRate == 96000 Hz * jitterFrequency == 50 Hz * jitterPicoseconds == 1 ps の場合 * * サンプル位置posのθ= 2 * PI * pos * 50 / 96000 (ラジアン) * * サンプル間隔= 1/96000秒 = 10.4 μs * * 1ms = 10^-3秒 * 1μs= 10^-6秒 * 1ns = 10^-9秒 * 1ps = 10^-12秒 * * 1psのずれ x サンプルのずれ * ───────────── = ───────── * 10.4 μs(1/96000)sのずれ 1 サンプルのずれ * * 1psのサンプルずれA = 10^-12 ÷ (1/96000) (サンプルのずれ) * * サンプルを採取する位置= pos + Asin(θ) * */ args.thetaCoefficientSeqJitter = 2.0 * Math.PI * args.sequentialJitterFrequency / pcmDataIn.SampleRate; args.ampSeqJitter = 1.0e-12 * pcmDataIn.SampleRate * args.sequentialJitterPicoseconds; args.ampTpdfJitter = 1.0e-12 * pcmDataIn.SampleRate * args.tpdfJitterPicoseconds; args.ampRpdfJitter = 1.0e-12 * pcmDataIn.SampleRate * args.rpdfJitterPicoseconds; PrepareResamplePosArray( args, pcmDataIn.SampleRate, pcmDataOut.SampleRate, (int)pcmDataIn.NumFrames, sampleTotalTo, args.resamplePosArray, args.fractionArray); } System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); int hr = 0; if (args.device == ProcessDevice.Gpu) { hr = GpuUpsample(args, pcmDataIn, pcmDataOut); } else { hr = CpuUpsample(args, pcmDataIn, pcmDataOut); } // args.resamplePosArrayは中でコピーされるのでここで不要になる。 args.resamplePosArray = null; args.fractionArray = null; if (m_USAQworker.CancellationPending) { e.Result = string.Format("キャンセル完了。"); e.Cancel = true; return; } if (hr < 0) { e.Result = string.Format("Upsample エラー 0x{0:X8}", hr); return; } sw.Stop(); // 成功した。レベル制限する。 float scale = pcmDataOut.LimitLevelOnFloatRange(); if (args.outputVRT != PcmData.ValueRepresentationType.SFloat) { // ビットフォーマット変更。 var formatConv = new WasapiPcmUtil.PcmFormatConverter(pcmDataOut.NumChannels); pcmDataOut = formatConv.Convert(pcmDataOut, WasapiCS.BitAndFormatToSampleFormatType(args.outputBitsPerSample, args.outputBitsPerSample, (WasapiCS.BitFormatType)args.outputVRT), null); } try { WriteWavFile(pcmDataOut, args.outputPath); } catch (IOException ex) { // 書き込みエラー。 e.Result = ex.ToString(); return; } e.Result = string.Format("書き込み成功。処理時間 {0}秒\r\n", sw.ElapsedMilliseconds * 0.001); if (scale < 1.0f) { e.Result = string.Format("書き込み成功。処理時間 {0}秒。" + "レベルオーバーのため音量調整{1}dB({2}倍)しました。\r\n", sw.ElapsedMilliseconds * 0.001, 20.0 * Math.Log10(scale), scale); } m_USAQworker.ReportProgress(100); }
private bool HilbertDo(FirWorkerArgs argsFir, PcmData pcmDataIn, out PcmData pcmDataOutput) { HilbertWorkerArgs args = argsFir as HilbertWorkerArgs; var dft = new WWDirectComputeCS.WWDftCpu(); var hilb = WWHilbert.HilbertFirCoeff(args.hilbertFilterType, args.firLength); System.Diagnostics.Debug.Assert(hilb.Length == args.firLength); // 窓関数 double [] window; if (args.windowFunc == WindowFuncType.Blackman) { WWWindowFunc.BlackmanWindow(args.firLength, out window); } else { WWWindowFunc.KaiserWindow(args.firLength, args.kaiserAlpha, out window); } // FIR coeffの個数は、window.Length個。 // ヒルベルト変換パラメータは未来から過去の方向に並んでいるので左右をひっくり返す。 double [] coeff = new double[args.firLength]; for (int i = 0; i < coeff.Length; ++i) { int pos = coeff.Length - i - 1; coeff[i] = hilb[pos] * window[i]; } /* * for (int i=0; i < coeff.Length; ++i) { * System.Console.WriteLine("coeff {0:D2} {1}", i, coeff[i]); * } * System.Console.WriteLine(""); */ pcmDataOutput = new PcmData(); pcmDataOutput.CopyFrom(pcmDataIn); for (int ch = 0; ch < pcmDataOutput.NumChannels; ++ch) { // 全てのチャンネルでループ。 var pcm1ch = new double[pcmDataOutput.NumFrames]; for (long i = 0; i < pcm1ch.Length; ++i) { pcm1ch[i] = pcmDataOutput.GetSampleValueInDouble(ch, i); } // 少しずつFIRする。 var fir = new WWFirCpu(); fir.Setup(coeff, pcm1ch); const int FIR_SAMPLE = 65536; for (int offs = 0; offs < pcm1ch.Length; offs += FIR_SAMPLE) { int nSample = FIR_SAMPLE; if (pcm1ch.Length < offs + nSample) { nSample = pcm1ch.Length - offs; } var pcmFir = new double[nSample]; fir.Do(offs - window.Length / 2, nSample, pcmFir); // 結果を出力に書き込む。 for (long i = 0; i < pcmFir.Length; ++i) { var re = pcmFir[i]; pcmDataOutput.SetSampleValueInDouble(ch, i + offs, re); } // 進捗Update。 int percentage = (int)( (100L * ch / pcmDataOutput.NumChannels) + (100L * (offs + 1) / pcm1ch.Length / pcmDataOutput.NumChannels)); m_HilbWorker.ReportProgress(percentage); } fir.Unsetup(); } return(true); }
private bool FirDo(FirWorkerArgs args, PcmData pcmDataIn, out PcmData pcmDataOut) { var dft = new WWDirectComputeCS.WWDftCpu(); var from = FreqGraphToIdftInput(pcmDataIn.SampleRate); double [] idftResult; dft.Idft1d(from, out idftResult); // 窓関数の要素数は、IDFT結果の複素数の個数 -1個。 double [] window; if (args.windowFunc == WindowFuncType.Blackman) { WWWindowFunc.BlackmanWindow((idftResult.Length / 2) - 1, out window); } else { WWWindowFunc.KaiserWindow((idftResult.Length / 2) - 1, args.kaiserAlpha, out window); } // FIR coeffの個数は、window.Length個。 double [] coeff; dft.IdftComplexToFirCoeff1d(idftResult, window, out coeff); /* * for (int i=0; i < coeff.Length; ++i) { * System.Console.WriteLine("coeff {0:D2} {1}", i, coeff[i]); * } * System.Console.WriteLine(""); */ pcmDataOut = new PcmData(); pcmDataOut.CopyFrom(pcmDataIn); for (int ch = 0; ch < pcmDataOut.NumChannels; ++ch) { // 全てのチャンネルでループ。 var pcm1ch = new double[pcmDataOut.NumFrames]; for (long i = 0; i < pcm1ch.Length; ++i) { pcm1ch[i] = pcmDataOut.GetSampleValueInDouble(ch, i); } // 少しずつFIRする。 var fir = new WWFirCpu(); fir.Setup(coeff, pcm1ch); const int FIR_SAMPLE = 65536; for (int offs = 0; offs < pcm1ch.Length; offs += FIR_SAMPLE) { int nSample = FIR_SAMPLE; if (pcm1ch.Length < offs + nSample) { nSample = pcm1ch.Length - offs; } var pcmFir = new double[nSample]; fir.Do(offs - window.Length / 2, nSample, pcmFir); // 結果を出力に書き込む。 for (long i = 0; i < pcmFir.Length; ++i) { var re = pcmFir[i]; pcmDataOut.SetSampleValueInDouble(ch, i + offs, re); } // 進捗Update。 int percentage = (int)( (100L * ch / pcmDataOut.NumChannels) + (100L * (offs + 1) / pcm1ch.Length / pcmDataOut.NumChannels)); m_FirWorker.ReportProgress(percentage); } fir.Unsetup(); } return(true); }
private void m_AnalyticSignalWorker_DoWork(object sender, DoWorkEventArgs e) { AnalyticSignalWorkerArgs args = e.Argument as AnalyticSignalWorkerArgs; Stopwatch sw = new Stopwatch(); sw.Start(); // pcmファイルを読み込んでサンプル配列pcm1chを作成。 PcmData pcmDataIn = null; try { pcmDataIn = ReadWavFile(args.inputPath); } catch (IOException ex) { e.Result = string.Format("WAVファイル {0} 読み込み失敗\r\n{1}", args.inputPath, ex); } if (null == pcmDataIn) { e.Result = string.Format("WAVファイル {0} 読み込み失敗", args.inputPath); } var formatConv = new WasapiPcmUtil.PcmFormatConverter(pcmDataIn.NumChannels); PcmData pcmDataReal = formatConv.Convert(pcmDataIn, Wasapi.WasapiCS.SampleFormatType.Sdouble, null); PcmData pcmDataImaginary = new PcmData(); pcmDataImaginary.CopyFrom(pcmDataReal); var dft = new WWDirectComputeCS.WWDftCpu(); var hilb = WWHilbert.HilbertFirCoeff(args.hilbertFilterType, args.firLength); System.Diagnostics.Debug.Assert(hilb.Length == args.firLength); // 窓関数 double [] window; if (args.windowFunc == WindowFuncType.Blackman) { WWWindowFunc.BlackmanWindow(args.firLength, out window); } else { WWWindowFunc.KaiserWindow(args.firLength, args.kaiserAlpha, out window); } // FIR coeffの個数は、window.Length個。 // ヒルベルト変換パラメータは未来から過去の方向に並んでいるので左右をひっくり返す。 double [] coeff = new double[args.firLength]; for (int i = 0; i < coeff.Length; ++i) { int pos = coeff.Length - i - 1; coeff[i] = hilb[pos] * window[i]; } for (int ch = 0; ch < pcmDataImaginary.NumChannels; ++ch) { var pcm1ch = new double[pcmDataImaginary.NumFrames]; for (long i = 0; i < pcm1ch.Length; ++i) { pcm1ch[i] = pcmDataImaginary.GetSampleValueInDouble(ch, i); } // 少しずつFIRする。 var fir = new WWFirCpu(); fir.Setup(coeff, pcm1ch); const int FIR_SAMPLE = 65536; for (int offs = 0; offs < pcm1ch.Length; offs += FIR_SAMPLE) { int nSample = FIR_SAMPLE; if (pcm1ch.Length < offs + nSample) { nSample = pcm1ch.Length - offs; } var pcmFir = new double[nSample]; fir.Do(offs - window.Length / 2, nSample, pcmFir); // 結果を出力に書き込む。 for (long i = 0; i < pcmFir.Length; ++i) { var re = pcmFir[i]; pcmDataImaginary.SetSampleValueInDouble(ch, i + offs, (float)(re)); } // 進捗Update。 int percentage = (int)( (100L * ch / pcmDataImaginary.NumChannels) + (100L * (offs + 1) / pcm1ch.Length / pcmDataImaginary.NumChannels)); m_ASWorker.ReportProgress(percentage); } fir.Unsetup(); } // 解析信号を出力。 m_analyticSignalList.Clear(); for (int ch = 0; ch < pcmDataReal.NumChannels; ++ch) { double [] signal = new double[pcmDataImaginary.NumFrames * 2]; for (long pos = 0; pos < pcmDataReal.NumFrames; ++pos) { signal[pos * 2 + 0] = pcmDataReal.GetSampleValueInDouble(ch, pos); signal[pos * 2 + 1] = pcmDataImaginary.GetSampleValueInDouble(ch, pos); } m_analyticSignalList.Add(signal); } sw.Stop(); e.Result = ""; }