libsndfile-ocaml source files.
[Faustine.git] / interpretor / lib / src / libsndfile-ocaml / test_sndfile_bigarray.ml
1 (* Rudimentary testing of the Ocaml libsndfile wrapper. *)
2
3 let write_test filename =
4 let fmt = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
5 let file = Sndfile.openfile filename ~info:(Sndfile.WRITE, fmt, 2, 44100) () in
6 if Sndfile.error file != 0 then
7 Printf.printf "Error writing '%s' : %s\n" filename (Sndfile.strerror file)
8 else
9 let writecount = Sndfile_bigarray.write_short file [| 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.0 ; 0.5 |] in
10 Printf.printf "Wrote %d items.\n" writecount ;
11 Sndfile.close file
12
13 let read_test filename =
14 let file = Sndfile.openfile filename () in
15 Printf.printf "File contains %Ld frames.\n" (Sndfile.frames file) ;
16 let data = Array.create 100 0.0 in
17 let readcount = Sndfile_bigarray.read_short file data in
18 Printf.printf "Read %d items.\n" readcount ;
19 Sndfile.close file
20
21 let finalize_test filename =
22 let sub_open_file =
23 let file = Sndfile.openfile filename () in
24 ignore file
25 in
26 (* Compact the heap. *)
27 Gc.compact () ;
28 let pre_stat = Gc.stat () in
29 sub_open_file ;
30 (* Compact the heap again. *)
31 Gc.compact () ;
32 (* Compare before and after. *)
33 let post_stat = Gc.stat () in
34 if pre_stat.Gc.heap_words != post_stat.Gc.heap_words then
35 ( Printf.printf "\nFinalize not working : before %d -> after %d\n\n" pre_stat.Gc.heap_words post_stat.Gc.heap_words ;
36 exit 1
37 )
38 else ()
39
40
41 let _ =
42 print_endline "------------------------" ;
43 let filename = "a.wav" in
44 write_test filename ;
45 read_test filename ;
46 finalize_test filename