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

Add functionality for creating loops with an intro #61

Merged
merged 3 commits into from
Mar 5, 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
17 changes: 17 additions & 0 deletions mamar-web/src/app/doc/SegmentMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styles from "./SegmentMap.module.scss"

import TrackControls from "../emu/TrackControls"
import { useBgm, useDoc, useVariation } from "../store"
import { variationReducer } from "../store/variation"
import useSelection, { SelectionProvider } from "../util/hooks/useSelection"

interface Loop {
Expand Down Expand Up @@ -81,6 +82,16 @@ function PianoRollThumbnail({ trackIndex, trackListIndex }: { trackIndex: number
}
}

function useAddNewSegment() {
const [variation, dispatch] = useVariation()
return () => dispatch({ type: "add_segment", id: variation?.segments.length ?? 1, trackList: variation?.segments.length ?? 1 }) // new action
}

function useAddLoopStart() {
const [variation, dispatch] = useVariation()
return () => dispatch({ type: "add_loop_start", id: variation?.segments.length ?? 1, iterCount: 0 }) // new action
}

function Container() {
const [variation] = useVariation()
const selection = useSelection()
Expand Down Expand Up @@ -126,12 +137,18 @@ function Container() {
}
})}
</tr>)}

</tbody>}
</table>

}

export default function SegmentMap() {
const addLoopStart = useAddLoopStart()
const addNewSegment = useAddNewSegment()
return <SelectionProvider>
<Container />
<button onClick={addLoopStart}>Add loop start</button>
<button onClick={addNewSegment}>Add new region</button>
</SelectionProvider>
}
49 changes: 49 additions & 0 deletions mamar-web/src/app/store/variation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export type VariationAction = {
type: "move_segment"
id: number
toIndex: number
} | {
type: "add_segment"
id: number
trackList: number
} | {
type: "add_loop_start"
id: number
iterCount: number
}

export function variationReducer(variation: Variation, action: VariationAction): Variation {
Expand Down Expand Up @@ -73,6 +81,47 @@ export function variationReducer(variation: Variation, action: VariationAction):
} else {
return variation
}
case "add_segment":
const newSeg: Segment = {
type: "Subseg",
id: action.id,
trackList: action.trackList,
}
return {
...variation,
segments: [
...variation.segments,
newSeg,
],
}
case "add_loop_start": {

const startLoopSeg: Segment = {
id: action.id,
type: "StartLoop",
label_index: variation.segments.length ?? 0,
}
const loopSubSeg: Segment = {
type: "Subseg",
id: action.id + 1,
trackList: variation.segments.length + 1,
}
const endLoopSeg: Segment = {
type: "EndLoop",
id: action.id + 2,
label_index: variation.segments.length + 2,
iter_count: action.iterCount,
}
return {
...variation,
segments: [
...variation.segments,
startLoopSeg,
bates64 marked this conversation as resolved.
Show resolved Hide resolved
loopSubSeg,
endLoopSeg,
],
}
}
}
}

Expand Down
Loading