Skip to content

Commit

Permalink
global settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabor3141 committed Oct 20, 2024
1 parent 16d7691 commit 46f3b82
Show file tree
Hide file tree
Showing 11 changed files with 237,508 additions and 35,928 deletions.
745 changes: 436 additions & 309 deletions app/components/instrument-stage.tsx

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/components/result-stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const ResultStage = ({ song, onBack }: ResultStageProps) => {
setCopySuccess(copyAttempt)
setBlueprintString(blueprint)
postHog?.capture('Generated blueprint', {
Title: song.midi.name,
'Song Settings': song.settings,
'Factorio Version': targetVersion,
Blueprint: blueprint,
Warnings: warnings,
Expand Down
272,602 changes: 237,025 additions & 35,577 deletions app/lib/__snapshots__/song-to-factorio.test.ts.snap

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/lib/blueprint/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const toBlueprint = ({

const finalBlueprint = {
blueprint: {
label: song.midi.name,
icons: [
{
signal: {
Expand Down
4 changes: 2 additions & 2 deletions app/lib/data/factorio-drumkit-sounds-by-id.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/lib/data/gm-percussion-to-factorio-drumkit.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions app/lib/factorio-instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
FactorioInstrumentName,
factorioInstrumentNameToId,
} from '@/app/lib/data/factorio-instruments-by-id'
import { factorioDrumSoundToSignal } from '@/app/lib/data/factorio-drumkit-sounds-by-id'
import { factorioDrumSoundToNoteNumber } from '@/app/lib/data/factorio-drumkit-sounds-by-id'
import { defaultDrumMap, drumMapWithOverrides } from '@/app/lib/drum-map'
import { NoteExtremes } from '@/app/lib/song'
import { Settings, TrackSettings } from '@/app/components/select-stage'
Expand Down Expand Up @@ -184,16 +184,21 @@ export const getFactorioInstrumentList = () => {
id: '2',
volumeCorrection: 0.9,
noteToFactorioNote: (note, { drumMapOverrides }) => {
note =
typeof note === 'object' && 'midi' in note
? (note.midi as MidiNote)
: note

const drumMap = drumMapWithOverrides(defaultDrumMap, drumMapOverrides)
const factorioSound = drumMap(note as MidiNote)
const factorioSound = drumMap(note)

if (!factorioSound)
return {
valid: false,
}
return {
valid: true,
factorioNote: Number(factorioDrumSoundToSignal[factorioSound]),
factorioNote: Number(factorioDrumSoundToNoteNumber[factorioSound]),
}
},
},
Expand Down
21 changes: 3 additions & 18 deletions app/lib/song-to-factorio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,9 @@ describe('Song to Blueprint', async () => {
const song = new Midi(file)
const processedSong = midiToSong(song, testFile)

expect(songToFactorioData(processedSong)).toMatchSnapshot()
},
)

test.sequential.for(testFiles)(
'Blueprint consistency - %s',
async (testFile, { expect }) => {
const file = await readFile(`test-data/${testFile}`)

const song = new Midi(file)
const processedSong = midiToSong(song, testFile)

const bp = songToFactorio(processedSong, signals)

expect(Object.keys(bp)).toEqual(['blueprint', 'warnings'])
expect(bp.blueprint).toMatch(/^0\S+/)
expect(bp.blueprint).toMatchSnapshot('blueprint')
expect(bp.warnings).toMatchSnapshot('warnings')
const factorioData = songToFactorioData(processedSong)
expect(Object.keys(factorioData)).toMatchSnapshot()
expect(factorioData).toMatchSnapshot()
},
)

Expand Down
4 changes: 2 additions & 2 deletions app/lib/song-to-factorio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export const songToFactorioData = ({ midi, settings }: Song): Speakers => {
}
}

const START_OF_SONG_TICKS_MARGIN = 10
const START_OF_SONG_TICKS_MARGIN = 4
const factorioTick =
Math.round(midiNote.time * 60 * settings.speedMultiplier) + // Seconds to 1/60 sec tick
Math.round((midiNote.time * 60) / settings.speedMultiplier) + // Seconds to 1/60 sec tick
START_OF_SONG_TICKS_MARGIN
if (
!instrumentsAfterVelocity[factorioDataInstrumentId].chords[
Expand Down
36 changes: 24 additions & 12 deletions app/lib/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import {
toFactorioInstrument,
} from '@/app/lib/factorio-instrument'
import { getVelocityValues } from '@/app/components/instrument-stage'
import { MidiNote } from 'tone/build/esm/core/type/NoteUnits'

export type NoteExtremes = {
min: number
max: number
}

export type NoteDistribution = Record<number, number>

export type Song = {
midi: Midi
additionalInfo: {
noteExtremes: NoteExtremes
trackExtremes: NoteExtremes[]
totalNotes: number
trackNoteDistribution: NoteDistribution[]
}
settings: Settings
}
Expand Down Expand Up @@ -57,7 +61,7 @@ export const getNoteExtremes = (
}

export const noteToFactorioNote = (
note: Note,
note: MidiNote | Note,
trackSettings: TrackSettings,
settings: Omit<Settings, 'tracks'>,
): FactorioNoteResultWithInstrument => {
Expand Down Expand Up @@ -90,17 +94,10 @@ export const noteToFactorioNote = (
}

export const getOutOfRangeNotes = (
input: Midi | Note[],
noteDistribution: NoteDistribution,
trackSettings: TrackSettings,
settings: Omit<Settings, 'tracks'>,
): { higher: number; lower: number } => {
const notes =
'tracks' in input
? input.tracks
.filter((track) => !track.instrument.percussion)
.flatMap((track) => track.notes)
: input

const result = {
higher: 0,
lower: 0,
Expand All @@ -110,11 +107,15 @@ export const getOutOfRangeNotes = (
// No instruments are assigned, so we consider nothing out of range
return result

notes.forEach((note) => {
const factorioNoteResult = noteToFactorioNote(note, trackSettings, settings)
Object.entries(noteDistribution).forEach(([note, numberOfOccurrences]) => {
const factorioNoteResult = noteToFactorioNote(
Number(note) as MidiNote,
trackSettings,
settings,
)

if (!factorioNoteResult.valid && factorioNoteResult.outOfRangeDirection)
result[factorioNoteResult.outOfRangeDirection]++
result[factorioNoteResult.outOfRangeDirection] += numberOfOccurrences
})

return result
Expand Down Expand Up @@ -168,11 +169,22 @@ export const midiToSong = (originalMidi: Midi, filename: string): Song => {
)
}

const trackNoteDistribution = midi.tracks.map((track) =>
track.notes.reduce(
(acc, currentValue) => ({
...acc,
[currentValue.midi]: (acc[currentValue.midi] || 0) + 1,
}),
{} as NoteDistribution,
),
)

return {
midi,
additionalInfo: {
noteExtremes: getNoteExtremes(midi),
trackExtremes: midi.tracks.map((track) => getNoteExtremes(track.notes)),
trackNoteDistribution,
totalNotes: midi.tracks.reduce(
(acc, track) => acc + track.notes.reduce((trAcc) => trAcc + 1, 0),
0,
Expand Down
6 changes: 3 additions & 3 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { SelectStage } from './components/select-stage'
import { InstrumentStage } from '@/app/components/instrument-stage'
import { ResultStage } from '@/app/components/result-stage'
import { Song } from '@/app/lib/song'
import posthog from 'posthog-js'
import { usePostHog } from 'posthog-js/react'

export type Stage = 'select' | 'instrument' | 'result'
Expand All @@ -23,7 +22,7 @@ export default function Home() {
}, [flowStage, song])

return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-8 xl:p-20">
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-dvh p-8 pb-20 gap-8 xl:p-20">
<header className="text-center">
<a href="/" className="!text-3xl !text-[#ffe6c0] bold">
miditorio.com
Expand All @@ -44,7 +43,8 @@ export default function Home() {
})
setSong((song) => {
if (!song) return
return { ...song, settings: newSettings }
// Unwrap to copy, make sure dependencies change
return { ...song, settings: { ...newSettings } }
})
}}
onBack={() => {
Expand Down

0 comments on commit 46f3b82

Please sign in to comment.