# Path: lib/color_fade.py
# class ColorFade(object):
#     """Represents the fade of one color to another"""
# 
#     def __init__(self, keyframes, steps):
#         """
#         keyframes: a list of 3-element tuples representing the colors to fade between.
#         """
# 
#         self._steps = steps
#         self.keyframes = keyframes
#         self.color_cache = np.zeros(steps + 1, dtype=dtypes.pixel_color)
# 
#         # Warmup the cache
#         for i in range(steps + 1):
#             overall_progress = float(i) * (len(self.keyframes) - 1) / self._steps
#             stage = int(overall_progress)
#             stage_progress = overall_progress - stage # 0 to 1 float
# 
#             # special case stage_progress=0, so if progress=1, we don't get
#             # an IndexError
#             if stage_progress == 0:
#                 color = self.keyframes[stage]
#             else:
#                 frame1 = self.keyframes[stage]
#                 frame1_weight = 1 - stage_progress
# 
#                 frame2 = self.keyframes[stage + 1]
#                 frame2_weight = stage_progress
# 
#                 color = tuple([c1 * frame1_weight + c2 * frame2_weight for c1, c2 in zip(frame1, frame2)])
#             self.color_cache[i] = tuple(color)
# #            print progress, self.color_cache[progress]
# 
#     def get_color(self, progress):
#         """
#         Given a progress value between 0 and steps, returns the color for that
#         progress as a (h, l, s) tuple with float values
#         """
# 
#         progress = clip(0, int(progress), self._steps)
# 
#         return self.color_cache[progress]


import logging
import numpy as np
from scipy import signal
from lib.color_fade import ColorFade
from PyQt5 import QtCore

log = logging.getLogger("firemix.core.mixer")

#curve = [0.1, 0.2, 0.3, 0.6, 0.9, 1.0, 1.0, 1.0, 0.9, 0.6, 0.3, 0.2, 0.1]

class Audio(QtCore.QObject):
    """
    Audio handles looking at sound data and setting it up for use in patterns
    """
    transition_starting = QtCore.pyqtSignal()
    onset = QtCore.pyqtSignal()

    _fader_steps = 256
    SIM_BEATS_PER_MINUTE = 120.0
    SIM_AUTO_ENABLE_DELAY = 3000

    """
    todo zero these at start
    """
    def __init__(self, mixer):
        super(Audio, self).__init__()

        self.mixer = mixer
        self.fft = [[0]]
        self.smoothed = []
        self.average = [[0]]
        self.peak = [[0]]
        self.peakFrequency = [[0]]
        self.gain = 1.0
        self.maxGain = 5.0

# Path: lib/color_fade.py
# class ColorFade(object):
#     """Represents the fade of one color to another"""
# 
#     def __init__(self, keyframes, steps):
#         """
#         keyframes: a list of 3-element tuples representing the colors to fade between.
#         """
# 
#         self._steps = steps
#         self.keyframes = keyframes
#         self.color_cache = np.zeros(steps + 1, dtype=dtypes.pixel_color)
# 
#         # Warmup the cache
#         for i in range(steps + 1):
#             overall_progress = float(i) * (len(self.keyframes) - 1) / self._steps
#             stage = int(overall_progress)
#             stage_progress = overall_progress - stage # 0 to 1 float
# 
#             # special case stage_progress=0, so if progress=1, we don't get
#             # an IndexError
#             if stage_progress == 0:
#                 color = self.keyframes[stage]
#             else:
#                 frame1 = self.keyframes[stage]
#                 frame1_weight = 1 - stage_progress
# 
#                 frame2 = self.keyframes[stage + 1]
#                 frame2_weight = stage_progress
# 
#                 color = tuple([c1 * frame1_weight + c2 * frame2_weight for c1, c2 in zip(frame1, frame2)])
#             self.color_cache[i] = tuple(color)
# #            print progress, self.color_cache[progress]
# 
#     def get_color(self, progress):
#         """
#         Given a progress value between 0 and steps, returns the color for that
#         progress as a (h, l, s) tuple with float values
#         """
# 
#         progress = clip(0, int(progress), self._steps)
# 
#         return self.color_cache[progress]


# Path: core/audio.py
import logging
import numpy as np
from scipy import signal
from lib.color_fade import ColorFade
from PyQt5 import QtCore


log = logging.getLogger("firemix.core.mixer")

#curve = [0.1, 0.2, 0.3, 0.6, 0.9, 1.0, 1.0, 1.0, 0.9, 0.6, 0.3, 0.2, 0.1]

class Audio(QtCore.QObject):
    """
    Audio handles looking at sound data and setting it up for use in patterns
    """
    transition_starting = QtCore.pyqtSignal()
    onset = QtCore.pyqtSignal()

    _fader_steps = 256
    SIM_BEATS_PER_MINUTE = 120.0
    SIM_AUTO_ENABLE_DELAY = 3000

    """
    todo zero these at start
    """
    def __init__(self, mixer):
        super(Audio, self).__init__()

        self.mixer = mixer
        self.fft = [[0]]
        self.smoothed = []
        self.average = [[0]]
        self.peak = [[0]]
        self.peakFrequency = [[0]]
        self.gain = 1.0
        self.maxGain = 5.0

        self.fader = ColorFade([(0,0,1), (0,1,1)], self._fader_steps)