Add Sway and Bob

This commit is contained in:
2022-12-06 00:48:50 +01:00
parent 04a4ebc899
commit 825d86f271
26 changed files with 1513 additions and 116 deletions

View File

@@ -0,0 +1,20 @@
@tool
extends EditorPlugin
func _enter_tree() -> void:
add_custom_type(
"SfxrStreamPlayer", "AudioStreamPlayer", load("res://addons/godot_sfxr/SfxrStreamPlayer.gd"),
get_editor_interface().get_base_control().get_theme_icon("AudioStreamPlayer", "EditorIcons"))
add_custom_type(
"SfxrStreamPlayer2D", "AudioStreamPlayer2D", load("res://addons/godot_sfxr/SfxrStreamPlayer2D.gd"),
get_editor_interface().get_base_control().get_theme_icon("AudioStreamPlayer2D", "EditorIcons"))
add_custom_type(
"SfxrStreamPlayer3D", "AudioStreamPlayer3D", load("res://addons/godot_sfxr/SfxrStreamPlayer3D.gd"),
get_editor_interface().get_base_control().get_theme_icon("AudioStreamPlayer3D", "EditorIcons"))
func _exit_tree() -> void:
remove_custom_type("SfxrStreamPlayer")
remove_custom_type("SfxrStreamPlayer2D")
remove_custom_type("SfxrStreamPlayer3D")

View File

