File: resample.ml

package info (click to toggle)
ocaml-ffmpeg 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 908 kB
  • sloc: ansic: 6,412; ml: 6,166; makefile: 3
file content (153 lines) | stat: -rw-r--r-- 5,033 bytes parent folder | download | duplicates (2)
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
module R = Swresample.Make (Swresample.FloatArray) (Swresample.S32Bytes)
module R0 = Swresample.Make (Swresample.FloatArray) (Swresample.S16Frame)
module R1 = Swresample.Make (Swresample.Frame) (Swresample.U8BigArray)
module R2 = Swresample.Make (Swresample.U8BigArray) (Swresample.DblPlanarFrame)

module R3 =
  Swresample.Make (Swresample.DblPlanarFrame) (Swresample.S32PlanarBigArray)

module R4 =
  Swresample.Make (Swresample.S32PlanarBigArray) (Swresample.FltPlanarBigArray)

module R5 =
  Swresample.Make (Swresample.FltPlanarBigArray) (Swresample.PlanarFloatArray)

module R6 = Swresample.Make (Swresample.PlanarFloatArray) (Swresample.S32Frame)
module R7 = Swresample.Make (Swresample.S32Frame) (Swresample.FloatArray)
module R8 = Swresample.Make (Swresample.FloatArray) (Swresample.S32BigArray)
module R9 = Swresample.Make (Swresample.S32BigArray) (Swresample.S32Bytes)
module R10 = Swresample.Make (Swresample.S32Bytes) (Swresample.S32Bytes)
module ConverterInput = Swresample.Make (Swresample.Frame)
module Converter = ConverterInput (Swresample.PlanarFloatArray)

let write_bytes = output_bytes

(* let write_bytes dst bytes = () *)

let foi = float_of_int
let pi = 4.0 *. atan 1.0
let rate = 44100
let frate = float_of_int rate

let test () =
  let dst1 = open_out_bin "test_swresample_out1.raw" in
  let r =
    R.create Avutil.Channel_layout.mono rate Avutil.Channel_layout.stereo 44100
  in

  let dst2 = open_out_bin "test_swresample_out2.raw" in

  let r0 =
    R0.create Avutil.Channel_layout.mono rate
      Avutil.Channel_layout.five_point_one 96000
  in
  let r1 =
    R1.create Avutil.Channel_layout.five_point_one ~in_sample_format:`S16 96000
      Avutil.Channel_layout.stereo 16000
  in
  let r2 =
    R2.create Avutil.Channel_layout.stereo 16000 Avutil.Channel_layout.stereo
      44100
  in
  let r3 =
    R3.create Avutil.Channel_layout.stereo 44100 Avutil.Channel_layout.stereo
      48000
  in
  let r4 =
    R4.create Avutil.Channel_layout.stereo 48000
      Avutil.Channel_layout.(find "downmix")
      31000
  in
  let r5 =
    R5.create
      Avutil.Channel_layout.(find "downmix")
      31000 Avutil.Channel_layout.stereo 73347
  in
  let r6 =
    R6.create Avutil.Channel_layout.stereo 73347 Avutil.Channel_layout.stereo
      44100
  in
  let r7 =
    R7.create Avutil.Channel_layout.stereo 44100 Avutil.Channel_layout.stereo
      48000
  in
  let r8 =
    R8.create Avutil.Channel_layout.stereo 48000 Avutil.Channel_layout.stereo
      96000
  in
  let r9 =
    R9.create Avutil.Channel_layout.stereo 96000 Avutil.Channel_layout.stereo
      44100
  in
  let r10 =
    R10.create Avutil.Channel_layout.stereo 44100 Avutil.Channel_layout.mono
      44100
  in

  for note = 0 to 95 do
    let freq = 22.5 *. (2. ** (foi note /. 12.)) in
    let len = int_of_float (frate /. freq *. floor (freq /. 4.)) in
    let c = 2. *. pi *. freq /. frate in
    let src = Array.init len (fun t -> sin (foi t *. c)) in

    src |> R.convert r |> write_bytes dst1;

    src |> R0.convert r0 |> R1.convert r1 |> R2.convert r2 |> R3.convert r3
    |> R4.convert r4 |> R5.convert r5 |> R6.convert r6 |> R7.convert r7
    |> R8.convert r8 |> R9.convert r9 |> R10.convert r10 |> write_bytes dst2
  done;

  close_out dst1;
  close_out dst2;

  let output_planar_float_to_s16le audio_output_file planes =
    let nb_chan = Array.length planes in
    let bytes = Bytes.create 2 in
    let cx = float_of_int 0x7FFF in

    for i = 0 to Array.length planes.(0) - 1 do
      for c = 0 to nb_chan - 1 do
        let v = int_of_float (planes.(c).(i) *. cx) in

        Bytes.set bytes 0 (char_of_int (v land 0xFF));
        Bytes.set bytes 1 (char_of_int ((v lsr 8) land 0xFF));
        write_bytes audio_output_file bytes
      done
    done
  in

  Sys.argv |> Array.to_list |> List.tl
  |> List.iter (fun url ->
         try
           let src = Av.open_input url in
           let idx, is, ic = src |> Av.find_best_audio_stream in
           let rsp =
             Converter.from_codec ic Avutil.Channel_layout.stereo 44100
           in

           let p = try String.rindex url '/' + 1 with Not_found -> 0 in
           let audio_output_filename =
             String.(
               sub url p (length url - p)
               ^ "." ^ string_of_int idx ^ ".s16le.raw")
           in
           let audio_output_file = open_out_bin audio_output_filename in

           print_endline ("Convert " ^ url ^ " to " ^ audio_output_filename);
           let rec f () =
             match Av.read_input ~audio_frame:[is] src with
               | `Audio_frame (i, frame) when i = idx ->
                   Converter.convert rsp frame
                   |> output_planar_float_to_s16le audio_output_file;
                   f ()
               | exception Avutil.Error `Eof -> ()
               | _ -> f ()
           in
           f ();

           Av.get_input is |> Av.close;
           close_out audio_output_file
         with _ -> print_endline ("No audio stream in " ^ url));

  Gc.full_major ();
  Gc.full_major ()