From 3515f9231605086f4938c283c26f219094baaae7 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Tue, 10 May 2022 10:48:33 -0500 Subject: [PATCH] defining some more harmony utils --- app.ts | 12 ++++-------- src/harmonyUtil.ts | 45 ++++++++++++++++++++++++++++++++++--------- src/toneGeneration.ts | 12 ++++++++++-- tsconfig.json | 2 +- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/app.ts b/app.ts index e908bdc..1e8a9e7 100644 --- a/app.ts +++ b/app.ts @@ -4,7 +4,7 @@ import { extractPitchset, } from "./src/toneGeneration.js"; -const pitchsets = [sopranoTones, altoTones, tenorTones, bassTones]; +const pitchsets: string[][] = [sopranoTones, altoTones, tenorTones, bassTones]; // initialize four synth voices const soprano = new Tone.Synth().toDestination(); @@ -21,7 +21,7 @@ export const audioTest = () => { } // allows a chord to be generated with input from another function -export const soundChord = (pitches) => { +export const soundChord = (pitches: string[]) => { const [s,a,t,b] = pitches; soprano.triggerAttackRelease(s, "8n"); alto.triggerAttackRelease(a, "8n"); @@ -31,10 +31,10 @@ export const soundChord = (pitches) => { // initial test: generate a single, random chord export const fullRandomChord = () => { - let pitches = []; + let pitches: string[]; for (let voice of pitchsets) { // finds a random index, excluding any which may already exist in the array - let index; + let index: number; do { index = Math.floor(Math.random() * 100) % voice.length; @@ -55,9 +55,6 @@ export const fullRandomChord = () => { } // set up transport -let clock = 0; -let slowClock = 0; - const transportStart = document.getElementById('transport-start'); const loop = new Tone.Loop((time) => { @@ -69,4 +66,3 @@ loop.probability = 0.8; transportStart.onclick = () => { Tone.Transport.start(); } - diff --git a/src/harmonyUtil.ts b/src/harmonyUtil.ts index 144f998..d8d1c77 100644 --- a/src/harmonyUtil.ts +++ b/src/harmonyUtil.ts @@ -1,7 +1,11 @@ import { extractPitchset } from "./toneGeneration.js"; // interval definitions: -const intervals = { +export interface IntervalDef { + number: string +} + +export const IntervalDefNames = { 0: "unison", 1: "minor second", 2: "major second", @@ -9,7 +13,7 @@ const intervals = { 4: "major third", 5: "perfect fourth", 6: "tritone" - // all intervals beyond this invert to one of the previous intervals + // ... all intervals beyond this invert to one of the previous intervals } // helper functions @@ -48,17 +52,40 @@ const findVector = (pitches: number[]) => { return intervalClasses; } +export const labelIntervals = (vector: number[]): [number, IntervalDef][] => { + let result: [number, IntervalDef][] = []; + + for (let x of vector) { + result.push([x, IntervalDefNames[x]]); + } + + return result; +} + // analysis let dMajor = extractPitchset(["D", "F#", "A", "D"]); const eMajor = transposePitches(dMajor, 2); console.log(eMajor); console.log(''); -let dMajVector = findVector(dMajor); -console.log(dMajVector); +/** + * 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 + */ -let complexVector = findVector([0,3,4,7,8,11]); -console.log(complexVector); - -let splitThird = findVector([0,3,4,7]); -console.log(splitThird); diff --git a/src/toneGeneration.ts b/src/toneGeneration.ts index 59224b6..25a7de8 100644 --- a/src/toneGeneration.ts +++ b/src/toneGeneration.ts @@ -1,3 +1,5 @@ +import * as harmUtil from './harmonyUtil'; + // we start with a selection of pitches that generally work okay together export const sopranoTones = ["B5", "A5", "G5", "F#5", "F5", "E5", "D5", "C5", "B4", "Bb4", "A4", "G4", "F#4", "F4", "E4"]; export const altoTones = ["E5", "D5", "C5", "B4", "Bb4", "A4", "G4", "F#4", "F4", "E4", "D4", "C4", "B3", "Bb3", "A3", "G3"]; @@ -11,7 +13,7 @@ const musicalPitches = ['A', "Bb", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G export const extractPitchset = (pitches: string[]) => { // 1) determine pitch set from given array of pitches - let pitchset: number[]; + let pitchset: number[] = []; for (let each of pitches) { // filters numbers from above tones @@ -32,4 +34,10 @@ export const extractPitchset = (pitches: string[]) => { } // no tritones -// no minor 2nds or major 7ths \ No newline at end of file +// no minor 2nds or major 7ths + +const evaluateVector = (vector: number[]): boolean => { + + + return true; +} diff --git a/tsconfig.json b/tsconfig.json index 5fd7e6a..b7b0f55 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "outDir": "./built", "allowJs": true, - "target": "es2016", + "target": "es2017", "moduleResolution": "node" }, "include": ["./src/**/*"]