Skip to content

Instantly share code, notes, and snippets.

@reedjones
Forked from anonymous/GZWJZG.markdown
Created March 19, 2016 14:51
Show Gist options
  • Save reedjones/5b08631d13ca52ed70fd to your computer and use it in GitHub Desktop.
Save reedjones/5b08631d13ca52ed70fd to your computer and use it in GitHub Desktop.
GZWJZG
// draw a sphere with radius 200
let x;
let osc_playing = false;
let player_types = ['triangle', 'sawtooth', 'sine', 'square']
let notes = [{
note: 220.00,
name: 'A3'
}, {
note: 233.08,
name: 'A#'
}, {
note: 246.94,
name: 'B3'
}, {
note: 261.63,
name: 'C4'
}, {
note: 277.18,
name: 'C#'
}, {
note: 293.66,
name: 'D4'
}, {
note: 311.13,
name: 'D#'
}, {
note: 329.63,
name: 'E4'
}, {
note: 349.23,
name: 'F4'
}, {
note: 369.99,
name: 'F#'
}, {
note: 392.00,
name: 'G4'
}, {
note: 415.30,
name: 'G#'
}, {
note: 440.00,
name: 'A4'
}, {
note: 466.16,
name: 'A#4'
}, {
note: 493.88,
name: 'B4'
}, {
note: 523.25,
name: 'C5'
} ]
function sort_notes_by(t){
let sharps = [];
let norm = [];
for(let n = 0; n < notes.length; n++){
if(notes[n]['name'].endsWith('#')){
sharps.push(notes[n]);
}
else {
norm.push(notes[n]);
}
}
if (t == 'sharps') {
return sharps;
}
else {
return norm;
}
}
function sort_notes_into_threes(list){
let final_group = []; // a group of groups
let added = 0; let current_group = [];
for (let i =0; i < list.length - 1; i ++){
added =0; current_group = [];
for(let j = i; j < list.length -1; j+=2){
current_group.push(list[j]);
added+=1;
if(added == 3){
added = 0;
final_group.push(current_group);
break;
}
}
}
return final_group;
}
function get_note_groups(){
let norms = sort_notes_by('norm');
let sharp = sort_notes_by('sharps');
let flats = sort_notes_into_threes(norms);
let sharps = sort_notes_into_threes(sharp);
return {
sharps:sharps,
flats:flats
}
}
let note_groups = get_note_groups();
console.log(note_groups);
//console.log(note_groups[0][0]) //
//console.log(note_groups[0][1]) // < won't work
function setup() {
createCanvas(400, 400);
x = new sound_actor(50, 50, 50, 220.0, {
t: 'sine'
});
//x.draw_me()
}
function draw() {
x.draw_me();
x.play_on_mouse();
}
function sound_actor(x, y, rad, freq, o) {
this.player = new p5.Oscillator();
this.player.setType(o['t']);
//this.player.freq(freq, 10);
this.player.amp(0);
this.player.start();
//this.player.stop();
this.x = x;
this.y = y;
this.rad = rad;
this.freq = freq;
this.draw_me = function() {
fill(0, 0, 0)
ellipse(this.x, this.y, this.rad, this.rad)
}
this.play_me = function() {
this.player.freq(this.freq);
// this.player.start();
this.player.amp(0.5, 0.06);
}
this.play_sound = function() {
//osc.amp(0.5, 0.06);
//
}
this.check_mouse_over = function() {
let d = dist(mouseX, mouseY, this.x, this.y)
if (d < (this.rad / 2)) {
//console.log("on")
return true;
} else {
//console.log("not on")
return false;
}
}
this.play_on_mouse = function() {
if (this.check_mouse_over()) {
//this.play_sound();
this.play_me();
} else {
//osc.amp(0.0, 0.05);
this.player.amp(0.0, 0.05);
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/addons/p5.sound.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.23/addons/p5.dom.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment