-
Notifications
You must be signed in to change notification settings - Fork 0
/
mood.ts
211 lines (175 loc) · 7.79 KB
/
mood.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
namespace soundtrack {
export class Mood {
key: number;
beatVal: BeatFraction;
beatsPM: number;
scaleType: musicUtils.ScaleType;
chords: musicUtils.Chord[];
currentChordIdx: number;
nextChordChangeTime: number;
flavorPlayStyle: PlayStyle;
bassNotes: Image; // array of notes. If there is a note there, play loud. Otherwise play random note in the current chord.
bassNoteGenStyle: (n: Note[]) => Note[];
drumPattern: Image;
drumPlayStyle: DrumPlayStyle;
// chordProgKey is the key the chord progression was written in. If you dont wanna pass it pass in your chords in the key of C
constructor(key: number, timeTop: number, timeBottom: number, scaleType: musicUtils.ScaleType, chordProg: string, bassNotes?: Image, chordProgKey = Note.C,) {
this.key = key;
this.scaleType = scaleType;
this.flavorPlayStyle = PlayStyle.OneToOne;
this.bassNotes = bassNotes
this.setTimeSignature(timeTop, timeBottom);
this.setChords(chordProg, chordProgKey)
this.drumPattern = img`
. . . .
2 3 2 3
. . 7 .
3 . . .
`
}
setChords(chordProg: string, chordProgKey: Note) {
this.generateChordsForMood(chordProg, chordProgKey);
this.currentChordIdx = -1;
this.nextChordChangeTime = 0;
}
getNote(role: TrackRole, note: PixelNote): NoteWave[] {
switch (role) {
case TrackRole.Bass: return this.getBassNotes(note);
case TrackRole.Melody: return this.getMelodyNotes(note);
case TrackRole.Rhythm: return this.getDrumNotes(note);
case TrackRole.Flavor: return this.getFlavorNotes(note);
}
}
getScale(bassOct: number, numOct: number) {
const scale = musicUtils.getScale(this.key, this.scaleType, bassOct, numOct);
return scale
}
update() {
if (this.nextChordChangeTime <= game.runtime()) {
this.nextChordChangeTime += music.beat(this.beatVal) * 4 * this.beatsPM;
this.currentChordIdx = (this.currentChordIdx + 1) % this.chords.length;
}
}
getCurrentChord() {
return this.chords[this.currentChordIdx]
}
getBassNotes(note: PixelNote): NoteWave[] {
if (this.bassNotes) {
const x = note.x % this.bassNotes.width;
const yPixelPos = this.getPixelsInCol(x, this.bassNotes);
const scale = musicUtils.getScale(
this.getCurrentChord().root,
this.getCurrentChord().isMajor()
? musicUtils.ScaleType.Major
: musicUtils.ScaleType.HarmonicMinor,
2,
this.computeNumberOctavesFromOffset(this.bassNotes.height));
const notes = []
for (let i = 0; i < yPixelPos.length; i++) {
notes.push(new NoteWave(scale[yPixelPos[i]]))
}
return notes;
} else if (this.bassNoteGenStyle) {
const notes = this.bassNoteGenStyle(this.getCurrentChord().getNotes(3, 4));
const nw = []
for (let n = 0; n < notes.length; n++) {
nw.push(new NoteWave(notes[n], music.beat(note.pixelVal)*4 * n))
}
return nw;
} else {
return [new NoteWave(musicUtils.getNoteInOctave(this.getCurrentChord().root, 2))];
}
}
getMelodyNotes(note: PixelNote): NoteWave[] {
const chord = this.getCurrentChord()
const notes = chord.getNotes(4, this.computeNumberOctavesFromOffset(note.pitchOffset, chord.getNotes(2, 1)));
return [new NoteWave(notes[note.pitchOffset])]
}
getDrumNotes(note: PixelNote): NoteWave[] {
if (this.drumPlayStyle == DrumPlayStyle.OneToOne) {
return [new NoteWave(note.pitchOffset % 4)] // 4 because we have 4 drum sounds
}
const ys = this.getPixelsInCol(note.x % this.drumPattern.width, this.drumPattern);
const notes = ys.map(y => (new NoteWave(y)))
return notes;
}
private getPixelsInCol(x: number, img: Image) {
let yPixelPos = [];
for (let y = 0; y < img.height; y++) {
if (img.getPixel(x, y) != 0) {
if (x == 0 || img.getPixel(x - 1, y) != img.getPixel(x, y)) {
// This is a fresh attack. add it
yPixelPos.push(img.height - y - 1);
}
}
}
return yPixelPos;
}
getFlavorNotes(note: PixelNote): NoteWave[] {
switch (this.flavorPlayStyle) {
case PlayStyle.Arpeggiated:
// const currentChord = this.getCurrentChord();
const arpg = musicUtils.arpeggiateMe(this.getCurrentChord().getNotes(3, 1), 6, 3)
const notes: NoteWave[] = []
for (let i = 0; i < arpg.length; i++) {
notes.push(new NoteWave(arpg[i], music.beat(note.pixelVal) / 2 * i))
}
return notes;
case PlayStyle.Octaves:
// Adventure uses octaves
const octs = musicUtils.arpeggiateMe([this.getCurrentChord().root], 4, 4);
const ret: NoteWave[] = []
octs.forEach((n, index) => {
ret.push(new NoteWave(n, index * note.pixelVal / 4))
})
return ret;
default:
// Default is 1:1
const off = note.pitchOffset
const scale = this.getScale(2, this.computeNumberOctavesFromOffset(off));
return [new NoteWave(scale[off])]
}
}
updateKey(newKey: Note) {
this.transposeChords(this.key, newKey)
}
computeNumberOctavesFromOffset(offset: number, noteOptions?: number[]) {
const len = noteOptions ? noteOptions.length : this.getScale(1,1).length;
return (Math.floor(offset / len) + 1);
}
transposeChords(oldKey: Note, newKey: Note) {
const interval = musicUtils.intervalBetweenNotes(oldKey, newKey);
for (let c = 0; c < this.chords.length; c++) {
this.chords[c].transpose(interval)
}
this.key = newKey;
}
generateChordsForMood(chordProg: string, chordProgKey: Note) {
const chordNames = chordProg.split(" ")
this.chords = [];
for (let id = 0; id < chordNames.length; id++) {
this.chords.push(musicUtils.makeChord(chordNames[id]))
}
this.transposeChords(chordProgKey, this.key)
}
setTimeSignature(top: number, bottom: number) {
switch (bottom) {
case 4: this.beatVal = BeatFraction.Quarter; break;
case 8: this.beatVal = BeatFraction.Eighth; break;
case 16: this.beatVal = BeatFraction.Sixteenth; break
case 2: this.beatVal = BeatFraction.Half; break;
}
this.beatsPM = top;
}
setFlavorPlayStyle(style: PlayStyle) {
this.flavorPlayStyle = style;
}
setDrumPattern(pattern: Image) {
this.drumPattern = pattern;
this.drumPlayStyle = DrumPlayStyle.Pattern
}
setDrumPlayStyle(style: DrumPlayStyle) {
this.drumPlayStyle = style;
}
}
}