From d3c62d61b82f4d4b71dece9063994dd4139a9f7b Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Tue, 10 May 2022 11:14:47 -0500 Subject: [PATCH] can not push to undefined array --- app.ts | 10 +++----- index.html | 5 ++-- src/audioUtils.ts | 34 +++++++++++++++++++++++++ src/harmonyUtil.ts | 27 +++++++++++++++++--- src/toneGeneration.ts | 59 ++++++++++++++++++------------------------- 5 files changed, 87 insertions(+), 48 deletions(-) create mode 100644 src/audioUtils.ts diff --git a/app.ts b/app.ts index 1e8a9e7..7e2856f 100644 --- a/app.ts +++ b/app.ts @@ -1,10 +1,6 @@ import * as Tone from 'tone'; -import { - sopranoTones, altoTones, tenorTones, bassTones, - extractPitchset, -} from "./src/toneGeneration.js"; - -const pitchsets: string[][] = [sopranoTones, altoTones, tenorTones, bassTones]; +import { pitchsets } from "./src/toneGeneration.js"; +import { extractPitchset } from './src/harmonyUtil.js'; // initialize four synth voices const soprano = new Tone.Synth().toDestination(); @@ -31,7 +27,7 @@ export const soundChord = (pitches: string[]) => { // initial test: generate a single, random chord export const fullRandomChord = () => { - let pitches: string[]; + let pitches: string[] = []; for (let voice of pitchsets) { // finds a random index, excluding any which may already exist in the array let index: number; diff --git a/index.html b/index.html index 5562843..c99b8c0 100644 --- a/index.html +++ b/index.html @@ -118,9 +118,10 @@ - + + + - \ No newline at end of file diff --git a/src/audioUtils.ts b/src/audioUtils.ts new file mode 100644 index 0000000..325e69d --- /dev/null +++ b/src/audioUtils.ts @@ -0,0 +1,34 @@ +import { evaluateVector, pitchsets } from "./toneGeneration"; +import { extractPitchset, findVector } from "./harmonyUtil"; +import { soundChord } from '../app'; + +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 { + 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); + let pitchValues: number[] = extractPitchset(pitches); + pitchValues = findVector(pitchValues); + + let evaluated = evaluateVector(pitchValues); + + console.log(pitches); + console.log(evaluated); + return evaluated; +} \ No newline at end of file diff --git a/src/harmonyUtil.ts b/src/harmonyUtil.ts index d8d1c77..e5b7ae1 100644 --- a/src/harmonyUtil.ts +++ b/src/harmonyUtil.ts @@ -1,5 +1,3 @@ -import { extractPitchset } from "./toneGeneration.js"; - // interval definitions: export interface IntervalDef { number: string @@ -16,6 +14,8 @@ export const IntervalDefNames = { // ... all intervals beyond this invert to one of the previous intervals } +export const musicalPitches = ['A', "Bb", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]; + // helper functions const transposePitches = (pitches: number[], interval: number) => { let transposed = []; @@ -23,7 +23,27 @@ const transposePitches = (pitches: number[], interval: number) => { return transposed; } -const findVector = (pitches: number[]) => { +export const extractPitchset = (pitches: string[]) => { + // 1) determine pitch set from given array of pitches + let pitchset: number[] = []; + + for (let each of pitches) { + // filters numbers from above tones + const str = each; + const regex = /[0-9]/g; + const withoutNums = str.replace(regex, ''); + const pitchNumber = musicalPitches.indexOf(withoutNums); + + // ... so that they may be mapped onto numbers corresponding to the chromatic scale + pitchset.push(pitchNumber); + } + + // these are sorted from lowest to highest index (something like an interval vector) + pitchset.sort((a,b) => a - b); + return pitchset; +} + +export const findVector = (pitches: number[]) => { let sorted = pitches.sort((x,y) => x - y); // sorted = sorted.filter((num, idx) => { // return sorted.indexOf(num) === idx; @@ -88,4 +108,3 @@ console.log(''); * references @interface IntervalDef to select from @constant IntervalDefNames * @returns an array of duples, each containing a number and an entry from IntervalDef */ - diff --git a/src/toneGeneration.ts b/src/toneGeneration.ts index 25a7de8..76a2cb7 100644 --- a/src/toneGeneration.ts +++ b/src/toneGeneration.ts @@ -1,43 +1,32 @@ -import * as harmUtil from './harmonyUtil'; +import { findVector, IntervalDefNames, musicalPitches } 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"]; -export const tenorTones = ["G4", "F#4", "F4", "E4", "D4", "C4", "B3", "Bb3", "A3", "G3", "F3", "E3", "D3", "C3"]; -export const bassTones = ["C2", "D2", "E2", "F2", "G2", "A2", "Bb2", "B2", "C3", "D3", "E3", "F3", "G3"]; +const sopranoTones = ["B5", "A5", "G5", "F#5", "F5", "E5", "D5", "C5", "B4", "Bb4", "A4", "G4", "F#4", "F4", "E4"]; +const altoTones = ["E5", "D5", "C5", "B4", "Bb4", "A4", "G4", "F#4", "F4", "E4", "D4", "C4", "B3", "Bb3", "A3", "G3"]; +const tenorTones = ["G4", "F#4", "F4", "E4", "D4", "C4", "B3", "Bb3", "A3", "G3", "F3", "E3", "D3", "C3"]; +const bassTones = ["C2", "D2", "E2", "F2", "G2", "A2", "Bb2", "B2", "C3", "D3", "E3", "F3", "G3"]; -// now we define some rules to allow for the program to follow so it can some basic tenets of music theory -// we're going to include all pitches, so that it can use semitone-based pitch logic. -// this is focused on base-12, something computers understand quite well -const musicalPitches = ['A', "Bb", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]; +export const pitchsets: string[][] = [sopranoTones, altoTones, tenorTones, bassTones]; -export const extractPitchset = (pitches: string[]) => { - // 1) determine pitch set from given array of pitches - let pitchset: number[] = []; +/** + * now we define some rules to allow for the program to follow so it can some basic tenets of music theory + * we include all pitches, so that it can use semitone-based pitch logic focused on base-12 + * + * some basic rules: + * no tritones + * no minor 2nds or major 7ths +*/ - for (let each of pitches) { - // filters numbers from above tones - const str = each; - const regex = /[0-9]/g; - const withoutNums = str.replace(regex, ''); - const pitchNumber = musicalPitches.indexOf(withoutNums); +export const evaluateVector = (vector: number[]): boolean => { + let result: boolean; - // ... so that they may be mapped onto numbers corresponding to the chromatic scale - pitchset.push(pitchNumber); + for (let x of vector) { + if (x === 1 || x === 6) { + result = false; + return; + } else { + result = true; + } } - - // these are sorted from lowest to highest index (something like an interval vector) - pitchset.sort((a,b) => a - b); - console.log(pitchset); - - return pitchset; -} - -// no tritones -// no minor 2nds or major 7ths - -const evaluateVector = (vector: number[]): boolean => { - - - return true; + return result; }