From c926a690db78fb5317e09cc5c76f2e52b55fd8c8 Mon Sep 17 00:00:00 2001 From: libertyernie Date: Wed, 22 May 2024 12:49:26 -0500 Subject: [PATCH] (#96) Allow upconverting to a different sample rate --- LoopingAudioConverter.Conversion/Converter.cs | 2 +- LoopingAudioConverter.FFmpeg/FFmpegEngine.cs | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/LoopingAudioConverter.Conversion/Converter.cs b/LoopingAudioConverter.Conversion/Converter.cs index 395231c..f5b8f8e 100644 --- a/LoopingAudioConverter.Conversion/Converter.cs +++ b/LoopingAudioConverter.Conversion/Converter.cs @@ -200,7 +200,7 @@ public static async Task ConvertFileAsync( env.UpdateStatus(filename_no_ext, "Applying effects"); w = await effectEngine.ApplyEffectsAsync(w, channels: o.Channels ?? w.Channels, - rate: o.SampleRate ?? w.SampleRate, + rate: o.SampleRate, db: o.AmplifydB ?? 0M, amplitude: o.AmplifyRatio ?? 1M, pitch_semitones: o.PitchSemitones ?? 0, diff --git a/LoopingAudioConverter.FFmpeg/FFmpegEngine.cs b/LoopingAudioConverter.FFmpeg/FFmpegEngine.cs index c7c4729..853621a 100644 --- a/LoopingAudioConverter.FFmpeg/FFmpegEngine.cs +++ b/LoopingAudioConverter.FFmpeg/FFmpegEngine.cs @@ -139,12 +139,10 @@ IEnumerable getArgs() { /// Tempo ratio (if 1, this effect will not be applied) /// If true, always return a new PCM16Audio object /// A new PCM16Audio object if one or more effects are applied; the same PCM16Audio object if no effects are applied. - public async Task ApplyEffectsAsync(PCM16Audio lwav, int channels = int.MaxValue, decimal db = 0, decimal amplitude = 1, int rate = int.MaxValue, double pitch_semitones = 0, double tempo_ratio = 1, bool force = false) { - // This is where I wish I had F# list comprehensions - + public async Task ApplyEffectsAsync(PCM16Audio lwav, int? channels = null, decimal db = 0, decimal amplitude = 1, int? rate = null, double pitch_semitones = 0, double tempo_ratio = 1, bool force = false) { IEnumerable getParameters() { - if (channels != lwav.Channels) - yield return $"-ac {channels}"; + if (channels is int ch && ch != lwav.Channels) + yield return $"-ac {ch}"; IEnumerable getFilters() { if (db != 0) @@ -167,8 +165,8 @@ IEnumerable getFilters() { if (tempo != 1) { yield return $"atempo={tempo}"; } - if (newrate > rate) { - yield return $"aresample={rate}"; + if (rate is int rr && newrate != rr) { + yield return $"aresample={rr}"; } }