fbpx Skip to Content
×

Testing

//produced by MyFi Studio
//This code may not be used for any commercial purposes.
//This code is being provided as an instructional example for the purpose of developing the website for MyFi Studio’s new instrument that was commissioned by The Bass Museum.

//XXX
//NOTE:
//Token ID represents the instruent that will be loaded.
//Values of token ID must be between 0 and 453
//In production, you should set the tokenId of the specific instrument here.
let tokenId;

let graphicsBuffers = [];

//cyanotypes
let cyano = [44,46,75];
let creamSpore = [173, 161, 128];

//west west
let west = [173, 161, 128];
let rust = [185,71,30];

//kurama
let kuramaOrange = [234,97,42];
let kuramaDark = [26, 37, 39];

//night garden haze
let nightGardenGreen = [117, 251 , 76];
let nigthGardenPink = [232,57,183] ;

//gitched cyanotype palm tree
let glitchPalmBlue = [44,98,209];
let glitchPalmOrange = [193,94,48];

//pastel glitch of roller coaster
let pastelRollerCoasterYellow = [226,216,133];
let pastelRollerCoasterBlue = [142,208,224]

//ice cream infront of a car
let ube = [183,185,239];
let car = [213,111,49];

//sg
let sgSkin = [162,210,169]
let sgNi = [191,179,129]

let colorPalettes = [ [cyano,creamSpore], [kuramaOrange, kuramaDark], [west, rust], [nightGardenGreen, nigthGardenPink], [glitchPalmBlue, glitchPalmOrange], [pastelRollerCoasterYellow,pastelRollerCoasterBlue],[ube,car],[sgSkin,sgNi]] ;

let colorPalette;
let backgroundColor;
let sporeColor;

//audio
let startTime = 0;
let tempo;// = 400.0;
let millisPerBeat; //= (60.0/tempo)*1000;
let lastBeat = 0.0;
let beat = 0;
let subdivision = 0;
let beatsPerMeasure;
let subdivisionsPerBeat;
let sounds = [];

//sequences
let chromaticUp = [440, 446.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99];
let chromaticUpDown = [440, 446.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99,739.99,698.46,659.25, 622.25, 587.33, 554.37, 523.25, 493.88, 446.16];
let foamyWell = [2*493.88, 2*392, 2*369.99, 2*392, 2*493.88, 2*392, 2*369.99,2*369.99, 2*392];
let bcEnd = [392, 987.77, 392, 987.77, 415.30, 1046.50, 415.30, 1046.50, 466.16, 1174.66, 466.16, 1174.66, 415.30, 1046.50, 415.30, 1046.50]
let nappingSlap = [587.33,587.33,739.99,1046.50,1046.50,739.99, 523.25,523.25,659.25,932.33,932.33, 554.37,698.46,698.46]
let runesGlowing = [0.5*698.46,0.5*880.00,0.5*987.77,0.5*1174.66, 698.46,880.00,987.77,1318.51, 2*698.46,2*880.00,2*987.77,2*1567.98, 2*1396.91,2*1174.66,2*987.77,2*1174.66];
let manteca = [349.23, 698.46, 349.23, 349.23, 698.46, 349.23, 698.46, 349.23, 698.46, 698.46, 622.25, 523.25, 698.46, 622.25, 698.46, 622.25, 523.25, 698.46, 622.25, 523.25, 698.46, 622.25, 523.25, 698.46, 622.25, 622.25, 622.25, 622.25];
let dewdew =[392, 783.99, 587.33, 659.25, 698.46, 392, 783.99, 587.33, 659.25, 698.46, 659.25, 587.33, 659.25];
let naps = [587.33,523.25,440.00,587.33,523.25,440.00,369.99,369.99,369.99,587.33,523.25,440.00, 659.25, 830.61,1174.66, 622.25,783.99, 1108.73];
let bc = [392.00,987.77,349.23,880.00,349.23,392.00,987.77,392.00,987.77,349.23,880.00,349.23, 880.00, 392.00,987.77,349.23,880.00,293.66,369.99,440.00,554.37,587.33,554.37];
let daynight = [329.63,493.88 ,830.61,493.88,369.99,554.37,880.00,415.30,622.25,987.77,622.25,440,659.25, 1108.73 ];
let sequences =[chromaticUp, chromaticUpDown, foamyWell, bcEnd, nappingSlap, runesGlowing, manteca,dewdew,naps,bc,daynight];
//let sequences = [daynight,daynight]
let sequence;

let rotationAngle;
let colorFactor;
let colorFactors = [1,1,1,2,3,6,7,8,8,8,9,10,15,15,15,15,15,15,30,30,11,11,13,13,13];

