Skip to content

Instantly share code, notes, and snippets.

@hsnee
Last active September 3, 2018 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsnee/49d40db32eb4560f498a7dec6063f8cc to your computer and use it in GitHub Desktop.
Save hsnee/49d40db32eb4560f498a7dec6063f8cc to your computer and use it in GitHub Desktop.
processing.js visualization script & python audio script for µViz
// setting intial values
float angle=0;
color[] colarray = new color[20];
color[] colarray2 = new color[20];
boolean changecolor = false;
int col;
// setup loop
void setup() {
size(1600, 1200); // define size of window
background(0, 0, 0); // set background color
// define color palettes
colarray[0] = color(#FCF5BB);
colarray[1] = color(#FCDFA4);
colarray[2] = color(#FCCD94);
colarray[3] = color(#FCB07C);
colarray[4] = color(#FB996E);
colarray[5] = color(#F98264);
colarray[6] = color(#F06260);
colarray[7] = color(#E35267);
colarray[8] = color(#CF4470);
colarray[9] = color(#BC3D77);
colarray[10] = color(#AF397B);
colarray[11] = color(#95307F);
colarray[12] = color(#7E2A81);
colarray[13] = color(#66207E);
colarray[14] = color(#481B76);
colarray[15] = color(#4D1C79);
colarray[16] = color(#3D186F);
colarray[17] = color(#1A1241);
colarray[18] = color(#0C0925);
colarray[19] = color(#000003);
colarray2[0] = color(#430753);
colarray2[1] = color(#471965);
colarray2[2] = color(#482975);
colarray2[3] = color(#45397F);
colarray2[4] = color(#414986);
colarray2[5] = color(#3A578A);
colarray2[6] = color(#35648B);
colarray2[7] = color(#30708D);
colarray2[8] = color(#2D7D8D);
colarray2[9] = color(#2A8A8C);
colarray2[10] = color(#28968B);
colarray2[11] = color(#2AA287);
colarray2[12] = color(#32AE80);
colarray2[13] = color(#43BA77);
colarray2[14] = color(#5AC46B);
colarray2[15] = color(#76CE5C);
colarray2[16] = color(#97D64C);
colarray2[17] = color(#B9DC3D);
colarray2[18] = color(#DCE137);
colarray2[19] = color(#FCE540);
colorMode(HSB);
// end of setup loop
}
// draw loop
void draw() {
// create a colored rectangle
fill(0, 20, 20, 2);
rect(0, 0, width, height);
for(int j = 0; j < 20; j++){
col = colarray[j];
fill(col);
}
translate(mouseX, mouseY);
// continuously rotate the rectangle
angle +=0.1;
rotate(angle);
fill(255);
col = colarray[0];
fill(col);
// map some key-presses, these also work on wacom tablet buttons.
if (keyPressed) {
if (keyCode == 16) { // maps to shift
changecolor=true;
}
if (keyCode == 17) { // maps to ctrl
changecolor=false;
}
if (keyCode == 18) { // maps to alt
background(0,0,0);
}
}
if(changecolor==true){
magma();
}
else{
virdis();
}
stroke(0);
for(int i = 0; i< mouseY; i++){
rect(-i/16,-i/16, i/8,i/8,i%10);
}
noFill();
noStroke();
rect(frameCount * frameCount % width, 0, 40, height);
// end of draw loop
}
void magma(){ // mapping color palette1 to x-axis
for(int i=0; i<20; i=i+1){
if(mouseX >= width*i/20 && mouseX < width*(i+1)/20) {
col = colarray[i];
fill(col);
}
}
}
void virdis(){ // mapping color palette2 to x-axis
for(int j=0; j<20; j=j+1) {
if(mouseX >= width*j/20 && mouseX <= width*(j+1)/20){
col = colarray2[j];
fill(col);
}
}
}
import numpy as np
import time
import pymouse
import pyaudio
import keyboard
mouse = pymouse.PyMouse()
# create a dictionary with frequencies representing musical notes
notes = {1:130.8, 2:146.8, 3: 164.8, 4: 185, 5:196, 6:220, 7:246.9,
8:261.6, 9:293.6, 10: 329.6, 11: 370, 12:392, 13:440, 14:523.2}
# create a window size
nx, ny = (14, 10)
x = np.linspace(0, 1200, nx)
y = np.linspace(0, 800, ny)
def play_sin(frequency,volume=0.2):
p = pyaudio.PyAudio()
volume = volume # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 300/frequency # in seconds, may be float
f = frequency # sine frequency, Hz, may be float
# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
idx=-1
duration = 1
while True:
if keyboard.is_pressed('shift'):
print 'shift pressed'
duration = 0.5
elif keyboard.is_pressed('ctrl'):
duration = 1
if idx==-1:
sound = play_sin(notes[1])
idx =1
else:
old_idx=idx
idx = np.searchsorted(x,[mouse.position()[0],],side='right')[0]
idxy = np.searchsorted(y,[mouse.position()[1],],side='right')[0]
sound = play_sin(frequency=notes[idx],volume=0.1*idxy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment