public Simulated WorkWith(float roomSize, float[] samples) { Position = position * roomSize; Distance = Position.Length(); Samples = FastConvolver.Convolve(this.samples, samples); return(this); }
/// <summary> /// Constructs a benchmark for Cavern's <see cref="FastConvolver"/> filter. /// </summary> public Convolution(int length) { this.length = length; filter = new FastConvolver(new float[length]); }
static void Main(string[] args) { // Find where this executable is launched from string[] cargs = Environment.GetCommandLineArgs(); _thisFolder = Path.GetDirectoryName(cargs[0]); if (String.IsNullOrEmpty(_thisFolder)) { _thisFolder = Environment.CurrentDirectory; } string appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); _impulsesFolder = Path.GetFullPath(Path.Combine(appData, "InguzEQ" + slash + "Impulses" + slash)); string[] inFiles = new string[4]; string inL = ""; string inR = ""; if (!DisplayInfo()) { return; } bool ok = (args.Length > 0); bool longUsage = false; for (int j = 0; ok && j < args.Length; j++) { string arg = args[j]; switch (args[j].ToUpperInvariant()) { case "/?": case "-?": case "/H": case "/HELP": ok = false; longUsage = true; break; case "/L": case "/0": inFiles[0] = args[++j]; _nInFiles = Math.Max(_nInFiles, 1); break; case "/R": case "/1": inFiles[1] = args[++j]; _nInFiles = Math.Max(_nInFiles, 2); break; case "/2": inFiles[2] = args[++j]; _nInFiles = Math.Max(_nInFiles, 3); break; case "/3": inFiles[3] = args[++j]; _nInFiles = Math.Max(_nInFiles, 4); break; case "/LENGTH": _filterLen = int.Parse(args[++j], CultureInfo.InvariantCulture); if (_filterLen < 16) { throw new Exception("Length is too small."); } break; case "/DBL": _dbl = true; break; case "/PCM": _pcm = true; break; case "/NODRC": _noDRC = true; break; case "/NOSKEW": _noSkew = true; break; case "/NONORM": // No normalization of the impulse response (undocumented) _noNorm = true; break; case "/SPLIT": _split = true; break; case "/COPY": _copy = true; break; case "/GAIN": _gain = double.Parse(args[++j], CultureInfo.InvariantCulture); break; case "/ALL": // Returns negative-time components as part of the impulse response // (experimental, to be used for THD measurement) _returnAll = true; break; case "/POWER": // Raises sweep to power n // (experimental, to be used for THD measurement) _power = int.Parse(args[++j], CultureInfo.InvariantCulture); break; case "/FMIN": // (experimental, i.e. broken) _fmin = int.Parse(args[++j], CultureInfo.InvariantCulture); _fminSpecified = true; break; case "/FMAX": // (experimental, i.e. broken) _fmax = int.Parse(args[++j], CultureInfo.InvariantCulture); _fmaxSpecified = true; break; case "/DIRECT": // Create filtered (direct-sound) filters _doDirectFilters = true; break; case "/NOSUB": // Don't apply subsonic filter to the impulse response _noSubsonicFilter = true; break; case "/NOOVER": // Don't override DRC's settings for filter type and length _noOverrideDRC = true; break; case "/KEEPTEMP": // Undocumented _keepTempFiles = true; break; case "/REFCH": // Override the reference-channel detection _refchannel = int.Parse(args[++j], CultureInfo.InvariantCulture); if (_refchannel<0 || _refchannel > _nInFiles - 1) { throw new Exception(String.Format("RefCh can only be from 0 to {0}.", _nInFiles-1)); } break; case "/ENV": // Undocumented. Save the Hilbert envelope _env = true; break; case "-": // ignore break; default: ok = false; break; } } if (!ok) { DisplayUsage(longUsage); } else { try { if (!_noDRC) { if (!File.Exists(GetDRCExe())) { stderr.WriteLine("Denis Sbragion's DRC (http://drc-fir.sourceforge.net/) was not found."); stderr.WriteLine("Only the impulse response will be calculated, not correction filters."); stderr.WriteLine(""); _noDRC = true; } } if (!_noDRC) { FileInfo[] drcfiles = new DirectoryInfo(_thisFolder).GetFiles("*.drc"); if (drcfiles.Length == 0) { stderr.WriteLine("No .drc files were found in the current folder."); stderr.WriteLine("Only the impulse response will be calculated, not correction filters."); stderr.WriteLine(""); _noDRC = true; } } for(int i=0; i<_nInFiles; i++) { string inFile = inFiles[i]; if (String.IsNullOrEmpty(inFile)) { stderr.WriteLine("Error: The {0} input file was not specified.", FileDescription(i)); return; } if (!File.Exists(inFile)) { stderr.WriteLine("Error: The {0} input file {1} was not found.", FileDescription(i), inFile); return; } for (int j = 0; j < i; j++) { if (inFile.Equals(inFiles[j])) { stderr.WriteLine("Warning: The same input file ({0}) was specified for both {1} and {2}!", inFile, FileDescription(j), FileDescription(i)); //stderr.WriteLine(); } } } // Temporary if (_nInFiles != 2) { stderr.WriteLine("Error: Two input files must be specified."); return; } inL = inFiles[0]; inR = inFiles[1]; // end temporary uint sampleRate; List<SoundObj> impulses; List<ISoundObj> filteredImpulses; List<string> impDirects; List<Complex[]> impulseFFTs; List<double> maxs; SoundObj impulseL; SoundObj impulseR; ISoundObj filteredImpulseL = null; ISoundObj filteredImpulseR = null; string impDirectL = null; string impDirectR = null; Complex[] impulseLFFT; Complex[] impulseRFFT; WaveWriter writer; ISoundObj buff; double g; if (!_keepTempFiles) { _tempFiles.Add("rps.pcm"); _tempFiles.Add("rtc.pcm"); } // Find the left impulse stderr.WriteLine("Processing left measurement ({0})...", inL); impulseL = Deconvolve(inL, out impulseLFFT, out _peakPosL); sampleRate = impulseL.SampleRate; _sampleRate = sampleRate; double peakM = Math.Round(MathUtil.Metres(_peakPosL, sampleRate), 2); double peakFt = Math.Round(MathUtil.Feet(_peakPosL, sampleRate), 2); stderr.WriteLine(" Impulse peak at sample {0} ({1}m, {2}ft)", _peakPosL, peakM, peakFt); // Write to PCM string impFileL = Path.GetFileNameWithoutExtension(inL) + "_imp" + ".pcm"; if (!_keepTempFiles) { _tempFiles.Add(impFileL); } writer = new WaveWriter(impFileL); writer.Input = impulseL; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Raw = true; writer.Run(); writer.Close(); // Write the impulseFFT to disk int L = impulseLFFT.Length; string impTempL = Path.GetFileNameWithoutExtension(inL) + "_imp" + ".dat"; _tempFiles.Add(impTempL); writer = new WaveWriter(impTempL); writer.Input = new CallbackSource(2, sampleRate, delegate(long j) { if (j >= L / 2) { return null; } Complex si = impulseLFFT[j]; // +impulseLFFT[L - j - 1]; ISample s = new Sample2(); s[0] = si.Magnitude; s[1] = si.Phase / Math.PI; return s; }); writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Raw = false; writer.Run(); writer.Close(); writer = null; impulseLFFT = null; GC.Collect(); if (_doDirectFilters) { // Sliding low-pass filter over the impulse stderr.WriteLine(" Filtering..."); filteredImpulseL = SlidingLowPass(impulseL, _peakPosL); // Write PCM for the filtered impulse impDirectL = Path.GetFileNameWithoutExtension(inL) + "_impfilt" + ".pcm"; if (!_keepTempFiles) { _tempFiles.Add(impDirectL); } writer = new WaveWriter(impDirectL); writer.Input = filteredImpulseL; writer.Format = WaveFormat.IEEE_FLOAT; writer.SampleRate = _sampleRate; writer.BitsPerSample = 32; writer.Raw = false; writer.Run(); writer.Close(); writer = null; filteredImpulseL.Reset(); } GC.Collect(); stderr.WriteLine(" Deconvolution: left impulse done."); stderr.WriteLine(); // Find the right impulse stderr.WriteLine("Processing right measurement ({0})...", inR); impulseR = Deconvolve(inR, out impulseRFFT, out _peakPosR); peakM = Math.Round(MathUtil.Metres(_peakPosR, sampleRate), 2); peakFt = Math.Round(MathUtil.Feet(_peakPosR, sampleRate), 2); stderr.WriteLine(" Impulse peak at sample {0} ({1}m, {2}ft)", _peakPosR, peakM, peakFt); // Write to PCM string impFileR = Path.GetFileNameWithoutExtension(inR) + "_imp" + ".pcm"; if (!_keepTempFiles) { _tempFiles.Add(impFileR); } writer = new WaveWriter(impFileR); writer.Input = impulseR; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Raw = true; writer.Run(); writer.Close(); // Write the impulseFFT magnitude to disk L = impulseRFFT.Length; string impTempR = Path.GetFileNameWithoutExtension(inR) + "_imp" + ".dat"; _tempFiles.Add(impTempR); writer = new WaveWriter(impTempR); writer.Input = new CallbackSource(2, impulseR.SampleRate, delegate(long j) { if (j >= L / 2) { return null; } Complex si = impulseRFFT[j]; // +impulseRFFT[L - j - 1]; ISample s = new Sample2(); s[0] = si.Magnitude; s[1] = si.Phase / Math.PI; return s; }); writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Raw = false; writer.Run(); writer.Close(); writer = null; impulseRFFT = null; GC.Collect(); if (_doDirectFilters) { // Sliding low-pass filter over the impulse stderr.WriteLine(" Filtering..."); filteredImpulseR = SlidingLowPass(impulseR, _peakPosR); // Write PCM for the filtered impulse impDirectR = Path.GetFileNameWithoutExtension(inR) + "_impfilt" + ".pcm"; if (!_keepTempFiles) { _tempFiles.Add(impDirectR); } writer = new WaveWriter(impDirectR); writer.Input = filteredImpulseR; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Raw = false; writer.Run(); writer.Close(); writer = null; filteredImpulseR.Reset(); } GC.Collect(); stderr.WriteLine(" Deconvolution: right impulse done."); stderr.WriteLine(); // Join the left and right impulse files (truncated at 65536) into a WAV // and normalize loudness for each channel stderr.WriteLine("Splicing and normalizing (1)"); ChannelSplicer longstereoImpulse = new ChannelSplicer(); // (Don't normalize each channel's volume separately if _returnAll, it's just too expensive) if (_returnAll) { buff = impulseL; } else { buff = new SoundBuffer(new SampleBuffer(impulseL).Subset(0, 131071)); g = Loudness.WeightedVolume(buff); (buff as SoundBuffer).ApplyGain(1 / g); } longstereoImpulse.Add(buff); if (_returnAll) { buff = impulseR; } else { buff = new SoundBuffer(new SampleBuffer(impulseR).Subset(0, 131071)); g = Loudness.WeightedVolume(buff); (buff as SoundBuffer).ApplyGain(1 / g); } longstereoImpulse.Add(buff); ISoundObj stereoImpulse = longstereoImpulse; _impulseFiles.Add("Impulse_Response_Measured.wav: stereo impulse response from measurements"); writer = new WaveWriter("Impulse_Response_Measured.wav"); writer.Input = longstereoImpulse; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Normalization = -1; writer.Raw = false; writer.Run(); writer.Close(); writer = null; if (_env) { // Also save the Hilbert envelope HilbertEnvelope env = new HilbertEnvelope(8191); env.Input = longstereoImpulse; _impulseFiles.Add("Impulse_Response_Envelope.wav: Hilbert envelope of the impulse response"); writer = new WaveWriter("Impulse_Response_Envelope.wav"); writer.Input = env; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Normalization = -1; writer.Raw = false; writer.Run(); writer.Close(); writer = null; } if (_dbl) { // Create DBL files for Acourate _impulseFiles.Add("PulseL.dbl: impulse response, raw data (64-bit float), left channel "); _impulseFiles.Add("PulseR.dbl: impulse response, raw data (64-bit float), right channel"); _impulseFiles.Add(" (use skew=" + (_peakPosL - _peakPosR) + " for time alignment)"); WriteImpulseDBL(stereoImpulse, "PulseL.dbl", "PulseR.dbl"); } if (_pcm) { // Create PCM files for Octave (etc) _impulseFiles.Add("LUncorrected.pcm: impulse response, raw data (32-bit float), left channel"); _impulseFiles.Add("RUncorrected.pcm: impulse response, raw data (32-bit float), right channel"); WriteImpulsePCM(stereoImpulse, "LUncorrected.pcm", "RUncorrected.pcm"); } stereoImpulse = null; longstereoImpulse = null; buff = null; GC.Collect(); if (_doDirectFilters) { // Same for the filtered impulse response stderr.WriteLine("Splicing and normalizing (2)"); ChannelSplicer longstereoImpulseF = new ChannelSplicer(); buff = new SoundBuffer(new SampleBuffer(filteredImpulseL).Subset(0, 131071)); double gL = Loudness.WeightedVolume(buff); (buff as SoundBuffer).ApplyGain(1 / gL); longstereoImpulseF.Add(buff); FilterProfile lfgDirectL = new FilterProfile(buff, 0.5); buff = new SoundBuffer(new SampleBuffer(filteredImpulseR).Subset(0, 131071)); double gR = Loudness.WeightedVolume(buff); (buff as SoundBuffer).ApplyGain(1 / gR); longstereoImpulseF.Add(buff); FilterProfile lfgDirectR = new FilterProfile(buff, 0.5); _impulseFiles.Add("Impulse_Response_Filtered.wav: approximation to direct-sound impulse response"); writer = new WaveWriter("Impulse_Response_Filtered.wav"); writer.Input = longstereoImpulseF; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Normalization = -1; writer.Raw = false; writer.Run(); writer.Close(); double gg = writer.Gain; writer = null; longstereoImpulseF = null; ChannelSplicer longstereoImpulseD = new ChannelSplicer(); Mixer diffuse = new Mixer(); diffuse.Add(impulseL, 1.0); diffuse.Add(filteredImpulseL, -1.0); buff = new SoundBuffer(new SampleBuffer(diffuse).Subset(0, 131071)); (buff as SoundBuffer).ApplyGain(1 / gL); longstereoImpulseD.Add(buff); FilterProfile lfgDiffuseL = new FilterProfile(buff, 0.5); diffuse = new Mixer(); diffuse.Add(impulseR, 1.0); diffuse.Add(filteredImpulseR, -1.0); buff = new SoundBuffer(new SampleBuffer(diffuse).Subset(0, 131071)); (buff as SoundBuffer).ApplyGain(1 / gR); longstereoImpulseD.Add(buff); FilterProfile lfgDiffuseR = new FilterProfile(buff, 0.5); _impulseFiles.Add("Impulse_Response_Diffuse.wav: approximation to diffuse-field remnant"); writer = new WaveWriter("Impulse_Response_Diffuse.wav"); writer.Input = longstereoImpulseD; writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Gain = gg; writer.Raw = false; writer.Run(); writer.Close(); writer = null; // Filter the diffuse-field curve against double the diffuse-field curve FilterImpulse fiDiffuse = new FilterImpulse(8192, HRTF.diffuseDiff0() * 2, FilterInterpolation.COSINE, sampleRate); FastConvolver co = new FastConvolver(longstereoImpulseD, fiDiffuse); SoundBuffer buffd = new SoundBuffer(co); _impulseFiles.Add("Impulse_Response_Diffuse_Comp.wav: filtered diffuse-field remnant"); writer = new WaveWriter("Impulse_Response_Diffuse_Comp.wav"); writer.Input = buffd.Subset(4096); writer.Format = WaveFormat.IEEE_FLOAT; writer.BitsPerSample = 32; writer.SampleRate = _sampleRate; writer.Gain = gg; writer.Raw = false; writer.Run(); writer.Close(); writer = null; longstereoImpulseD = null; bool any = false; string jsonFile = "Diff.json"; FileStream fs = new FileStream(jsonFile, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("{"); FilterProfile lfgDiffL = lfgDirectL - lfgDiffuseL; if (lfgDiffL != null) { if (any) sw.WriteLine(","); any = true; sw.Write(lfgDiffL.ToJSONString("DiffL", "Diffuse field relative to direct, left channel")); } FilterProfile lfgDiffR = lfgDirectR - lfgDiffuseR; if (lfgDiffR != null) { if (any) sw.WriteLine(","); any = true; sw.Write(lfgDiffR.ToJSONString("DiffR", "Diffuse field relative to direct, right channel")); } sw.WriteLine("}"); sw.Close(); fs.Close(); } buff = null; GC.Collect(); System.Console.Error.WriteLine(); if (!_noDRC) { // Analyze the freq response // and create targets // target_full.txt and target_half.txt stderr.WriteLine("Analyzing response curves."); Prep(impTempL, impTempR, "Impulse_Response_Measured.wav", "NoCorrection"); // Call DRC to create the filters // then splice the DRC left & right output files together stderr.WriteLine("Preparing for DRC."); if (DoDRC(impFileL, impFileR, impDirectL, impDirectR, _peakPosL, _peakPosR, "Impulse_Response_Measured.wav", "Impulse_Response_Filtered.wav")) { stderr.WriteLine("Success!"); } } // Report names of the impulse files created if (_impulseFiles.Count == 0) { System.Console.Error.WriteLine("No impulse response files were created."); } if (_impulseFiles.Count > 0) { System.Console.Error.WriteLine("Impulse response files were created:"); foreach (string f in _impulseFiles) { string s = " " + f; System.Console.Error.WriteLine(s); } } // Report names of the filter files created if (_filterFiles.Count == 0 && !_noDRC) { System.Console.Error.WriteLine("No correction filter files were created."); } if (_filterFiles.Count > 0) { System.Console.Error.WriteLine("Correction filter files were created:"); foreach (string f in _filterFiles) { string s = " " + f; if (_copy) { try { File.Copy(f, Path.Combine(_impulsesFolder, f), true); s += " (copied)"; } catch (Exception e) { s += " (not copied: " + e.Message + ")"; } } System.Console.Error.WriteLine(s); } } if (_peakPosL == _peakPosR) { System.Console.Error.WriteLine(); System.Console.Error.WriteLine("Zero time difference between channels. Are you sure the recordings are correct?"); } } catch (Exception e) { stderr.WriteLine(); stderr.WriteLine(e.Message); stderr.WriteLine(e.StackTrace); } finally { foreach (string tempFile in _tempFiles) { try { File.Delete(tempFile); } catch (Exception) { /* ignore */ } } } } stderr.Flush(); }
static ISoundObj LowPassFiltered(ISoundObj input, double freqStart, double gainEnd) { uint sampleRate = input.SampleRate; FilterProfile lfg = new FilterProfile(); lfg.Add(new FreqGain(freqStart, 0)); lfg.Add(new FreqGain(0.499*sampleRate, gainEnd)); FastConvolver conv = new FastConvolver(); conv.Input = input; conv.impulse = new FilterImpulse(8192, lfg, FilterInterpolation.COSINE, sampleRate); return conv; }
static void FindPeaks(ISoundObj impulse) { // Input: a single-channel impulse // Find the peak positions: // - unfiltered // - filtered with various bandpass filters uint sr = impulse.SampleRate; ushort nc = impulse.NumChannels; double peakM = Math.Round(MathUtil.Metres(_peakPosL, sr), 2); double peakFt = Math.Round(MathUtil.Feet(_peakPosL, sr), 2); stderr.WriteLine(" Impulse peak at sample {0} ({1}m, {2}ft)", _peakPosL, peakM, peakFt); FilterImpulse fi; WaveWriter wri; FastConvolver co; fi = new FilterImpulse(2048, bandpass(400,sr), FilterInterpolation.COSINE, sr); co = new FastConvolver(impulse, fi); wri = new WaveWriter("bp_400.wav", nc, sr, 16, DitherType.NONE, WaveFormat.PCM); wri.Input = co; wri.Normalization = -1; wri.Run(); wri.Close(); fi = new FilterImpulse(2048, bandpass(6000, sr), FilterInterpolation.COSINE, sr); co = new FastConvolver(impulse, fi); wri = new WaveWriter("bp_6000.wav", nc, sr, 16, DitherType.NONE, WaveFormat.PCM); wri.Input = co; wri.Normalization = -1; wri.Run(); wri.Close(); // and fi = new FilterImpulse(2048, bandpass(160, sr), FilterInterpolation.COSINE, sr); co = new FastConvolver(impulse, fi); wri = new WaveWriter("bp_160.wav", nc, sr, 16, DitherType.NONE, WaveFormat.PCM); wri.Input = co; wri.Normalization = -1; wri.Run(); wri.Close(); fi = new FilterImpulse(2048, bandpass(2560, sr), FilterInterpolation.COSINE, sr); co = new FastConvolver(impulse, fi); wri = new WaveWriter("bp_2560.wav", nc, sr, 16, DitherType.NONE, WaveFormat.PCM); wri.Input = co; wri.Normalization = -1; wri.Run(); wri.Close(); fi = new FilterImpulse(2048, bandpass(18000, sr), FilterInterpolation.COSINE, sr); co = new FastConvolver(impulse, fi); wri = new WaveWriter("bp_18k.wav", nc, sr, 16, DitherType.NONE, WaveFormat.PCM); wri.Input = co; wri.Normalization = -1; wri.Run(); wri.Close(); }
static bool ExecDRC(string drcfile, string target, string outfile, string infileL, string infileR, int peakPosL, int peakPosR, string stereoImpulseFile) { GC.Collect(); bool ok = false; string args; FastConvolver conv; WaveWriter wri; if (!File.Exists(drcfile)) { stderr.WriteLine(); stderr.WriteLine("{0} not found.", drcfile); return ok; } string tmpL; string tmpR; tmpL = Path.GetFileNameWithoutExtension(infileL) + ".tmp"; tmpR = Path.GetFileNameWithoutExtension(infileR) + ".tmp"; _tempFiles.Add(tmpL); _tempFiles.Add(tmpR); stderr.WriteLine("Exec DRC for {0}, left channel", drcfile); stderr.WriteLine(); ok = RunDRC(drcfile, infileL, target, tmpL, peakPosL, out args); if (ok) { stderr.WriteLine(); stderr.WriteLine("Exec DRC for {0}, right channel", drcfile); stderr.WriteLine(); ok = RunDRC(drcfile, infileR, target, tmpR, peakPosR, out args); } if (ok) { stderr.WriteLine(); if (_noSkew) { stderr.WriteLine("Creating stereo filter {0}", outfile + ".wav" ); } else { stderr.WriteLine("Creating stereo filter {0} (skew {1} samples)", outfile + ".wav", peakPosR - peakPosL); } ISoundObj stereoFilter = Splice(tmpL, peakPosL, tmpR, peakPosR, outfile + ".wav"); stderr.WriteLine(); // Convolve noise with the stereo filter /* NoiseGenerator noise = new NoiseGenerator(NoiseType.WHITE_FLAT, 2, (int)131072, stereoFilter.SampleRate, 1.0); conv = new FastConvolver(); conv.impulse = stereoFilter; conv.Input = noise; wri = new WaveWriter(drcfile + "_Test.wav"); wri.Input = conv; wri.Format = WaveFormat.IEEE_FLOAT; wri.BitsPerSample = 32; wri.SampleRate = _sampleRate; wri.Normalization = -1; wri.Run(); wri.Close(); wri = null; conv = null; noise = null; * */ // Convolve filter with the in-room impulse response WaveReader rea = new WaveReader(stereoImpulseFile); conv = new FastConvolver(); conv.impulse = rea; conv.Input = stereoFilter; if (_pcm) { _impulseFiles.Add("LCorrected_" + outfile + ".pcm: corrected test convolution, raw data (32-bit float), left channel"); _impulseFiles.Add("RCorrected_" + outfile + ".pcm: corrected test convolution, raw data (32-bit float), right channel"); WriteImpulsePCM(conv, "LCorrected_" + outfile + ".pcm", "RCorrected_" + outfile + ".pcm"); } wri = new WaveWriter(outfile + "_TestConvolution.wav"); wri.Input = conv; wri.Format = WaveFormat.PCM; wri.Dither = DitherType.TRIANGULAR; wri.BitsPerSample = 16; wri.SampleRate = _sampleRate; wri.Normalization = -1; wri.Run(); wri.Close(); rea.Close(); wri = null; rea = null; conv = null; GC.Collect(); } return ok; }
static void Prep(string infileL, string infileR, string stereoImpulseFile, string outFile) { // Input files are complex // 0=mag, 1=phase/pi (so it looks OK in a wave editor!) // FFTs of the room impulse response // Take two half-FFT-of-impulse WAV files // Average them, into an array int n; SoundBuffer buff; WaveWriter wri; // NoiseGenerator noise; FastConvolver conv; /* // Convolve noise with the in-room impulse noise = new NoiseGenerator(NoiseType.WHITE_FLAT, 2, (int)131072, stereoImpulse.SampleRate, 1.0); conv = new FastConvolver(); conv.impulse = stereoImpulse; conv.Input = noise; wri = new WaveWriter("ImpulseResponse_InRoom.wav"); wri.Input = conv; wri.Format = WaveFormat.IEEE_FLOAT; wri.BitsPerSample = 32; wri.Normalization = 0; wri.Run(); wri.Close(); wri = null; conv = null; */ WaveReader rdrL = new WaveReader(infileL); buff = new SoundBuffer(rdrL); n = (int)buff.ReadAll(); uint sampleRate = buff.SampleRate; uint nyquist = sampleRate / 2; double binw = (nyquist / (double)n); WaveReader rdrR = new WaveReader(infileR); IEnumerator<ISample> enumL = buff.Samples; IEnumerator<ISample> enumR = rdrR.Samples; // For easier processing and visualisation // read this in to an ERB-scale (not quite log-scale) array // then we can smooth by convolving with a single half-cosine. // int nn = (int)ERB.f2bin(nyquist, sampleRate) + 1; double[] muff = new double[nn]; int prevbin = 0; int nbin = 0; double v = 0; int j = 0; while (true) { double f = (double)j * binw; // equiv freq, Hz int bin = (int)ERB.f2bin(f, sampleRate); // the bin we drop this sample in if (bin > nn) { // One of the channels has more, but we're overrun so stop now break; } j++; bool more = false; more |= enumL.MoveNext(); more |= enumR.MoveNext(); if (!more) { muff[prevbin] = v / nbin; break; } v += enumL.Current[0]; // magnitude v += enumR.Current[0]; // magnitude nbin++; if (bin > prevbin) { muff[prevbin] = v / nbin; v = 0; nbin = 0; prevbin = bin; } } double[] smoo = ERB.smooth(muff, 38); // Pull out the freq response at ERB centers FilterProfile lfg = ERB.profile(smoo, sampleRate); // Write this response as a 'target' file /* FileStream fs = new FileStream("target_full.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.ASCII); foreach (FreqGain fg in lfg) { sw.WriteLine("{0} {1:f4}", Math.Round(fg.Freq), fg.Gain); } sw.Close(); */ /* fs = new FileStream("target_half.txt", FileMode.Create); sw = new StreamWriter(fs, Encoding.ASCII); foreach (FreqGain fg in lfg) { sw.WriteLine("{0} {1:f4}", Math.Round(fg.Freq), fg.Gain/2); } sw.Close(); */ // Create a filter to invert this response FilterProfile ifg = new FilterProfile(); foreach (FreqGain fg in lfg) { ifg.Add(new FreqGain(fg.Freq, -fg.Gain)); } ISoundObj filterImpulse = new FilterImpulse(0, ifg, FilterInterpolation.COSINE, sampleRate); filterImpulse.SampleRate = sampleRate; // Write the filter impulse to disk string sNoCorr = outFile + ".wav"; wri = new WaveWriter(sNoCorr); wri.Input = filterImpulse; // invertor; wri.Format = WaveFormat.IEEE_FLOAT; wri.BitsPerSample = 32; wri.SampleRate = _sampleRate; wri.Normalization = -1; wri.Run(); wri.Close(); _filterFiles.Add(sNoCorr); /* // Convolve noise with the NoCorrection filter noise = new NoiseGenerator(NoiseType.WHITE_FLAT, 2, (int)131072, stereoImpulse.SampleRate, 1.0); conv = new FastConvolver(); conv.impulse = invertor; conv.Input = noise; wri = new WaveWriter("NoCorrection_Test.wav"); wri.Input = conv; wri.Format = WaveFormat.IEEE_FLOAT; wri.BitsPerSample = 32; wri.SampleRate = _sampleRate; wri.Normalization = 0; wri.Run(); wri.Close(); wri = null; conv = null; */ // Convolve this with the in-room impulse response WaveReader rea = new WaveReader(outFile + ".wav"); conv = new FastConvolver(); conv.impulse = rea; conv.Input = new WaveReader(stereoImpulseFile); wri = new WaveWriter(outFile + "_TestConvolution.wav"); wri.Input = conv; wri.Format = WaveFormat.PCM; wri.Dither = DitherType.TRIANGULAR; wri.BitsPerSample = 16; wri.SampleRate = _sampleRate; wri.Normalization = -1; wri.Run(); wri.Close(); rea.Close(); wri = null; conv = null; }
static SoundObj GetEQImpulse(ISoundObj mainImpulse, uint sampleRate, out string filterName) { DateTime dtStart = DateTime.Now; SoundObj filterImpulse = null; // Construct a string describing the filter string filterDescription = "EQ" + _eqBands + "_" + _inputSampleRate + "_" + _eqLoudness + "_" + FlatnessFilterPath(_impulsePath, sampleRate, _eqFlatness); bool nothingToDo = (_eqLoudness==0); nothingToDo &= (_impulsePath == null) || (_eqFlatness == 100); List<string> fgd = new List<string>(); foreach (FreqGain fg in _eqValues) { fgd.Add(fg.Freq + "@" + fg.Gain); nothingToDo &= (fg.Gain == 0); } filterDescription = filterDescription + String.Join("_", fgd.ToArray()); filterDescription = filterDescription + "_IM_" + _impulsePath; // Cached filters are named by hash of this string filterName = "EQ" + filterDescription.GetHashCode().ToString("x10").ToUpperInvariant(); if (nothingToDo) { Trace.WriteLine("EQ flat"); WriteJSON(_eqValues, null, null); return null; } else { Trace.WriteLine(filterName); } string filterFile = Path.Combine(_tempFolder, filterName + ".filter"); // Does the cached filter exist? if (File.Exists(filterFile)) { try { // Just read the cached EQ filter from disk filterImpulse = new WaveReader(filterFile); } catch (Exception e) { if (_debug) { Trace.WriteLine("GetEQImpulse1: " + e.Message); } } } if(filterImpulse==null) { // Construct a filter impulse from the list of EQ values SoundObj eqFilter = new FilterImpulse(0, _eqValues, FilterInterpolation.COSINE, _inputSampleRate); filterImpulse = eqFilter; ISoundObj qtFilter = GetQuietnessFilter(_inputSampleRate, _eqLoudness); if (qtFilter != null) { // Convolve the two, to create a EQ-and-loudness filter FastConvolver tmpConvolver = new FastConvolver(); tmpConvolver.partitions = 0; tmpConvolver.impulse = qtFilter; tmpConvolver.Input = eqFilter; filterImpulse = tmpConvolver; } ISoundObj ftFilter = GetFlatnessFilter(_impulsePath, mainImpulse, _eqFlatness); if (ftFilter != null) { // Convolve the two, to create a EQ-and-loudness filter FastConvolver tmpConvolver2 = new FastConvolver(); tmpConvolver2.partitions = 0; tmpConvolver2.impulse = filterImpulse; tmpConvolver2.Input = ftFilter; filterImpulse = tmpConvolver2; } // Blackman window to make the filter smaller? try { // Write the filter impulse to disk WaveWriter wri = new WaveWriter(filterFile); wri.Input = filterImpulse; wri.Format = WaveFormat.IEEE_FLOAT; wri.BitsPerSample = 64; wri.Run(); wri.Close(); if (_debug) { // DEBUG: Write the filter impulse as wav16 wri = new WaveWriter(filterFile + ".wav"); wri.Input = filterImpulse; wri.Format = WaveFormat.PCM; wri.BitsPerSample = 16; wri.Normalization = -1.0; wri.Dither = DitherType.NONE;//.TRIANGULAR; wri.Run(); wri.Close(); } // Write a JSON description of the filter WriteJSON(_eqValues, filterImpulse, filterName); } catch (Exception e) { if (_debug) { Trace.WriteLine("GetEQImpulse2: " + e.Message); } } } filterImpulse.Reset(); if (_debug) { TimeSpan ts = DateTime.Now.Subtract(dtStart); Trace.WriteLine("GetEQImpulse " + ts.TotalMilliseconds); } // Copy the filter's JSON description (if available) into "current.json" CopyJSON(filterName); return filterImpulse; }
static ISoundObj DecodeBFormatUHJ(ISoundObj source) { ISoundObj input = source; uint sr = input.SampleRate; /* if (_ambiUseShelf) { // Shelf-filters // boost W at high frequencies, and boost X, Y at low frequencies FilterProfile lfgXY = new FilterProfile(); lfgXY.Add(new FreqGain(_ambiShelfFreq / 2, 0)); lfgXY.Add(new FreqGain(_ambiShelfFreq * 2, -1.25)); FilterImpulse fiXY = new FilterImpulse(0, lfgXY, FilterInterpolation.COSINE, sr); FilterProfile lfgW = new FilterProfile(); lfgW.Add(new FreqGain(_ambiShelfFreq / 2, 0)); lfgW.Add(new FreqGain(_ambiShelfFreq * 2, 1.76)); FilterImpulse fiW = new FilterImpulse(0, lfgW, FilterInterpolation.COSINE, sr); } if (_ambiUseDistance) { // Distance compensation filters // apply phase shift to X, Y at (very) low frequencies double fc = MathUtil.FcFromMetres(_ambiDistance); IIR1 discomp = new IIR1LP(sr, fc, 8192); // tbd: chain this } */ // Transformation filters // // Primary reference: // Gerzon 1985 "Ambisonics in Multichannel Broadcasting and Video" // // Coefficients from: http://en.wikipedia.org/wiki/Ambisonic_UHJ_format: // S = 0.9396926*W + 0.1855740*X // D = j(-0.3420201*W + 0.5098604*X) + 0.6554516*Y // Left = (S + D)/2.0 // Right = (S - D)/2.0 // which makes // Left = (0.092787 + 0.2549302j)X + (0.4698463 - 0.17101005j)W + (0.3277258)Y // Right= (0.092787 - 0.2549302j)X + (0.4698463 + 0.17101005j)W - (0.3277258)Y // // Coefficients from: http://www.york.ac.uk/inst/mustech/3d_audio/ambis2.htm // Left = (0.0928 + 0.255j)X + (0.4699 - 0.171j)W + (0.3277)Y // Right= (0.0928 - 0.255j)X + (0.4699 + 0.171j)W - (0.3277)Y // The Mid-Side versions are simpler // L+R = (0.0928 + 0.255j)X + (0.4699 - 0.171j)W + (0.3277)Y + ((0.0928 - 0.255j)X + (0.4699 + 0.171j)W - (0.3277)Y) // = (0.1856)X + (0.9398)W // L-R = (0.0928 + 0.255j)X + (0.4699 - 0.171j)W + (0.3277)Y - ((0.0928 - 0.255j)X + (0.4699 + 0.171j)W - (0.3277)Y) // = (0.510j)X + (0.342j)W + (0.6554)Y // but since we're delaying signal via convolution anyway, not *too* much extra processing to do in LR mode... // Separate the WXY channels ISoundObj channelW = new SingleChannel(input, 0); ISoundObj channelX = new SingleChannel(input, 1, true); ISoundObj channelY = new SingleChannel(input, 2, true); // Z not used; height is discarded in UHJ conversion. // Don't assume it's there; horizontal-only .AMB files won't have a fourth channel // ISoundObj channelZ = new SingleChannel(input, 3); // Phase shift j is implemented with Hilbert transforms // so let's load up some filters, multiply by the appropriate coefficients. int len = 8191; PhaseMultiplier xl = new PhaseMultiplier(new Complex(0.0927870, 0.25493020), len, sr); PhaseMultiplier wl = new PhaseMultiplier(new Complex(0.4698463, -0.17101005), len, sr); PhaseMultiplier yl = new PhaseMultiplier(new Complex(0.3277258, 0.00000000), len, sr); PhaseMultiplier xr = new PhaseMultiplier(new Complex(0.0927870, -0.25493020), len, sr); PhaseMultiplier wr = new PhaseMultiplier(new Complex(0.4698463, 0.17101005), len, sr); PhaseMultiplier yr = new PhaseMultiplier(new Complex(-0.3277258, 0.00000000), len, sr); // The convolvers to filter FastConvolver cwl = new FastConvolver(channelW, wl); FastConvolver cxl = new FastConvolver(channelX, xl); FastConvolver cyl = new FastConvolver(channelY, yl); FastConvolver cwr = new FastConvolver(channelW, wr); FastConvolver cxr = new FastConvolver(channelX, xr); FastConvolver cyr = new FastConvolver(channelY, yr); // Sum to get the final output of these things: Mixer mixerL = new Mixer(); mixerL.Add(cwl, 1.0); mixerL.Add(cxl, 1.0); mixerL.Add(cyl, 1.0); Mixer mixerR = new Mixer(); mixerR.Add(cwr, 1.0); mixerR.Add(cxr, 1.0); mixerR.Add(cyr, 1.0); // output in stereo ChannelSplicer uhj = new ChannelSplicer(); uhj.Add(mixerL); uhj.Add(mixerR); return uhj; }
static ISoundObj DecodeBFormatBinaural(ISoundObj source) { throw new NotImplementedException(); ISoundObj input = source; uint sr = input.SampleRate; // Convolve the BFormat data with the matrix filter if (!String.IsNullOrEmpty(_bformatFilter)) { string ignore; WaveReader rdr = GetAppropriateImpulseReader(_bformatFilter, out ignore); FastConvolver ambiConvolver = new FastConvolver(source, rdr); input = ambiConvolver; } // Cardioid directed at four (or six) virtual loudspeakers IEnumerator<ISample> src = input.Samples; CallbackSource bin = new CallbackSource(2, sr, delegate(long j) { if (src.MoveNext()) { ISample s = src.Current; double w = s[0]; double x = s[1]; double y = s[2]; double z = s[3]; double wFactor = -0.5; double left = x + y + z + (wFactor * w); double right = x - y + z + (wFactor * w); ISample sample = new Sample2(left, right); return sample; } return null; }); return bin; }
static void LoadImpulse() { DateTime dtStart = DateTime.Now; string theImpulsePath = null; string theEQImpulseName = null; ISoundObj main = GetMainImpulse(out theImpulsePath); uint sr = (main == null ? _inputSampleRate : main.SampleRate); if (sr == 0) { if (_debug) { Trace.WriteLine("oops: no sample rate!"); } sr = 44100; } ISoundObj eq = GetEQImpulse(main, sr, out theEQImpulseName); ISoundObj combinedFilter = null; if (main == null && eq != null) { combinedFilter = eq; } else if (main != null && eq == null) { combinedFilter = main; } else if (main != null && eq != null) { // Check whether we have (and can load) a cached version of the combined filter string tempString = theEQImpulseName + "_" + theImpulsePath; string filterName = "CC" + tempString.GetHashCode().ToString("x10").ToUpperInvariant(); string filterFile = Path.Combine(_tempFolder, filterName + ".filter"); if (_debug) { Trace.WriteLine(filterName); } if (File.Exists(filterFile)) { try { // Just read the cached EQ filter from disk combinedFilter = new WaveReader(filterFile); } catch (Exception e) { if (_debug) { Trace.WriteLine("LoadImpulse1: " + e.Message); } } } if (combinedFilter == null) { // Convolve the room-correction impulse with the EQ impulse to make just one. // (this is quite slow) FastConvolver temp = new FastConvolver(); temp.partitions = 0; temp.impulse = eq; temp.Input = main; combinedFilter = temp; try { // Write the combined impulse temp.Reset(); WaveWriter tempWriter = new WaveWriter(filterFile); tempWriter.Format = WaveFormat.IEEE_FLOAT; tempWriter.BitsPerSample = 64; tempWriter.Input = temp; tempWriter.Run(); tempWriter.Close(); if (_debug) { // DEBUG: Write the combined impulse as WAV16 temp.Reset(); tempWriter = new WaveWriter(filterFile + ".wav"); tempWriter.Format = WaveFormat.PCM; tempWriter.BitsPerSample = 16; tempWriter.Gain = 0.1; tempWriter.Dither = DitherType.NONE;//.TRIANGULAR; tempWriter.Input = temp; tempWriter.Run(); tempWriter.Close(); } } catch (Exception e) { if (_debug) { Trace.WriteLine("LoadImpulse2: " + e.Message); } } } } _MainConvolver.impulse = combinedFilter; if (combinedFilter != null) { // Calculate loudness-adjusted volume of each channel of the impulse _impulseVolumes.Clear(); for (ushort j = 0; j < combinedFilter.NumChannels; j++) { double v = Loudness.WeightedVolume(combinedFilter.Channel(j)); _impulseVolumes.Add(v); if (_debug) { Trace.WriteLine("WV{0}: {1}", j, v); } } } combinedFilter = null; if (_debug) { TimeSpan ts = DateTime.Now.Subtract(dtStart); Trace.WriteLine("Loadmpulse " + ts.TotalMilliseconds); } }