Skip to content

Commit

Permalink
Add FLTP module (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
toots authored Oct 20, 2024
1 parent 40d7ccb commit e99e388
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.8.6 (unreleased)
=====
- Added FLTP module.

0.8.5 (2024-02-05)
=====
- Fix note names (#18).
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(lang dune 3.7)
(version 0.8.5)
(version 0.8.6)
(name mm)
(source (github savonet/ocaml-mm))
(license LGPL-2.1-or-later)
Expand Down
2 changes: 1 addition & 1 deletion mm.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.8.5"
version: "0.8.6"
synopsis:
"The mm library contains high-level APIs to create and manipulate multimedia streams (audio, video, MIDI)"
maintainer: ["The Savonet Team <[email protected]>"]
Expand Down
20 changes: 20 additions & 0 deletions src/audio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,26 @@ module S32LE = struct
= "caml_mm_audio_convert_s32le"
end

module FLTP = struct
external of_audio :
src:buffer ->
src_offset:int ->
dst:(float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t ->
dst_offset:int ->
len:int ->
stride:int ->
unit = "caml_mm_audio_to_fltp_bytes" "caml_mm_audio_to_fltp"

external to_audio :
src:(float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t ->
src_offset:int ->
dst:buffer ->
dst_offset:int ->
len:int ->
stride:int ->
unit = "caml_mm_audio_convert_fltp_bytes" "caml_mm_audio_convert_fltp"
end

let add b1 ofs1 b2 ofs2 len =
Array.iteri (fun i b -> Mono.add b ofs1 b2.(i) ofs2 len) b1

Expand Down
20 changes: 20 additions & 0 deletions src/audio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,26 @@ module S32LE : sig
val to_audio : string -> int -> t -> int -> int -> unit
end

module FLTP : sig
val of_audio :
src:t ->
src_offset:int ->
dst:(float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t ->
dst_offset:int ->
len:int ->
stride:int ->
unit

val to_audio :
src:(float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t ->
src_offset:int ->
dst:t ->
dst_offset:int ->
len:int ->
stride:int ->
unit
end

val resample : ?mode:[ `Nearest | `Linear ] -> float -> t -> int -> int -> t
val blit : t -> int -> t -> int -> int -> unit
val sub : t -> int -> int -> t
Expand Down
91 changes: 86 additions & 5 deletions src/audio_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ static inline int32_t int32_of_int24(int24_t x) {
}

#define bswap_16(x) \
((int16_t)((((int16_t)(x)&0xff00) >> 8) | (((int16_t)(x)&0x00ff) << 8)))
((int16_t)((((int16_t)(x) & 0xff00) >> 8) | (((int16_t)(x) & 0x00ff) << 8)))

#define bswap_32(x) \
((int32_t)((((int32_t)(x)&0xff000000) >> 24) | \
(((int32_t)(x)&0x00ff0000) >> 8) | \
(((int32_t)(x)&0x0000ff00) << 8) | \
(((int32_t)(x)&0x000000ff) << 24)))
((int32_t)((((int32_t)(x) & 0xff000000) >> 24) | \
(((int32_t)(x) & 0x00ff0000) >> 8) | \
(((int32_t)(x) & 0x0000ff00) << 8) | \
(((int32_t)(x) & 0x000000ff) << 24)))

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -208,6 +208,46 @@ CAMLprim value caml_mm_audio_to_s32le(value _src, value _src_offs, value _dst,
CAMLreturn(Val_unit);
}

CAMLprim value caml_mm_audio_to_fltp(value _src, value _src_offs, value _dst,
value _dst_offs, value _len,
value _stride) {
CAMLparam2(_src, _dst);
CAMLlocal1(src);
int c, i;
int dst_offs = Int_val(_dst_offs);
int src_offs = Int_val(_src_offs);
int nc = Wosize_val(_src);
if (nc == 0)
CAMLreturn(Val_unit);
int len = Int_val(_len);
int stride = Int_val(_stride);
float *dst = (float *)Caml_ba_data_val(_dst);

if (stride < len)
caml_invalid_argument("caml_mm_audio_to_fltp: invalid dst length/stride");

if (len < dst_offs)
caml_invalid_argument("caml_mm_audio_to_fltp: invalid dst_offset");

if (Caml_ba_array_val(_dst)->dim[0] < stride * nc)
caml_invalid_argument(
"caml_mm_audio_to_fltp: destination buffer too short");

for (c = 0; c < nc; c++) {
src = Field(_src, c);
for (i = 0; i < len; i++) {
dst[c * stride + i + dst_offs] = clip(Double_field(src, i + src_offs));
}
}

CAMLreturn(Val_unit);
}

CAMLprim value caml_mm_audio_to_fltp_bytes(value *argv, value argn) {
return caml_mm_audio_to_fltp(argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5]);
}

CAMLprim value caml_mm_audio_to_s24le(value _src, value _src_offs, value _dst,
value _dst_offs, value _len) {
CAMLparam2(_src, _dst);
Expand Down Expand Up @@ -402,6 +442,47 @@ CAMLprim value caml_mm_audio_convert_s32le(value _src, value _src_offs,
CAMLreturn(Val_unit);
}

CAMLprim value caml_mm_audio_convert_fltp(value _src, value _src_offs,
value _dst, value _dst_offs,
value _len, value _stride) {
CAMLparam2(_src, _dst);
CAMLlocal1(dst);
const float *src = Caml_ba_data_val(_src);
int src_offs = Int_val(_src_offs);
int dst_offs = Int_val(_dst_offs);
int nc = Wosize_val(_dst);
if (nc == 0)
CAMLreturn(Val_unit);
int len = Int_val(_len);
int stride = Int_val(_stride);
int i, c;

if (stride < len)
caml_invalid_argument(
"caml_mm_audio_convert_fltp: invalid src length/stride");

if (len < src_offs)
caml_invalid_argument("caml_mm_audio_convert_fltp: invalid src_offset");

if (Caml_ba_array_val(_src)->dim[0] < stride * nc)
caml_invalid_argument(
"caml_mm_audio_convert_fltp: output buffer too small");

for (c = 0; c < nc; c++) {
dst = Field(_dst, c);
for (i = 0; i < len; i++)
Store_double_field(dst, i + dst_offs,
clip(src[stride * c + src_offs + i]));
}

CAMLreturn(Val_unit);
}

CAMLprim value caml_mm_audio_convert_fltp_bytes(value *argv, value argn) {
return caml_mm_audio_convert_fltp(argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5]);
}

CAMLprim value caml_mm_audio_convert_s24le(value _src, value _src_offs,
value _dst, value _dst_offs,
value _len) {
Expand Down

0 comments on commit e99e388

Please sign in to comment.