Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Copyright 2015 Gareth Poole, All Rights Reserved
- import sys
- import math
- from array import array
- sample_rate = 44100
- def floatToInt(n):
- return int(n * 32768)
- def byteToBits(byte):
- bits = []
- for i in range(7, -1, -1):
- bits += [(byte >> i) & 1]
- return bits
- def genSine(freq, length):
- samples = []
- length_samples = int(length * sample_rate)
- period = (1 / freq) * sample_rate
- for i in range(0, length_samples):
- samples += [floatToInt(math.sin(i * (2 * math.pi) / period))]
- return samples
- def sumSamples(sample_list):
- return int(sum(sample_list) / len(sample_list))
- def sumSignals(signal_list):
- return map(sumSamples, zip(*signal_list))
- def encodeByte(bits, upshift, length):
- sines = []
- length_samples = int(length * sample_rate)
- for i in range(0, 8):
- if bits[i]:
- sines += [genSine((upshift * 800) + 100 * (i + 1), length)]
- else:
- sines += [[0] * length_samples]
- return sumSignals(sines)
- def encodeWord(word, length):
- byte_signals = [genSine(50, length)] # pilot tone
- for i in range(16):
- byte_signals += [encodeByte(word[i], i, length)]
- return sumSignals(byte_signals)
- def encodeString(string, length):
- words = []
- current_word = []
- word_position = 0
- strlen = len(string)
- samples = []
- for i in range(strlen):
- if word_position < 16:
- current_word += [byteToBits(string[i])]
- word_position += 1
- if i == strlen - 1:
- words += [current_word] # Otherwise the final word would not get saved
- else:
- words += [current_word]
- current_word = []
- word_position = 0
- for word in words:
- lenword = len(word)
- if(lenword < 16):
- word += [[0,0,0,0,0,0,0,0]] * (16 - lenword)
- samples += encodeWord(word, length)
- return samples
- infile = open(sys.argv[0], "rb")
- outfile = open(sys.argv[1], "wb")
- array('h', encodeString(infile.read(), 0.03125)).tofile(outfile)
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement