random chord method checks for distinct members
This commit is contained in:
25
app.js
25
app.js
@@ -25,31 +25,6 @@ export const soundChord = (pitchNames) => {
|
||||
bass.triggerAttackRelease(b, "8n");
|
||||
}
|
||||
|
||||
// initial test: generate a single, random chord
|
||||
export const fullRandomChord = () => {
|
||||
let pitches = [];
|
||||
for (let voice of pitchsets) {
|
||||
// finds a random index, excluding any which may already exist in the array
|
||||
let index;
|
||||
|
||||
do {
|
||||
index = Math.floor(Math.random() * 100) % voice.length;
|
||||
} while (pitches.includes(voice[index]));
|
||||
|
||||
pitches.push(voice[index]);
|
||||
console.log(voice[index]);
|
||||
}
|
||||
|
||||
for (let i = 0; i < pitches.length; i++) {
|
||||
if (pitches[i] === pitches[i+1]) {
|
||||
console.log("CAUGHT");
|
||||
}
|
||||
}
|
||||
|
||||
soundChord(pitches);
|
||||
extractPitchset(pitches);
|
||||
}
|
||||
|
||||
// set up transport
|
||||
const transportStart = document.getElementById('transport-start');
|
||||
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
<button id="mute-all">Mute all oscillators</button>
|
||||
<button id="reset-lfos">Reset LFO values to default</button>
|
||||
<button id="rand-chord">Full random chord</button>
|
||||
<button id="eval-chord">Evaluated chord</button>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { soundChord } from "../app.js";
|
||||
import { getRandomPitches } from "./harmonyUtil.js";
|
||||
import { extractPitchset } from "./vector_logic/extractPitchset.js";
|
||||
|
||||
// initial test: generate a single, random chord
|
||||
export const fullRandomChord = () => {
|
||||
let pitches = getRandomPitches();
|
||||
|
||||
soundChord(pitches);
|
||||
extractPitchset(pitches);
|
||||
let pitchset = extractPitchset(pitches);
|
||||
|
||||
return pitchset;
|
||||
}
|
||||
@@ -31,16 +31,28 @@ const transposePitches = (pitchNames, interval) => {
|
||||
export const getRandomPitches = () => {
|
||||
// pitches stored in Tone.js string format
|
||||
let pitches = [];
|
||||
let formattedPitches = [];
|
||||
|
||||
for (let voice of pitchsets) {
|
||||
// finds a random index, excluding any which may already exist in the array
|
||||
let index;
|
||||
|
||||
do {
|
||||
index = Math.floor(Math.random() * 100) % voice.length;
|
||||
} while (pitches.includes(voice[index]));
|
||||
const regex = /[A-Gb#]/g;
|
||||
let pitch;
|
||||
let formattedPitch;
|
||||
|
||||
pitches.push(voice[index]);
|
||||
// loops until four distinct chord members are received
|
||||
while (formattedPitches.length < 4) {
|
||||
index = Math.floor(Math.random() * 100) % voice.length;
|
||||
pitch = voice[index];
|
||||
formattedPitch = pitch.match(regex).join('');
|
||||
if (!formattedPitches.includes(formattedPitch)) {
|
||||
formattedPitches.push(formattedPitch);
|
||||
pitches.push(pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(formattedPitches);
|
||||
console.log(pitches);
|
||||
return pitches;
|
||||
}
|
||||
|
||||
30
src/helper.js
Normal file
30
src/helper.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { pitchsets } from "./harmonyUtil.js";
|
||||
|
||||
let index;
|
||||
let pitch;
|
||||
let formattedPitch = 'caught';
|
||||
let regex = /[A-Gb#]/g;
|
||||
|
||||
let pitches = [];
|
||||
let formattedPitches = [];
|
||||
|
||||
// index = Math.floor(Math.random() * 100) % pitchsets[0].length;
|
||||
|
||||
// const toParse = pitchsets[0][index];
|
||||
// const parsed = toParse.match(regex).join('');
|
||||
// console.log(parsed);
|
||||
|
||||
for (let voice of pitchsets) {
|
||||
while (formattedPitches.length < 4) {
|
||||
index = Math.floor(Math.random() * 100) % voice.length;
|
||||
pitch = voice[index];
|
||||
formattedPitch = pitch.match(regex).join('');
|
||||
if (!formattedPitches.includes(formattedPitch)) {
|
||||
formattedPitches.push(formattedPitch);
|
||||
pitches.push(pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(formattedPitches)
|
||||
console.log(pitches);
|
||||
@@ -1,4 +1,5 @@
|
||||
import { audioTest, fullRandomChord } from '../app.js';
|
||||
import { audioTest } from '../app.js';
|
||||
import { fullRandomChord } from './audioUtil.js';
|
||||
|
||||
// slider variables referring to DOM
|
||||
export const sopranoVol = document.getElementById('soprano-vol');
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { getRandomPitches } from "../harmonyUtil.js";
|
||||
import { extractPitchset } from "./extractPitchset.js";
|
||||
import { findVector } from "./findVector.js";
|
||||
|
||||
export const evaluateVector = (vector) => {
|
||||
return ((vector.includes(1) || vector.includes(6)));
|
||||
}
|
||||
|
||||
export const rejectDissonance = (pitchset) => {
|
||||
const vector = findVector(pitchset);
|
||||
|
||||
// returns the pitchset and its vector if evaluateVector returns true,
|
||||
if (evaluateVector(vector)) return vector;
|
||||
|
||||
// and recursively calls the function otherwise.
|
||||
if (!evaluateVector(vector)) {
|
||||
let newPitches = getRandomPitches();
|
||||
let newPitchset = extractPitchset(newPitches);
|
||||
rejectDissonance(newPitchset);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user