@@ -0,0 +1,309 @@
extends RefCounted
class_name SfxrGenerator
var params
var wave_shape: int
var repeat_time: float
var elapsed_since_repeat: float
var arpeggio_time: int
var arpeggio_multiplier: float
var period: float
var period_mult: float
var period_mult_slide: float
var period_max: float
var enable_frequency_cutoff: bool
var duty_cycle: float
var duty_cycle_slide: float
var fltw: float
var fltw_d: float
var fltdmp: float
var flthp: float
var flthp_d: float
var enable_low_pass_filter: bool
var vibrato_speed: float
var vibrato_amplitude: float
var envelope_length: Array
var envelope_punch: float
var flanger_offset: float
var flanger_offset_slide: float
var gain: float
var sample_rate: float
func init_params(stream_player) -> void:
params = stream_player
prepare_values()
# Wave shape
wave_shape = params.wave_type
# Filter
fltw = pow(params.p_lpf_freq, 3.0) * 0.1
enable_low_pass_filter = params.p_lpf_freq != 1.0
fltw_d = 1.0 + params.p_lpf_ramp * 0.0001
fltdmp = 5.0 / (1.0 + pow(params.p_lpf_resonance, 2.0) * 20.0) * (0.01 + fltw)
if (fltdmp > 0.8):
fltdmp = 0.8
flthp = pow(params.p_hpf_freq, 2.0) * 0.1
flthp_d = 1 + params.p_hpf_ramp * 0.0003
# Vibrato
vibrato_speed = pow(params.p_vib_speed, 2.0) * 0.01
vibrato_amplitude = params.p_vib_strength * 0.5
# Envelope
envelope_length = [
floor(params.p_env_attack * params.p_env_attack * 100000.0),
floor(params.p_env_sustain * params.p_env_sustain * 100000.0),
floor(params.p_env_decay * params.p_env_decay * 100000.0),
]
envelope_punch = params.p_env_punch
# Flanger
flanger_offset = pow(params.p_pha_offset, 2.0) * 1020.0
if (params.p_pha_offset < 0.0):
flanger_offset = -flanger_offset
flanger_offset_slide = pow(params.p_pha_ramp, 2.0) * 1.0
if (params.p_pha_ramp < 0.0):
flanger_offset_slide = -flanger_offset_slide
# Repeat
repeat_time = floor(pow(1 - params.p_repeat_speed, 2.0) * 20000.0 + 32.0)
if (params.p_repeat_speed == 0.0):
repeat_time = 0.0
gain = exp(params.sound_vol) - 1.0
sample_rate = params.sample_rate
func prepare_values() -> void:
elapsed_since_repeat = 0.0
period = 100.0 / (params.p_base_freq * params.p_base_freq + 0.001)
period_max = 100.0 / (params.p_freq_limit * params.p_freq_limit + 0.001)
enable_frequency_cutoff = params.p_freq_limit > 0.0
period_mult = 1.0 - pow(params.p_freq_ramp, 3.0) * 0.01
period_mult_slide = -pow(params.p_freq_dramp, 3.0) * 0.000001
duty_cycle = 0.5 - params.p_duty * 0.5
duty_cycle_slide = -params.p_duty_ramp * 0.00005
if (params.p_arp_mod >= 0.0):
arpeggio_multiplier = 1.0 - pow(params.p_arp_mod, 2.0) * 0.9
else:
arpeggio_multiplier = 1.0 + pow(params.p_arp_mod, 2.0) * 10.0
arpeggio_time = floor(pow(1.0 - params.p_arp_speed, 2.0) * 20000.0 + 32.0)
if (params.p_arp_speed == 1.0):
arpeggio_time = 0
func get_raw_buffer() -> Array:
randomize()
var fltp: float = 0.0
var fltdp: float = 0.0
var fltphp: float = 0.0
var noise_buffer_length: int = 32
var noise_buffer: Array = []
for i in noise_buffer_length:
noise_buffer.append(randf() * 2.0 - 1.0)
var envelope_stage: int = 0
var envelope_elapsed: float = 0.0
var vibrato_phase: float = 0.0
var phase: int = 0
var ipp: int = 0
var flanger_buffer_length: int = 1024
var flanger_buffer: Array = []
for i in flanger_buffer_length:
flanger_buffer.append(0.0)
var _buffer: Array = []
var sample_sum: float = 0.0
var num_summed: float = 0.0
var summands: int = floor(44100.0 / sample_rate)
var t: float = -1.0
while t < INF:
t += 1
# Repeats
elapsed_since_repeat += 1.0
if (repeat_time != 0.0 and elapsed_since_repeat >= repeat_time):
prepare_values()
# Arpeggio (single)
if (arpeggio_time != 0 and t >= arpeggio_time):
arpeggio_time = 0
period *= arpeggio_multiplier
# Frequency slide, and frequency slide slide!
period_mult += period_mult_slide
period *= period_mult
if (period > period_max):
period = period_max
if (enable_frequency_cutoff):
break
# Vibrato
var rfperiod: float = period
if (vibrato_amplitude > 0.0):
vibrato_phase += vibrato_speed
rfperiod = period * (1.0 + sin(vibrato_phase) * vibrato_amplitude)
var iperiod: int = floor(rfperiod)
if (iperiod < SfxrGlobals.OVERSAMPLING):
iperiod = SfxrGlobals.OVERSAMPLING
# Square wave duty cycle
duty_cycle = duty_cycle + duty_cycle_slide
if (duty_cycle > 0.5):
duty_cycle = 0.5
elif (duty_cycle < 0.0):
duty_cycle = 0.0
# Volume envelope
envelope_elapsed += 1.0
if (envelope_elapsed > envelope_length[envelope_stage]):
envelope_elapsed = 0.0
envelope_stage += 1.0
if (envelope_stage > 2.0):
break
if (envelope_length[envelope_stage] == 0):
continue
var env_vol: float = 0.0
var envf: float = envelope_elapsed / envelope_length[envelope_stage]
if (envelope_stage == 0.0): # Attack
env_vol = envf
elif (envelope_stage == 1.0): # Sustain
env_vol = 1.0 + (1.0 - envf) * 2.0 * envelope_punch
else: # Decay
env_vol = 1.0 - envf
# Flanger step
flanger_offset += flanger_offset_slide
var iphase: int = abs(floor(flanger_offset))
if (iphase > 1023):
iphase = 1023
if (flthp_d != 0.0):
flthp = flthp * flthp_d
if (flthp > 0.1):
flthp = 0.1
elif (flthp < 0.00001):
flthp = 0.00001
# 8x Oversampling
var sample: float = 0.0
for i in SfxrGlobals.OVERSAMPLING:
var sub_sample: float = 0.0
phase += 1
if (phase >= iperiod):
phase %= iperiod
if (wave_shape == SfxrGlobals.WAVE_SHAPES.NOISE):
for j in noise_buffer_length:
noise_buffer[i] = randf() * 2.0 - 1.0
# Base waveform
var fp: float = float(phase) / float(iperiod)
if (wave_shape == SfxrGlobals.WAVE_SHAPES.SQUARE):
if (fp < duty_cycle):
sub_sample = 0.5
else:
sub_sample = -0.5
elif (wave_shape == SfxrGlobals.WAVE_SHAPES.SAWTOOTH):
if (fp < duty_cycle):
sub_sample = -1.0 + 2.0 * fp / duty_cycle
else:
sub_sample = 1.0 - 2.0 * (fp - duty_cycle) / (1 - duty_cycle)
elif (wave_shape == SfxrGlobals.WAVE_SHAPES.SINE):
sub_sample = sin(fp * 2.0 * PI)
elif (wave_shape == SfxrGlobals.WAVE_SHAPES.NOISE):
sub_sample = noise_buffer[int(floor(phase * 32.0 / iperiod))]
else:
printerr("ERROR: Bad wave type: " + str(wave_shape))
sub_sample = 0
# Low-pass filter
var pp: float = fltp
fltw *= fltw_d
if (fltw > 0.1):
fltw = 0.1
elif (fltw < 0.0):
fltw = 0.0
if (enable_low_pass_filter):
fltdp += (sub_sample - fltp) * fltw
fltdp -= fltdp * fltdmp
else:
fltp = sub_sample
fltdp = 0.0
fltp += fltdp
# High-pass filter
fltphp += fltp - pp
fltphp -= fltphp * flthp
sub_sample = fltphp
# Flanger
flanger_buffer[ipp & 1023] = sub_sample
sub_sample += flanger_buffer[(ipp - iphase + 1024) & 1023]
ipp = (ipp + 1) & 1023
# Final accumulation and envelope application
sample += sub_sample * env_vol
# Accumulate samples appropriately for sample rate
sample_sum += sample
num_summed += 1.0
if (num_summed >= summands):
num_summed = 0.0
sample = sample_sum / summands
sample_sum = 0.0
else:
continue
sample = sample / SfxrGlobals.OVERSAMPLING * SfxrGlobals.MASTER_VOLUME
sample *= gain
sample = floor((sample + 1) * 128)
if (sample > 255):
sample = 255;
elif (sample < 0):
sample = 0
sample += 128
if sample > 255:
sample -= 255
_buffer.append(sample)
return _buffer
func build_sample(stream_player):
init_params(stream_player)
var sample: AudioStreamWAV = stream_player.stream
if (not sample):
stream_player.stream = AudioStreamWAV.new()
sample = stream_player.stream
sample.mix_rate = sample_rate
sample.data = PackedByteArray(get_raw_buffer())
return sample

