Skip to content

Commit a6820a0

Browse files
committed
Add sound effect preview keyframes
1 parent b82598b commit a6820a0

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/mods/keyframeMod.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,35 @@ export function injectCustomKeyframes() {
7474
})
7575

7676
for (const channel of Object.keys(OLD_CHANNELS)) {
77+
if (channel === 'sound') continue
7778
delete EffectAnimator.prototype.channels[channel]
7879
}
7980

8081
// Modify keyframe functionality
81-
EffectAnimator.prototype.displayFrame = function (this: EffectAnimator) {
82+
EffectAnimator.prototype.displayFrame = function (this: EffectAnimator, inLoop: boolean) {
83+
// Default Blockbench Sound keyframe handling
84+
if (inLoop && !this.muted.sound) {
85+
this.sound.forEach((kf: _Keyframe) => {
86+
const diff = kf.time - this.animation.time
87+
if (diff >= 0 && diff < (1 / 60) * (Timeline.playback_speed / 100)) {
88+
if (kf.data_points[0].file && !kf.cooldown) {
89+
const media = new Audio(kf.data_points[0].file as string)
90+
media.playbackRate = Math.clamp(Timeline.playback_speed / 100, 0.1, 4.0)
91+
media.volume = Math.clamp(settings.volume.value / 100, 0, 1)
92+
media.play().catch(() => null)
93+
Timeline.playing_sounds.push(media)
94+
media.onended = function () {
95+
Timeline.playing_sounds.remove(media)
96+
}
97+
kf.cooldown = true
98+
setTimeout(() => {
99+
delete kf.cooldown
100+
}, 400)
101+
}
102+
}
103+
})
104+
}
105+
82106
if (!Project || !Project.animated_java_variants) return
83107
if (!this.muted.variants) {
84108
let after, before, result: _Keyframe | undefined
@@ -114,19 +138,20 @@ export function injectCustomKeyframes() {
114138
this.last_displayed_time = this.animation.time
115139
}
116140

117-
EffectAnimator.prototype.startPreviousSounds = function (this: EffectAnimator) {
118-
// Do nothing. Blockbench throws an error if this isn't overwritten.
119-
}
141+
// EffectAnimator.prototype.startPreviousSounds = function (this: EffectAnimator) {
142+
// // Do nothing. Blockbench throws an error if this isn't overwritten.
143+
// }
120144

121145
installed = true
122146
}
123147

124148
export function extractCustomKeyframes() {
125149
if (!installed) return
126150
EffectAnimator.prototype.displayFrame = oldEffectAnimatorDisplayFrame
127-
EffectAnimator.prototype.startPreviousSounds = oldEffectAnimatorStartPreviousSounds
151+
// EffectAnimator.prototype.startPreviousSounds = oldEffectAnimatorStartPreviousSounds
128152

129153
for (const channel of Object.keys(OLD_CHANNELS)) {
154+
if (channel === 'sound') continue
130155
EffectAnimator.prototype.channels[channel] = OLD_CHANNELS[channel]
131156
}
132157

0 commit comments

Comments
 (0)