Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# ComputationalArt
This is the base repository for the computational art mini project.
Mini Project 5 Revisions for Mini Project 2 (Computational Art).

Revised recursive_art.py using ninja feedback from Reviewable.
Added rec_art.py that visualizes changes in images based on changes in volume captured through microphone (Going Beyond: Pulsating Music Visualizer).


Used 50 of own randomly generated images from recursive_art.py, stored in folder "images". Can download zip and rec_art.py should still be able to access them.
Binary file added images/myart0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/myart31.png
Binary file added images/myart32.png
Binary file added images/myart33.png
Binary file added images/myart34.png
Binary file added images/myart35.png
Binary file added images/myart36.png
Binary file added images/myart37.png
Binary file added images/myart38.png
Binary file added images/myart39.png
Binary file added images/myart4.png
Binary file added images/myart40.png
Binary file added images/myart41.png
Binary file added images/myart42.png
Binary file added images/myart43.png
Binary file added images/myart44.png
Binary file added images/myart45.png
Binary file added images/myart46.png
Binary file added images/myart47.png
Binary file added images/myart48.png
Binary file added images/myart49.png
Binary file added images/myart5.png
Binary file added images/myart50.png
Binary file added images/myart6.png
Binary file added images/myart7.png
Binary file added images/myart8.png
Binary file added images/myart9.png
114 changes: 114 additions & 0 deletions rec_art.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""
author:
Rachel Yang

Recursive Art Visualizer:
Recursive Art previously generated by recursive_art.py responds
in real time to changes in audio volume captured by microphone
"""

import alsaaudio
import audioop
import pygame
import os, sys


# initialize pygame
pygame.init()

CURR_DIR = os.path.dirname(os.path.realpath(__file__))

class Visualizer(object):
""" Loads recursively generated images and displays
a different one based on level of audio volume """

def __init__(self, image_size=(350,350), rate=16000, period_size=160):

# initialize known constant of image size
self.image_size, self.rate, self.period_size = image_size, rate, period_size

# initialize screen to size of recursive art images
self.screen = pygame.display.set_mode(self.image_size)

# initialize settings of capturing input audio through mic
self.input = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,0)
self.input.setchannels(1)
self.input.setrate(self.rate)
self.input.setformat(alsaaudio.PCM_FORMAT_S16_LE)
self.input.setperiodsize(self.period_size)

def load_image(self):
""" Loads images and stores in a list """
self.images = []

for image in range(50):
self.images.append(pygame.image.load(CURR_DIR + "/myart" + str(image) + ".png"))

def remap_interval(self, val,
input_interval_start=0,
input_interval_end=30000,
output_interval_start=0,
output_interval_end=29):
""" Given an input value in the interval [input_interval_start,
input_interval_end], return an output value scaled to fall within
the output interval [output_interval_start, output_interval_end].

val: the value to remap
input_interval_start: the start of the interval that contains all
possible values for val
input_interval_end: the end of the interval that contains all possible
values for val
output_interval_start: the start of the interval that contains all
possible output values
output_inteval_end: the end of the interval that contains all possible
output values
returns: the value remapped from the input to the output interval

