-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from no-chris/auto-transpose-repeated-chords
Auto transpose repeated chords
- Loading branch information
Showing
12 changed files
with
315 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/chord-mark/src/renderer/helpers/renderAllChords.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { chordRendererFactory } from 'chord-symbol'; | ||
import getChordSymbol from '../helpers/getChordSymbol'; | ||
|
||
import lineTypes from '../../parser/lineTypes'; | ||
import { | ||
transposeKey, | ||
getSemitonesBetweenKeys, | ||
} from '../../parser/helper/keyHelpers'; | ||
|
||
// eslint-disable-next-line max-lines-per-function | ||
export default function renderAllChords( | ||
allLines, | ||
detectedKey, | ||
{ | ||
transposeValue, | ||
accidentalsType, | ||
chordSymbolRenderer, | ||
simplifyChords, | ||
useShortNamings, | ||
} | ||
) { | ||
let currentKey; | ||
let baseKey; | ||
|
||
if (detectedKey) { | ||
currentKey = transposeKey(detectedKey, transposeValue, accidentalsType); | ||
} | ||
|
||
let renderChord = getChordSymbolRenderer(); | ||
|
||
function renderChords(line) { | ||
if (line.type === lineTypes.KEY_DECLARATION) { | ||
currentKey = transposeKey( | ||
line.model, | ||
transposeValue, | ||
accidentalsType | ||
); | ||
line.symbol = currentKey.string; | ||
|
||
if (!baseKey) { | ||
baseKey = currentKey; | ||
} | ||
} else if (line.type === lineTypes.CHORD) { | ||
let transposeOffSet = 0; | ||
if (shouldTransposeRepeatedChords(line)) { | ||
transposeOffSet = getSemitonesBetweenKeys( | ||
baseKey && baseKey.string, | ||
currentKey && currentKey.string | ||
); | ||
} | ||
renderChord = getChordSymbolRenderer(transposeOffSet); | ||
|
||
line.model.allBars.forEach((bar) => { | ||
bar.allChords.forEach((chord) => { | ||
chord.symbol = getChordSymbol(chord.model, renderChord); | ||
}); | ||
}); | ||
} | ||
return line; | ||
} | ||
|
||
function shouldTransposeRepeatedChords(line) { | ||
return ( | ||
line.isFromAutoRepeatChords || | ||
line.isFromSectionCopy || | ||
line.isFromChordLineRepeater | ||
); | ||
} | ||
|
||
function getChordSymbolRenderer(transposeOffSet) { | ||
if (typeof chordSymbolRenderer === 'function') { | ||
return chordSymbolRenderer; | ||
} | ||
const accidental = | ||
accidentalsType === 'auto' | ||
? currentKey | ||
? currentKey.accidental | ||
: 'sharp' | ||
: accidentalsType; | ||
|
||
return chordRendererFactory({ | ||
simplify: simplifyChords, | ||
useShortNamings, | ||
transposeValue: transposeValue + transposeOffSet, | ||
accidental, | ||
}); | ||
} | ||
|
||
return allLines.map(renderChords); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/chord-mark/tests/integration/renderer/components/renderSong/data/song5-input.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
key C | ||
|
||
#v | ||
C % F G | ||
my 1st verse | ||
|
||
#c | ||
Dm G7 C % | ||
my 1st chorus | ||
|
||
key D | ||
|
||
#v | ||
my 2nd verse | ||
|
||
key E | ||
|
||
#v | ||
my 3rd verse | ||
|
||
key Bb | ||
|
||
#v | ||
|
||
#o | ||
key G | ||
|
||
#c | ||
my second chorus | ||
|
||
#b | ||
% | ||
%% |
38 changes: 38 additions & 0 deletions
38
.../tests/integration/renderer/components/renderSong/data/song5-output-key-transposition.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
key: C | ||
|
||
Verse 1 | ||
|C |% |F |G | | ||
my 1st verse | ||
|
||
Chorus 1 | ||
|Dm |G7 |C |% | | ||
my 1st chorus | ||
|
||
key: D | ||
|
||
Verse 2 | ||
|D |% |G |A | | ||
my 2nd verse | ||
|
||
key: E | ||
|
||
Verse 3 | ||
|E |% |A |B | | ||
my 3rd verse | ||
|
||
key: Bb | ||
|
||
Verse 4 | ||
|Bb |% |Eb |F | | ||
my 1st verse | ||
|
||
Outro | ||
key: G | ||
|
||
Chorus 2 | ||
|Am |D7 |G |% | | ||
my second chorus | ||
|
||
Bridge | ||
|Am |D7 |G |% | | ||
|G |% |C |D | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.