diff --git a/src/chordChart.ts b/src/chordChart.ts new file mode 100644 index 0000000..2f58093 --- /dev/null +++ b/src/chordChart.ts @@ -0,0 +1,74 @@ +import Edge from "./lib/Graph/Edge"; +import GraphNode from "./lib/Graph/GraphNode"; +import Graph from "./lib/Graph/index"; +import { ChordQuality, EventData } from "./lib/helpers/index"; + +// factory to create data to populate graph +function createChord(root: string, quality: ChordQuality): EventData { + return { + root: root, + quality: quality + } +} + +// primary usable triads in C major +const cmaj = createChord("C", ChordQuality.Major); +const dmin = createChord("D", ChordQuality.Minor); +const emin = createChord("E", ChordQuality.Minor); +const fmaj = createChord("F", ChordQuality.Major); +const gmaj = createChord("G", ChordQuality.Major); +const amin = createChord("A", ChordQuality.Minor); + +// borrowed triads +const dmaj = createChord("D", ChordQuality.Major); +const emaj = createChord("E", ChordQuality.Major); +const ebmaj = createChord("Eb", ChordQuality.Major); +const fmin = createChord("F", ChordQuality.Minor); +const gmin = createChord("G", ChordQuality.Minor); +const abmaj = createChord("Ab", ChordQuality.Major); +const bbmaj = createChord("Bb", ChordQuality.Major); +const bmaj = createChord("B", ChordQuality.Major); + +// graph to host music theory logic +class ChordNode extends GraphNode { + constructor(data: EventData, id = -1, edges?: Edge[]) { + super(data, id, edges); + } + + override print() { + if (this.edges.length) { + for (let edge of this.edges) { + console.log(this); + console.log(`${this.data.root, ChordQuality[this.data.quality]} --> ${edge.end.data.root, ChordQuality[edge.end.data.quality]}`); + } + } + + console.log(this.data.root, ChordQuality[this.data.quality]); + } +} + +const chordChart = new Graph(true, true); + +chordChart.addPoints( + new ChordNode(cmaj), + new ChordNode(dmin), + new ChordNode(emin), + new ChordNode(fmaj), + new ChordNode(gmaj), + new ChordNode(amin), + new ChordNode(dmaj), + new ChordNode(emaj), + new ChordNode(ebmaj), + new ChordNode(fmin), + new ChordNode(gmin), + new ChordNode(abmaj), + new ChordNode(bbmaj), + new ChordNode(bmaj) +); + +const cmajnode = chordChart.getPointByData(cmaj) as ChordNode; +const dminnode = chordChart.getPointByData(dmin) as ChordNode; + +chordChart.addEdge(cmajnode, dminnode); + +export default chordChart; \ No newline at end of file diff --git a/src/lib/Graph/GraphNode.ts b/src/lib/Graph/GraphNode.ts index 2b87ee7..6ff1990 100644 --- a/src/lib/Graph/GraphNode.ts +++ b/src/lib/Graph/GraphNode.ts @@ -2,12 +2,16 @@ import Node from "../helpers/Node"; import Edge from "./Edge"; export default class GraphNode extends Node { - edges: Edge[] - id: number + protected edges: Edge[] + protected id: number - constructor(data: T, id: number, edges?: Edge[]) { + constructor(data: T, id?: number, edges?: Edge[]) { super(data); this.edges = edges || new Array>(); + this.id = id || -1; + } + + setID(id: number) { this.id = id; } diff --git a/src/lib/Graph/index.ts b/src/lib/Graph/index.ts index 7e17ff4..716a451 100644 --- a/src/lib/Graph/index.ts +++ b/src/lib/Graph/index.ts @@ -1,10 +1,10 @@ import GraphNode from "./GraphNode"; export default class Graph { - private isWeighted: boolean; - private isDirected: boolean; - private points: GraphNode[]; - private count = 0; + protected isWeighted: boolean; + protected isDirected: boolean; + protected points: GraphNode[]; + protected count = 0; constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode[]) { this.isWeighted = isWeighted; @@ -12,14 +12,21 @@ export default class Graph { this.points = points || new Array>(); } - createPoint(data: T) { - const newPoint = new GraphNode(data, this.count); + createPoint(data: T | GraphNode) { + let newPoint: GraphNode; + if (data instanceof GraphNode) { + data.setID(this.count); + newPoint = data; + } else { + newPoint = new GraphNode(data, this.count); + } + this.points.push(newPoint); this.count++; return newPoint; } - addPoints(...points: T[]) { + addPoints(...points: T[] | GraphNode[]) { for (let point of points) { this.createPoint(point); } diff --git a/src/lib/helpers/Node.ts b/src/lib/helpers/Node.ts index ebd5738..98855b3 100644 --- a/src/lib/helpers/Node.ts +++ b/src/lib/helpers/Node.ts @@ -7,7 +7,7 @@ **/ export default class Node { - data?: T + public data: T constructor(data: any) { this.data = data; diff --git a/src/lib/helpers/index.ts b/src/lib/helpers/index.ts index 22882da..073e4de 100644 --- a/src/lib/helpers/index.ts +++ b/src/lib/helpers/index.ts @@ -1,8 +1,8 @@ export interface EventData { - root: string + root: string | number quality: ChordQuality - duration: number - beatStrength: 'Weak' | 'Moderate' | 'Strong' + duration?: number + beatStrength?: 'Weak' | 'Moderate' | 'Strong' } export enum ChordQuality { diff --git a/src/main.ts b/src/main.ts index 2042099..1412ee6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,3 @@ -import data from "./lib/helpers/sample"; -const { eventlist, eventgraph } = data; +import chordChart from "./chordChart"; -// eventlist.print(); -eventgraph.print(); - -// export default content; - -// const parser = new DOMParser(); -// const document = parser.parseFromString(content, 'text/html'); - -// // const target = document.getElementById("where-thing-go") as HTMLElement; - -// // target.appendChild(document); \ No newline at end of file +chordChart.print();