Sound In Space Week 2

For the mono assignment, I really wanted to focus on learning tone.js. All the projects I’ve done in the past have sounded somewhat grotesque, this one included, and I am going to try and work on that. I decided to do something very simple with this assignment because I still am not completely comfortable with tone.js and I’m also not familiar with creating sounds. I was looking at the oscillator example and I wanted to work with pure signals first because I’m still a beginner. I started out with getting a simple oscillator to work. I wanted to explore different partial counts, so I am playing consecutive sine tones with varying partial counts, possibly overlapping tones in order to play with spatiality. I set a time limit (~1 min) for myself because these tones could go on forever basically. I initially tried to create an array of partials and increase the size of the array, and load the array in as the partials value of the oscillator, but I don’t think I did this right. The code that I wrote for this is not efficient at all and I’m not sure if somethings work, like the stops, which I’m pretty sure is not working. In order to prevent too many sounds from playing at once I just change the volume randomly. I started out trying one set frequency and changing the partial count, but that was kind of bland, so i switched to random frequencies within a set range but after a while that just turned into noise. Then I made it a little more random by setting a certain frequency for each partial count that is triggered randomly, hopefully the frequencies I looked up aren’t too jarring. I also tried to incorporate phase and other attributes within the oscillator but it didn’t work out, so I mainly played with, I think, partial count, frequency and volume.

I consider this to be more of an experiment, so it lacks a narrative or real direction in the final sounds. I had a lot of difficulty with working with time and just getting tones to play, so I hope to work on my tone.js skills and create more elaborate sounds for my next assignment. I tried to work with tone.js in jsfiddle, but am more comfortable with p5.js, so decided to use p5.tone. At first p5.tone didn’t work because the github page is down to the link that allows the script to be embedded, but I was able to work around that. Below is a video of my assignment being played and the source code for the mono assignment. In the video, there is a mysterious clicking noise, so please ignore that. Click here for a link to my sketch.

 
 
// let partialArray = [];
// let partialLength = 20;
let timer = 0;
let osc1, osc2, osc3, osc4, osc5, osc6, osc7, osc8, osc9, osc10;

function setup() {
  createCanvas(500, 500);
  frameRate(1);
  rectMode(CENTER);
}

