Skip to content

Commit 143a557

Browse files
committed
Add playback mode selection
1 parent 57d53ec commit 143a557

4 files changed

Lines changed: 42 additions & 7 deletions

File tree

app/components/result-stage.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import signals from '@/app/lib/data/signals.json'
44
import signalsDlc from '@/app/lib/data/signals-dlc.json'
55
import { usePostHog } from 'posthog-js/react'
66
import { Song } from '@/app/lib/song'
7+
import { PlaybackMode } from '@/app/lib/factorio-blueprint-schema'
78

89
/**
910
* @param text
@@ -29,13 +30,20 @@ const versionOptions: Record<Version, string> = {
2930
'2SA': 'Factorio 2.x with Space Age DLC',
3031
}
3132

33+
const playbackModeOptions: Record<PlaybackMode, [string, string]> = {
34+
'global': ['Global' ,'The song can be heard everywhere'],
35+
'surface': ['Surface', 'The song can be heard on speaker\'s surface'],
36+
'local': ['Local', 'The song can be heard within the audible range around the speaker'],
37+
}
38+
3239
export type ResultStageProps = {
3340
song: Song
3441
}
3542
export const ResultStage = ({ song }: ResultStageProps) => {
3643
const postHog = usePostHog()
3744

3845
const [targetVersion, setTargetVersion] = useState<Version>('2')
46+
const [playbackMode, setPlaybackMode] = useState<PlaybackMode>('global')
3947
const [copySuccess, setCopySuccess] = useState<boolean>(false)
4048
const [blueprintString, setBlueprintString] = useState('')
4149
const [warnings, setWarnings] = useState<string[]>([])
@@ -44,7 +52,7 @@ export const ResultStage = ({ song }: ResultStageProps) => {
4452
setCopySuccess(false)
4553
const signalSet = targetVersion === '2SA' ? signalsDlc : signals
4654

47-
const { blueprint, warnings } = songToFactorio(song, signalSet)
55+
const { blueprint, warnings } = songToFactorio(song, signalSet, playbackMode)
4856
const copyAttempt = await copyToClipboard(blueprint)
4957
setWarnings(warnings)
5058
setCopySuccess(copyAttempt)
@@ -53,11 +61,12 @@ export const ResultStage = ({ song }: ResultStageProps) => {
5361
Title: song.midi.name,
5462
'Song Settings': song.settings,
5563
'Factorio Version': targetVersion,
64+
'Playback Mode': playbackMode,
5665
Blueprint: blueprint,
5766
Warnings: warnings,
5867
'Clipboard Success': copyAttempt,
5968
})
60-
}, [postHog, song, targetVersion])
69+
}, [postHog, song, targetVersion, playbackMode])
6170

6271
return (
6372
<div className="flex-column items-start gap-4">
@@ -93,6 +102,28 @@ export const ResultStage = ({ song }: ResultStageProps) => {
93102
</div>
94103
)}
95104

105+
<div className="flex-column gap-2">
106+
<p>Playback mode:</p>
107+
{Object.entries(playbackModeOptions).map(([value, texts]) => (
108+
<div className="flex gap-2 ml-4" key={value} title={texts[1]}>
109+
<label>
110+
<input
111+
type="radio"
112+
name="playback-mode"
113+
onChange={({ target: { value } }) => {
114+
setCopySuccess(false)
115+
setPlaybackMode(value as PlaybackMode)
116+
}}
117+
value={value}
118+
disabled={targetVersion === '1'}
119+
checked={value === playbackMode}
120+
/>
121+
{texts[0]}
122+
</label>
123+
</div>
124+
))}
125+
</div>
126+
96127
<div className="flex items-center gap-4">
97128
<button
98129
className={`button button-green box-border ${targetVersion === '1' ? 'disabled' : ''}`}

app/lib/blueprint/speaker-section.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const getSpeakerSection = (
1919
const en = localEntityNumberToAbsolute(entitiesSoFar)
2020

2121
Object.values(speakers).forEach(
22-
({ instrumentName, volume }, speakerIndex) => {
22+
({ instrumentName, volume, playbackMode }, speakerIndex) => {
2323
const instrument = getFactorioInstrument(instrumentName)
2424
const speakerEntity = en(speakerIndex * 2 + 1)
2525
const speakerCombinatorEntity = en(speakerIndex * 2 + 2)
@@ -49,7 +49,7 @@ export const getSpeakerSection = (
4949
},
5050
parameters: {
5151
playback_volume: instrument.volumeCorrection * volume,
52-
playback_mode: 'global',
52+
playback_mode: playbackMode,
5353
allow_polyphony: true,
5454
},
5555
},

app/lib/factorio-blueprint-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type Networks = {
3030
green: boolean
3131
}
3232

33-
export type PlaybackMode = 'surface' | 'global'
33+
export type PlaybackMode = 'local' | 'surface' | 'global'
3434

3535
export type Entity = {
3636
entity_number: number

app/lib/song-to-factorio.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { roundToNearestClusterCenter } from '@/app/lib/kmeans'
88
import groupBy from 'lodash.groupby'
99
import { FactorioInstrumentName } from '@/app/lib/data/factorio-instruments-by-id'
1010
import { noteToFactorioNote, Song } from '@/app/lib/song'
11+
import { PlaybackMode } from './factorio-blueprint-schema'
1112

1213
type FactorioNote = number
1314
type Chord = FactorioNote[]
@@ -22,10 +23,11 @@ export type Speakers = Record<
2223
chords: Chord[]
2324
instrumentName: FactorioInstrumentName
2425
volume: number
26+
playbackMode: PlaybackMode
2527
}
2628
>
2729

28-
export const songToFactorioData = ({ midi, settings }: Song): Speakers => {
30+
export const songToFactorioData = ({ midi, settings }: Song, playbackMode: PlaybackMode): Speakers => {
2931
const instrumentsAfterVelocity: Speakers = {}
3032

3133
for (const trackNumber in midi.tracks) {
@@ -54,6 +56,7 @@ export const songToFactorioData = ({ midi, settings }: Song): Speakers => {
5456
chords: [],
5557
instrumentName,
5658
volume,
59+
playbackMode,
5760
}
5861
}
5962

@@ -83,8 +86,9 @@ export const songToFactorioData = ({ midi, settings }: Song): Speakers => {
8386
export const songToFactorio = (
8487
song: Song,
8588
signals: RawSignal[],
89+
playbackMode: PlaybackMode
8690
): BlueprintResult => {
87-
const instruments = songToFactorioData(song)
91+
const instruments = songToFactorioData(song, playbackMode)
8892

8993
type Event = {
9094
time: number

0 commit comments

Comments
 (0)