Skip to content

Commit

Permalink
refactor: Extract createRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
sustained committed Sep 9, 2019
1 parent ca8049f commit 0bdc4a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/library/instruments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { note, transpose } from "@tonaljs/tonal"
import { enharmonic } from "@tonaljs/note"
import { createRange } from "./music"
import { Sampler } from "tone"

const NOTES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
Expand All @@ -20,7 +21,7 @@ export const SAMPLE_RANGES = {
harmonium: ["A2", "G#4"],
harp: ["A2", "G5"],
organ: ["A1", "F#5"],
piano: ["A0", "G#6"], // TODO: Find alternate samples with full piano range.
piano: ["A0", "C7"], // TODO: Find alternate samples with full piano range.
saxophone: ["A3", "G#4"],
trombone: ["A#0", "G#2"],
trumpet: ["A2", "G3"],
Expand All @@ -43,18 +44,10 @@ export default class Instruments {

generateSampleMaps() {
for (const [instrument, [from, to]] of Object.entries(SAMPLE_RANGES)) {
let sampleMap = {}

let fromNote = note(from)
let toNote = note(to)
let numNotes = Math.abs(toNote.height - fromNote.height)

for (let i = 0, currNote = fromNote; i < numNotes; i++) {
sampleMap[currNote.name] = `${currNote.name.replace("#", "s")}.mp3`
currNote = note(enharmonic(transpose(currNote, "m2")))
}

this.sampleMaps[instrument] = sampleMap
this.sampleMaps[instrument] = createRange(from, to).reduce((map, note) => {
map[note.name] = note.name.replace("#", "s") + ".mp3"
return map
}, {})
}

return this.sampleMaps
Expand Down
24 changes: 24 additions & 0 deletions src/library/music.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { note, transpose } from "@tonaljs/tonal"
import { enharmonic } from "@tonaljs/note"

export function createRange(from, to) {
let fromNote = note(from)
const toNote = note(to)

if (fromNote.height >= toNote.height) {
throw new Error("Reverse ranges are not yet implemented.")
}

if (fromNote.acc === "b") {
fromNote = note(enharmonic(fromNote))
}

let range = []

for (let i = 0, l = toNote.height - fromNote.height, currNote = fromNote; i < l; i++) {
range.push(currNote)
currNote = note(enharmonic(transpose(currNote, "m2")))
}

return range
}

0 comments on commit 0bdc4a4

Please sign in to comment.