function setup()
{
//XXX
//Note: here the token ID is random set to a number between 0 and 453
//in production you will specify what the token ID is,
//preferably at the top when the tokenId variable is declared.
tokenId = floor(random(0,454))

console.log(“current instrument token ID: “+tokenId)

document.body.style.TouchAction = “none”;

//make audio work on iphone
getAudioContext().suspend();

if(typeof tokenId !== “undefined”)
{
//885 is a magic number for us to
//specify a specific piece for token 0
randomSeed(tokenId+885);
}
else
{
tokenId= floor(random(885,1885));

console.log(“token id: “+tokenId);
randomSeed(tokenId);
}

//choose note sequence
let chosenSeqIndex = tokenId % sequences.length
sequence = sequences[chosenSeqIndex];
//rotation angle
let max = [0.3,0.3,0.8, 1,1, 1.1];
let maxRotation = random(max);
rotationAngle = random(0.1, maxRotation);

let colorChoice = tokenId % colorFactors.length;

colorFactor = colorFactors[colorChoice];

rectMode(CENTER);
createCanvas(windowWidth, windowHeight);

//choose color palette + set spore start and background
colorPalette = random(colorPalettes);

let p =random([1,2,3,4]);
if(p == 1)
{
backgroundColor = colorPalette[0];
sporeColor = colorPalette[1];
}
else if(p == 2)
{
backgroundColor = colorPalette[1];
sporeColor = colorPalette[0];
}
else if(p == 3)
{
backgroundColor = colorPalette[0];
sporeColor = colorPalette[0];
}
else if(p == 4)
{
backgroundColor = colorPalette[1];
sporeColor = colorPalette[1];
}

let colors = getColors(666);
let density = random(0.55,0.8);

//create buffers
for(let i=0; i<13; i++) { let buffer = createGraphics(windowWidth*2,windowHeight*2); buffer.background(backgroundColor[0],backgroundColor[1],backgroundColor[2]); buffer.noStroke(); buffer.translate(windowWidth,windowHeight); for(let j=0; j<500; j++) { buffer.rotate(random(0,rotationAngle)) let colorIndex = (colorFactor*(i + j)) % colors.length; if(density < random()) { buffer.fill(colors[colorIndex][0],colors[colorIndex][1],colors[colorIndex][2], 252) if(j<400) { buffer.circle(100,j,100); } else { let growthFactor = random(1.5,3); buffer.circle(100,j,100*growthFactor); } } } graphicsBuffers[i] = buffer; } noCursor() imageMode(CENTER); image(graphicsBuffers[0], 0, 0); //audio tempo = 79.0; beatsPerMeasure = 4; subdivisionsPerBeat = 4; millisPerBeat = (60.0/(tempo*subdivisionsPerBeat))*1000; let wind = new sound('noise', 490, 'hi_hat'); wind.setADSR(0.0001, 0.07, 0.001, 0.02); sounds[0] = wind; sounds[0].drone(); let chunk = new sound('square', 49, 'chunk') sounds[1] = chunk; } let layerCounter =0; let bufferCounter = 0; let drawCounter =0; function draw() { let currentSpeed = (windowHeight - mouseY)/windowHeight; currentSpeedSmooth = map(currentSpeed,0,1,8,3); currentSpeed = floor(currentSpeedSmooth); //change speed + size if(drawCounter>=currentSpeed)
{
imageMode(CENTER);
background(backgroundColor[0],backgroundColor[1],backgroundColor[2]);
let sizeFactor = map(currentSpeedSmooth, 3,8, 1.75,0.05)
image(graphicsBuffers[bufferCounter], mouseX+windowWidth/22, mouseY+windowHeight/22, windowWidth*sizeFactor, windowHeight*sizeFactor)
bufferCounter++;
bufferCounter = bufferCounter % graphicsBuffers.length;
drawCounter =0;
}
else
{
drawCounter++;
}

//audio
//wind
sounds[0].oscillator.pan(map(mouseX/windowWidth, 0,1,-1,1));
sounds[0].setFilter(map(mouseY/windowHeight- 0.0001, 0,1,1000,300))

//melody
let newTempo = floor(map(mouseY/windowHeight, 1,0,30,192));
if(newTempo != tempo)
{
tempo = newTempo;
millisPerBeat = (60.0/(tempo*subdivisionsPerBeat))*1000;
}

sounds[1].setFilter(map(mouseX/windowWidth, 0,1,200,800));

let now = millis();
if(now – lastBeat >= millisPerBeat)
{
sounds[1].next();
sounds[1].play();
lastBeat = now;
}

}

function getSpread(t)
{
let spread = [];
spread[0] = 40*(noise(t)-0.5);
spread[1] = 38*(noise(t+5)-0.5);
spread[2] = 255*(noise(t+10));
return spread;
}

class sound
{
constructor(waveShape, freq, instrument)
{
this.envelope = new p5.Envelope();
this.waveShape = waveShape;
this.bassDrum = false;
this.hiHat = false;
this.instrument = instrument;
this.sequenceIndex = 0;

if(instrument == ‘hi_hat’)
{
this.oscillator = new p5.Noise();
this.hiHat = true;
this.filter = new p5.LowPass();

this.oscillator.disconnect();
this.oscillator.connect(this.filter);

//this.filter.freq(56);
this.filter.res(10);

}
else if(instrument == ‘chunk’)
{
let wave = random(0,1);
if(wave<0.33) { this.oscillator = new p5.SqrOsc(); // console.log("square") } else if(wave<0.66) { this.oscillator = new p5.SawOsc(); // console.log("saw") } else { this.oscillator = new p5.TriOsc(); // console.log("tri") } this.chunk = true; this.filter = new p5.LowPass() this.oscillator.disconnect(); this.oscillator.connect(this.filter); //this.filter.freq(56); this.filter.res(18); let attack = random(0.0001, 0.003); let decay = random(0.015,0.04); let sustain = random(0.01,0.2) let release = random(0.1,0.4); this.envelope.setADSR(attack, decay, sustain, release); this.oscillator.amp(this.envelope); this.oscillator.freq(sequence[0]); this.sequenceIndex = 1; } } next() { this.oscillator.freq(sequence[this.sequenceIndex]); if(this.sequenceIndex == sequence.length-1) { this.sequenceIndex = 0; } else { this.sequenceIndex++; } } setADSR(attackTime, decayTime, sustainTime, releaseTime ) { this.oscillator.start(); this.envelope.setADSR(attackTime, decayTime, sustainTime, releaseTime); } setFilter(cutOff) { this.filter.freq(cutOff); } play() { this.oscillator.start(); this.envelope.play(); } drone() { if(this.hiHat) { this.oscillator.amp(0.1); } } } function getColors(layers) { let colors = []; for(let i=0; i

SHARE!