Working encoder
This commit is contained in:
parent
53ad5cfe08
commit
a1ca788da7
35
encode.py
35
encode.py
@ -25,23 +25,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
import wave
|
||||||
|
import struct
|
||||||
|
|
||||||
NAME = 'manchester-encoder'
|
NAME = 'manchester-encoder'
|
||||||
VERSION = '0.1'
|
VERSION = '0.1'
|
||||||
DESCRIPTION = 'Encodes a file using the manchester encoding and outputs it as audio file'
|
DESCRIPTION = 'Encodes a file using the manchester encoding and outputs it as audio file'
|
||||||
|
|
||||||
FRAME_DELIMITER = 129 # (1, 0, 0, 0, 0, 0, 0, 1)
|
FRAME_DELIMITER = 129 # (1, 0, 0, 0, 0, 0, 0, 1)
|
||||||
|
AUDIO_VOLUME = 16384 # 0 to 32767
|
||||||
|
AUDIO_BITRATE = 44100
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._log = logging.getLogger('main')
|
self._log = logging.getLogger('main')
|
||||||
|
self.audioSink = None
|
||||||
|
|
||||||
def run(self, inputFile, outputFile, clock):
|
def run(self, inputFile, outputFile, clock):
|
||||||
# Open output file
|
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()
|
||||||
|
|
||||||
# Read input file
|
# Read input file
|
||||||
bytesToEncode = []
|
bytesToEncode = []
|
||||||
@ -65,6 +81,7 @@ class Main:
|
|||||||
# Encode byte
|
# Encode byte
|
||||||
self.encodeByte(byte)
|
self.encodeByte(byte)
|
||||||
|
|
||||||
|
self.audioSink.close()
|
||||||
self._log.info('Completed')
|
self._log.info('Completed')
|
||||||
|
|
||||||
def encodeByte(self, byte):
|
def encodeByte(self, byte):
|
||||||
@ -94,7 +111,7 @@ class Main:
|
|||||||
# Outputs the preable: a sequence of 64 "1" and "0" used to facilitate the receiver
|
# 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
|
# syncronizing on our clock. The sequence starts with 1 and ends with 0
|
||||||
for x in range(64):
|
for x in range(64):
|
||||||
self.encode(x % 2 == 0)
|
self.encodeBit(x % 2 == 0)
|
||||||
|
|
||||||
def encodeBit(self, bit):
|
def encodeBit(self, bit):
|
||||||
# Encodes a single bit in a pair of bits to be written on the media.
|
# Encodes a single bit in a pair of bits to be written on the media.
|
||||||
@ -109,10 +126,16 @@ class Main:
|
|||||||
|
|
||||||
def out(self, encodedBit):
|
def out(self, encodedBit):
|
||||||
# Write already encoded bit on the media
|
# Write already encoded bit on the media
|
||||||
|
value = 0
|
||||||
if encodedBit:
|
if encodedBit:
|
||||||
print('―', end='')
|
value = AUDIO_VOLUME
|
||||||
else:
|
else:
|
||||||
print('_', end='')
|
value = -AUDIO_VOLUME
|
||||||
|
|
||||||
|
# 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))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user