# 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
    # http://www.dspdesignline.com/showArticle.jhtml?articleID=199901970
    Nfft = 2 ** nextpow2(len(h))
    H = numpy.fft.fft(h, Nfft)
    lslice = Nfft - len(h) + 1 # because lslice + len(h) - 1 == Nfft
    numslices = int(numpy.ceil(float(len(x)) / lslice))
    y = numpy.zeros(numslices * lslice + len(h) - 1)
    start = 0
    while start < len(x):
        slice = x[start:start + lslice]
        X = numpy.fft.fft(slice, Nfft)
        Y = X * H
        y[start:start+Nfft] += numpy.real(numpy.fft.ifft(Y, Nfft))
        start = start + lslice
    return y


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:

# 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

    # http://www.dspdesignline.com/showArticle.jhtml?articleID=199901970
    Nfft = 2 ** nextpow2(len(h))
    H = numpy.fft.fft(h, Nfft)
    lslice = Nfft - len(h) + 1 # because lslice + len(h) - 1 == Nfft
    numslices = int(numpy.ceil(float(len(x)) / lslice))
    y = numpy.zeros(numslices * lslice + len(h) - 1)
    start = 0
    while start < len(x):
        slice = x[start:start + lslice]
        X = numpy.fft.fft(slice, Nfft)
        Y = X * H
        y[start:start+Nfft] += numpy.real(numpy.fft.ifft(Y, Nfft))
        start = start + lslice
    return y


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)