Skip to content

Commit 52b245f

Browse files
committed
refactor(synth): rename to Pitch.c4
1 parent 03c55ae commit 52b245f

14 files changed

Lines changed: 182 additions & 196 deletions

File tree

apps/synth-playground/public/examples/bpm-ramp.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const envelope = new ADSREnvelopeSynthNode(synth)
77
oscillator.connect(envelope)
88
envelope.connect(synth.output)
99

10-
oscillator.frequency.initialValue = Pitch.frequency('A4')
10+
oscillator.frequency.initialValue = Pitch.a4.hertz
1111
oscillator.waveShape.value = 'sawtooth'
1212
envelope.attack.initialValue = Time.n1.notes
1313
envelope.decay.initialValue = Time.n1.notes

apps/synth-playground/public/examples/default.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const oscillator = new OscillatorSynthNode(synth)
55

66
oscillator.connect(synth.output)
77

8-
oscillator.frequency.initialValue = Pitch.frequency('A4')
8+
oscillator.frequency.initialValue = Pitch.a4.hertz
99
oscillator.frequency.curve.holdValueAt(Time.n2)
10-
oscillator.frequency.curve.rampValueUntil(Time.n1, Pitch.frequency('A5'))
10+
oscillator.frequency.curve.rampValueUntil(Time.n1, Pitch.a5.hertz)
Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GeneratorSynthNode, GainSynthNode, Time as T, Factor, Pitch, PitchNotation } from 'synth'
1+
import { GeneratorSynthNode, GainSynthNode, Time as T, Factor, Pitch as P } from 'synth'
22
import { synth } from 'synth/playground'
33

44
const generator = new GeneratorSynthNode(synth, { maxPolyphony: 16 })
@@ -11,66 +11,61 @@ generator.waveShape.value = 'triangle'
1111
generator.attack.initialValue = T.n16.notes
1212
generator.release.initialValue = T.n8.notes
1313

14-
gain.factor.initialValue = Factor.orClamp(0.5)
15-
16-
const play = (time: T, note: PitchNotation, duration: T) => {
17-
generator.attackAt(time, Pitch.frequency(note))
18-
generator.releaseAt(time.add(duration), Pitch.frequency(note))
19-
}
14+
gain.factor.initialValue = Factor(0.5)
2015

2116
// "Still alive" © Valve
22-
const notes: [PitchNotation | null, T][] = [
23-
['C4', T.n4],
24-
['F4', T.n4],
25-
['E4', T.n8],
26-
['D4', T.n8],
27-
['D4', T.n8],
28-
['C4', T.n8],
29-
['D4', T.n8],
30-
['C4', T.n8],
31-
['C4', T.n4],
32-
['C4', T.n8],
33-
[null, T.n8],
34-
['A3', T.n8],
35-
['A#3', T.n8],
36-
['C4', T.n4],
37-
['F4', T.n4],
38-
['G4', T.n8],
39-
['F4', T.n8],
40-
['E4', T.n8],
41-
['D4', T.n8],
42-
['D4', T.n8],
43-
['E4', T.n8],
44-
['F4', T.n4],
45-
['F4', T.n8],
46-
[null, T.n8],
47-
['G4', T.n8],
48-
['A4', T.n8],
49-
['A#4', T.n8],
50-
['A#4', T.n8],
51-
['A4', T.n4],
52-
['G4', T.n4],
53-
['F4', T.n8],
54-
['G4', T.n8],
55-
['A4', T.n8],
56-
['A4', T.n8],
57-
['G4', T.n4],
58-
['F4', T.n4],
59-
['D4', T.n8],
60-
['C4', T.n8],
61-
['D4', T.n8],
62-
['F4', T.n8],
63-
['F4', T.n8],
64-
['E4', T.n4],
65-
['E4', T.n8],
66-
['F#4', T.n8],
67-
['F#4', T.n4],
68-
]
17+
const notes = [
18+
[P.c4, T.n4],
19+
[P.f4, T.n4],
20+
[P.e4, T.n8],
21+
[P.d4, T.n8],
22+
[P.d4, T.n8],
23+
[P.c4, T.n8],
24+
[P.d4, T.n8],
25+
[P.c4, T.n8],
26+
[P.c4, T.n4],
27+
[P.c4, T.n8],
28+
['no', T.n8],
29+
[P.a3, T.n8],
30+
[P.A3, T.n8],
31+
[P.c4, T.n4],
32+
[P.f4, T.n4],
33+
[P.g4, T.n8],
34+
[P.f4, T.n8],
35+
[P.e4, T.n8],
36+
[P.d4, T.n8],
37+
[P.d4, T.n8],
38+
[P.e4, T.n8],
39+
[P.f4, T.n4],
40+
[P.f4, T.n8],
41+
['no', T.n8],
42+
[P.g4, T.n8],
43+
[P.a4, T.n8],
44+
[P.A4, T.n8],
45+
[P.A4, T.n8],
46+
[P.a4, T.n4],
47+
[P.g4, T.n4],
48+
[P.f4, T.n8],
49+
[P.g4, T.n8],
50+
[P.a4, T.n8],
51+
[P.a4, T.n8],
52+
[P.g4, T.n4],
53+
[P.f4, T.n4],
54+
[P.d4, T.n8],
55+
[P.c4, T.n8],
56+
[P.d4, T.n8],
57+
[P.f4, T.n8],
58+
[P.f4, T.n8],
59+
[P.e4, T.n4],
60+
[P.e4, T.n8],
61+
[P.F4, T.n8],
62+
[P.F4, T.n4],
63+
] as const
6964

