Skip to content

Commit

Permalink
#12 Normalize state for measures
Browse files Browse the repository at this point in the history
  • Loading branch information
tscz committed Jan 6, 2020
1 parent 1b497ee commit ef0c7b0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/components/audioManagement/audioManagement.tsx
Expand Up @@ -27,7 +27,7 @@ interface PropsFromState {
audioUrl: string;
status: LoadingStatus;
sections: NormalizedObjects<Section>;
measures: Measure[];
measures: NormalizedObjects<Measure>;
zoomLevel: number;
firstMeasureStart: number;
syncFirstMeasureStart: boolean;
Expand Down Expand Up @@ -108,7 +108,7 @@ class AudioManagement extends React.Component<AllProps> {
this.props.secondsPerMeasure,
this.props.audioSampleRate,
document.getElementById(ZOOMVIEW_CONTAINER)!.clientWidth,
this.props.measures.length
this.props.measures.allIds.length
)
);
}
Expand All @@ -117,10 +117,10 @@ class AudioManagement extends React.Component<AllProps> {
if (this.props.sections && this.peaks) {
this.peaks.segments.removeAll();
this.peaks.segments.add(
PeaksOptions.sectionsToSegment(this.props.sections)
PeaksOptions.sectionsToSegment(this.props.sections, this.props.measures)
);
this.peaks.points.removeAll();
this.peaks.points.add(this.props.measures);
this.peaks.points.add(PeaksOptions.measuresToPoints(this.props.measures));
this.peaks.zoom.setZoom(this.props.zoomLevel);
}
}
Expand Down
31 changes: 22 additions & 9 deletions src/components/audioManagement/peaksConfig.ts
@@ -1,6 +1,6 @@
import { SegmentAddOptions } from "peaks.js";
import { PointAddOptions, SegmentAddOptions } from "peaks.js";

import { Section, SectionType } from "../../states/analysisSlice";
import { Measure, Section, SectionType } from "../../states/analysisSlice";
import { NormalizedObjects } from "../../states/store";

export const AUDIO_DOM_ELEMENT = "audio_dom_element";
Expand Down Expand Up @@ -31,19 +31,19 @@ class PeaksOptions {
};
};

static sectionsToSegment = (sections: NormalizedObjects<Section>) => {
static sectionsToSegment = (
sections: NormalizedObjects<Section>,
measures: NormalizedObjects<Measure>
) => {
let segments: SegmentAddOptions[] = [];
sections.allIds.forEach(id => {
const section = sections.byId[id];

let segment: SegmentAddOptions = {
id:
section.type + "_" + section.firstMeasure + "-" + section.lastMeasure,
id,
labelText: section.type,
startTime: PeaksOptions.measureStartToMilliseconds(
section.firstMeasure
),
endTime: PeaksOptions.measureEndToMilliseconds(section.lastMeasure),
startTime: measures.byId[section.firstMeasure].time,
endTime: measures.byId[section.lastMeasure].time,
color: PeaksOptions.SECTIONTYPE_TO_COLOR.get(section.type),
editable: false
};
Expand All @@ -54,6 +54,19 @@ class PeaksOptions {
return segments;
};

static measuresToPoints = (measures: NormalizedObjects<Measure>) => {
let points: PointAddOptions[] = [];
measures.allIds.forEach(id => {
const measure = measures.byId[id];

let point: PointAddOptions = measure;

points.push(point);
});

return points;
};

static measureStartToMilliseconds = (measureStart: number) => measureStart;
static measureEndToMilliseconds = (measureStart: number) => measureStart;

Expand Down
6 changes: 3 additions & 3 deletions src/states/analysisSlice.test.ts
Expand Up @@ -20,7 +20,7 @@ const initialState: AnalysisState = {
secondsPerMeasure: 2.423,
audioLength: 180,
audioSampleRate: 44400,
measures: [],
measures: { allIds: [], byId: {} },
timeSignature: TimeSignatureType.FOUR_FOUR
};

Expand All @@ -31,7 +31,7 @@ const persistedAnalysisState: AnalysisState = {
secondsPerMeasure: 3,
audioLength: 190,
audioSampleRate: 48000,
measures: [],
measures: { allIds: [], byId: {} },
timeSignature: TimeSignatureType.THREE_FOUR
};

Expand Down Expand Up @@ -64,7 +64,7 @@ it("can reset an analysis from a persisted state", () => {
secondsPerMeasure: 3,
audioLength: 190,
audioSampleRate: 48000,
measures: [],
measures: { allIds: [], byId: {} },
timeSignature: TimeSignatureType.THREE_FOUR
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/states/analysisSlice.ts
Expand Up @@ -11,7 +11,7 @@ export interface AnalysisState {
readonly secondsPerMeasure: number;
readonly timeSignature: TimeSignatureType;
readonly bpm: number;
readonly measures: Measure[];
readonly measures: NormalizedObjects<Measure>;
}

export interface Section {
Expand Down Expand Up @@ -49,7 +49,7 @@ export const initialAnalysisState: AnalysisState = {
secondsPerMeasure: 2,
audioLength: 180,
audioSampleRate: 44400,
measures: [],
measures: { allIds: [], byId: {} },
timeSignature: TimeSignatureType.FOUR_FOUR
};

Expand Down Expand Up @@ -124,16 +124,17 @@ const analysisSlice = createSlice({

var lengthOfOneMeasure = (60 * timeSignature.beatsPerMeasure) / bpm;

var measures = new Array<Measure>(Math.floor(numberOfMeasures));

state.measures.allIds = new Array<string>(Math.floor(numberOfMeasures));
state.measures.byId = {};
var index = 0;

for (
let start = firstMeasureStart;
start < length;
start += lengthOfOneMeasure
) {
measures[index] = {
state.measures.allIds.push("" + index);
state.measures.byId[index] = {
time: start,
color: "",
editable: false,
Expand All @@ -143,7 +144,6 @@ const analysisSlice = createSlice({
index++;
}

state.measures = measures;
state.bpm = bpm;
state.timeSignature = timeSignatureType;
state.firstMeasureStart = firstMeasureStart;
Expand Down

0 comments on commit ef0c7b0

Please sign in to comment.