Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # (Amplitude bug has been fixed)
- # (Severe LSB bug has been fixed...)
- # This program generates a classic digital EAS (SAME) header, with preamble followed by message data
- # According to standard, the header is repeated 3 times, including preamble, with 1 second inbetween.
- # It then saves the signal to an uncompressed .wav file with the given file name
- # The bits are encoded as 4 sine wave cycles for a 1 and 3 cycles for a 0, over a standard timespan of 1.92ms
- # Each character (ASCII standard) is transmitted least significant bit first
- # This program allows you to generate any header you want, but for valid headers, the format can be found at:
- # https://en.wikipedia.org/wiki/Specific_Area_Message_Encoding
- from math import sin, pi
- import wave
- # Freely modifiable variables:
- message = "Example message, type anything you want! (as long as it's ASCII)"
- filename = 'signalaudio.wav'
- rate = 44100 # sample rate in hz
- bitlength = 0.00192 # bit length in seconds, recommend not changing
- amplitude = 1.0 # range between 0 and 1
- # ----------
- sample_width = 1
- max_amplitude = 2 ** (8 * sample_width) - 1
- half_max_amp = 0.5 * max_amplitude
- wav = wave.open(filename, 'wb')
- wav.setnchannels(1)
- wav.setsampwidth(sample_width)
- wav.setframerate(rate)
- def bits_lsb(_bytes):
- for byte in _bytes:
- for i in range(8):
- yield (byte >> i) & 1
- def generate_bit_signal(ncycles):
- cycle = bytearray()
- samples_per_cycle = round(rate * bitlength / ncycles)
- for i in range(samples_per_cycle):
- cycle.append(round(amplitude * half_max_amp * sin(i * 2 * pi / samples_per_cycle) + half_max_amp))
- return bytes(cycle * ncycles)
- MARK = generate_bit_signal(4)
- SPACE = generate_bit_signal(3)
- def generate_byte_signal(_bytes):
- signal = bytearray()
- for bit in bits_lsb(_bytes):
- signal.extend(MARK if bit == 1 else SPACE)
- return bytes(signal)
- data = (generate_byte_signal(b'\xAB' * 16 + bytes(message, encoding='ascii')) + bytes([int(half_max_amp)]) * rate) * 3
- wav.writeframes(data)
- wav.close()
Add Comment
Please, Sign In to add comment