File: wav2opus.ml

package info (click to toggle)
ocaml-xiph 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: ml: 4,494; ansic: 3,994; makefile: 3
file content (184 lines) | stat: -rw-r--r-- 5,513 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(*
 * Copyright 2003 Savonet team
 *
 * This file is part of OCaml-opus.
 *
 * OCaml-opus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OCaml-opus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OCaml-opus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

(**
  * An wav to ogg converter using OCaml-opus.
  *
  * @author Samuel Mimram, and many others...
  *)

open Opus

let src = ref ""
let dst = ref ""
let buflen = ref 4096

let input_string chan len =
  let ans = Bytes.create len in
  (* TODO: check length *)
  ignore (input chan ans 0 len);
  Bytes.unsafe_to_string ans

let input_int chan =
  let buf = input_string chan 4 in
  int_of_char buf.[0]
  + (int_of_char buf.[1] lsl 8)
  + (int_of_char buf.[2] lsl 16)
  + (int_of_char buf.[3] lsl 24)

let input_short chan =
  let buf = input_string chan 2 in
  int_of_char buf.[0] + (int_of_char buf.[1] lsl 8)

let usage = "usage: wav2ogg [options] source destination"
let use_ba = ref false

let _ =
  Arg.parse
    [
      ( "--buflen",
        Arg.Int (fun i -> buflen := i),
        "Size of chunks successively encoded" );
      ("-ba", Arg.Set use_ba, "Use big arrays");
    ]
    (let pnum = ref (-1) in
     fun s ->
       incr pnum;
       match !pnum with
         | 0 -> src := s
         | 1 -> dst := s
         | _ ->
             Printf.eprintf "Error: too many arguments\n";
             exit 1)
    usage;
  if !src = "" || !dst = "" then (
    Printf.printf "%s\n" usage;
    exit 1);
  let ic = open_in_bin !src in
  let oc = open_out_bin !dst in
  (* TODO: improve! *)
  if input_string ic 4 <> "RIFF" then invalid_arg "No RIFF tag";
  ignore (input_string ic 4);
  if input_string ic 4 <> "WAVE" then invalid_arg "No WAVE tag";
  if input_string ic 4 <> "fmt " then invalid_arg "No fmt tag";
  let _ = input_int ic in
  let _ = input_short ic in
  (* TODO: should be 1 *)
  let channels = input_short ic in
  let infreq = input_int ic in
  let _ = input_int ic in
  (* bytes / s *)
  let _ = input_short ic in
  (* block align *)
  let bits = input_short ic in
  let fos buf =
    let len = String.length buf / (2 * channels) in
    let ans = Array.init channels (fun _ -> Array.make len 0.) in
    for i = 0 to len - 1 do
      for c = 0 to channels - 1 do
        let n =
          int_of_char buf.[(2 * channels * i) + (2 * c)]
          + (int_of_char buf.[(2 * channels * i) + (2 * c) + 1] lsl 8)
        in
        let n =
          if n land (1 lsl 15) = 0 then n
          else (n land 0b111111111111111) - 32768
        in
        ans.(c).(i) <- float n /. 32768.;
        ans.(c).(i) <- max (-1.) (min 1. ans.(c).(i))
      done
    done;
    ans
  in
  let os = Ogg.Stream.create () in
  let enc =
    Encoder.create ~samplerate:infreq ~channels ~application:`Audio os
  in
  Ogg.Stream.put_packet os (Opus.Encoder.header enc);
  let ph, pb = Ogg.Stream.flush_page os in
  output_string oc (ph ^ pb);
  Ogg.Stream.put_packet os (Opus.Encoder.comments enc);
  let ph, pb = Ogg.Stream.flush_page os in
  output_string oc (ph ^ pb);
  let rem = ref (Array.make channels [||]) in
  let encode buf =
    let encoded, buf =
      if !use_ba then (
        let buf = fos buf in
        let buf = Array.mapi (fun c d -> Array.append d buf.(c)) !rem in
        let bbuf =
          Array.map
            (fun d ->
              Bigarray.Array1.of_array Bigarray.float32 Bigarray.c_layout d)
            buf
        in
        let encoded =
          Encoder.encode_float_ba enc bbuf 0 (Bigarray.Array1.dim bbuf.(0))
        in
        (encoded, buf))
      else (
        let buf = fos buf in
        let buf = Array.mapi (fun c d -> Array.append d buf.(c)) !rem in
        let encoded = Encoder.encode_float enc buf 0 (Array.length buf.(0)) in
        (encoded, buf))
    in
    rem :=
      Array.map (fun d -> Array.sub d encoded (Array.length d - encoded)) buf
  in
  let start = Unix.time () in
  Printf.printf "Input detected: PCM WAVE %d channels, %d Hz, %d bits\n%!"
    channels infreq bits;
  Printf.printf "Encoding to: ogg/opus %d channels, %d Hz\nPlease wait...\n%!"
    channels infreq;
  (* skip headers *)
  let rec aux () =
    let tag = input_string ic 4 in
    match tag with
      | "LIST" ->
          let n = input_int ic in
          let _ = input_string ic n in
          aux ()
      | "data" -> ()
      | _ -> invalid_arg "No data tag"
  in
  aux ();
  let buflen = !buflen in
  let buf = Bytes.create buflen in
  begin
    try
      while true do
        try
          really_input ic buf 0 buflen;
          encode (Bytes.unsafe_to_string buf);
          while true do
            let ph, pb = Ogg.Stream.get_page os in
            output_string oc (ph ^ pb)
          done
        with Ogg.Not_enough_data -> ()
      done
    with End_of_file -> ()
  end;
  List.iter
    (fun (ph, pb) -> output_string oc (ph ^ pb))
    (Ogg.Stream.terminate os);
  close_in ic;
  close_out oc;
  Printf.printf "Finished in %.0f seconds.\n" (Unix.time () -. start);
  Gc.full_major ()