View File

@@ -0,0 +1,30 @@
extends Object
class_name SfxrGlobals
enum WAVE_SHAPES {
SQUARE,
SAWTOOTH,
SINE,
NOISE,
}
enum PRESETS {
NONE,
PICKUP,
LASER,
EXPLOSION,
POWERUP,
HIT,
JUMP,
CLICK,
BLIP,
SYNTH,
RANDOM,
TONE,
MUTATE,
}
const OVERSAMPLING = 8
const MASTER_VOLUME = 1

View File

@@ -0,0 +1,119 @@
@tool
extends AudioStreamPlayer
# Wave Shape
var wave_type: int
# Envelope
var p_env_attack: float
var p_env_sustain: float
var p_env_punch: float
var p_env_decay: float
# Tone
var p_base_freq: float
var p_freq_limit: float
var p_freq_ramp: float
var p_freq_dramp: float
# Vibrato
var p_vib_strength: float
var p_vib_speed: float
# Tonal Change
var p_arp_mod: float
var p_arp_speed: float
# Square wve duty (proportion of time signal is high vs low)
var p_duty: float
var p_duty_ramp: float
# Repeat
var p_repeat_speed: float
# Flanger
var p_pha_offset: float
var p_pha_ramp: float
# Low-pass filter
var p_lpf_freq: float
var p_lpf_ramp: float
var p_lpf_resonance: float
# High-pass filter
var p_hpf_freq: float
var p_hpf_ramp: float
# Sample parameters
var sound_vol: float
var sample_rate: float
# Sfx Generation
var sfx_timer: SceneTreeTimer
##################################
# Inspector Properties
##################################
func _get_property_list() -> Array:
return SfxrStreamPlayerInterface.object_get_property_list()
func _get(property):
return SfxrStreamPlayerInterface.object_get(self, property)
func _set(property, value) -> bool:
return SfxrStreamPlayerInterface.object_set(self, property, value)
##################################
# Defaults
##################################
func _init():
SfxrStreamPlayerInterface.object_set_defaults(self)
func property_can_revert(property: String):
return SfxrStreamPlayerInterface.object_property_can_revert(property)
func property_get_revert(property: String):
return SfxrStreamPlayerInterface.object_property_get_revert(property)
##################################
# Presets
##################################
func random_preset() -> bool:
return SfxrStreamPlayerInterface.random_preset(self)
func preset_values(preset_key: int) -> bool:
return SfxrStreamPlayerInterface.preset_values(self, preset_key)
##################################
# Playback
##################################
func _on_sfx_timer_timeout(timer: SceneTreeTimer, play_after_build: bool):
SfxrStreamPlayerInterface._on_sfx_timer_timeout(self, timer, play_after_build)
func build_sfx(play_after_build: bool = false):
SfxrStreamPlayerInterface.build_sfx(self, play_after_build)
func play(from_position: float = 0.0):
if playing:
stop()
super.play(from_position)

View File

