Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
IT LIVES! see if rests are ok in exploratory?
Browse files Browse the repository at this point in the history
  • Loading branch information
hcientist committed Feb 26, 2024
1 parent fc429d2 commit 986251f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
26 changes: 24 additions & 2 deletions components/flatEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function FlatEditor({
.ready()
.then(() => {
if (score.scoreId === 'blank' && orig) {
let result = pitchesToRests(JSON.parse(orig));
let result = pitchesToRests(JSON.parse(orig)); // TODO: watch out here for the uuid issues
if (trim) {
result = trimScore(result, trim);
}
Expand Down Expand Up @@ -293,15 +293,37 @@ function FlatEditor({
embed.loadJSON(result).then(() => {
embed.off('cursorPosition');
embed.on('cursorPosition', (ev) => {
// FIXME probably we need to debounce this event. when the paste has succeeded, don't notice that resulting change of cursor and try to do this again.
console.log('cursorPos', ev)
// selectedMeasure
console.log('json.current', json.current)
if (json.current && selectedMeasure && selectedMeasure.current && JSON.stringify(selectedMeasure.current)!== JSON && json.current !== '{}') {
if (json.current && selectedMeasure && selectedMeasure.current && JSON.stringify(selectedMeasure.current)!== '{}' && json.current !== '{}') {
console.log('selectedMeasure.current', selectedMeasure.current)
const scoreData = JSON.parse(json.current);
const correctedSelection = correctMeasure(JSON.parse(JSON.stringify(selectedMeasure.current)));
console.log('correctedSelection', correctedSelection)
scoreData['score-partwise'].part[0].measure[ev.measureIdx] = correctedSelection
//FIXME
if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx], 'attributes')) {
console.log('no attributes', scoreData['score-partwise'].part[0].measure[ev.measureIdx])
scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes = [{}];

}
if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0], '$adagio-location')) {
scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0]['$adagio-location'] = {
"timePos": 0,
"dpq": 1
}
}
if (!Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0], '$adagio-time')) {
scoreData['score-partwise'].part[0].measure[ev.measureIdx].attributes[0]['$adagio-time'] = {
"beats": "4",
"beat-type": "4"
}
}
if (Object.hasOwn(scoreData['score-partwise'].part[0].measure[ev.measureIdx], 'barline')) {
delete scoreData['score-partwise'].part[0].measure[ev.measureIdx].barline;
}
selectedMeasure.current = {};
const toLoad = JSON.stringify(scoreData);
console.log('erroneous toLoad', toLoad);
Expand Down
6 changes: 6 additions & 0 deletions lib/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ function colorMap (color) {


const pitchesToRests = (pieceScoreJSON) => {
// overwrite divisions
// ensure that duration and timePos reflect the updated divisions
const getMeasureTimeSignature = (measure, current) => {
let duration = 8; // default to 8 because i reasoned it might be a quarter in some cases
let maxRests = 4; // bc 4 is a common denominator for musicians
let updated = false;
if (measure.attributes) {
measure.attributes.forEach((attribute) => {
if (attribute.divisions) {
console.log('p2Rests::getMTimeSig:divisions', attribute.divisions)
duration = attribute.divisions;
updated = true;
}
Expand Down Expand Up @@ -49,6 +52,9 @@ const pitchesToRests = (pieceScoreJSON) => {

// }
composeScoreJSON['score-partwise'].part[0].measure.forEach((measure) => {
if (measure.attributes && measure.attributes[0].divisions) {
measure.attributes[0].divisions = 2;
}
currentTimeSig = getMeasureTimeSignature(measure, currentTimeSig); // FIXME: this overwrites later measures with the default
if (measure.direction) {
measure.direction.forEach((directionObj) => {
Expand Down
7 changes: 5 additions & 2 deletions lib/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ function correctMeasure(measure) {
// console.log("origDivisions", alteredMeasure.attributes[0].divisions);
// console.log("inside correctMeasure measure", JSON.parse(JSON.stringify(alteredMeasure)));

let modify = false;
// check if reconstruction necessary
let modify = origDivisions != defaultDiv && (alteredMeasure.attributes[0].divisions = defaultDiv);
if (Object.hasOwn(alteredMeasure, 'attributes') && Array.isArray(alteredMeasure.attributes) && alteredMeasure.attributes.length > 0) {
modify = origDivisions != defaultDiv && (alteredMeasure.attributes[0].divisions = defaultDiv);
}

// only modify if necessary
modify && (alteredMeasure.note.forEach((note) => {
Expand All @@ -62,7 +65,7 @@ function correctMeasure(measure) {
// an explicit property describing whether a beat is a rest makes life easier
// (idt you can *always* use null in the way you can in a lang like C, so
// this is more out of an abundance of caution)
alteredMeasure.note.forEach((note) => {
alteredMeasure?.note?.forEach((note) => {
note.isRest = (Boolean) (note.rest ?? false);
});
// console.log(`outgoing corrected Measure w/ isRest property`, [...alteredMeasure.note]);
Expand Down

0 comments on commit 986251f

Please sign in to comment.