gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/examples/gb/wav_sample/utils/cvtsample.py
1 #!/usr/bin/env python3
2 import sys
3 import wave
4
5 def main(argv=None):
6 argv = argv or sys.argv
7 if len(argv) < 2:
8 print("cvtsample.py: no filename; try cvtsample.py --help")
9 sys.exit(1)
10 infilename = argv[1]
11 ident = argv[2] if len(argv) > 2 else "sample"
12 fmt = argv[3].upper() if len(argv) > 3 else "C"
13 if infilename in ('--help', '-h'):
14 print("usage: cvtsample.py SOURCE [IDENTIFIER] [FMT]")
15 return
16
17 if (fmt == "C"):
18 sHDR = "const UINT8 {:s}[] = {{\n"
19 sFOOT = "};\n"
20 sEMIT = "0x{:x}"
21 sNEW = ",\n"
22 sNONEW = ","
23 elif (fmt == "ASM"):
24 sHDR = "{:s}::"
25 sFOOT = ""
26 sEMIT = "${:x}"
27 sNEW = "\n"
28 sNONEW = ","
29
30 with wave.open(infilename, mode="rb") as f:
31 p = f.getparams()
32 if (p.nchannels == 1) and (p.sampwidth == 1) and (p.framerate >= 8000) and (p.framerate <= 8192) and (p.comptype == 'NONE'):
33 data = f.readframes(p.nframes)
34 c = 0
35 cnt = 0;
36 flag = False
37 sys.stdout.write(sHDR.format(ident))
38 for i in range(len(data) - len(data) % 32):
39 c = ((c << 4) | (data[i] >> 4)) & 0xFF
40 if flag:
41 sys.stdout.write(sEMIT.format(c))
42 cnt += 1
43 sys.stdout.write(sNEW if (cnt % 16 == 0) else sNONEW)
44
45 flag = not flag
46 sys.stdout.write(sFOOT)
47 sys.stdout.flush()
48 else:
49 sys.stderr.write("ERROR: Invalid wav file format\n")
50 sys.stderr.write("Requires - nChannels: 1, sampWidth: 1, rate: 8000 - 8192, compType: NONE\n")
51 sys.stderr.write("Found - nChannels: %d, sampWidth: %d, rate: %d, compType: %s\n" % (p.nchannels, p.sampwidth, p.framerate, p.comptype))
52 sys.stderr.flush()
53
54 if __name__=='__main__':
55 main()
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.