From e25324aec24e52c650e25e3d56ad44c9731456f8 Mon Sep 17 00:00:00 2001
From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com>
Date: Wed, 11 May 2022 12:35:58 -0500
Subject: [PATCH] several problems to solve -- project structure better defined
---
index.html | 1 +
src/audioUtils.ts | 40 +++++++++---------------
src/harmonyUtil.ts | 46 ++++++++++++++++------------
src/inputHandling.ts | 5 ++-
src/vector_logic/evaluateVector.ts | 18 +++++++++--
src/vector_logic/extractPitchset.ts | 2 +-
src/vector_logic/findVector.ts | 4 +--
src/vector_logic/numbersToPitches.ts | 5 +++
8 files changed, 69 insertions(+), 52 deletions(-)
create mode 100644 src/vector_logic/numbersToPitches.ts
diff --git a/index.html b/index.html
index 7388fb8..931ca61 100644
--- a/index.html
+++ b/index.html
@@ -112,6 +112,7 @@
+
diff --git a/src/audioUtils.ts b/src/audioUtils.ts
index 568098e..6e75b36 100644
--- a/src/audioUtils.ts
+++ b/src/audioUtils.ts
@@ -1,38 +1,28 @@
import { soundChord } from '../app.js';
-import { pitchsets } from "./harmonyUtil.js";
+import { getRandomPitches } from "./harmonyUtil.js";
-import { evaluateVector } from "./vector_logic/evaluateVector.js";
+import { evaluateVector, rejectDissonance } from "./vector_logic/evaluateVector.js";
import { findVector } from "./vector_logic/findVector.js";
import { extractPitchset } from "./vector_logic/extractPitchset.js";
export const fullRandomChord = () => {
- let pitches: string[];
- for (let voice of pitchsets) {
- // finds a random index, excluding any which may already exist in the array
- let index: number;
-
- do {
- if (!pitches) pitches = [];
- index = Math.floor(Math.random() * 100) % voice.length;
- } while (pitches.includes(voice[index]) ?? false);
-
- pitches ? pitches.push(voice[index]) : pitches = [voice[index]];
- console.log(voice[index]);
- }
-
- for (let i = 0; i < pitches.length; i++) {
- if (pitches[i] === pitches[i+1]) {
- console.log("CAUGHT");
- }
- }
-
+ let pitches: string[] = getRandomPitches();
soundChord(pitches);
+
let pitchValues: number[] = extractPitchset(pitches);
pitchValues = findVector(pitchValues);
let evaluated = evaluateVector(pitchValues);
-
- console.log(pitches);
- console.log(evaluated);
return evaluated;
+}
+
+export const evaluatedChord = () => {
+ let pitches: string[] = getRandomPitches();
+ let pitchNums: number[] = extractPitchset(pitches);
+ let firstVector = findVector(pitchNums);
+ let finalVector = rejectDissonance(firstVector);
+
+ if (finalVector !== firstVector) console.log('caught');
+
+ soundChord(pitches);
}
\ No newline at end of file
diff --git a/src/harmonyUtil.ts b/src/harmonyUtil.ts
index 5ba32df..1642599 100644
--- a/src/harmonyUtil.ts
+++ b/src/harmonyUtil.ts
@@ -32,6 +32,7 @@ const transposePitches = (pitches: number[], interval: number) => {
return transposed;
}
+// quality-of-life tool for debugging
export const labelIntervals = (vector: number[]): [number, IntervalDef][] => {
let result: [number, IntervalDef][] = [];
@@ -42,23 +43,28 @@ export const labelIntervals = (vector: number[]): [number, IntervalDef][] => {
return result;
}
-/**
- * sample uses of these functions detailed below:
- *
- * @var dMajVector = @function findVector(dMajor);
- * @param dMajor number[] ( result of earlier call to @function extractPitchset )
- * ... @returns [0,3,4,5]
- * this indicates this pitchset contains a unison, a minor third, a major third,
- * and a perfect fourth (or a corresponding inversion)
- *
- * @var complexVector = @function findVector([0,3,4,7,8,11]);
- * @returns [1,2,3,4,5]
- *
- * @var splitThird = @function findVector([0,3,4,7]);
- * @returns [1,3,4]
- *
- * @function labelIntervals
- * @param vector = number[] corresponding to sorted vector
- * references @interface IntervalDef to select from @constant IntervalDefNames
- * @returns an array of duples, each containing a number and an entry from IntervalDef
- */
+// iterates through each voice's pitchset, and selects a random pitch from each
+export const getRandomPitches = (): string[] => {
+ let pitches: string[];
+ for (let voice of pitchsets) {
+ // will store a random index
+ let index: number;
+
+ const regex = /[0-9]/g;
+
+ // repeat this iteration until it returns a number not already included in the list
+ do {
+ if (!pitches) pitches = [];
+
+ index = Math.floor(Math.random() * 100) % voice.length;
+
+ console.log(`${voice[0]}: ${index}`);
+ } while (pitches.includes(voice[index]));
+
+ // if pitches is not already initialized to an empty array, do so; otherwise, push the received value
+ pitches ? pitches.push(voice[index]) : pitches = [voice[index]];
+ console.log(voice[index]);
+ }
+
+ return pitches;
+}
diff --git a/src/inputHandling.ts b/src/inputHandling.ts
index 6b31b0b..0aa4e7c 100644
--- a/src/inputHandling.ts
+++ b/src/inputHandling.ts
@@ -1,5 +1,5 @@
import { audioTest } from '../app.js';
-import { fullRandomChord } from './audioUtils.js';
+import { evaluatedChord, fullRandomChord } from './audioUtils.js';
// slider variables referring to DOM
export const sopranoVol = document.getElementById('soprano-vol');
@@ -73,3 +73,6 @@ synthButton.onclick = audioTest;
const randChord = document.getElementById('rand-chord');
randChord.onclick = fullRandomChord;
+
+const evalChord = document.getElementById('eval-chord');
+evalChord.onclick = evaluatedChord;
diff --git a/src/vector_logic/evaluateVector.ts b/src/vector_logic/evaluateVector.ts
index 3f11878..7c10385 100644
--- a/src/vector_logic/evaluateVector.ts
+++ b/src/vector_logic/evaluateVector.ts
@@ -1,7 +1,21 @@
+import { getRandomPitches } from "../harmonyUtil.js";
+import { extractPitchset } from "./extractPitchset.js";
+import { findVector } from "./findVector.js";
+
export const evaluateVector = (vector: number[]): boolean => {
return ((vector.includes(1) || vector.includes(6)));
}
-export const rejectDissonance = (vector: number[]) => {
- return;
+export const rejectDissonance = (pitchset: number[]) => {
+ 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: string[] = getRandomPitches();
+ let newPitchset: number[] = extractPitchset(newPitches);
+ rejectDissonance(newPitchset);
+ };
}
\ No newline at end of file
diff --git a/src/vector_logic/extractPitchset.ts b/src/vector_logic/extractPitchset.ts
index 4c63b80..aed92b1 100644
--- a/src/vector_logic/extractPitchset.ts
+++ b/src/vector_logic/extractPitchset.ts
@@ -1,6 +1,6 @@
import { musicalPitches } from "../harmonyUtil.js";
-export const extractPitchset = (pitches: string[]) => {
+export const extractPitchset = (pitches: string[]): number[] => {
// 1) determine pitch set from given array of pitches
let pitchset: number[] = [];
diff --git a/src/vector_logic/findVector.ts b/src/vector_logic/findVector.ts
index 2989244..bdd8109 100644
--- a/src/vector_logic/findVector.ts
+++ b/src/vector_logic/findVector.ts
@@ -1,8 +1,5 @@
export const findVector = (pitches: number[]) => {
let sorted = pitches.sort((x,y) => x - y);
- // sorted = sorted.filter((num, idx) => {
- // return sorted.indexOf(num) === idx;
- // });
// finds each interval and logs it as a duple
let intervalClasses: number[] = [];
@@ -24,5 +21,6 @@ export const findVector = (pitches: number[]) => {
}
intervalClasses = intervalClasses.sort((x,y) => x-y);
+
return intervalClasses;
}
\ No newline at end of file
diff --git a/src/vector_logic/numbersToPitches.ts b/src/vector_logic/numbersToPitches.ts
new file mode 100644
index 0000000..2be75b7
--- /dev/null
+++ b/src/vector_logic/numbersToPitches.ts
@@ -0,0 +1,5 @@
+export const vectorToPitches = (vector: number[]): string[] => {
+
+
+ return [''];
+}
\ No newline at end of file