|
| 1 | +open Raylib |
| 2 | + |
| 3 | +let width = 800. |
| 4 | +let max_samples = 512 |
| 5 | +let frequency = ref 440.0 |
| 6 | +let audio_frequency = ref 440.0 |
| 7 | +let data = Array.make max_samples 0.0 |
| 8 | +let sine_idx = ref 0.0 |
| 9 | + |
| 10 | +let audio_input_callback buf (frames : Unsigned.uint) = |
| 11 | + ignore buf; |
| 12 | + ignore frames; |
| 13 | + ignore audio_frequency; |
| 14 | + ignore sine_idx; |
| 15 | + (* print_endline "hello from callback"; *) |
| 16 | + audio_frequency := !frequency +. ((!audio_frequency -. !frequency) *. 0.95); |
| 17 | + |
| 18 | + let incr = !audio_frequency /. 44100.0 in |
| 19 | + let shortbuf = Ctypes.(coerce (ptr void) (ptr short) buf) in |
| 20 | + let iframes = Unsigned.UInt.to_int frames in |
| 21 | + let d = Ctypes.CArray.from_ptr shortbuf iframes in |
| 22 | + for i = 0 to iframes - 1 do |
| 23 | + Ctypes.CArray.set d i |
| 24 | + (int_of_float (23000.0 *. sin (2.0 *. Float.pi *. !sine_idx))); |
| 25 | + sine_idx := !sine_idx +. incr; |
| 26 | + if Float.compare !sine_idx 1.0 > 0 then sine_idx := !sine_idx -. 1.0 |
| 27 | + done; |
| 28 | + () |
| 29 | + |
| 30 | +let setup () = |
| 31 | + init_window (int_of_float width) 450 |
| 32 | + "raylib [audio] example - raw audio streaming"; |
| 33 | + init_audio_device (); |
| 34 | + set_audio_stream_buffer_size_default 4096; |
| 35 | + let stream = load_audio_stream 44100 16 1 in |
| 36 | + Raylib_callbacks.set_audio_stream_callback stream audio_input_callback; |
| 37 | + play_audio_stream stream; |
| 38 | + set_target_fps 30; |
| 39 | + (stream, 1.0) |
| 40 | + |
| 41 | +let rec run (stream, old_frequency) = |
| 42 | + if window_should_close () then ( |
| 43 | + unload_audio_stream stream; |
| 44 | + close_audio_device (); |
| 45 | + close_window ()) |
| 46 | + else |
| 47 | + let mouse_pos = get_mouse_position () in |
| 48 | + |
| 49 | + if is_mouse_button_down MouseButton.Left then ( |
| 50 | + let fp = Vector2.y mouse_pos in |
| 51 | + let pan = Vector2.x mouse_pos /. width in |
| 52 | + set_audio_stream_pan stream pan; |
| 53 | + frequency := 40.0 +. fp); |
| 54 | + |
| 55 | + if !frequency <> old_frequency then ( |
| 56 | + let wavelength = 22050.0 /. !frequency |> int_of_float in |
| 57 | + let wavelength = |
| 58 | + if wavelength > max_samples / 2 then max_samples / 2 |
| 59 | + else if wavelength < 1 then 1 |
| 60 | + else wavelength |
| 61 | + in |
| 62 | + |
| 63 | + (* write sine wave *) |
| 64 | + for i = 0 to (wavelength * 2) - 1 do |
| 65 | + let value = |
| 66 | + sin Float.(pi *. 2.0 *. of_int i /. of_int wavelength) *. 32000.0 |
| 67 | + in |
| 68 | + Array.set data i value |
| 69 | + done; |
| 70 | + for j = wavelength * 2 to max_samples - 1 do |
| 71 | + Array.set data j 0.0 |
| 72 | + done); |
| 73 | + |
| 74 | + begin_drawing (); |
| 75 | + clear_background Color.raywhite; |
| 76 | + draw_text |
| 77 | + (Format.sprintf "sine frequency: %i" (int_of_float !frequency)) |
| 78 | + (get_screen_width () - 220) |
| 79 | + 10 20 Color.red; |
| 80 | + draw_text "click mouse button to change frequency or pan" 10 10 20 |
| 81 | + Color.darkgray; |
| 82 | + |
| 83 | + for i = 0 to int_of_float width - 1 do |
| 84 | + let y = |
| 85 | + 250.0 |
| 86 | + +. 50.0 |
| 87 | + *. Array.get data (i * max_samples / int_of_float width) |
| 88 | + /. 32000.0 |
| 89 | + in |
| 90 | + draw_pixel_v (Vector2.create (float_of_int i) y) Color.red |
| 91 | + done; |
| 92 | + |
| 93 | + Raylib_callbacks.end_drawing2 (); |
| 94 | + run (stream, !frequency) |
| 95 | + |
| 96 | +let () = setup () |> run |
0 commit comments