7065
let time = T.start
71-
for (const [note, duration] of notes) {
72-
if (note !== null) {
73-
play(time, note, duration)
66+
for (const [pitch, duration] of notes) {
67+
if (pitch !== 'no') {
68+
generator.attackAtFor(time, duration.notes, pitch.hertz)
7469
}
7570
time = time.add(duration)
7671
}

packages/editor/src/__mock__/project/simple-project.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ export const simpleProjectMock: Project = {
3030
beatsPerMinute: 125,
3131
endingNote: 2,
3232
notes: {
33-
'note-1': { synth: 'gen', pitch: 'C4', time: 0, duration: 0.25 },
34-
'note-2': { synth: 'gen', pitch: 'E4', time: 0.25, duration: 0.25 },
35-
'note-3': { synth: 'gen', pitch: 'D4', time: 0.5, duration: 0.25 },
36-
'note-4': { synth: 'gen', pitch: 'F4', time: 0.75, duration: 0.25 },
33+
'note-1': { synth: 'gen', pitch: 'c4', time: 0, duration: 0.25 },
34+
'note-2': { synth: 'gen', pitch: 'e4', time: 0.25, duration: 0.25 },
35+
'note-3': { synth: 'gen', pitch: 'd4', time: 0.5, duration: 0.25 },
36+
'note-4': { synth: 'gen', pitch: 'f4', time: 0.75, duration: 0.25 },
3737
},
3838
},
3939
}

packages/editor/src/components/music-sheet-tile/music-sheet-dimensions.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import { assertDefined } from '@repo/common/assert'
12
import { Notes, Pitch, type PitchNotation, Range, Time } from '@repo/synth'
23

3-
const HIGHEST_PITCH: PitchNotation = 'B9'
4-
5-
const LOWEST_PITCH: PitchNotation = 'C0'
6-
7-
export const NOTE_HALF_STEPS = Pitch.midi(HIGHEST_PITCH) - Pitch.midi(LOWEST_PITCH) + 1
4+
const HIGHEST_PITCH: PitchNotation = 'b9'
5+
const LOWEST_PITCH: PitchNotation = 'c0'
6+
const NOTE_HALF_STEPS = Pitch[HIGHEST_PITCH].midi - Pitch[LOWEST_PITCH].midi + 1
87

98
export type MusicSheetDimensions = ReturnType<typeof musicSheetDimensions>
109

@@ -37,10 +36,14 @@ export const musicSheetDimensions = (params: Params) => {
3736
height: halfStepHeightPx,
3837
width: (duration: Time) => duration.notes * wholeNoteWidthPx,
3938
left: (time: Time) => time.notes * wholeNoteWidthPx,
40-
top: (pitch: PitchNotation) => (Pitch.midi(HIGHEST_PITCH) - Pitch.midi(pitch)) * halfStepHeightPx,
39+
top: (pitch: PitchNotation) => (Pitch[HIGHEST_PITCH].midi - Pitch[pitch].midi) * halfStepHeightPx,
4140
time: (left: number) => Time.atNote(Notes.orClamp(Range.positive.clamp(left / wholeNoteWidthPx))),
42-
pitch: (top: number) =>
43-
Pitch.parseMidi(Pitch.midiRange.clamp(-top / halfStepHeightPx + Pitch.midi(HIGHEST_PITCH), 'ceil')).notation,
41+
pitch: (top: number) => {
42+
const nearestMidi = Math.ceil(-top / halfStepHeightPx + Pitch[HIGHEST_PITCH].midi)
43+
const midiPitch = Pitch[nearestMidi]
44+
assertDefined(midiPitch)
45+
return midiPitch.notation
46+
},
4447
},
4548
} as const
4649
}

