Skip to content

Commit

Permalink
Merge pull request #254 from cuthbertLab/stem-directions
Browse files Browse the repository at this point in the history
Fix stem directions for unbeamed notes
  • Loading branch information
mscuthbert authored Jun 11, 2024
2 parents 906f6d3 + e0b3cb3 commit f6dc7d5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1523,18 +1523,21 @@ export class Stream<ElementType extends base.Music21Object = base.Music21Object>

/**
* makeNotation does not do anything yet, but it is a placeholder
* so it can start to be called.
* so it can start to be called. NOTE: Currently assumes that
* it is being called on FLAT Stream!
*
* TODO: move call to makeBeams from renderVexflow to here.
* TODO: move call to makeBeams from renderVexflow to here once
* it works on recursive streams.
*/
makeNotation({ inPlace=true, overrideStatus=false }={}): this {
makeNotation({ inPlace=false, overrideStatus=false }={}): this {
let out: this;
if (inPlace) {
out = this;
} else {
out = this.clone(true);
}
// already made a copy
makeNotation.setStemDirectionForUnspecified(out);
out.makeAccidentals({ inPlace: true, overrideStatus });
return out;
}
Expand Down
36 changes: 35 additions & 1 deletion src/stream/makeNotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as beam from '../beam';
import * as clef from '../clef';

import type * as meter from '../meter';
import type * as note from '../note';
import * as note from '../note';
import type * as pitch from '../pitch';
import * as stream from '../stream';
import {StreamException} from '../stream';
Expand Down Expand Up @@ -165,6 +165,40 @@ export function setStemDirectionForBeamGroups(
}
}

/**
* Sets the stem direction for unspecified notes. For beamed notes,
* they should have already had their stem directions set in setBeams
*/
export function setStemDirectionForUnspecified(
s: stream.Stream,
): void {
let single_clef_context: clef.Clef;
const all_clefs = s.rc(clef.Clef);
if (all_clefs.length === 1) {
// most common. do one search;
single_clef_context = all_clefs.first();
} else if (all_clefs.length === 0) {
// no clef here. in outer part;
single_clef_context = s.getContextByClass(clef.Clef);
}

for (const n of s.rc(note.NotRest)) {
if (n.stemDirection !== 'unspecified') {
continue;
}
let clef_context: clef.Clef;
if (!single_clef_context) {
// slower...
clef_context = n.getContextByClass(clef.Clef);
} else {
clef_context = single_clef_context;
}
if (clef_context) {
n.stemDirection = clef_context.getStemDirectionForPitches(n.pitches);
}
}
}

const _up_down: readonly string[] = ['up', 'down'];
const _up_down_unspecified: readonly string[] = ['up', 'down', 'unspecified'];

Expand Down
2 changes: 1 addition & 1 deletion src/vfShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export class Renderer {
optionalStave?: VFStave,
optional_renderOp?: renderOptions.RenderOptions,
): VFStave {
s.makeNotation({ overrideStatus: true });
s.makeNotation({ inPlace: true, overrideStatus: true });
const stave: VFStave = optionalStave ?? this.renderStave(s, optional_renderOp);
s.activeVFStave = stave;
const vf_voice = this.getVoice(s, stave);
Expand Down
2 changes: 1 addition & 1 deletion tests/moduleTests/pitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export default function tests() {
"4/4 fn1 fn1 e-8 e'-8 fn4 en4 e'n4"
).flatten();
// Function does not work, stream.ts 1353
//convertedNotes.makeNotation(inPlace=True, cautionaryNotImmediateRepeat=False);
// convertedNotes.makeNotation(inPlace=True, cautionaryNotImmediateRepeat=False);
const els = convertedNotes.elements as music21.note.Note[];
assert.equal(els[2].pitch.accidental.name, 'natural', 'Natural');
//assert.equal(els[2].pitch.accidental.displayStatus, 'True');
Expand Down

0 comments on commit f6dc7d5

Please sign in to comment.