git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/examples/sms/wav_sample/utils/cvtsamplesms.py

      1 #!/usr/bin/env python3
      2 import sys
      3 import wave
      4 from pathlib import Path
      5 from optparse import OptionParser
      6 
      7 def main(argv=None):
      8     parser = OptionParser("Usage: cvtsamplesms.py [options] INPUT_FILE_NAME.WAV")
      9     parser.add_option("-o", '--out',        dest='outfilename',                                      help='output file name')
     10     parser.add_option("-i", '--identifier', dest='identifier',                                       help='source identifier')
     11 
     12     parser.add_option("-b", '--bank',       dest='bank',        default="255",                       help='BANK number (default AUTO=255)')    
     13 
     14     (options, args) = parser.parse_args()
     15 
     16     if (len(args) == 0):
     17         parser.print_help()
     18         parser.error("Input file name required\n")
     19     
     20     infilename = Path(args[0])
     21     
     22     if options.outfilename == None:
     23         outfilename = infilename.with_suffix('.c')
     24     else:
     25         outfilename = Path(options.outfilename)
     26         
     27     if options.identifier == None:
     28         identifier = str(Path(infilename.name).with_suffix(''))
     29     else: 
     30         identifier = options.identifier
     31 
     32     sHDR    = ("#pragma bank {1:s}\n\n"
     33                "#include <gbdk/platform.h>\n"
     34                "#include <stdint.h>\n\n"
     35                "BANKREF({0:s})\n"
     36                "const uint8_t {0:s}[{2:d}] = {{\n")
     37     sFOOT   = "\n}};\n"
     38     sEMIT   = "0x{:x}"
     39     sNEW    = ",\n"
     40     sNONEW  = ","
     41 
     42     with wave.open(str(infilename), mode="rb") as f:
     43         p = f.getparams()
     44         if (p.nchannels == 1) and (p.sampwidth == 1) and (p.framerate >= 8000) and (p.framerate <= 8192) and (p.comptype == 'NONE'):
     45             data = f.readframes(p.nframes)
     46             c = 0
     47             cnt = 0;
     48 
     49             # output C source file
     50             with open(str(outfilename), "wb") as outf:                
     51                 outf.write(bytes(sHDR.format(identifier, options.bank, len(data) - (len(data) % 16)), "ascii"))
     52                 result = ""
     53                 for c in data:
     54                     result += sEMIT.format(c)
     55                     cnt += 1
     56                     if (cnt % 16 == 0):
     57                         result = "{}{}".format(result, sNEW)
     58                         outf.write(bytes(result, "ascii"))
     59                         result = ""
     60                     else:
     61                         result += sNONEW
     62                 outf.write(bytes(sFOOT.format(), "ascii"))
     63         else:
     64             print("ERROR: Invalid WAV file format")
     65             sys.exit(1)
     66 
     67         # output C header file
     68         if outfilename.suffix == ".c":
     69             with open(str(outfilename.with_suffix('.h')), "wb") as hdrf:
     70                 hdrf.write(bytes(("#ifndef __{0:s}_INCLUDE__\n"
     71                                   "#define __{0:s}_INCLUDE__\n\n"
     72                                   "#include <gbdk/platform.h>\n"
     73                                   "#include <stdint.h>\n\n"
     74                                   "BANKREF_EXTERN({0:s})\n"
     75                                   "extern const uint8_t {0:s}[{2:d}];\n"
     76                                   "#endif\n").format(identifier, 4, len(data) - (len(data) % 16)), "ascii"))
     77 
     78 if __name__=='__main__':
     79     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.