Advertisement
GenesisFan64

SCDPCM2WAV

Apr 11th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 KB | None | 0 0
  1. #======================================================================
  2. # SCD PCM to WAV
  3. #
  4. # Args:
  5. # SOURCE,DESTINATION,OFFSET/OPTION
  6. #
  7. # OPTIONS:
  8. # "LUNAR2"
  9. #======================================================================
  10.  
  11. import sys
  12.      
  13. #======================================================================
  14. # -------------------------------------------------
  15. # Init
  16. # -------------------------------------------------
  17.  
  18. # For lunar eternal blue:
  19. # 16000 - Original
  20. # 16323 - SEGA CD style
  21. SAMPLE_RATE = 16323
  22.      
  23. input_file = open(sys.argv[1],"rb")
  24. output_file = open(sys.argv[2],"wb")
  25. option_mode = sys.argv[3]
  26.  
  27. if option_mode == "LUNAR2":
  28.     print("FORMAT: LUNAR ETERNAL BLUE")
  29.     print("Searching (poorly) for pcm data...")
  30.     b = True
  31.     input_file.seek(0x40)   # start from here
  32.     while b:
  33.         a = ord((input_file.read(1)))
  34.         if a == 0x80:
  35.             input_file.seek(-2,1)
  36.            
  37.             WAVE_START = input_file.tell()
  38.             input_file.seek(0,2)
  39.             WAVE_END =input_file.tell()
  40.             input_file.seek(WAVE_START)
  41.             print("Found data at:",hex(WAVE_START))
  42.             b = False
  43.    
  44. else:
  45.     input_file.seek(int(sys.argv[3]))
  46.     WAVE_START = input_file.tell()
  47.     input_file.seek(0,2)
  48.     WAVE_END =input_file.tell()
  49.     input_file.seek(WAVE_START)
  50.     output_file.seek(0)
  51.  
  52. # head
  53. output_file.seek(0x0)
  54. output_file.write( bytes([0x52,0x49,0x46,0x46]) )
  55. output_file.seek(0x8)
  56. output_file.write( bytes([0x57,0x41,0x56,0x45,0x66,0x6D,0x74,0x20]) )
  57. output_file.write( bytes([16]) ) #size 16
  58.  
  59. output_file.seek(20)
  60. output_file.write( bytes([1]) ) # PCM=1
  61. output_file.seek(22)
  62. output_file.write( bytes([1]) ) # MONO
  63.  
  64. a = SAMPLE_RATE         # Samplerate
  65. output_file.seek(24)        # 16000 in reverse
  66. output_file.write( bytes([a&0xFF,
  67.               a>>8&0xFF,
  68.               a>>16&0xFF,
  69.               a>>24&0xFF
  70.               ]) )
  71. a = SAMPLE_RATE         # Samplespeed
  72. output_file.write( bytes([a&0xFF,
  73.               a>>8&0xFF,
  74.               a>>16&0xFF,
  75.               a>>24&0xFF
  76.               ]) )
  77.  
  78. output_file.seek(0x20)
  79. output_file.write( bytes([0x01,0x00,0x08,0x00]) )
  80. output_file.write( bytes([0x64,0x61,0x74,0x61]) )
  81. output_file.seek(0x2C)
  82.  
  83. #output_file.seek(0x20)
  84. #output_file.write(chr(0x01))
  85. #output_file.write(chr(0x00))
  86. #output_file.write(chr(0x08))
  87. #output_file.write(chr(0x00))
  88. #output_file.write("data")
  89. #output_file.seek(0x2C)
  90.  
  91. # write the head
  92. #output_file.seek(0x0)
  93. #output_file.write("RIFF")
  94. #output_file.seek(0x8)
  95. #output_file.write("WAVEfmt ")
  96. #output_file.write(chr(16)) #size 16
  97.  
  98. #output_file.seek(20)       #pcm=1
  99. #output_file.write(chr(1))
  100. #output_file.seek(22)       # MONO (1)
  101. #output_file.write(chr(1))
  102.  
  103. ## SAMPLERATE
  104. #a = SAMPLE_RATE
  105. #output_file.seek(24)       # 16000 in reverse
  106. #output_file.write(chr(a&0xFF))
  107. #output_file.write(chr((a>>8)&0xFF))
  108. #output_file.write(chr((a>>16)&0xFF))
  109. #output_file.write(chr((a>>24)&0xFF))
  110. ## SAMPLESPEED
  111. #a = SAMPLE_RATE
  112. #output_file.write(chr(a&0xFF))
  113. #output_file.write(chr((a>>8)&0xFF))
  114. #output_file.write(chr((a>>16)&0xFF))
  115. #output_file.write(chr((a>>24)&0xFF))
  116.  
  117. #output_file.seek(0x20)
  118. #output_file.write(chr(0x01))
  119. #output_file.write(chr(0x00))
  120. #output_file.write(chr(0x08))
  121. #output_file.write(chr(0x00))
  122. #output_file.write("data")
  123. #output_file.seek(0x2C)
  124.  
  125. # lets go to work
  126. working=True
  127.  
  128. #======================================================================
  129. # -------------------------------------------------
  130. # Start
  131. # -------------------------------------------------
  132.  
  133. print("Converting...")
  134. working = (WAVE_END-WAVE_START)
  135.  
  136. while working:
  137.    
  138.     if option_mode == "LUNAR2":
  139.         a = ord(input_file.read(1))
  140.         a = ord(input_file.read(1))
  141.         working -= 2
  142.     else:
  143.         a = ord(input_file.read(1))
  144.         working -= 1
  145.        
  146.     #0x00 - 0x7F == [-128, -1]
  147.     if a < 0x7F:
  148.         a = 0x80 + a
  149.        
  150.     #0x80 - 0xFF == [0, 127]
  151.     elif a >= 0x80:
  152.         a = 0x80 - (a & ~0x80)
  153.    
  154.     output_file.write(bytes([a&0xFF]))
  155.  
  156.    
  157. # ------------------
  158. # Last steps
  159. # ------------------
  160.  
  161. a = output_file.tell() - 0x2C
  162. b = a + 36
  163.  
  164. output_file.seek(0x04)
  165. output_file.write( bytes([b&0xFF,
  166.               b>>8&0xFF,
  167.               b>>16&0xFF,
  168.               b>>24&0xFF
  169.               ]) )
  170. output_file.seek(0x28)
  171. output_file.write( bytes([a&0xFF,
  172.               a>>8&0xFF,
  173.               a>>16&0xFF,
  174.               a>>24&0xFF
  175.               ]) )
  176.  
  177. #output_file.seek(0x4)
  178. #output_file.write( chr(b&0xFF) )
  179. #output_file.write( chr(b>>8&0xFF) )
  180. #output_file.write( chr(b>>16&0xFF) )
  181. #output_file.write( chr(b>>24&0xFF) )
  182.  
  183. #output_file.seek(0x28)
  184. #output_file.write( chr(a&0xFF) )
  185. #output_file.write( chr(a>>8&0xFF) )
  186. #output_file.write( chr(a>>16&0xFF) )
  187. #output_file.write( chr(a>>24&0xFF) )
  188.  
  189. # ----------------------------
  190. # End
  191. # ----------------------------
  192.  
  193. print("Done.")
  194. input_file.close()
  195. output_file.close()
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement