getProceduralPitches calls and functions as expected
This commit is contained in:
@@ -2,9 +2,73 @@ import { pitchsets, musicalPitches } from "../harmonyUtil.js";
|
||||
import { extractPitchName } from "../vector_logic/extractPitchName.js";
|
||||
import { getRandomIndex } from "./getRandomPitches.js";
|
||||
|
||||
// iterator to prevent stack overflow
|
||||
let callCount = 0;
|
||||
|
||||
export const getProceduralPitches = () => {
|
||||
callCount++;
|
||||
|
||||
if (callCount >= 10) {
|
||||
return ["E3", "C4", "D4", "G4"];
|
||||
}
|
||||
|
||||
const bass = pitchsets[0];
|
||||
let bassNote = bass[getRandomIndex(bass)];
|
||||
|
||||
// initialize pitches to an array holding only a bass note
|
||||
let pitches = [bassNote];
|
||||
|
||||
for (let i = 1; i < 4; i++) {
|
||||
let voice = pitchsets[i];
|
||||
let idx = getRandomIndex(voice);
|
||||
let pitch = voice[idx];
|
||||
pitches.push(pitch);
|
||||
}
|
||||
|
||||
let pitchNames = pitches.map(x => extractPitchName(x));
|
||||
let pitchNums = pitchNames.map(x => musicalPitches.indexOf(x));
|
||||
|
||||
// console.log(pitches);
|
||||
// console.log(pitchNames);
|
||||
// console.log(pitchNums);
|
||||
|
||||
// this value is assigned true only if the loop below finds an interval value
|
||||
// which corresponds to a dissonance, 1 or 6
|
||||
let isDissonant = false;
|
||||
|
||||
for (let i = 0; i < pitchNums.length; i++) {
|
||||
for (let j = i; j < pitchNums.length; j++) {
|
||||
let interval = Math.abs(pitchNums[i] - pitchNums[j]);
|
||||
if (interval > 6) {
|
||||
interval = 12 - interval;
|
||||
}
|
||||
|
||||
let intervalIsDissonant = ((interval === 1) || (interval === 6));
|
||||
|
||||
if (!intervalIsDissonant) {
|
||||
continue;
|
||||
} else {
|
||||
isDissonant = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if a dissonance is found, the function is called recursively, and its value returned
|
||||
if (isDissonant) {
|
||||
let newPitches = getProceduralPitches();
|
||||
return newPitches;
|
||||
// otherwise, the original value itself is returned
|
||||
} else {
|
||||
console.log(`call count: ${callCount}`);
|
||||
callCount = 0;
|
||||
console.log(pitches);
|
||||
return pitches;
|
||||
}
|
||||
}
|
||||
|
||||
// an additional method based on the structure of getRandomPitches,
|
||||
// but taking some principles of music theory into account.
|
||||
export const getProceduralPitches = (prevPitches) => {
|
||||
export const old_getProceduralPitches = (prevPitches) => {
|
||||
// prevPitches is passed in to ensure there is no linear dissonance within voices
|
||||
let pitches = [];
|
||||
let formattedPitches = [];
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { pitchsets, musicalPitches } from '../harmonyUtil.js';
|
||||
import { extractPitchName } from './extractPitchName.js';
|
||||
import { getRandomIndex } from '../pitch_generation/getRandomPitches.js';
|
||||
import { findVector } from './findVector.js';
|
||||
|
||||
let callCount = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user