Skip to content

Commit c315d0f

Browse files
committed
WIP add second end_drawing function
which releases the runtime lock
1 parent e6cf76a commit c315d0f

File tree

8 files changed

+148
-19
lines changed

8 files changed

+148
-19
lines changed

examples/audio/audio_raw_stream.ml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

examples/audio/dune

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
(name audio_music_stream)
88
(modules Audio_music_stream)
99
(libraries raylib))
10+
11+
(executable
12+
(name audio_raw_stream)
13+
(modules Audio_raw_stream)
14+
(libraries raylib raylib-callbacks))

src/c/raygui/functions.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,8 @@ module Functions (F : Ctypes.FOREIGN) = struct
171171
(* List View with extended parameters *)
172172
let _list_view_ex =
173173
foreign "GuiListViewEx"
174-
(Raylib.Rectangle.t
175-
@-> ptr (const string)
176-
@-> int @-> ptr int @-> ptr int @-> int @-> returning int)
174+
(Raylib.Rectangle.t @-> ptr string @-> int @-> ptr int @-> ptr int @-> int
175+
@-> returning int)
177176

178177
(* Message Box control, displays a message *)
179178
let _message_box =

src/c/raylib_callbacks/functions.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ module Functions (F : Ctypes.FOREIGN) = struct
55
open Raylib_c.Types
66

77
let audio_callback =
8-
Foreign.funptr Ctypes.(ptr void @-> uint @-> returning void)
8+
Foreign.funptr ~thread_registration:false ~runtime_lock:true
9+
Ctypes.(ptr void @-> uint @-> returning void)
910

10-
let set_audio_stream_callback =
11-
foreign "SetAudioStreamCallback"
12-
(AudioStream.t @-> audio_callback @-> returning void)
11+
(* let set_audio_stream_callback = *)
12+
(* foreign "SetAudioStreamCallback" *)
13+
(* (AudioStream.t @-> audio_callback @-> returning void) *)
1314

1415
let attach_audio_stream_processor =
1516
foreign "AttachAudioStreamProcessor"

src/callbacks/callbacks.ml

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/callbacks/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
2-
(name callbacks)
2+
(name raylib_callbacks)
33
(public_name raylib-callbacks)
4-
(libraries raylib raylib_callbacks_c))
4+
(libraries raylib raylib_callbacks_c ctypes-foreign))

src/callbacks/raylib_callbacks.ml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
type audio_callback = unit Ctypes.ptr -> Unsigned.uint -> unit
2+
3+
type load_file_data_callback =
4+
string -> int Ctypes.ptr -> Unsigned.uchar Ctypes.ptr
5+
6+
type save_file_data_callback = string -> unit Ctypes.ptr -> int -> bool
7+
type load_file_text_callback = string -> char Ctypes.ptr
8+
type save_file_text_callback = string -> char Ctypes.ptr -> bool
9+
10+
include Raylib_callbacks_c.Raylib_callbacks_generated.Functions
11+
open Ctypes
12+
open Foreign
13+
14+
(* module Audio_callback = *)
15+
(* (val dynamic_funptr ~runtime_lock:true ~thread_registration:true *)
16+
(* (ptr void @-> uint @-> returning void)) *)
17+
18+
let end_drawing2 =
19+
foreign ~release_runtime_lock:true "EndDrawing" (void @-> returning void)
20+
21+
let save = ref None
22+
23+
let _set_audio_stream_callback =
24+
let audio_callback =
25+
Foreign.funptr ~thread_registration:true ~runtime_lock:true
26+
Ctypes.(ptr void @-> uint @-> returning void)
27+
in
28+
foreign ~release_runtime_lock:true "SetAudioStreamCallback"
29+
(* (Raylib.AudioStream.t @-> Audio_callback.t @-> returning void) *)
30+
(Raylib.AudioStream.t @-> audio_callback @-> returning void)
31+
32+
let set_audio_stream_callback stream cb =
33+
save := Some cb;
34+
(* Gc.full_major (); *)
35+
_set_audio_stream_callback stream (Option.get !save)
36+
(* (Audio_callback.of_fun cb) *)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
type audio_callback = unit Ctypes.ptr -> Unsigned.uint -> unit
44

5+
val end_drawing2 : unit -> unit
6+
57
val set_audio_stream_callback : Raylib.AudioStream.t -> audio_callback -> unit
68
(** [set_audio_stream_callback stream callback] Audio thread callback to request new data*)
79

0 commit comments

Comments
 (0)