Last active
August 29, 2015 14:16
-
-
Save rniwase/618a607be738b2b595e9 to your computer and use it in GitHub Desktop.
バイナリにRIFFフォーマットのヘッダを追加してWAVで保存します
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# usage : python any2wav.py <inputfile> <outputfile> | |
import sys | |
import struct | |
def uint2ToStr(x) : return struct.pack('<H',x) | |
def uint4ToStr(x) : return struct.pack('<I',x) | |
#setup | |
bitdepth = 16 | |
channel = 2 | |
samplingrate = 44100 | |
blocksize = channel * (bitdepth/8) | |
byterate = samplingrate * blocksize | |
infile = open(sys.argv[1], 'rb') | |
data = infile.read() | |
filesize = len(data) | |
shortage = blocksize - (filesize % blocksize) | |
for i in range(shortage) : | |
data += '\0' | |
filesize += shortage | |
outfile = open(sys.argv[2] + '.wav', 'wb') | |
outfile.write('RIFF'\ | |
+ uint4ToStr(filesize + 36)\ | |
+ 'WAVEfmt '\ | |
+ uint4ToStr(16) + uint2ToStr(1)\ | |
+ uint2ToStr(channel)\ | |
+ uint4ToStr(samplingrate)\ | |
+ uint4ToStr(byterate)\ | |
+ uint2ToStr(blocksize)\ | |
+ uint2ToStr(bitdepth)\ | |
+ 'data'\ | |
+ uint4ToStr(filesize)\ | |
+ data) | |
infile.close() | |
outfile.close() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment