static SpeexResampler() { var resolver = new DynamicLibraryImportResolver( OSTailoredCode.IsUnixHost ? "libspeexdsp.so.1" : "libspeexdsp.dll", hasLimitedLifetime: false); NativeDSP = BizInvoker.GetInvoker <LibSpeexDSP>(resolver, CallingConventionAdapters.Native); }
public void Dispose() { if (st != IntPtr.Zero) { LibSpeexDSP.speex_resampler_destroy(st); st = IntPtr.Zero; GC.SuppressFinalize(this); } }
/// <summary>flush as many input samples as possible, generating output samples right now</summary> /// <exception cref="Exception">unmanaged call failed</exception> public void Flush() { uint inal = (uint)_inbufpos / 2; uint outal = (uint)_outbuf.Length / 2; LibSpeexDSP.speex_resampler_process_interleaved_int(_st, _inbuf, ref inal, _outbuf, ref outal); // reset inbuf if (inal != _inbufpos / 2) { throw new Exception("Speexresampler didn't eat the whole array?"); } _inbufpos = 0; _drainer(_outbuf, (int)outal); }
/// <summary> /// flush as many input samples as possible, generating output samples right now /// </summary> public void Flush() { uint inal = (uint)inbufpos / 2; uint outal = (uint)outbuf.Length / 2; LibSpeexDSP.speex_resampler_process_interleaved_int(st, inbuf, ref inal, outbuf, ref outal); // reset inbuf if (inal != inbufpos / 2) { throw new Exception("Speexresampler didn't eat the whole array?"); } inbufpos = 0; //Buffer.BlockCopy(inbuf, (int)inal * 2 * sizeof(short), inbuf, 0, inbufpos - (int)inal * 2); //inbufpos -= (int)inal * 2; // dispatch outbuf drainer(outbuf, (int)outal); }
/// <summary> /// /// </summary> /// <param name="quality">0 to 10</param> /// <param name="rationum">numerator of srate change ratio (inrate / outrate)</param> /// <param name="ratioden">demonenator of srate change ratio (inrate / outrate)</param> /// <param name="sratein">sampling rate in, rounded to nearest hz</param> /// <param name="srateout">sampling rate out, rounded to nearest hz</param> /// <param name="drainer">function which accepts output as produced. if null, act as an ISyncSoundProvider</param> /// <param name="input">source to take input from when output is requested. if null, no autofetching</param> public SpeexResampler(int quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action <short[], int> drainer = null, ISoundProvider input = null) { if (drainer != null && input != null) { throw new ArgumentException("Can't autofetch without being an ISyncSoundProvider?"); } LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS; st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err); if (st == IntPtr.Zero) { throw new Exception("LibSpeexDSP returned null!"); } CheckError(err); this.drainer = drainer ?? InternalDrain; this.input = input; outbuf = new short[inbuf.Length * ratioden / rationum / 2 * 2 + 128]; }
/// <summary> /// Initializes a new instance of the <see cref="SpeexResampler"/> class /// </summary> /// <param name="quality">0 to 10</param> /// <param name="rationum">numerator of sample rate change ratio (inrate / outrate)</param> /// <param name="ratioden">denominator of sample rate change ratio (inrate / outrate)</param> /// <param name="sratein">sampling rate in, rounded to nearest hz</param> /// <param name="srateout">sampling rate out, rounded to nearest hz</param> /// <param name="drainer">function which accepts output as produced. if null, act as an <seealso cref="ISoundProvider"/></param> /// <param name="input">source to take input from when output is requested. if null, no auto-fetching</param> public SpeexResampler(Quality quality, uint rationum, uint ratioden, uint sratein, uint srateout, Action <short[], int> drainer = null, ISoundProvider input = null) { if (drainer != null && input != null) { throw new ArgumentException($"Can't autofetch without being an {nameof(ISoundProvider)}?"); } LibSpeexDSP.RESAMPLER_ERR err = LibSpeexDSP.RESAMPLER_ERR.SUCCESS; _st = LibSpeexDSP.speex_resampler_init_frac(2, rationum, ratioden, sratein, srateout, quality, ref err); if (_st == IntPtr.Zero) { throw new Exception($"{nameof(LibSpeexDSP)} returned null!"); } CheckError(err); _drainer = drainer ?? InternalDrain; _input = input; _outbuf = new short[(_inbuf.Length * ratioden / rationum / 2 * 2) + 128]; }
/// <summary>change sampling rate on the fly</summary> /// <param name="rationum">numerator of srate change ratio (inrate / outrate)</param> /// <param name="ratioden">demonenator of srate change ratio (inrate / outrate)</param> /// <param name="sratein">sampling rate in, rounded to nearest hz</param> /// <param name="srateout">sampling rate out, rounded to nearest hz</param> public void ChangeRate(uint rationum, uint ratioden, uint sratein, uint srateout) { CheckError(LibSpeexDSP.speex_resampler_set_rate_frac(st, rationum, ratioden, sratein, srateout)); outbuf = new short[inbuf.Length * ratioden / rationum / 2 * 2 + 128]; }
public void Dispose() { LibSpeexDSP.speex_resampler_destroy(st); st = IntPtr.Zero; }