Skip to content

Commit

Permalink
improve chord toolbox; add note/chord insertion from toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Apr 20, 2019
1 parent 702d29b commit 5e0155c
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 83 deletions.
53 changes: 47 additions & 6 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Song, Note, Chord, MeterChange, KeyChange } from "../song/song.js"
import { Song, Note, SongChord, MeterChange, KeyChange } from "../song/song.js"
import { EditorMarkers } from "./editorMarkers.js"
import { EditorNotes } from "./editorNotes.js"
import { EditorChords } from "./editorChords.js"
import { Rational } from "../util/rational.js"
import { Range } from "../util/range.js"
import { Rect } from "../util/rect.js"
import { Key, scales } from "../util/theory.js"
import { Key, scales, Chord } from "../util/theory.js"


export class Editor
Expand Down Expand Up @@ -35,16 +35,16 @@ export class Editor
.upsertNote(new Note(new Range(new Rational(6, 4), new Rational(7, 4)), 71))
.upsertNote(new Note(new Range(new Rational(7, 4), new Rational(8, 4)), 72))

.upsertChord(new Chord(new Range(new Rational(0, 4), new Rational(3, 4)), 0, 0, 0))
.upsertChord(new Chord(new Range(new Rational(4, 4), new Rational(7, 4)), 9, 0, 2))
.upsertChord(new Chord(new Range(new Rational(8, 4), new Rational(13, 4)), 2, 0, 3))
.upsertChord(new SongChord(new Range(new Rational(0, 4), new Rational(3, 4)), new Chord(0, 0, 0)))
.upsertChord(new SongChord(new Range(new Rational(4, 4), new Rational(7, 4)), new Chord(9, 0, 2)))
.upsertChord(new SongChord(new Range(new Rational(8, 4), new Rational(13, 4)), new Chord(2, 0, 3)))

.upsertMeterChange(new MeterChange(new Rational(0, 4), 4, 4))
.upsertMeterChange(new MeterChange(new Rational(11, 4), 5, 4))

.upsertKeyChange(new KeyChange(new Rational(0, 4), new Key(0, 0, scales.major.pitches)))
.upsertKeyChange(new KeyChange(new Rational(7, 4), new Key(5, 1, scales.minor.pitches)))
.upsertKeyChange(new KeyChange(new Rational(9, 4), new Key(7, -1, scales.dorian.pitches)))
.upsertKeyChange(new KeyChange(new Rational(9, 4), new Key(7, -1, scales.doubleHarmonic.pitches)))

this.timeScale = 200
this.timeScroll = 0
Expand All @@ -60,6 +60,7 @@ export class Editor
this.cursorTime = new Range(new Rational(0), new Rational(0))
this.cursorTrack = { start: 0, end: 0 }
this.cursorShow = true
this.insertionDuration = new Rational(1, 4)

this.mouseDown = false
this.mouseDownData = { pos: { x: -1, y: -1 }, time: new Rational(0) }
Expand Down Expand Up @@ -93,6 +94,46 @@ export class Editor
}


insertNoteAtCursor(pitch)
{
const time = this.cursorTime.start.min(this.cursorTime.end)
const duration = this.insertionDuration

const id = this.song.nextId
this.song = this.song.upsertNote(new Note(Range.fromStartDuration(time, duration), 60 + pitch))

this.cursorTime = Range.fromPoint(time.add(duration))
this.cursorTrack = { start: 1, end: 1 }
this.cursorShow = false

this.selectionClear()
this.selection.add(id)

this.toolboxRefreshFn()
this.draw()
}


insertChordAtCursor(chord)
{
const time = this.cursorTime.start.min(this.cursorTime.end)
const duration = this.insertionDuration

const id = this.song.nextId
this.song = this.song.upsertChord(new SongChord(Range.fromStartDuration(time, duration), chord))

this.cursorTime = Range.fromPoint(time.add(duration))
this.cursorTrack = { start: 2, end: 2 }
this.cursorShow = false

this.selectionClear()
this.selection.add(id)

this.toolboxRefreshFn()
this.draw()
}


selectionClear()
{
this.selection.clear()
Expand Down
8 changes: 4 additions & 4 deletions src/editor/editorChords.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { KeyChange } from "../song/song.js"
import { Editor } from "./editor.js"
import { Rect } from "../util/rect.js"
import { Range } from "../util/range.js"
import { Key, scales, chordList, getRomanNumeralScaleDegreeStr, getScaleDegreeForPitch, getColorRotationForScale, getColorForScaleDegree } from "../util/theory.js"
import { Key, scales, chords, getRomanNumeralScaleDegreeStr, getScaleDegreeForPitch, getColorRotationForScale, getColorForScaleDegree } from "../util/theory.js"


export class EditorChords
Expand Down Expand Up @@ -172,7 +172,7 @@ export class EditorChords
{
const rect = this.getChordRect(chord, xStart, xEnd)

const scaleDegree = getScaleDegreeForPitch(key, chord.pitch + chord.accidental)
const scaleDegree = getScaleDegreeForPitch(key, chord.chord.rootPitch + chord.chord.rootAccidental)
const scaleDegreeRotation = getColorRotationForScale(key.scalePitches)
const color = getColorForScaleDegree(scaleDegree + scaleDegreeRotation)

Expand All @@ -188,9 +188,9 @@ export class EditorChords
this.owner.ctx.textAlign = "center"
this.owner.ctx.textBaseline = "middle"

const chordData = chordList[chord.chordKind]
const chordData = chords[chord.chord.kind]

let mainStr = getRomanNumeralScaleDegreeStr(scaleDegree, chord.accidental)
let mainStr = getRomanNumeralScaleDegreeStr(scaleDegree, chord.chord.rootAccidental)
if (chordData.symbol[0])
mainStr = mainStr.toLowerCase()

Expand Down
10 changes: 4 additions & 6 deletions src/song/song.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,19 @@ export class Note
}


export class Chord
export class SongChord
{
constructor(range, pitch, accidental, chordKind)
constructor(range, chord)
{
this.id = -1
this.range = range
this.pitch = pitch
this.accidental = accidental
this.chordKind = chordKind
this.chord = chord
}


withChanges(obj)
{
return Object.assign(new Chord(this.range, this.pitch, this.accidental, this.chordKind), { id: this.id }, obj)
return Object.assign(new SongChord(this.range, this.chord), { id: this.id }, obj)
}
}

Expand Down
Loading

0 comments on commit 5e0155c

Please sign in to comment.