Skip to content

Commit

Permalink
fix #1169 applyAccidentals
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Oct 5, 2021
1 parent 11db474 commit 6a56fe5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/accidental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ export class Accidental extends Modifier {
if (!keySignature) keySignature = 'C';

// Get the scale map, which represents the current state of each pitch.
const scaleMap = music.createScaleMap(keySignature);
const scaleMapKey = music.createScaleMap(keySignature);
const scaleMap: Record<string, string> = {};

tickPositions.forEach((tickPos: number) => {
const tickables = tickNoteMap[tickPos];
Expand All @@ -416,18 +417,20 @@ export class Accidental extends Modifier {
const staveNote = t;
staveNote.keys.forEach((keyString: string, keyIndex: number) => {
const key = music.getNoteParts(keyString.split('/')[0]);
const octave = keyString.split('/')[1];

// Force a natural for every key without an accidental
const accidentalString = key.accidental || 'n';
const pitch = key.root + accidentalString;

// Determine if the current pitch has the same accidental
// as the scale state
const sameAccidental = scaleMap[key.root] === pitch;
if (!scaleMap[key.root + octave]) scaleMap[key.root + octave] = scaleMapKey[key.root];
const sameAccidental = scaleMap[key.root + octave] === pitch;

// Determine if an identical pitch in the chord already
// modified the accidental state
const previouslyModified = modifiedPitches.indexOf(pitch) > -1;
const previouslyModified = modifiedPitches.indexOf(keyString) > -1;

// Remove accidentals
staveNote.getModifiers().forEach((modifier, index) => {
Expand All @@ -444,7 +447,7 @@ export class Accidental extends Modifier {
if (!sameAccidental || (sameAccidental && previouslyModified)) {
// Modify the scale map so that the root pitch has an
// updated state
scaleMap[key.root] = pitch;
scaleMap[key.root + octave] = pitch;

// Create the accidental
const accidental = new Accidental(accidentalString);
Expand All @@ -453,7 +456,7 @@ export class Accidental extends Modifier {
staveNote.addAccidental(keyIndex, accidental);

// Add the pitch to list of pitches that modified accidentals
modifiedPitches.push(pitch);
modifiedPitches.push(keyString);
}
});

Expand Down
16 changes: 8 additions & 8 deletions tests/accidental_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ function autoAccidentalWorking(): void {
equal(hasAccidental(notes[2]), true, 'Added flat');
equal(hasAccidental(notes[3]), true, 'Added sharp');
equal(hasAccidental(notes[4]), false, 'Sharp remembered');
equal(hasAccidental(notes[5]), false, 'Flat remembered');
equal(hasAccidental(notes[6]), false, 'Flat remembered');
equal(hasAccidental(notes[5]), true, 'Added flat(different octave)');
equal(hasAccidental(notes[6]), true, 'Added flat(different octave)');
equal(hasAccidental(notes[7]), false, 'sharp remembered');

notes = [
Expand Down Expand Up @@ -930,12 +930,12 @@ function automaticAccidentalsMultiVoiceOffset(options: TestOptions): void {

equal(hasAccidental(notes1[0]), true);
equal(hasAccidental(notes1[1]), false);
equal(hasAccidental(notes1[2]), false);
equal(hasAccidental(notes1[3]), false);
equal(hasAccidental(notes1[4]), false);
equal(hasAccidental(notes1[5]), false);
equal(hasAccidental(notes1[6]), false);
equal(hasAccidental(notes1[7]), false);
equal(hasAccidental(notes1[2]), true);
equal(hasAccidental(notes1[3]), true);
equal(hasAccidental(notes1[4]), true);
equal(hasAccidental(notes1[5]), true);
equal(hasAccidental(notes1[6]), true);
equal(hasAccidental(notes1[7]), true);

new Formatter().joinVoices([voice0, voice1]).formatToStave([voice0, voice1], stave);

Expand Down

0 comments on commit 6a56fe5

Please sign in to comment.