# Path: gum/lib/edit.py
# def resample(frames, ratio):
# def mix_channels(frames, gain_lists):
# def mix_channels_auto(frames, n):
# 
# Path: gum/controllers/effect.py
# def reverse(x):
# def normalize(x):
# def negate(x):
# def fade(x, type='in'):
# def fade_out(x):
# def mkfx_overwrite_selection(function):
#     def process(sound, start, end):
#         M = abs(x).max()
#     N = len(x)
# 
# Path: gum/models/clipboard.py

# 
# Path: gum/models/sound.py
# def list_extensions():
#     def __init__(self, filename=None):
#     def numchan(self):
#     def save(self):
#     def save_as(self, filename):
#     def cut(self, start, end):
#     def _do_cut(self, start, end):
#     def copy(self, start, end):
#     def paste(self, start, end, clip):
#     def _do_paste(self, start, end, clip):
#     def mix(self, start, end, clip):
#     def _do_mix(self, start, end, clip):
#     def undo(self):
#     def redo(self):
#     def is_empty(self):
#     def is_fresh(self):
#     def is_saved(self):
# def testSound():
#     def fake_sndfile(filename):
# class Sound(object):
#     SR = 44100


from gum.lib import edit
from gum.controllers import effect
from gum.models import clipboard, sound
import numpy
def convolution(sound, start, end):

    x = sound.frames
    h = clipboard.clip

    numchan = max(x.ndim, h.ndim)
    if numchan == 1:
        y = ola_fftconvolve(x[start:end], h)
    else:
        y = []
        if h.ndim == 1:
            #! H will be computed two times !
            h = edit.mix_channels_auto(h, 2)
        if x.ndim == 1:
            x = edit.mix_channels_auto(x, 2)
        h = h.transpose()
        x = x.transpose()
        for nchan in range(2):
            tmp = ola_fftconvolve(x[nchan][start:end], h[nchan])
            y.append(tmp)
        y = numpy.array(y)
        y = y.transpose()

    # normalize
    if y.any():
        M = abs(y).max()
        factor = 1. / M
        y = y * factor

    l = len(sound.frames)

# Path: gum/lib/edit.py
# def resample(frames, ratio):
# def mix_channels(frames, gain_lists):
# def mix_channels_auto(frames, n):
# 
# Path: gum/controllers/effect.py
# def reverse(x):
# def normalize(x):
# def negate(x):
# def fade(x, type='in'):
# def fade_out(x):
# def mkfx_overwrite_selection(function):
#     def process(sound, start, end):
#         M = abs(x).max()
#     N = len(x)
# 
# Path: gum/models/clipboard.py

# 
# Path: gum/models/sound.py
# def list_extensions():
#     def __init__(self, filename=None):
#     def numchan(self):
#     def save(self):
#     def save_as(self, filename):
#     def cut(self, start, end):
#     def _do_cut(self, start, end):
#     def copy(self, start, end):
#     def paste(self, start, end, clip):
#     def _do_paste(self, start, end, clip):
#     def mix(self, start, end, clip):
#     def _do_mix(self, start, end, clip):
#     def undo(self):
#     def redo(self):
#     def is_empty(self):
#     def is_fresh(self):
#     def is_saved(self):
# def testSound():
#     def fake_sndfile(filename):
# class Sound(object):
#     SR = 44100


# Path: gum/fx/convolution.py
from gum.lib import edit
from gum.controllers import effect
from gum.models import clipboard, sound
import numpy

def convolution(sound, start, end):

    x = sound.frames
    h = clipboard.clip

    numchan = max(x.ndim, h.ndim)
    if numchan == 1:
        y = ola_fftconvolve(x[start:end], h)
    else:
        y = []
        if h.ndim == 1:
            #! H will be computed two times !
            h = edit.mix_channels_auto(h, 2)
        if x.ndim == 1:
            x = edit.mix_channels_auto(x, 2)
        h = h.transpose()
        x = x.transpose()
        for nchan in range(2):
            tmp = ola_fftconvolve(x[nchan][start:end], h[nchan])
            y.append(tmp)
        y = numpy.array(y)
        y = y.transpose()

    # normalize
    if y.any():
        M = abs(y).max()
        factor = 1. / M
        y = y * factor

    l = len(sound.frames)

    sound.paste(0, l, y)