Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stem directions for unbeamed notes #254

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading