fractaL_sound2.pyde 2.56 KB
Newer Older
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
add_library('minim')
add_library('peasycam')

cylindrical, isPlaying, isMute = False, False, False
pts, radius, latheRadius, segments = 10, 1, 300, 500 
vertices, vertices2 = [[PVector() for e in range(pts+1)] for e in range(2)]

def setup():
    global song, fftLin, fftLog, cam
    size(1200, 800, P3D)
    frameRate(1000)
    smooth(8)

    cam = PeasyCam(this, 1400)
    cam.setMaximumDistance(600)
    cam.rotateX(-PI/2.4)
    perspective(60 * DEG_TO_RAD, width/float(height), 2, 6000)

    minim = Minim(this)
    song = minim.loadFile("multi.mp3")
    
     
    fftLin, fftLog = [FFT(song.bufferSize(), song.sampleRate()) for e in range(2)]
    fftLin.linAverages(30), fftLog.logAverages(22, 3)
    
def draw():
    background(0, 0, 0)
    lights()
    ambientLight(111, 20, 185)
    directionalLight(255,143,192, 1, 20, 60)
    pointLight(155,215,60, width, height/2, 0)
    rotateZ(frameCount*PI/560)
    fill(111, 11, 179)
    noStroke()

    song.play() if isPlaying else song.pause()
    song.mute() if isMute else song.unmute()
    
    fftLin.forward(song.mix)
    fftLog.forward(song.mix)

    latheAngle = 0
    for s in range(segments):
        angle = 0
        beginShape(QUAD_STRIP)
        for i, v in enumerate(vertices2):
            division = 1 if s%2 == 0 else 6
            step = s*2 # select 1 freq every 2 freq (up to 256 out of 512)
            c_step = (s*2)+((s%2)*2) # select 1 freq every 4 freq (up to 256 freq out of 512)
            if cylindrical: sscale = map(fftLin.getBand(c_step)*((s+10)/10), 0, 35, .8, 35)
            else: sscale = map(fftLin.getBand(step)*((s+10)/10), 0, 30, .8, 24)
            sscale = constrain(sscale, 0, 50)
            
            vertices[i].x = latheRadius + sin(radians(angle)) * radius * sscale 
            vertices[i].z = cos(radians(angle)) * radius * sscale 
            angle+=360.0/pts
            vertex(v.x, v.y, v.z)

            v.x = cos(radians(latheAngle))  * vertices[i].x 
            v.y = sin(radians(latheAngle))  * vertices[i].x 
            v.z = vertices[i].z 
            vertex(v.x, v.y, v.z)
            
        latheAngle += (360.0+260)/(segments*6/division) if cylindrical else 360.0/segments
        endShape()
       
    cam.beginHUD()
    text("'p' = PLAY/PAUSE", 20, 30)
    text("'r' = REPLAY", 20, 50)
    text("'m' = MUTE", 20, 70)
    text("'c' = MODE", 20, 90)
    cam.endHUD()
    
def keyPressed():
    global isPlaying, cylindrical, isMute
    if key == 'p': isPlaying = not isPlaying
    if key == 'm': isMute = not isMute
    if key == 'c': cylindrical = not cylindrical
    if key == 'r': song.rewind()