#!/usr/bin/env python

import sys
import string

#define FormatID 'fmt '   /* chunkID for Format Chunk. NOTE: There is a space at the end of this ID. */

# typedef struct {
# 	ID             chunkID;
# 	long           chunkSize;
# 	short          wFormatTag;
# 	unsigned short wChannels;
# 	unsigned long  dwSamplesPerSec;
# 	unsigned long  dwAvgBytesPerSec;
# 	unsigned short wBlockAlign;
# 	unsigned short wBitsPerSample;
# 	/* Note: there may be additional fields here, depending upon wFormatTag. */
# } FormatChunk;

def gimme_list(number, number_of_bytes):
	temp_number = number
#	print 'temp_number is', temp_number
	list = []
	for byteno in range(number_of_bytes):
		(quotient, remainder) = divmod(temp_number, 256)
		list.append(remainder)
		temp_number = quotient
	#list.reverse()
#	print 'list is', list
	return list

def condense(list):
	#return reduce(lambda y,x: x*256 + y, list, 0)
	return string.joinfields(map(chr,list), '')

def signed_short_bytes(s):
	# we're assuming the values will neither be too large, nor negative, so it won't matter
	return unsigned_short_bytes(s)

def signed_long_bytes(s):
	# we're assuming the values will neither be too large, nor negative, so it won't matter
	return unsigned_long_bytes(s)

def unsigned_short_bytes(s):
	return condense(gimme_list(s, 2))

def unsigned_long_bytes(s):
	return condense(gimme_list(s, 4))

def write_wav(chunkSize, wFormatTag, wChannels, dwSamplesPerSec, dwAvgBytesPerSec, wBlockAlign, wBitsPerSample, sound_data):
	filename = 'testfile-%d-%04d-%d-%d-%d-%d-%d.wav' % ( \
		chunkSize, \
		wFormatTag, \
		wChannels, \
		dwSamplesPerSec, \
		dwAvgBytesPerSec, \
		wBlockAlign, \
		wBitsPerSample)
	file = open(filename, 'w')
	file.write('RIFFxH\0\0WAVEfmt %s%s%s%s%s%s%sdata%s%s' % ( \
		signed_long_bytes(16), \
		signed_short_bytes(wFormatTag), \
		unsigned_short_bytes(wChannels), \
		unsigned_long_bytes(dwSamplesPerSec), \
		unsigned_long_bytes(dwAvgBytesPerSec), \
		unsigned_short_bytes(wBlockAlign), \
		unsigned_short_bytes(wBitsPerSample), \
		signed_long_bytes(len(sound_data)), \
		sound_data
		))
	file.close()

for_real = 1

if for_real:
	containerless = open('containerless', 'r').read()

	chunkSize = 512 # semi educated guess, may be a problem
	for wFormatTag in range(1024):
		wChannels = 1
		dwSamplesPerSec = 22050 # not necessarily correct, but this is what the output .wav's have
		dwAvgBytesPerSec = 6360 # totally wild guess, may be a problem
		wBlockAlign = 512
		wBitsPerSample = 16
		write_wav(chunkSize, wFormatTag, wChannels, dwSamplesPerSec, dwAvgBytesPerSec, wBlockAlign, wBitsPerSample, containerless)
else:
	# just some testing...
	sys.stdout.write(unsigned_short_bytes(257) + '\0')
	sys.stdout.write(unsigned_short_bytes(258) + '\0')
	sys.stdout.write(unsigned_short_bytes(513) + '\0')
	sys.stdout.write(unsigned_long_bytes(257) + '\0')
	sys.stdout.write(unsigned_long_bytes(258) + '\0')
	sys.stdout.write(unsigned_long_bytes(513) + '\0')
	sys.stdout.write(unsigned_long_bytes(0x01020304) + '\0')
	sys.stdout.write(unsigned_long_bytes(0x04030201) + '\0')