function draw() {
  background(0);
  let osc1 = new Tone.Oscillator({
    "type": "sine1",
    "frequency": 440,
    "volume": -15,
  }).toMaster();

  let osc2 = new Tone.Oscillator({
    "type": "sine2",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc3 = new Tone.Oscillator({
    "type": "sine3",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc4 = new Tone.Oscillator({
    "type": "sine4",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc5 = new Tone.Oscillator({
    "type": "sine5",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc6 = new Tone.Oscillator({
    "type": "sine6",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc7 = new Tone.Oscillator({
    "type": "sine7",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc8 = new Tone.Oscillator({
    "type": "sine8",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc9 = new Tone.Oscillator({
    "type": "sine9",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  let osc10 = new Tone.Oscillator({
    "type": "sine10",
    "frequency": 440,
    "volume": -15
  }).toMaster();

  if (second() % 10 == 1) {
    let rannn = random(15);
    let vol = 0;
    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);
      stroke(freq / 2, 120, 120);
    } else if (rannn > 5) {
      freq = 880;
      vol = random(-20, -15);

      stroke(120, freq * .2, 120);
    } else {
      vol = -40;
    }
    osc1 = new Tone.Oscillator({
      "type": "sine1",
      "frequency": freq,
      "volume": vol,

    }).toMaster();
    osc1.start();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();

  } else if (second() % 10 == 2) {
    let rannn = random(10);
    let vol = 0;
    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 50, 120);
      fill(freq / 2, 50, 120, 40);

    } else if (rannn > 5) {
      freq = 784;
      vol = random(-20, -15);

      stroke(50, freq * .2, 120);
      fill(50, freq * .2, 120, 40);

    } else {
      vol = -40;
    }
    osc2 = new Tone.Oscillator({
      "type": "sine2",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc2.start();
    osc1.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 3) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 120, 50);
      fill(freq / 2, 120, 50, 40);

    } else if (rannn > 5) {
      freq = 698;
      vol = random(-20, -15);

      stroke(120, freq * .2, 50);
      fill(120, freq * .2, 50, 40);

    } else {
      vol = -40;
    }
    osc3 = new Tone.Oscillator({
      "type": "sine3",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc3.start();
    osc1.stop();
    osc2.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();

  } else if (second() % 10 == 4) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 50, 120);
      fill(freq / 2, 50, 120, 40);

    } else if (rannn > 5) {
      freq = 784;
      vol = random(-20, -15);

      stroke(50, freq * .2, 120);
      fill(50, freq * .2, 120, 40);

    } else {
      vol = -40;
    }
    osc4 = new Tone.Oscillator({
      "type": "sine4",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc4.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 5) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 50, 50);
      fill(freq / 2, 50, 50, 40);

    } else if (rannn > 5) {
      freq = 740;
      vol = random(-20, -15);

      stroke(50, freq * .2, 50);
      fill(50, freq * .2, 50, 40);

    } else {
      vol = -40;
    }
    osc5 = new Tone.Oscillator({
      "type": "sine5",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc5.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 6) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 30, 200);
      fill(freq / 2, 30, 200, 40);

    } else if (rannn > 5) {
      freq = 698;
      vol = random(-20, -15);

      stroke(30, freq * .2, 200);
      fill(30, freq * .2, 200, 40);

    } else {
      vol = -40;
    }
    osc6 = new Tone.Oscillator({
      "type": "sine6",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc6.start();

    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 7) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 200, 30);
      fill(freq / 2, 200, 30, 40);

    } else if (rannn > 5) {
      freq = 659;
      vol = random(-20, -15);

      stroke(200, freq * .2, 30);
      fill(200, freq * .2, 30, 40);

    } else {
      vol = -40;
    }
    osc8 = new Tone.Oscillator({
      "type": "sine7",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc7.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc8.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 8) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);
      
      stroke(90, freq * .2, 168);
      fill(90, freq * .2, 168, 40);

    } else if (rannn > 5) {
      freq = 494;
      vol = random(-20, -15);
      
      stroke(168, freq * .2, 90);
      fill(168, freq * .2, 90, 40);

    } else {
      vol = -40;
    }
    osc8 = new Tone.Oscillator({
      "type": "sine8",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc8.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc9.stop();
    osc10.stop();
  } else if (second() % 10 == 9) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 30, 168);
      fill(freq / 2, 168, 30, 40);

    } else if (rannn > 5) {
      freq = 587;
      vol = random(-20, -15);

      stroke(168, freq * .2, 30);
      fill(168, freq * .2, 30, 40);

    } else {
      vol = -40;
    }
    osc9 = new Tone.Oscillator({
      "type": "sine9",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc9.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc10.stop();
  } else if (second() % 10 == 0) {
    let rannn = random(10);
    let vol = 0;

    let freq = 0;
    if (rannn < 5) {
      freq = 440;
      vol = random(-30, -25);

      stroke(freq / 2, 168, 30);
      fill(freq / 2, 30, 168, 40);

    } else if (rannn > 5) {
      freq = 523;
      vol = random(-20, -15);

      stroke(30, freq * .2, 168);
      fill(30, freq * .2, 168, 40);

    } else {
      vol = -40;
    }
    osc10 = new Tone.Oscillator({
      "type": "sine",
      "frequency": freq,
      "volume": vol,
    }).toMaster();
    osc10.start();
    osc1.stop();
    osc2.stop();
    osc3.stop();
    osc4.stop();
    osc5.stop();
    osc6.stop();
    osc7.stop();
    osc8.stop();
    osc9.stop();
  }

  // }
  // console.log(osc.frequency.value);
  console.log(second());
  for (let x = 50; x <= width - 50; x += 25) {
    for (let y = 50; y <= width - 50; y += 25) {
      let rann = random(10);
      // noFill();
      if (rann < 5) {
        square(x, y, 10, 10);
      } else if (rann > 5) {
        circle(x, y, 5, 5);
      }
    }
  }
}

// let osc = new Tone.Oscillator({
//   "type": "sine8",
//   "frequency": 40,
//   // "partials": partialArray
// }).toMaster();
// osc.frequency.setValueAtTime(osc.frequency.value * 2, 40);
// for (var i = 0; i < partialLength; i++) {
//   partialArray.push('1');
// }
// if (partialArray.length < 15) {
//   // for(var i = 0; i<50; i++) {
//   partialArray.push('1');
//   // }
// } else {
//   partialArray = [];

// if (mouseIsPressed) {
//   osc10.start();
//     for (var i = 0; i < 10; i++) {
//       osc.frequency.value = i * 10;
//       osc.start();

//       // oscil(i*100, partialArray);
//     }

// function oscil (freuq, part) {
// let osc = new Tone.Oscillator({
//     "type": "sine",
//     "frequency": freuq,
//     "partials": part
//   }).toMaster();
// osc.start();
// }