@@ -0,0 +1,119 @@
@tool
extends AudioStreamPlayer2D
# Wave Shape
var wave_type: int
# Envelope
var p_env_attack: float
var p_env_sustain: float
var p_env_punch: float
var p_env_decay: float
# Tone
var p_base_freq: float
var p_freq_limit: float
var p_freq_ramp: float
var p_freq_dramp: float
# Vibrato
var p_vib_strength: float
var p_vib_speed: float
# Tonal Change
var p_arp_mod: float
var p_arp_speed: float
# Square wve duty (proportion of time signal is high vs low)
var p_duty: float
var p_duty_ramp: float
# Repeat
var p_repeat_speed: float
# Flanger
var p_pha_offset: float
var p_pha_ramp: float
# Low-pass filter
var p_lpf_freq: float
var p_lpf_ramp: float
var p_lpf_resonance: float
# High-pass filter
var p_hpf_freq: float
var p_hpf_ramp: float
# Sample parameters
var sound_vol: float
var sample_rate: float
# Sfx Generation
var sfx_timer: SceneTreeTimer
##################################
# Inspector Properties
##################################
func _get_property_list() -> Array:
return SfxrStreamPlayerInterface.object_get_property_list()
func _get(property):
return SfxrStreamPlayerInterface.object_get(self, property)
func _set(property, value) -> bool:
return SfxrStreamPlayerInterface.object_set(self, property, value)
##################################
# Defaults
##################################
func _init():
SfxrStreamPlayerInterface.object_set_defaults(self)
func property_can_revert(property: String):
return SfxrStreamPlayerInterface.object_property_can_revert(property)
func property_get_revert(property: String):
return SfxrStreamPlayerInterface.object_property_get_revert(property)
##################################
# Presets
##################################
func random_preset() -> bool:
return SfxrStreamPlayerInterface.random_preset(self)
func preset_values(preset_key: int) -> bool:
return SfxrStreamPlayerInterface.preset_values(self, preset_key)
##################################
# Playback
##################################
func _on_sfx_timer_timeout(timer: SceneTreeTimer, play_after_build: bool):
SfxrStreamPlayerInterface._on_sfx_timer_timeout(self, timer, play_after_build)
func build_sfx(play_after_build: bool = false):
SfxrStreamPlayerInterface.build_sfx(self, play_after_build)
func play(from_position: float = 0.0):
if playing:
stop()
super.play(from_position)

View File

@@ -0,0 +1,119 @@
@tool
extends AudioStreamPlayer3D
# Wave Shape
var wave_type: int
# Envelope
var p_env_attack: float
var p_env_sustain: float
var p_env_punch: float
var p_env_decay: float
# Tone
var p_base_freq: float
var p_freq_limit: float
var p_freq_ramp: float
var p_freq_dramp: float
# Vibrato
var p_vib_strength: float
var p_vib_speed: float
# Tonal Change
var p_arp_mod: float
var p_arp_speed: float
# Square wve duty (proportion of time signal is high vs low)
var p_duty: float
var p_duty_ramp: float
# Repeat
var p_repeat_speed: float
# Flanger
var p_pha_offset: float
var p_pha_ramp: float
# Low-pass filter
var p_lpf_freq: float
var p_lpf_ramp: float
var p_lpf_resonance: float
# High-pass filter
var p_hpf_freq: float
var p_hpf_ramp: float
# Sample parameters
var sound_vol: float
var sample_rate: float
# Sfx Generation
var sfx_timer: SceneTreeTimer
##################################
# Inspector Properties
##################################
func _get_property_list() -> Array:
return SfxrStreamPlayerInterface.object_get_property_list()
func _get(property):
return SfxrStreamPlayerInterface.object_get(self, property)
func _set(property, value):
return SfxrStreamPlayerInterface.object_set(self, property, value)
##################################
# Defaults
##################################
func _init():
SfxrStreamPlayerInterface.object_set_defaults(self)
func property_can_revert(property: String):
return SfxrStreamPlayerInterface.object_property_can_revert(property)
func property_get_revert(property: String):
return SfxrStreamPlayerInterface.object_property_get_revert(property)
##################################
# Presets
##################################
func random_preset() -> bool:
return SfxrStreamPlayerInterface.random_preset(self)
func preset_values(preset_key: int) -> bool:
return SfxrStreamPlayerInterface.preset_values(self, preset_key)
##################################
# Playback
##################################
func _on_sfx_timer_timeout(timer: SceneTreeTimer, play_after_build: bool):
SfxrStreamPlayerInterface._on_sfx_timer_timeout(self, timer, play_after_build)
func build_sfx(play_after_build: bool = false):
SfxrStreamPlayerInterface.build_sfx(self, play_after_build)
func play(from_position: float = 0.0):
if playing:
stop()
super.play(from_position)

View File

