File: wav.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (52 lines) | stat: -rw-r--r-- 1,137 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
#pragma once

namespace nall::Encode {

struct WAV {
  static auto stereo_16bit(const string& filename, array_view<s16> left, array_view<s16> right, u32 frequency) -> bool {
    if(left.size() != right.size()) return false;
    static u32 channels = 2;
    static u32 bits = 16;
    static u32 samples = left.size();

    file_buffer fp;
    if(!fp.open(filename, file::mode::write)) return false;

    fp.write('R');
    fp.write('I');
    fp.write('F');
    fp.write('F');
    fp.writel(4 + (8 + 16) + (8 + samples * 4), 4);

    fp.write('W');
    fp.write('A');
    fp.write('V');
    fp.write('E');

    fp.write('f');
    fp.write('m');
    fp.write('t');
    fp.write(' ');
    fp.writel(16, 4);
    fp.writel(1, 2);
    fp.writel(channels, 2);
    fp.writel(frequency, 4);
    fp.writel(frequency * channels * bits, 4);
    fp.writel(channels * bits, 2);
    fp.writel(bits, 2);

    fp.write('d');
    fp.write('a');
    fp.write('t');
    fp.write('a');
    fp.writel(samples * 4, 4);
    for(u32 sample : range(samples)) {
      fp.writel(left[sample], 2);
      fp.writel(right[sample], 2);
    }

    return true;
  }
};

}