>>> remap_interval(0.5, 0, 1, 0, 10)
5.0
>>> remap_interval(5, 4, 6, 0, 2)
1.0
>>> remap_interval(5, 4, 6, 1, 2)
1.5
>>> remap_interval(3, 1, 5, 6, 7)
6.5
"""
# return output value scaled to output interval
input_range = float(input_interval_end - input_interval_start)
output_range = float(output_interval_end - output_interval_start)
output_value = ((val - input_interval_start)/input_range) * (output_range) + output_interval_start
return output_value

def visual(self):
""" Collects audio input data and returns volume levels
and changes image based on data """
while 1:
# l is length, d is captured data
# reads both from audio stream
l,data = self.input.read()

# if l is 0, no audio data
if l:
# root mean square to avoid sign errors
vol = audioop.rms(data,2)
# prints volume levels
print vol

# loads image to screen at position 0,0
remap = int(self.remap_interval(vol))
self.screen.blit(self.images[remap],(0,0))
# updates screen
pygame.display.flip()

# exits program when ESC button is pressed
for exit in pygame.event.get():
if exit.type == pygame.KEYDOWN:
if (exit.key == pygame.K_ESCAPE):
pygame.quit()



if __name__ == '__main__':
stream = Visualizer()
stream.load_image()
stream.visual()
53 changes: 28 additions & 25 deletions recursive_art.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

def build_random_function(min_depth, max_depth):
""" Builds a random function of depth at least min_depth and depth
at most max_depth (see assignment writeup for definition of depth
in this context)
at most max_depth

min_depth: the minimum depth of the random function
max_depth: the maximum depth of the random function
returns: the randomly generated function represented as a nested list
(see assignment writeup for details on the representation of
these functions)

sometimes the doctests will fail (expected x, got y | expected y, got x) because
the function randomly chooses either 0 and 1, and will then pick either x or y depending on that
Expand All @@ -28,21 +25,15 @@ def build_random_function(min_depth, max_depth):
"""
build_blocks = ["prod","avg","cos_pi","sin_pi","arctan_pi","circle","x","y"]

if max_depth <= 0:
if (max_depth <= 0) or (min_depth <=0):
random1 = random.randint(0,1)
if random1 == 0:
return "x"
elif random1 == 1:
return "y"
elif min_depth <= 0:
random2 = random.randint(0,1)
if random2 == 0:
return "x"
elif random2 == 1:
return "y"
else:
brf = build_random_function(min_depth - 1, max_depth - 1)
index = random.randint(0,5)
index = random.randint(0,7)
if index <= 5:
return [build_blocks[index], brf, brf]
if index >= 6:
Expand Down Expand Up @@ -75,25 +66,37 @@ def evaluate_random_function(f, x, y):
5
"""
if f[0] == "x":
return x
return x
elif f[0] == "y":
return y
elif f[0] == "prod":
return evaluate_random_function(f[1], x, y) * evaluate_random_function(f[2], x, y)
elif f[0] == "avg":
return 0.5 * (evaluate_random_function(f[1], x, y) + evaluate_random_function(f[2], x, y))
elif f[0] == "cos_pi":
return math.cos(math.pi * evaluate_random_function(f[1], x, y))
return y
elif f[0] == "circle":
return x**2 + y**2

# evalutes random function if it has an "x" argument
# used for following functions
one = evaluate_random_function(f[1], x, y)

if f[0] == "cos_pi":
return math.cos(math.pi * one)
elif f[0] == "sin_pi":
return math.sin(math.pi * evaluate_random_function(f[1], x, y))
return math.sin(math.pi * one)
elif f[0] == "arctan_pi":
return math.atan(math.pi * evaluate_random_function(f[1], x, y))
return math.atan(math.pi * one)

# evaluates random function if it has an "x" and a "y" argument
# used for the following functions
two = evaluate_random_function(f[2], x, y)

if f[0] == "prod":
return one * two
elif f[0] == "avg":
return 0.5 * (one + two)

#elif f[0] == "cot_pi":
#return 1 / (math.tan(math.pi * evaluate_random_function(f[1], x, y)))
#elif f[0] == "squared":
#return (evaluate_random_function(f[1], x, y))**2 + (evaluate_random_function(f[2], x, y))**2
elif f[0] == "circle":
return x**2 + y**2


def remap_interval(val,
input_interval_start,
Expand Down Expand Up @@ -203,7 +206,7 @@ def generate_art(filename, x_size=350, y_size=350):
doctest.testmod()

# Create some computational art!
generate_art("myart40.png")
generate_art("myart51.png")

# Test that PIL is installed correctly
# test_image("noise.png")