@@ -0,0 +1,477 @@
extends Object
class_name SfxrStreamPlayerInterface
##################################
# Inspector Properties
##################################
const PROPERTY_MAP = {
# Sample params
"sample_params/sound_vol": {"name": "sound_vol", "hint_string": "0,1,0.000000001", "default": 0.25},
"sample_params/sample_rate": {"name": "sample_rate", "hint_string": "6000,44100,1", "default": 44100.0},
# Envelope
"envelope/attack_time": {"name": "p_env_attack", "hint_string": "0,1,0.000000001", "default": 0.0},
"envelope/sustain_time": {"name": "p_env_sustain", "hint_string": "0,1,0.000000001", "default": 0.6641},
"envelope/punch_time": {"name": "p_env_punch", "hint_string": "0,1,0.000000001", "default": 0.0},
"envelope/decay_time": {"name": "p_env_decay", "hint_string": "0,1,0.000000001", "default": 0.0},
# Frequency
"frequency/start_frequency": {"name": "p_base_freq", "hint_string": "0,1,0.000000001", "default": 0.35173364},
"frequency/min_freq_cutoff": {"name": "p_freq_limit", "hint_string": "0,1,0.000000001", "default": 0.0},
"frequency/slide": {"name": "p_freq_ramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
"frequency/delta_slide": {"name": "p_freq_dramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
# Vibrato
"vibrato/depth": {"name": "p_vib_strength", "hint_string": "0,1,0.000000001", "default": 0.0},
"vibrato/speed": {"name": "p_vib_speed", "hint_string": "0,1,0.000000001", "default": 0.0},
# Arpeggiation
"arpeggiation/frequency_mult": {"name": "p_arp_mod", "hint_string": "-1,1,0.000000001", "default": 0.0},
"arpeggiation/change_speed": {"name": "p_arp_speed", "hint_string": "0,1,0.000000001", "default": 0.0},
# Duty cycle
"duty_cycle/duty_cycle": {"name": "p_duty", "hint_string": "0,1,0.000000001", "default": 0.0},
"duty_cycle/sweep": {"name": "p_duty_ramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
# Retrigger
"retrigger/rate": {"name": "p_repeat_speed", "hint_string": "0,1,0.000000001", "default": 0.0},
# Flanger
"flanger/offset": {"name": "p_pha_offset", "hint_string": "-1,1,0.000000001", "default": 0.0},
"flanger/sweep": {"name": "p_pha_ramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
# Low-pass filter
"low_pass_filter/cutoff_frequency": {"name": "p_lpf_freq", "hint_string": "0,1,0.000000001", "default": 1.0},
"low_pass_filter/cutoff_sweep": {"name": "p_lpf_ramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
"low_pass_filter/resonance": {"name": "p_lpf_resonance", "hint_string": "0,1,0.000000001", "default": 0.0},
# High-pass filter
"high_pass_filter/cutoff_frequency": {"name": "p_hpf_freq", "hint_string": "0,1,0.000000001", "default": 0.0},
"high_pass_filter/cutoff_sweep": {"name": "p_hpf_ramp", "hint_string": "-1,1,0.000000001", "default": 0.0},
}
static func object_get_property_list() -> Array:
var presets = SfxrGlobals.PRESETS.keys()
presets.pop_front()
var props = []
props.append({
"name": "SfxrStreamPlayer",
"type": TYPE_NIL,
"usage": PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_SCRIPT_VARIABLE,
})
for preset in presets:
props.append({
"name": "generators/" + str(preset).to_lower(),
"type": TYPE_BOOL,
"usage": PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NO_INSTANCE_STATE,
})
props.append({
"name": "wave/type",
"type": TYPE_INT,
"hint": PROPERTY_HINT_ENUM,
"hint_string": ",".join(PackedStringArray(SfxrGlobals.WAVE_SHAPES.keys())),
})
for property in PROPERTY_MAP:
props.append({
"name": property,
"type": TYPE_FLOAT,
"hint": PROPERTY_HINT_RANGE,
"hint_string": PROPERTY_MAP[property]["hint_string"],
})
props.append_array([
{
"name": "actions/force_rebuild",
"type": TYPE_BOOL,
"usage": PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_NO_INSTANCE_STATE,
},
])
return props
static func object_get(object: Node, property: String):
if property in PROPERTY_MAP:
return object[PROPERTY_MAP[property]["name"]]
elif property == "wave/type":
return object.wave_type
static func object_set(object: Node, property: String, value) -> bool:
var auto_build = Engine.is_editor_hint() and object.is_inside_tree()
if property in PROPERTY_MAP:
object[PROPERTY_MAP[property]["name"]] = value
if auto_build:
_schedule_build_sfx(object, true)
return true
elif property == "wave/type":
object.wave_type = value
if auto_build:
_schedule_build_sfx(object, true)
return true
elif property == "actions/force_rebuild":
if value and auto_build:
build_sfx(object, true)
return true
elif property == "sfxr_generator":
if not value:
value = 0
if preset_values(object, value) and auto_build:
build_sfx(object, true)
return true
elif property.begins_with("generators/"):
property = property.replace("generators/", "").to_upper()
if preset_values(object, SfxrGlobals.PRESETS.get(property, -1)) and auto_build:
build_sfx(object, true)
return true
return false
##################################
# Defaults
##################################
static func object_set_defaults(object: Node):
object.wave_type = SfxrGlobals.WAVE_SHAPES.SAWTOOTH
for property in PROPERTY_MAP:
object[PROPERTY_MAP[property]["name"]] = PROPERTY_MAP[property]["default"]
static func object_property_can_revert(property: String):
return property in PROPERTY_MAP
static func object_property_get_revert(property: String):
return PROPERTY_MAP[property]["default"]
##################################
# Helpers
##################################
static func frnd(rrange) -> float:
return randf() * rrange
static func rndr(from, to) -> float:
return randf() * (to - from) + from
static func rnd(rmax) -> float:
return floor(randf() * (rmax + 1))
##################################
# Presets
##################################
static func _presets_pickup(object: Node):
object_set_defaults(object)
object.wave_type = SfxrGlobals.WAVE_SHAPES.SAWTOOTH
object.p_base_freq = 0.4 + frnd(0.5)
object.p_env_attack = 0
object.p_env_sustain = frnd(0.1)
object.p_env_decay = 0.1 + frnd(0.4)
object.p_env_punch = 0.3 + frnd(0.3)
if rnd(1):
object.p_arp_speed = 0.5 + frnd(0.2)
object.p_arp_mod = 0.2 + frnd(0.4)
static func _presets_laser(object: Node):
object_set_defaults(object)
object.wave_type = rnd(2)
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SINE and rnd(1):
object.wave_type = rnd(1)
if rnd(2) == 0:
object.p_base_freq = 0.3 + frnd(0.6)
object.p_freq_limit = frnd(0.1)
object.p_freq_ramp = -0.35 - frnd(0.3)
else:
object.p_base_freq = 0.5 + frnd(0.5)
object.p_freq_limit = object.p_base_freq - 0.2 - frnd(0.6)
if object.p_freq_limit < 0.2:
object.p_freq_limit = 0.2
object.p_freq_ramp = -0.15 - frnd(0.2)
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SAWTOOTH:
object.p_duty = 1
if rnd(1):
object.p_duty = frnd(0.5)
object.p_duty_ramp = frnd(0.2)
else:
object.p_duty = 0.4 + frnd(0.5)
object.p_duty_ramp = -frnd(0.7)
object.p_env_attack = 0
object.p_env_sustain = 0.1 + frnd(0.2)
object.p_env_decay = frnd(0.4)
if rnd(1):
object.p_env_punch = frnd(0.3)
if rnd(2) == 0:
object.p_pha_offset = frnd(0.2)
object.p_pha_ramp = -frnd(0.2)
object.p_hpf_freq = frnd(0.3)
static func _presets_explosion(object: Node):
object_set_defaults(object)
object.wave_type = SfxrGlobals.WAVE_SHAPES.NOISE
if rnd(1):
object.p_base_freq = pow(0.1 + frnd(0.4), 2)
object.p_freq_ramp = -0.1 + frnd(0.4)
else:
object.p_base_freq = pow(0.2 + frnd(0.7), 2)
object.p_freq_ramp = -0.2 - frnd(0.2)
if rnd(4) == 0:
object.p_freq_ramp = 0
if rnd(2) == 0:
object.p_repeat_speed = 0.3 + frnd(0.5)
object.p_env_attack = 0
object.p_env_sustain = 0.1 + frnd(0.3)
object.p_env_decay = frnd(0.5)
if rnd(1):
object.p_pha_offset = -0.3 + frnd(0.9)
object.p_pha_ramp = -frnd(0.3)
object.p_env_punch = 0.2 + frnd(0.6)
if rnd(1):
object.p_vib_strength = frnd(0.7)
object.p_vib_speed = frnd(0.6)
if rnd(2) == 0:
object.p_arp_speed = 0.6 + frnd(0.3)
object.p_arp_mod = 0.8 - frnd(1.6)
static func _presets_powerup(object: Node):
object_set_defaults(object)
if rnd(1):
object.wave_type = SfxrGlobals.WAVE_SHAPES.SAWTOOTH
object.p_duty = 1
else:
object.p_duty = frnd(0.6)
object.p_base_freq = 0.2 + frnd(0.3)
if rnd(1):
object.p_freq_ramp = 0.1 + frnd(0.4)
object.p_repeat_speed = 0.4 + frnd(0.4)
else:
object.p_freq_ramp = 0.05 + frnd(0.2)
if rnd(1):
object.p_vib_strength = frnd(0.7)
object.p_vib_speed = frnd(0.6)
object.p_env_attack = 0
object.p_env_sustain = frnd(0.4)
object.p_env_decay = 0.1 + frnd(0.4)
static func _presets_hit(object: Node):
object_set_defaults(object)
object.wave_type = rnd(2)
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SINE:
object.wave_type = SfxrGlobals.WAVE_SHAPES.NOISE
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SQUARE:
object.p_duty = frnd(0.6)
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SAWTOOTH:
object.p_duty = 1
object.p_base_freq = 0.2 + frnd(0.6)
object.p_freq_ramp = -0.3 - frnd(0.4)
object.p_env_attack = 0
object.p_env_sustain = frnd(0.1)
object.p_env_decay = 0.1 + frnd(0.2)
if rnd(1):
object.p_hpf_freq = frnd(0.3)
static func _presets_jump(object: Node):
object_set_defaults(object)
object.wave_type = SfxrGlobals.WAVE_SHAPES.SQUARE
object.p_duty = frnd(0.6)
object.p_base_freq = 0.3 + frnd(0.3)
object.p_freq_ramp = 0.1 + frnd(0.2)
object.p_env_attack = 0
object.p_env_sustain = 0.1 + frnd(0.3)
object.p_env_decay = 0.1 + frnd(0.2)
if rnd(1):
object.p_hpf_freq = frnd(0.3)
if rnd(1):
object.p_lpf_freq = 1 - frnd(0.6)
static func _presets_blip(object: Node):
object_set_defaults(object)
object.wave_type = rnd(1)
if object.wave_type == SfxrGlobals.WAVE_SHAPES.SQUARE:
object.p_duty = frnd(0.6)
else:
object.p_duty = 1
object.p_base_freq = 0.2 + frnd(0.4)
object.p_env_attack = 0
object.p_env_sustain = 0.1 + frnd(0.1)
object.p_env_decay = frnd(0.2)
object.p_hpf_freq = 0.1
static func _presets_synth(object: Node):
object_set_defaults(object)
object.wave_type = rnd(1)
object.p_base_freq = [0.2723171360931539, 0.19255692561524382, 0.13615778746815113][rnd(2)]
object.p_env_attack = frnd(0.5) if rnd(4) > 3 else 0
object.p_env_sustain = frnd(1)
object.p_env_punch = frnd(1)
object.p_env_decay = frnd(0.9) + 0.1
object.p_arp_mod = [0, 0, 0, 0, -0.3162, 0.7454, 0.7454][rnd(6)]
object.p_arp_speed = frnd(0.5) + 0.4
object.p_duty = frnd(1)
object.p_duty_ramp = frnd(1) if rnd(2) == 2 else 0
object.p_lpf_freq = [1, frnd(1) * frnd(1)][rnd(1)]
object.p_lpf_ramp = rndr(-1, 1)
object.p_lpf_resonance = frnd(1)
object.p_hpf_freq = frnd(1) if rnd(3) == 3 else 0
object.p_hpf_ramp = frnd(1) if rnd(3) == 3 else 0
static func _presets_tone(object: Node):
object_set_defaults(object)
static func _presets_click(object: Node):
if rnd(1):
_presets_hit(object)
else:
_presets_explosion(object)
if rnd(1):
object.p_freq_ramp = -0.5 + frnd(1.0)
if rnd(1):
object.p_env_sustain = (frnd(0.4) + 0.2) * object.p_env_sustain
object.p_env_decay = (frnd(0.4) + 0.2) * object.p_env_decay
if rnd(3) == 0:
object.p_env_attack = frnd(0.3)
object.p_base_freq = 1 - frnd(0.25)
object.p_hpf_freq = 1 - frnd(0.1)
static func _presets_random(object: Node):
object_set_defaults(object)
object.wave_type = rnd(3)
if rnd(1):
object.p_base_freq = pow(frnd(2) - 1, 3) + 0.5
else:
object.p_base_freq = pow(frnd(1), 2)
object.p_freq_limit = 0
object.p_freq_ramp = pow(frnd(2) - 1, 5)
if object.p_base_freq > 0.7 and object.p_freq_ramp > 0.2:
object.p_freq_ramp = -object.p_freq_ramp
if object.p_base_freq < 0.2 and object.p_freq_ramp < -0.05:
object.p_freq_ramp = -object.p_freq_ramp
object.p_freq_dramp = pow(frnd(2) - 1, 3)
object.p_duty = frnd(2) - 1
object.p_duty_ramp = pow(frnd(2) - 1, 3)
object.p_vib_strength = pow(frnd(2) - 1, 3)
object.p_vib_speed = rndr(-1, 1)
object.p_env_attack = pow(rndr(-1, 1), 3)
object.p_env_sustain = pow(rndr(-1, 1), 2)
object.p_env_decay = rndr(-1, 1)
object.p_env_punch = pow(frnd(0.8), 2)
if object.p_env_attack + object.p_env_sustain + object.p_env_decay < 0.2:
object.p_env_sustain += 0.2 + frnd(0.3)
object.p_env_decay += 0.2 + frnd(0.3)
object.p_lpf_resonance = rndr(-1, 1)
object.p_lpf_freq = 1 - pow(frnd(1), 3)
object.p_lpf_ramp = pow(frnd(2) - 1, 3)
if object.p_lpf_freq < 0.1 and object.p_lpf_ramp < -0.05:
object.p_lpf_ramp = -object.p_lpf_ramp
object.p_hpf_freq = pow(frnd(1), 5)
object.p_hpf_ramp = pow(frnd(2) - 1, 5)
object.p_pha_offset = pow(frnd(2) - 1, 3)
object.p_pha_ramp = pow(frnd(2) - 1, 3)
object.p_repeat_speed = frnd(2) - 1
object.p_arp_speed = frnd(2) - 1
object.p_arp_mod = frnd(2) - 1
static func _presets_mutate(object: Node):
if rnd(1): object.p_base_freq += frnd(0.1) - 0.05
if rnd(1): object.p_freq_ramp += frnd(0.1) - 0.05
if rnd(1): object.p_freq_dramp += frnd(0.1) - 0.05
if rnd(1): object.p_duty += frnd(0.1) - 0.05
if rnd(1): object.p_duty_ramp += frnd(0.1) - 0.05
if rnd(1): object.p_vib_strength += frnd(0.1) - 0.05
if rnd(1): object.p_vib_speed += frnd(0.1) - 0.05
if rnd(1): object.p_env_attack += frnd(0.1) - 0.05
if rnd(1): object.p_env_sustain += frnd(0.1) - 0.05
if rnd(1): object.p_env_decay += frnd(0.1) - 0.05
if rnd(1): object.p_env_punch += frnd(0.1) - 0.05
if rnd(1): object.p_lpf_resonance += frnd(0.1) - 0.05
if rnd(1): object.p_lpf_freq += frnd(0.1) - 0.05
if rnd(1): object.p_lpf_ramp += frnd(0.1) - 0.05
if rnd(1): object.p_hpf_freq += frnd(0.1) - 0.05
if rnd(1): object.p_hpf_ramp += frnd(0.1) - 0.05
if rnd(1): object.p_pha_offset += frnd(0.1) - 0.05
if rnd(1): object.p_pha_ramp += frnd(0.1) - 0.05
if rnd(1): object.p_repeat_speed += frnd(0.1) - 0.05
if rnd(1): object.p_arp_speed += frnd(0.1) - 0.05
if rnd(1): object.p_arp_mod += frnd(0.1) - 0.05
static func random_preset(object: Node) -> bool:
return preset_values(object, (randi() % (len(SfxrGlobals.PRESETS) - 1)) + 1)
static func preset_values(object: Node, preset_key: int) -> bool:
if preset_key >= 0 and preset_key < len(SfxrGlobals.PRESETS):
var preset = SfxrGlobals.PRESETS.keys()[preset_key].to_lower()
match preset:
"pickup":
_presets_pickup(object)
return true
"laser":
_presets_laser(object)
return true
"explosion":
_presets_explosion(object)
return true
"powerup":
_presets_powerup(object)
return true
"hit":
_presets_hit(object)
return true
"jump":
_presets_jump(object)
return true
"click":
_presets_click(object)
return true
"blip":
_presets_blip(object)
return true
"synth":
_presets_synth(object)
return true
"random":
_presets_random(object)
return true
"tone":
_presets_tone(object)
return true
"mutate":
_presets_mutate(object)
return true
return false
##################################
# Playback
##################################
static func _schedule_build_sfx(object: Node, play_after_build: bool):
var timer: SceneTreeTimer = object.get_tree().create_timer(.5)
object.sfx_timer = timer
timer.timeout.connect(func(): object._on_sfx_timer_timeout(timer, play_after_build))
static func _on_sfx_timer_timeout(object: Node, timer: SceneTreeTimer, play_after_build: bool):
if timer == object.sfx_timer:
build_sfx(object, play_after_build)
static func build_sfx(object: Node, play_after_build: bool = false):
var sfxg = SfxrGenerator.new()
sfxg.build_sample(object)
if play_after_build:
object.play()
object.notify_property_list_changed()

View File

@@ -0,0 +1,7 @@
[plugin]
name="GodotSfxr"
description="Sfx Generator. Port of jsfxr (https://sfxr.me - by Eric Fredricksen) which is a port of sfxr (https://www.drpetter.se/project_sfxr.html - by DrPetter)."
author="Tomeyro"
version="1.0"
script="GodotSfxr.gd"