aboutsummaryrefslogtreecommitdiff
path: root/components/original.py
blob: d1caa7bee10ea0a4c79528f4a68623c6ffe5c43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
''' Original Audio Visualization '''
import numpy
from PIL import Image, ImageDraw
from PyQt4 import uic
import os, random


class Component:
    def widget(self,parent):
        self.parent = parent

        page = uic.loadUi(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'original.ui'))
        page.comboBox_visLayout.addItem("Classic")
        page.comboBox_visLayout.addItem("Split")
        page.comboBox_visLayout.addItem("Bottom")
        #visLayoutValue = int(self.settings.value('visLayout'))
        page.comboBox_visLayout.setCurrentIndex(0)
        page.comboBox_visLayout.currentIndexChanged.connect(self.update)

        return page
    def update(self):
        self.layout = self.page.comboBox_visLayout.currentIndex()
        print(self.layout)
        self.parent.drawPreview()

    def previewRender(self, previewWorker, widget):
        spectrum = numpy.fromfunction(lambda x: 0.008*(x-128)**2, (255,), dtype="int16")
        width = int(previewWorker.core.settings.value('outputWidth'))
        height = int(previewWorker.core.settings.value('outputHeight'))
        return drawBars(width, height, spectrum, (255, 255, 255), self.layout)
    
    def preFrameRender(self, **kwargs):
        for kwarg, value in kwargs.items():
            exec('self.%s = value' % kwarg)
        self.smoothConstantDown = 0.08
        self.smoothConstantUp = 0.8
        self.lastSpectrum = None
    
    def frameRender(self, moduleNo, frameNo):
        self.lastSpectrum = transformData(frameNo, self.completeAudioArray, self.sampleSize,
                    self.smoothConstantDown, self.smoothConstantUp, self.lastSpectrum)
        width = int(self.worker.core.settings.value('outputWidth'))
        height = int(self.worker.core.settings.value('outputHeight'))
        return drawBars(width, height, self.lastSpectrum, (255,255,255), self.layout)

def transformData(i, completeAudioArray, sampleSize, smoothConstantDown, smoothConstantUp, lastSpectrum):
    if len(completeAudioArray) < (i + sampleSize):
      sampleSize = len(completeAudioArray) - i
    numpy.seterr(divide='ignore')
    window = numpy.hanning(sampleSize)
    data = completeAudioArray[i:i+sampleSize][::1] * window
    paddedSampleSize = 2048
    paddedData = numpy.pad(data, (0, paddedSampleSize - sampleSize), 'constant')
    spectrum = numpy.fft.fft(paddedData)
    sample_rate = 44100
    frequencies = numpy.fft.fftfreq(len(spectrum), 1./sample_rate)

    y = abs(spectrum[0:int(paddedSampleSize/2) - 1])

    # filter the noise away
    # y[y<80] = 0

    y = 20 * numpy.log10(y)
    y[numpy.isinf(y)] = 0

    if lastSpectrum is not None:
      lastSpectrum[y < lastSpectrum] = y[y < lastSpectrum] * smoothConstantDown + lastSpectrum[y < lastSpectrum] * (1 - smoothConstantDown)
      lastSpectrum[y >= lastSpectrum] = y[y >= lastSpectrum] * smoothConstantUp + lastSpectrum[y >= lastSpectrum] * (1 - smoothConstantUp)
    else:
      lastSpectrum = y

    x = frequencies[0:int(paddedSampleSize/2) - 1]

    return lastSpectrum
    
def drawBars(width, height, spectrum, color, layout):
    vH = height-height/8
    bF = width / 64
    bH = bF / 2
    bQ = bF / 4
    imTop = Image.new("RGBA", (width, height),(0,0,0,0))
    draw = ImageDraw.Draw(imTop)
    r, g, b = color
    color2 = (r, g, b, 125)

    bP = height / 1200

    for j in range(0, 63):
      draw.rectangle((bH + j * bF, vH+bQ, bH + j * bF + bF, vH + bQ - spectrum[j * 4] * bP - bH), fill=color2)
      draw.rectangle((bH + bQ + j * bF, vH , bH + bQ + j * bF + bH, vH - spectrum[j * 4] * bP), fill=color)


    imBottom = imTop.transpose(Image.FLIP_TOP_BOTTOM)

    im = Image.new("RGBA", (width, height),(0,0,0,0))

    if layout == 0:
      y = 0 - int(height/100*43)
      im.paste(imTop, (0, y), mask=imTop)
      y = 0 + int(height/100*43)
      im.paste(imBottom, (0, y), mask=imBottom)

    if layout == 1:
      y = 0 + int(height/100*10)
      im.paste(imTop, (0, y), mask=imTop)
      y = 0 - int(height/100*10)
      im.paste(imBottom, (0, y), mask=imBottom)

    if layout == 2:
      y = 0 + int(height/100*10)
      im.paste(imTop, (0, y), mask=imTop)

    return im