manchester-encoding/encode.py

166 lines
4.8 KiB
Python
Raw Permalink Normal View History

2022-06-03 15:13:25 +02:00
#!/usr/bin/env python3
""" @package docstring
Manchester encoder
Encodes a file using the manchester encoding and outputs it as audio file
See https://www.youtube.com/watch?v=8BhjXqw9MqI&list=PLowKtXNTBypH19whXTVoG3oKSuOcw_XeW&index=3
and following videos by awesome Ben Eater
Very slow and inefficient implementation, meant only to be clear and didactic
@author Daniele Verducci <daniele.verducci@ichibi.eu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import logging
2022-06-03 15:57:35 +02:00
import wave
import struct
2022-06-03 15:13:25 +02:00
NAME = 'manchester-encoder'
VERSION = '0.1'
DESCRIPTION = 'Encodes a file using the manchester encoding and outputs it as audio file'
2022-06-04 09:11:59 +02:00
FRAME_DELIMITER = 126 # (01111110)
FRAME_DELIMITER_EVERY_BYTES = 64
2022-06-05 07:44:54 +02:00
PREAMBLE_DURATION = 512
2022-06-03 15:57:35 +02:00
AUDIO_VOLUME = 16384 # 0 to 32767
AUDIO_BITRATE = 44100
2022-06-03 15:13:25 +02:00
class Main:
def __init__(self):
self._log = logging.getLogger('main')
2022-06-03 15:57:35 +02:00
self.audioSink = None
2022-06-03 15:13:25 +02:00
def run(self, inputFile, outputFile, clock):
2022-06-03 15:57:35 +02:00
self.clock = int(clock)
# Check clock speed is valid
if self.clock > (AUDIO_BITRATE / 2):
raise ValueError("Clock too high: max supported clock is {}".format(AUDIO_BITRATE/2))
# Open output audio file
self.audioSink = wave.open(outputFile, 'w')
self.audioSink.setnchannels(1) # mono
self.audioSink.setsampwidth(2)
self.audioSink.setframerate(44100.0)
# Preamble
self.outputPreamble()
2022-06-03 15:13:25 +02:00
# Read input file
bytesToEncode = []
with open(inputFile, 'rb') as f:
position = 0
while 1:
byte = f.read(1)
if not byte:
# Finished reading file
# Terminate with delimiter and exit
#self.encodeByte(FRAME_DELIMITER, encode_frame_delimiter=False)
2022-06-03 15:13:25 +02:00
break
byte = byte[0]
# Every 64 bytes, outputs a frame delimiter: 01111110
# This is used by receiver to syncronize to the start of a byte
if position % FRAME_DELIMITER_EVERY_BYTES == 0:
2022-06-04 09:11:59 +02:00
self.encodeByte(FRAME_DELIMITER, encode_frame_delimiter=False)
2022-06-03 15:13:25 +02:00
position = position + 1
# Encode byte
self.encodeByte(byte)
2022-06-04 09:11:59 +02:00
2022-06-03 15:57:35 +02:00
self.audioSink.close()
2022-06-03 15:13:25 +02:00
self._log.info('Completed')
2022-06-04 09:11:59 +02:00
def encodeByte(self, byte, encode_frame_delimiter=True):
2022-06-03 15:13:25 +02:00
# Encodes a byte with the Mancester Encoding
# Note that the byte is read from the most important to the least important bit
# Es: 10000010 is not 130, but 65
consecutiveOnes = 0
for x in range(8):
# Shift byte and take last bit (with bitwise AND)
lastBit = ( byte >> x ) & 1
# Write bit
self.encodeBit(lastBit)
2022-06-04 09:11:59 +02:00
# If we have 5 consecutive "1", add a 0 after, to avoid being interpreted as a frame delimiter (01111110))
2022-06-03 15:13:25 +02:00
if lastBit:
consecutiveOnes = consecutiveOnes + 1
else:
consecutiveOnes = 0
2022-06-04 09:11:59 +02:00
if consecutiveOnes == 5 and encode_frame_delimiter:
2022-06-03 15:13:25 +02:00
self.encodeBit(0)
consecutiveOnes = 0
def outputPreamble(self):
# Outputs the preable: a sequence of 64 "1" and "0" used to facilitate the receiver
# syncronizing on our clock. The sequence starts with 1 and ends with 0
2022-06-03 17:33:58 +02:00
for x in range(PREAMBLE_DURATION):
2022-06-03 15:57:35 +02:00
self.encodeBit(x % 2 == 0)
2022-06-03 15:13:25 +02:00
def encodeBit(self, bit):
# Encodes a single bit in a pair of bits to be written on the media.
2022-06-04 09:11:59 +02:00
# The "1" is encoded as a transition from 0 to 1 (01) while the "0" is endoded as a
2022-06-03 15:13:25 +02:00
# transition from 1 to 0 (10)
if bit:
self.out(0)
self.out(1)
else:
self.out(1)
self.out(0)
def out(self, encodedBit):
# Write already encoded bit on the media
2022-06-03 15:57:35 +02:00
value = 0
2022-06-03 15:13:25 +02:00
if encodedBit:
2022-06-03 15:57:35 +02:00
value = AUDIO_VOLUME
2022-06-03 15:13:25 +02:00
else:
2022-06-03 15:57:35 +02:00
value = -AUDIO_VOLUME
2022-06-04 09:11:59 +02:00
2022-06-03 15:57:35 +02:00
# The duration of the signal is calculated based on the user-specified clock speed
duration = int(AUDIO_BITRATE / self.clock)
for x in range(duration):
self.audioSink.writeframesraw(struct.pack('<h', value))
2022-06-03 15:13:25 +02:00
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
prog = NAME + '.py',
description = NAME + ' ' + VERSION + '\n' + DESCRIPTION,
formatter_class = argparse.RawTextHelpFormatter
)
parser.add_argument('inputFile', help="file to encode")
parser.add_argument('outputFile', help="audio file to write")
parser.add_argument('clock', help="clock speed, in hz")
parser.add_argument('-v', '--verbose', action='store_true', help="verbose output")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig()
main = Main()
main.run(args.inputFile, args.outputFile, args.clock)
sys.exit(0)