can not push to undefined array
This commit is contained in:
10
app.ts
10
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;
|
||||
|
||||
@@ -118,9 +118,10 @@
|
||||
<!-- Tone.js library -->
|
||||
<script type="module" src="https://unpkg.com/tone@14.7.77/build/Tone.js"></script>
|
||||
<!-- internal scripts -->
|
||||
<script type="module" src="./built/src/inputHandling.js"></script>
|
||||
<script type="module" src="./built/app.js"></script>
|
||||
<script type="module" src="./built/src/toneGeneration.js"></script>
|
||||
<script type="module" src="./built/src/harmonyUtil.js"></script>
|
||||
<script type="module" src="./built/src/inputHandling.js"></script>
|
||||
<script type="module" src="./built/src/styleUtils.js"></script>
|
||||
<script type="module" src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
34
src/audioUtils.ts
Normal file
34
src/audioUtils.ts
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user