module Effects where import Data.List import Types addEffects :: [DSPEffect] -> SampledSignal -> SampledSignal addEffects = foldr (.) id applyEffectToInterval :: (Seconds, Seconds) -> SampledSignal -> DSPEffect -> SampledSignal applyEffectToInterval (from, to) signal effect -- assert that from >= 0, to - from >= 0, to <= length signal and simply return original signal if constraints aren't met | from < 0 || from > to || samplesPerSecond to > length signal = signal | otherwise = samplesBefore ++ samplesDuring ++ samplesAfter where samplesBefore = take nSamplesBefore signal -- unaffected samplesDuring = effect $ take nSamplesDuring signal -- effected samplesAfter = drop (nSamplesBefore + nSamplesDuring) signal -- unaffected nSamplesBefore = samplesPerSecond from nSamplesDuring = samplesPerSecond (to - from) gain :: Double gain = 2.0 clippingThreshold :: Double clippingThreshold = 0.8 clip :: Double -> DSPEffect clip threshold = map (\sample -> if sample > 0 then min threshold sample else max (negate threshold) sample) -- clip positive and negative samples addGain :: Double -> DSPEffect addGain value = map (* value) distortion :: DSPEffect distortion = clip clippingThreshold . addGain gain -- add more effects here addActualGain :: Double -> DSPEffect addActualGain vol [] = [] addActualGain vol (s:ss) = (s*vol/fromIntegral(length ss)) : addActualGain vol ss tremolo :: (Double, Double) -> DSPEffect tremolo (min,max) [] = [] tremolo (min,max) (s:ss) = (s*min) : tremolo (max,min) ss slowTremolo :: (Double, Double) -> Int -> Int -> DSPEffect slowTremolo (min,max) rep i [] = [] slowTremolo (min,max) rep 0 (s:ss) = (s*min) : slowTremolo (max,min) rep rep ss slowTremolo (min,max) rep i (s:ss) = (s*min) : slowTremolo (min,max) rep (i-1) ss bitCrunchSpeed :: DSPEffect bitCrunchSpeed [] = [] bitCrunchSpeed (x:y:xs) = x:bitCrunchSpeed xs bitCrunchSpeed (x:xs) = x:bitCrunchSpeed xs bitCrunch :: DSPEffect bitCrunch [] = [] bitCrunch (x:_:xs) = x:x:bitCrunch xs bitCrunch (x:xs) = x:bitCrunch xs wreck :: Double -> Double -> Double -> DSPEffect wreck i add factor [] = [] wreck i add factor (x:xs) = (x * (1 + factor * sin i)) : wreck (i+add) add factor xs