50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
import math
|
|
import pyray as ray
|
|
|
|
SAMPLE_RATE = 44100
|
|
BUFFER_SIZE = 2048
|
|
|
|
progress = 0.0 # progress within a cycle of a periodic function (0 to 1)
|
|
|
|
|
|
def tick(freq):
|
|
global progress
|
|
progress = (progress + freq / SAMPLE_RATE) % 1.0
|
|
return progress
|
|
|
|
|
|
def sine(p):
|
|
return math.sin(2 * math.pi * p)
|
|
|
|
|
|
def square(p):
|
|
return 1.0 if p < 0.5 else -1.0
|
|
|
|
|
|
def saw(p):
|
|
return 2.0 * p - 1.0
|
|
|
|
|
|
def tri(p):
|
|
return 4 * abs(p - 0.5) - 1.0
|
|
|
|
|
|
ray.init_window(800, 450, "Hello")
|
|
ray.init_audio_device()
|
|
ray.set_target_fps(60)
|
|
ray.set_audio_stream_buffer_size_default(BUFFER_SIZE)
|
|
stream = ray.load_audio_stream(SAMPLE_RATE, 32, 1)
|
|
ray.play_audio_stream(stream)
|
|
buffer = ray.ffi.new("float[]", BUFFER_SIZE)
|
|
while not ray.window_should_close():
|
|
if ray.is_key_down(ray.KeyboardKey.KEY_SPACE) and ray.is_audio_stream_processed(stream):
|
|
for i in range(BUFFER_SIZE):
|
|
buffer[i] = 0.1 * sine(tick(440))
|
|
ray.update_audio_stream(stream, buffer, BUFFER_SIZE)
|
|
ray.begin_drawing()
|
|
ray.clear_background((0x0B, 0x0B, 0x0B, 0xFF))
|
|
ray.draw_text("Hello world", 190, 200, 20, ray.VIOLET)
|
|
ray.end_drawing()
|
|
ray.close_window()
|
|
ray.close_audio_device()
|