Skip to content

Commit

Permalink
#12 Remove/Adjust sections after rhythm changes
Browse files Browse the repository at this point in the history
tscz committed Jan 18, 2020
1 parent c4d3234 commit 5c3f84a
Showing 2 changed files with 85 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/states/analysis/analysisSlice.test.ts
Original file line number Diff line number Diff line change
@@ -147,3 +147,24 @@ it("can remove a section", () => {
expect(state.sections.byId["BRIDGE_0_4"]).toBeUndefined();
expect(state.sections.byId["UNDEFINED_0_99"]).toBeDefined();
});

it("can remove sections after changing rhythm", () => {
let bridgeSection: Section = {
type: SectionType.BRIDGE,
measures: ArrayUtil.range(0, 98)
};

let state: AnalysisState = reducer(
initialState2,
addedSection(bridgeSection)
);

expect(state.sections.allIds.length).toBe(2);
expect(state.sections.byId["BRIDGE_0_98"]).toBeDefined();
expect(state.sections.byId["UNDEFINED_98_99"]).toBeDefined();

state = reducer(state, updatedRhythm({ bpm: 60 }));

expect(state.sections.allIds.length).toBe(1);
expect(state.sections.byId["UNDEFINED_0_44"]).toBeDefined();
});
64 changes: 64 additions & 0 deletions src/states/analysis/analysisSlice.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { NormalizedObjects } from "../store";
import {
distributeMeasures,
enclosingSectionOf,
generateSectionId,
mergeSections,
replaceSections,
sectionInvalid,
@@ -118,10 +119,73 @@ const analysisSlice = createSlice({
state.firstMeasureStart,
state.audioDuration
);

const lastSection =
state.sections.byId[
state.sections.allIds[state.sections.allIds.length - 1]
];

if (lastSection === undefined) return state;

if (
parseInt(ArrayUtil.last(state.measures.allIds)) >
parseInt(lastSection.measures[lastSection.measures.length - 1])
) {
const newLastSection: Section = {
type: lastSection.type,
measures: ArrayUtil.range(
parseInt(lastSection.measures[0]),
parseInt(ArrayUtil.last(state.measures.allIds))
)
};
const id = generateSectionId(newLastSection);
delete state.sections.byId[
state.sections.allIds[state.sections.allIds.length - 1]
];
state.sections.allIds[state.sections.allIds.length - 1] = id;
state.sections.byId[id] = newLastSection;
}

removeSectionsAfterLastMeasure(state);

return state;
}
}
});

const removeSectionsAfterLastMeasure: (
state: AnalysisState
) => void = state => {
const lastMeasure = state.measures.allIds.length - 1;
let removalStart = -1;
let removalStartMeasure = -1;

for (let index = 0; index <= state.sections.allIds.length - 1; index++) {
const section = state.sections.byId[state.sections.allIds[index]];
const sectionEnd = parseInt(ArrayUtil.last(section.measures));

if (sectionEnd > lastMeasure) {
removalStart = index;
removalStartMeasure = parseInt(section.measures[0]);
break;
}
}

if (removalStart === -1) return; // No sections after last measure

replaceSections(
state.sections,
removalStart,
state.sections.allIds.length - removalStart,
[
{
measures: ArrayUtil.range(removalStartMeasure, lastMeasure),
type: SectionType.UNDEFINED
}
]
);
};

const addSection: (
state: AnalysisState,
newSection: Section

0 comments on commit 5c3f84a

Please sign in to comment.