private void ThrowExceptionForError(int error) { if (error != 0) { throw new Exception(InteropWrapper.src_strerror(error)); } }
/// <summary> /// Sets the resampling ratio. Multiplying the input rate with the ratio factor results in the output rate. /// </summary> /// <param name="ratio">the resampling ratio</param> /// <param name="step">true for an instant change in the ratio, false for a gradual linear change during the next #Process call</param> public void SetRatio(double ratio, bool step) { if (step) { // force the ratio for the next #Process call instead of linearly interpolating from the previous // ratio to the current ratio error = InteropWrapper.src_set_ratio(srcState, ratio); ThrowExceptionForError(error); } this.ratio = ratio; }
/// <summary> /// Creates a new resampler instance with the supplied converter type (which equals the resampling quality) /// for a supplied number of channels. /// </summary> /// <param name="type">the type of the internal conversion algorithm (quality level)</param> /// <param name="channels">the number of channels that will be provided to the processing method</param> public SampleRateConverter(ConverterType type, int channels) { srcState = InteropWrapper.src_new(type, channels, out error); ThrowExceptionForError(error); srcData = new SRC_DATA(); SetRatio(1d); this.channels = channels; this.bufferedSamples = 0; }
/// <summary> /// Disposes this instance of the resampler, freeing its memory. /// </summary> public void Dispose() { if (srcState != IntPtr.Zero) { srcState = InteropWrapper.src_delete(srcState); if (srcState != IntPtr.Zero) { throw new Exception("could not delete the sample rate converter"); } } }
private unsafe void Process(float *input, int inputLength, float *output, int outputLength, bool endOfInput, out int inputLengthUsed, out int outputLengthGenerated) { srcData.data_in = input; srcData.data_out = output; srcData.end_of_input = endOfInput ? 1 : 0; srcData.input_frames = inputLength / channels; srcData.output_frames = outputLength / channels; srcData.src_ratio = ratio; error = InteropWrapper.src_process(srcState, ref srcData); ThrowExceptionForError(error); inputLengthUsed = srcData.input_frames_used * channels; outputLengthGenerated = srcData.output_frames_gen * channels; bufferedSamples += inputLengthUsed - (outputLengthGenerated / ratio); }
/// <summary> /// Resets the resampler, which essentially clears the internal buffer. /// </summary> public void Reset() { error = InteropWrapper.src_reset(srcState); ThrowExceptionForError(error); bufferedSamples = 0; }
/// <summary> /// Checks if a given resampling ratio is valid. /// </summary> /// <param name="ratio">true if the ratio is valid, else false</param> /// <returns></returns> public static bool CheckRatio(double ratio) { return(InteropWrapper.src_is_valid_ratio(ratio) == 1); }