packages/editor/src/components/music-sheet-tile/music-sheet-flow/music-sheet-background.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const SheetPattern: FC<PatternProps> = props => {
5151
y={index}
5252
width="100%"
5353
height={1}
54-
className={Pitch.parseName(name).isAccidental ? 'fill-secondary/50' : 'fill-background'}
54+
className={Pitch[name].isAccidental ? 'fill-secondary/50' : 'fill-background'}
5555
/>
5656
))}
5757
</pattern>

packages/editor/src/components/music-sheet-tile/music-sheet-flow/music-sheet-note-preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const MusicSheetNotePreview: FC<Props> = props => {
1616

1717
const { preview } = useUnit({ preview: editorModel.$notePreview })
1818

19-
const pitch = useRef<PitchNotation>('C0')
19+
const pitch = useRef<PitchNotation>('c0')
2020
const positionStyles = useRef<CSSProperties>({ display: 'none' })
2121

2222
if (preview) {

packages/editor/src/components/music-sheet-tile/music-sheet-piano-roll.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Pitch, type PitchName, type PitchOctave } from '@repo/synth'
1+
import { Pitch, type PitchNote, type PitchOctave } from '@repo/synth'
22
import { cn } from '@repo/ui-kit/classnames'
33
import { useUnit } from 'effector-react'
44
import { type FC, useMemo } from 'react'
@@ -29,7 +29,7 @@ export const MusicSheetPianoRoll: FC<PianoRollProps> = props => {
2929
)
3030
}
3131

32-
const FULL_STEP_PITCHES: PitchName[] = ['D', 'G', 'A']
32+
const FULL_STEP_PITCHES: PitchNote[] = ['d', 'g', 'a']
3333

3434
type PianoRollOctaveProps = {
3535
dimensions: MusicSheetDimensions
@@ -44,7 +44,7 @@ const PianoRollOctave: FC<PianoRollOctaveProps> = props => {
4444
return (
4545
<div className="relative flex flex-col-reverse" style={{ height: dimensions.octave.height }}>
4646
{Pitch.names.map((name, keyIndex) => {
47-
const { isAccidental } = Pitch.parseName(name)
47+
const { isAccidental } = Pitch[name]
4848

4949
const lineScale = FULL_STEP_PITCHES.includes(name) ? 2 : 1.5
5050

packages/editor/src/model/note-scheduler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const createNoteScheduler = createFactory((params: Params) => {
3535
const synth = nodes.get(note.synthId)?.synthNode
3636

3737
if (synth instanceof GeneratorSynthNode) {
38-
const frequency = Pitch.frequency(note.pitch)
39-
synth.attackAt(note.time, frequency)
40-
synth.releaseAt(note.time.add(note.duration), frequency)
38+
const { hertz } = Pitch[note.pitch]
39+
synth.attackAt(note.time, hertz)
40+
synth.releaseAt(note.time.add(note.duration), hertz)
4141
}
4242
}
4343
},

packages/synth/src/node/generator/generator-synth-node.stories.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export const Default: Story = {
6767
waveShape: 'triangle',
6868
})
6969

70-
generator.attackAt(Time.start, Pitch.frequency('C1'))
71-
generator.releaseAt(Time.n1.repeat(3), Pitch.frequency('C1'))
70+
generator.attackAt(Time.start, Pitch.c1.hertz)
71+
generator.releaseAt(Time.n1.repeat(3), Pitch.c1.hertz)
7272
},
7373
},
7474
}
@@ -93,10 +93,10 @@ export const Polyphony: Story = {
9393
destination: gain,
9494
})
9595

96-
generator.attackAt(Time.start, Pitch.frequency('C1'))
97-
generator.releaseAt(Time.n1.repeat(3), Pitch.frequency('C1'))
96+
generator.attackAt(Time.start, Pitch.c1.hertz)
97+
generator.releaseAt(Time.n1.repeat(3), Pitch.c1.hertz)
9898

99-
generator.attackAt(Time.n1.repeat(3), Pitch.frequency('A5'))
99+
generator.attackAt(Time.n1.repeat(3), Pitch.a5.hertz)
100100
},
101101
},
102102
}

0 commit comments

Comments
 (0)