65 lines
1.7 KiB
GDScript3
65 lines
1.7 KiB
GDScript3
|
extends Control
|
||
|
|
||
|
var sample_hz = 22050.0 # Keep the number of samples to mix low, GDScript is not super fast.
|
||
|
var pulse_hz = 440.0
|
||
|
var phase = 0.0
|
||
|
var morse_state := false
|
||
|
var playback: AudioStreamPlayback = null
|
||
|
var vol_on := -30
|
||
|
|
||
|
func _process(_delta):
|
||
|
fill_buffer()
|
||
|
|
||
|
func fill_buffer():
|
||
|
var increment = pulse_hz / sample_hz
|
||
|
var frames_available = playback.get_frames_available()
|
||
|
|
||
|
for i in range(frames_available):
|
||
|
playback.push_frame(Vector2.ONE * sin(phase * TAU))
|
||
|
phase = fmod(phase + increment, 1.0)
|
||
|
|
||
|
func _ready():
|
||
|
$Player.stream.mix_rate = sample_hz
|
||
|
$Player.volume_db = -100
|
||
|
$Player.play()
|
||
|
playback = $Player.get_stream_playback()
|
||
|
fill_buffer()
|
||
|
|
||
|
OS.open_midi_inputs()
|
||
|
print(OS.get_connected_midi_inputs())
|
||
|
|
||
|
func set_morse_state(state: bool):
|
||
|
morse_state = state
|
||
|
if state:
|
||
|
%ColorRect.color = Color(0, 128, 0)
|
||
|
$Player.volume_db = vol_on
|
||
|
else:
|
||
|
%ColorRect.color = Color(0, 0, 0)
|
||
|
$Player.volume_db = -100
|
||
|
|
||
|
func _on_morse_button_down() -> void:
|
||
|
set_morse_state(true)
|
||
|
|
||
|
func _on_morse_button_up() -> void:
|
||
|
set_morse_state(false)
|
||
|
|
||
|
func _input(input_event):
|
||
|
if input_event is InputEventMIDI:
|
||
|
_process_midi_event(input_event)
|
||
|
|
||
|
func _process_midi_event(midi_event):
|
||
|
if midi_event.channel in [0, 9]:
|
||
|
if midi_event.message == MIDI_MESSAGE_NOTE_ON:
|
||
|
set_morse_state(true)
|
||
|
elif midi_event.message == MIDI_MESSAGE_NOTE_OFF:
|
||
|
set_morse_state(false)
|
||
|
#print(midi_event)
|
||
|
#print("Channel ", midi_event.channel)
|
||
|
#print("Message ", midi_event.message)
|
||
|
#print("Pitch ", midi_event.pitch)
|
||
|
#print("Velocity ", midi_event.velocity)
|
||
|
#print("Instrument ", midi_event.instrument)
|
||
|
#print("Pressure ", midi_event.pressure)
|
||
|
#print("Controller number: ", midi_event.controller_number)
|
||
|
#print("Controller value: ", midi_event.controller_value)
|