关于Godot游戏引擎使用代码生成类锯齿波(相位器)

extends AudioStreamPlayer

var note_table = {
	"C":256,
	"D":288,
	"E":320,
	"F":341,
	"G":384,
	"A":426,
	"B":480,
}


const sample_rate = 22050.0
var frequency = 440.0
var amplitude = 0.5
var phase = 0

var playback: AudioStreamPlayback = null

var index = 0

func _ready():
	self.stream.mix_rate = sample_rate
	playback = get_stream_playback()
	_fill_buffer()
	self.play()
	
func _process(delta):
	_fill_buffer()
	
func cal_saw(p_value):
	return amplitude * sin(p_value)
	
func _fill_buffer():
	var increment = frequency / sample_rate

	var to_fill = playback.get_frames_available()
	while to_fill > 0:
		playback.push_frame(Vector2.ONE * phase) # Audio frames are stereo.
		phase = fmod(phase + increment, 2.0)
		to_fill -= 1
#	print(phase)


	pass # Replace with function body.

脚本挂载 

你可能感兴趣的:(多媒体程序,godot,游戏,游戏引擎,godot,多媒体程序)