Skip to content

Instantly share code, notes, and snippets.

@eiszfuchs
Created May 15, 2015 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eiszfuchs/06c9591124909a008ea8 to your computer and use it in GitHub Desktop.
Save eiszfuchs/06c9591124909a008ea8 to your computer and use it in GitHub Desktop.
import hashlib
import math
import re
import struct
import wave
def remap_value(value, min_x, max_x, min_y, max_y):
normalized = (value - min_x) / float(max_x)
return min_y + (normalized * (max_y - min_y))
sample_rate = 44100
def generate_line_tone(noise, line, amplitude):
m = hashlib.md5(line)
tones = []
tone_map = re.findall(".{4}", m.hexdigest())
for tone in tone_map:
tone = float(int("0x" + tone, 16))
tone = remap_value(tone, 0x0000, 0xffff, 220, 440)
tones.append(tone)
samples = []
sample_amount = int(sample_rate / 10.0)
for i in range(sample_amount):
sample = 1
for tone in tones:
sample *= math.sin(i * (sample_rate / tone))
samples.append(int(sample * amplitude))
noise.writeframes("".join(struct.pack("h", sample) for sample in samples))
max_amplitude = math.pow(2, 16) / 2.0
noise = wave.open("noise.wav", "wb")
noise.setnchannels(1) # mono
noise.setsampwidth(2) # 16 bit
noise.setframerate(sample_rate) # CD quality
with open("code.txt", "r") as code_file:
longest_line = 0
for code_line in code_file:
longest_line = max(longest_line, len(code_line))
longest_line = float(longest_line)
code_file.seek(0)
for code_line in code_file:
line_amplitude = max_amplitude * (len(code_line) / longest_line)
generate_line_tone(noise, code_line, line_amplitude)
noise.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment