"""
Authors: Peter Mawhorter
Consulted:
Date: 2022-2-25
Purpose: Applies audio effects from audioToolkit.py to provided .wav
  files. You need to uncomment the function calls in applyEffect that
  you want to run.
"""

import os
import waveTools
import audioToolkit


def applyEffects():
    """
    Applies effects to different files in the input sounds directory,
    writing output files to the same directory and playing the output files
    (if the simpleaudio package is installed).

    Uncomment the specific effects you want to apply.
    """

    # To test makeSofter, first apply "noEffect" (which does not change the
    # original file) and compare it with the result of "makeSofter"

    applyEffect("poker.wav", "noEffect")
    applyEffect("poker.wav", "makeSofter")

    # applyEffect("hello.wav", "noEffect")
    # applyEffect("hello.wav", "makeSofter")

    # applyEffect("prayer.wav", "noEffect")
    # applyEffect("prayer.wav", "makeSofter")

    # applyEffect("distance.wav", "noEffect")
    # applyEffect("distancea.wav", "makeSofter")

    # applyEffect("poker.wav", "chipmunk")
    # applyEffect("hello.wav", "chipmunk")
    # applyEffect("prayer.wav", "chipmunk")
    # applyEffect("distance.wav", "chipmunk")

    # applyEffect("distance.wav", "removeVocals") # works OK
    # applyEffect("hello.wav", "removeVocals") # works meh
    # applyEffect("poker.wav", "removeVocals") # does not work well
    # applyEffect("prayer.wav", "removeVocals") # does not work well

    # applyEffect("poker.wav", "reverse")
    # applyEffect("hello.wav", "reverse")
    # applyEffect("prayer.wav", "reverse")
    # applyEffect("distance.wav", "reverse")

    # applyEffect("poker.wav", "twoSampleDelay")
    # applyEffect("hello.wav", "twoSampleDelay")
    # applyEffect("prayer.wav", "twoSampleDelay")
    # applyEffect("distance.wav", "twoSampleDelay")

    # applyEffect("poker.wav", "ohYeah")
    # applyEffect("hello.wav", "ohYeah")
    # applyEffect("prayer.wav", "ohYeah")
    # applyEffect("distance.wav", "ohYeah")

    # applyEffect("poker.wav", "crescendo")
    # applyEffect("hello.wav", "crescendo")
    # applyEffect("prayer.wav", "crescendo")
    # applyEffect("distance.wav", "crescendo")

    # applyEffect("poker.wav", "custom")
    # applyEffect("hello.wav", "custom")
    # applyEffect("prayer.wav", "custom")
    # applyEffect("distance.wav", "custom")


#---------------------------------#
# Testing support (DO NOT CHANGE) #
#---------------------------------#

def applyEffect(wavFile, effectName):
    """
    Applies the effect with the given name to the given wavFile
    in the sounds directory and writes the resulting processed file to
    a file with _proc in the name. E.g., "poker.wav" will be
    written to "poker_proc.wav". If the simpleaudio package
    is installed, the resulting processed file will be played.
    """
    channelMode = "mono"
    lengthFunction = waveTools.sameLength
    reverseOrder = False
    localizer = None

    def noEffect(channel):
        return channel[:] # return a copy of the channel

    if effectName == "noEffect":
        effect = noEffect
    elif effectName == "makeSofter":
        effect = audioToolkit.makeSofter
    elif effectName == "removeVocals":
        effect = audioToolkit.removeVocals
        channelMode = "stereo"
    elif effectName == "chipmunk":
        effect = audioToolkit.chipmunk
        lengthFunction = waveTools.halfLength
    elif effectName == "reverse":
        effect = audioToolkit.reverse
        reverseOrder = True
    elif effectName == "ohYeah":
        effect = audioToolkit.ohYeah
        lengthFunction = waveTools.doubleLength
    elif effectName == "twoSampleDelay":
        effect = audioToolkit.twoSampleDelay
    elif effectName == "crescendo":
        effect = audioToolkit.crescendo
        localizer = waveTools.progressFractionLocalizer
    elif effect == "custom":
        lengthFunction = None
    else:
        raise ValueError(
            "You have entered the name of an effect that is invalid. "
          + "Please check your spelling."
        )

    soundsdir = os.path.join(os.getcwd(), "sounds")
    sourcePath = os.path.join(soundsdir, wavFile)
    (base, ext) = os.path.splitext(wavFile)
    # Convert a filename like "poker.wav" into "poker_proc.wav"
    destFile = base + "_proc" + ext
    destPath = os.path.join(soundsdir, destFile)

    # Actually do the work
    print('{}\nApplying the effect {} to file {} ...\n'
          .format('-' * 50, effectName, wavFile))
    waveTools.processFile(
        sourcePath,
        destPath,
        effect,
        channelMode,
        lengthFunction,
        reverseOrder,
        localizer
    )


# If you run this file, actually apply the selected effect(s):
if __name__ == "__main__":
    applyEffects()
