asingcrow/godot/PlayVideoRecordAudio.gd

130 lines
6.4 KiB
GDScript3

extends Control
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var spectrum_play
var spectrum_record
var recorder
var raw_recording
var raw_recording_data
var playable_recording_data
var position_changing_by_code
# Called when the node enters the scene tree for the first time.
func _ready():
Globals.loadConfig() # noop if startup has done it.
## This SHOULD be how the native support works :
#var stream = VideoStreamGDNative.new()
#$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.set_stream(load(OS.get_user_data_dir()+"/converted.webm"))
#var file = $MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.stream.get_file()
#print(file)
#stream.set_file(file)
#var vp = $MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer
#vp.stream = stream
#print(vp.stream.to_string())
#vp.stream_position = -1
#var duration = vp.stream_position
#vp.stream_position = 0
#$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPosSlider.max_value = duration
## So we're back to VideoStreamWebM
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.set_stream(load(OS.get_user_data_dir()+"/converted.webm"))
var vp = $MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer
print(vp.stream.to_string())
vp.stream_position = -1
#var duration = Globals.video_duration
var duration = 313.701
vp.stream_position = 0
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPosSlider.max_value = duration
print(duration)
# And set up the recording bus and meters
var idx = AudioServer.get_bus_index("Record")
recorder = AudioServer.get_bus_effect(idx, 0)
spectrum_play = AudioServer.get_bus_effect_instance(0,0)
spectrum_record = AudioServer.get_bus_effect_instance(1,1)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer/DelaySlider.value = Globals.audio_delay_seconds * 1000
position_changing_by_code = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var magnitude: float = spectrum_play.get_magnitude_for_frequency_range(0,11050.0).length()
var energy = clamp((60 + linear2db(magnitude)) / 60, 0, 1)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer/PlaybackMeter.value = energy * 100
magnitude = spectrum_record.get_magnitude_for_frequency_range(0,11050.0).length()
energy = clamp((60 + linear2db(magnitude)) / 60, 0, 1)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer2/RecordMeter.value = energy * 100
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer2/VideoPos.text = "Video Pos : "+String($MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.stream_position)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer2/AudioPos.text = "Audio Pos : "+String($AudioStreamPlayer.get_playback_position())
if !position_changing_by_code:
position_changing_by_code = true
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPosSlider.value = $MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.stream_position
position_changing_by_code = false
func _on_RecButton_pressed():
if recorder.is_recording_active():
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.stop()
raw_recording = recorder.get_recording()
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/PlayButton.disabled = false
recorder.set_recording_active(false)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/RecordButton.text = "Record"
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/Status.text = ""
else:
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.play()
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/PlayButton.disabled = true
recorder.set_recording_active(true)
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/RecordButton.text = "Stop"
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/Status.text = "Recording..."
func _on_PlayButton_pressed():
var data = raw_recording.get_data()
$AudioStreamPlayer.stream = raw_recording
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.play()
$AudioStreamPlayer.play()
$AudioStreamPlayer.stream_paused = true
$AudioStreamPlayer.seek($MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer/DelaySlider.value/1000)
$AudioStreamPlayer.stream_paused = false
#$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPosSlider.editable = true
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer/DelaySlider.editable = true
func _on_BackgroundaudioSlider_changed(value):
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.volume_db = linear2db(value/100)
func _on_VocalSlider_value_changed(value):
$AudioStreamPlayer.volume_db = linear2db(value/100)
func _on_DelaySlider_value_changed(value):
$MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer/Label.text = "Audio Delay: "+String(value/1000)+"s"
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.paused = true
$AudioStreamPlayer.stream_paused = true
$AudioStreamPlayer.seek($MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.stream_position + value/1000)
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.paused = false
$AudioStreamPlayer.stream_paused = false
Globals.audio_delay_seconds = value/1000
Globals.saveConfig()
func _on_VideoPosSlider_value_changed(value):
if !position_changing_by_code:
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.paused = true
$AudioStreamPlayer.stream_paused = true
position_changing_by_code = true
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.streamseek(value)
$AudioStreamPlayer.seek(value + $MarginContainer/VBoxContainer/HBoxContainer3/VBoxContainer/HBoxContainer3/HBoxContainer/VBoxContainer/DelaySlider.value/1000)
$MarginContainer/VBoxContainer/HBoxContainer/VBoxContainer/VideoPlayer.paused = false
position_changing_by_code = false
$AudioStreamPlayer.stream_paused = false
func _on_Quit_pressed():
get_tree().change_scene("res://main_menu.tscn")