Use keys 1 - 9 to start / stop a siren; use the letter keys or your left and right mouse buttons to change the parameters.
A fork of Dub sirens 2 by angeltapes
xxxxxxxxxx
//synth set up
var osc1, osc2, osc3, osc4, osc5, osc6, osc7, osc8, osc9;
var osc1amp, osc2amp, osc3amp, osc4amp, osc5amp, osc6amp, osc7amp, osc8amp, osc9amp;
var osc1playing, osc2playing, osc3playing, osc4playing, osc5playing, osc6playing, osc7playing, osc8playing, osc9playing;
var killSwitch;
var oscDelay;
var dragX, dragY;
var capture;
//synth ranges
var oscArray;
function setup() {
oscSetUp();
createCanvas(windowWidth, windowHeight);
oscDelay = new p5.Delay();
killSwitch = "off";
capture = createCapture(VIDEO);
capture.size(screen.availWidth * 2, screen.availHeight);
}
function draw() {
// background (0,115,71, 5);
image(capture, 0,-100, screen.availWidth, screen.availHeight);
freqChange();
strokeWeight(1);
for (i=0; i < 9; i++){
if (oscArray[i][6] == true){
fill(247,178,27)
}
else {
noFill()
}
//draw delay bars and frequency circles
rect(50, 30 + i * 50, oscArray[i][4] * 200, 20);
rect(50, 50 + i * 50, oscArray[i][5] * 200, 20);
ellipse(200 + oscArray[i][0] / 2, 50 + i * 50, 50, 50)
}
drawShapes();
if (killSwitch == "on"){
outputVolume(0)
}
else{
outputVolume(1)
}
}
function drawShapes(){
//draw visual reminders of each oscillator type
noFill();
ellipse(25, 50, 30, 30);
ellipse(25, 100, 30, 30);
ellipse(25, 150, 30, 30);
rect(10, 185, 30, 30);
rect(10, 235, 30, 30);
beginShape();
vertex(15, 280);
vertex(40, 300);
vertex(15, 325);
endShape(CLOSE);
beginShape();
vertex(15, 330);
vertex(40, 350);
vertex(15, 375);
endShape(CLOSE);
beginShape();
vertex(15, 380);
vertex(40, 390);
vertex(20, 400);
vertex(40, 410);
vertex(15, 425);
endShape(CLOSE);
beginShape();
vertex(15, 430);
vertex(40, 440);
vertex(20, 450);
vertex(40, 460);
vertex(15, 475);
endShape(CLOSE);
}
//adjust the frequencies
function freqChange() {
for (i = 0; i < 9; i++){
if (oscArray[i][0] > oscArray[i][1] + oscArray[i][3] || oscArray[i][0] < oscArray[i][1] - oscArray[i][3]){
oscArray[i][2] = oscArray[i][2] * -1
}
oscArray[i][0] = oscArray[i][0] + oscArray[i][2];
}
osc1.freq(oscArray[0][0]);
osc2.freq(oscArray[1][0]);
osc3.freq(oscArray[2][0]);
osc4.freq(oscArray[3][0]);
osc5.freq(oscArray[4][0]);
osc6.freq(oscArray[5][0]);
osc7.freq(oscArray[6][0]);
osc8.freq(oscArray[7][0]);
osc9.freq(oscArray[8][0]);
}
function keyTyped() {
if (key == '1') {
if (oscArray[0][6] == false){
osc1.start();
oscArray[0][6] = true
}
else if (oscArray[0][6] == true){
osc1.stop();
oscDelay.process(osc1, oscArray[0][4], oscArray[0][5], 2300);
oscArray[0][6] = false
}
}
if (key == '2') {
if (oscArray[1][6] == false){
osc2.start();
oscArray[1][6] = true
}
else if (oscArray[1][6] == true){
osc2.stop();
oscDelay.process(osc2, oscArray[1][4], oscArray[1][5], 2300);
oscArray[1][6] = false
}
}
if (key == '3') {
if (oscArray[2][6] == false){
osc3.start();
oscArray[2][6] = true
}
else if (oscArray[2][6] == true){
osc3.stop();
oscDelay.process(osc3, oscArray[2][4], oscArray[2][5], 2300);
oscArray[2][6] = false
}
}
if (key == '4') {
if (oscArray[3][6] == false){
osc4.start();
oscArray[3][6] = true
}
else if (oscArray[3][6] == true){
osc4.stop();
oscDelay.process(osc4, oscArray[3][4], oscArray[3][5], 2300);
oscArray[3][6] = false
}
}
if (key == '5') {
if (oscArray[4][6] == false){
osc5.start();
oscArray[4][6] = true
}
else if (oscArray[4][6] == true){
osc5.stop();
oscDelay.process(osc5, oscArray[4][4], oscArray[4][5], 2300);
oscArray[4][6] = false
}
}
if (key == '6') {
if (oscArray[5][6] == false){
osc6.start();
oscArray[5][6] = true
}
else if (oscArray[5][6] == true){
osc6.stop();
oscDelay.process(osc6, oscArray[5][4], oscArray[5][5], 2300);
oscArray[5][6] = false
}
}
if (key == '7') {
if (oscArray[6][6] == false){
osc7.start();
oscArray[6][6] = true
}
else if (oscArray[6][6] == true){
osc7.stop();
oscDelay.process(osc7, oscArray[6][4], oscArray[6][5], 2300);
oscArray[6][6] = false
}
}
if (key == '8') {
if (oscArray[7][6] == false){
osc8.start();
oscArray[7][6] = true
}
else if (oscArray[7][6] == true){
osc8.stop();
oscDelay.process(osc8, oscArray[7][4], oscArray[7][5], 2300);
oscArray[7][6] = false
}
}
if (key == '9') {
if (oscArray[8][6] == false){
osc9.start();
oscArray[8][6] = true
}
else if (oscArray[8][6] == true){
osc9.stop();
oscDelay.process(osc9, oscArray[8][4], oscArray[8][5], 2300);
oscArray[8][6] = false
}
}
if (key == '0') {
osc1.stop();
osc2.stop();
osc3.stop();
osc4.stop();
osc5.stop();
osc6.stop();
osc7.stop();
osc8.stop();
osc9.stop();
oscArray[0][6] = false;
oscArray[1][6] = false;
oscArray[2][6] = false;
oscArray[3][6] = false;
oscArray[4][6] = false;
oscArray[5][6] = false;
oscArray[7][6] = false;
oscArray[8][6] = false
}
if (key == 'q' || key == 'Q') {
oscSpeed(0);
}
if (key == 'a' || key == 'A') {
oscRange(0);
}
if (key == 'w' || key == 'W') {
oscSpeed(1);
}
if (key == 's' || key == 'S') {
oscRange(1);
}
if (key == 'e' || key == 'E') {
oscSpeed(2);
}
if (key == 'd' || key == 'D') {
oscRange(2);
}
if (key == 'r' || key == 'T') {
oscSpeed(3);
}
if (key == 'f' || key == 'F') {
oscRange(3);
}
if (key == 't' || key == 'T') {
oscSpeed(4);
}
if (key == 'g' || key == 'G') {
oscRange(4);
}
if (key == 'y' || key == 'Y') {
oscSpeed(5);
}
if (key == 'h' || key == 'H') {
oscRange(5);
}
if (key == 'u' || key == 'U') {
oscSpeed(6);
}
if (key == 'j' || key == 'J') {
oscRange(6);
}
if (key == 'i' || key == 'I') {
oscSpeed(7);
}
if (key == 'k' || key == 'K') {
oscRange(7)
}
if (key == 'o'|| key == 'O') {
oscSpeed(8);
}
if (key == 'l' || key == 'L') {
oscRange(8);
}
if (key == 'z' || key == 'Z') {
if (killSwitch == "off")
{killSwitch = "on"}
else{
killSwitch = "off"
}
}
if (key == 'x' || key == 'X') {
outputVolume(1);
}
}
function mouseClicked(){
dragX = mouseX;
dragY = int((mouseY + 25) / 50) - 1;
if (mouseButton === LEFT) {
if (mouseY < 475 && mouseY > 25){
setFreq(dragX, dragY)
}
}
if (mouseButton === RIGHT) {
if (mouseY < 475 && mouseY > 25){
setOscRange(dragX, dragY)
}
}
}
function mouseDragged(){
dragX = mouseX;
dragY = int((mouseY + 25) / 50) - 1;
if (mouseButton === LEFT) {
if (mouseY < 475 && mouseY > 25){
setFreq(dragX, dragY)
}
}
if (mouseButton === RIGHT) {
if (mouseY < 475 && mouseY > 25){
setOscRange(dragX, dragY)
}
}
}
//set frequency with the left mouse button
function setFreq(x, y) {
fill(255,100,27);
ellipse(mouseX, 50 + y * 50, 50, 50);
//current frequency, base frequency, speed, range, delay, playing
oscArray[y][0] = x;
oscArray[y][1] = x
}
//set range with the right mouse button
function setOscRange(x, y) {
//current frequency, base frequency, speed, range, delay, playing
oscArray[y][3] = x;
fill(247,178,27);
ellipse(200 + (oscArray[y][1] + oscArray[y][3]) / 2, 50 + y * 50, 50, 50);
ellipse(200 + (oscArray[y][1] - oscArray[y][3]) / 2, 50 + y * 50, 50, 50)
}
function oscSetUp(){
//set up oscillators
osc1 = new p5.Oscillator('sine');
osc2 = new p5.Oscillator('sine');
osc3 = new p5.Oscillator('sine');
osc4 = new p5.Oscillator('square');
osc5 = new p5.Oscillator('square');
osc6 = new p5.Oscillator('triangle');
osc7 = new p5.Oscillator('triangle');
osc8 = new p5.Oscillator('sawtooth');
osc9 = new p5.Oscillator('sawtooth');
oscArray = [];
//set up array - current frequency, base frequency, speed, range, delay, playing
for (i = 0; i < 9; i++){
let range = random (100,800)
oscArray[i] = [range, range, range / 100, range / 2, random (0.1, 1), random (0.1, 0.9), false];
}
}
//set the speed using the keyboard, keys Q to O
function oscSpeed(osc){
oscArray[osc][2] = oscArray[osc][2] + 2;
if (oscArray[osc][2] > 20) {oscArray[osc][2] = 1}
}
//set the range using the keyboard, keys A to L
function oscRange(osc){
oscArray[osc][3] = oscArray[osc][3] + 50;
if (oscArray[osc][3] > 1000) {oscArray[osc][3] = 50};
fill(247,178,27);
ellipse(200 + (oscArray[osc][1] + oscArray[osc][3]) / 2, 50 + osc * 50, 50, 50);
ellipse(200 + (oscArray[osc][1] - oscArray[osc][3]) / 2, 50 + osc * 50, 50, 50)
}
//disable context menu - so the right mouse button doesn't show a menu
if (document.addEventListener) {
document.addEventListener('contextmenu', function (e) {
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function () {
window.event.returnValue = false;
});
}