package llama_core

  1. Overview
  2. Docs
Core types and operations for the Llama synthesizer library

Install

Dune Dependency

Authors

Maintainers

Sources

0.0.1.tar.gz
sha256=4127d810390c659512a72d28e5deddda19845eb6723199a490f71db9bf9838d8
sha512=b2a6831bcaa2005d9809f0c6a3d0f0fd8f5ed645fa18c313254c67cf61415bcc0d9d9de7ff74921605d112116a5f77f182018c8a068a11ec1c6da6ecec29ba79

doc/src/llama_core.midi/byte_array_parser.ml.html

Source file byte_array_parser.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
exception Parse_exception of string

type 'a t = char array -> int -> 'a * int

let map t ~f a i =
  let x, i = t a i in
  (f x, i)

let both x y a i =
  let x_, i = x a i in
  let y_, i = y a i in
  ((x_, y_), i)

let bind t ~f a i =
  let x, i = t a i in
  f x a i

let ( >>| ) t f = map t ~f
let ( >>= ) t f = bind t ~f
let ( let+ ) = ( >>| )
let ( and+ ) = both
let ( let* ) = ( >>= )
let return x _ i = (x, i)
let skip n _ i = ((), i + n)

let repeat_until_relative_index_offset_exact offset t a i =
  let absolute_index_threshold = i + offset in
  let rec loop acc i =
    if i == absolute_index_threshold then (acc, i)
    else if i > absolute_index_threshold then
      raise (Parse_exception "Buffer region not aligned to contents")
    else
      let x, i = t a i in
      (loop [@tailcall]) (x :: acc) i
  in
  let xs, i = loop [] i in
  (List.rev xs, i)

let repeat_until_end_exact t a i =
  let rec loop acc i =
    if i >= Array.length a then (acc, i)
    else
      let x, i = t a i in
      (loop [@tailcall]) (x :: acc) i
  in
  let xs, i = loop [] i in
  (List.rev xs, i)

let read_string n a index =
  let[@tail_mod_cons] rec loop i =
    if i == n then [] else Array.get a (index + i) :: loop (i + 1)
  in
  loop 0 |> List.to_seq |> String.of_seq

let read_string4 = read_string 4

let read_int_be n a index =
  let rec loop acc i =
    if i == n then acc
    else
      let byte = int_of_char (Array.get a (index + i)) in
      let value = (acc lsl 8) lor byte in
      loop value (i + 1)
  in
  loop 0 0

let read_int32be = read_int_be 4
let read_int16be = read_int_be 2
let read_byte a index = int_of_char (Array.get a index)
let string4 a i = (read_string4 a i, i + 4)
let int32be a i = (read_int32be a i, i + 4)
let int16be a i = (read_int16be a i, i + 2)
let byte a i = (read_byte a i, i + 1)
let peek_byte a i = (read_byte a i, i)

let byte_msb0 =
  let+ byte = byte in
  if byte > 127 then
    raise
      (Parse_exception
         (Printf.sprintf "Expected byte with bit 7 set to 0 but got %d" byte));
  byte

let n_bytes n a i =
  let out = Array.init n (Fun.const (char_of_int 0)) in
  Array.blit a i out 0 n;
  (out, i + n)

let variable_length_quantity a i =
  let rec loop acc count i =
    if count >= 4 then
      raise
        (Parse_exception
           "read more than 4 bytes while parsing variable length quantity")
    else
      let this_byte, i = byte a i in
      let mask = 0x80 in
      let this_byte_value = this_byte land lnot mask in
      let acc = (acc lsl 7) lor this_byte_value in
      if this_byte land mask == 0 then (acc, i)
      else (loop [@tailcall]) acc (count + 1) i
  in
  loop 0 0 i

let run t a = fst (t a 0)
OCaml

Innovation. Community. Security.