forked from nicksmadscience/eas-same-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.py
More file actions
90 lines (65 loc) · 1.8 KB
/
Copy pathencoder.py
File metadata and controls
90 lines (65 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
import numpy as np
from scipy.io import wavfile
import random
import sys
import argparse
# parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--string", "-s", nargs='?', default="")
parser.add_argument("--file", "-f", nargs='?', default="")
args = parser.parse_args()
######## CONFIG / constants ########
markBitFrequency = 2200
markBitPhase = 0
markBitAmplitude = 1
markBitTime = 1.0 / (1200)
spaceBitFrequency = 2200
spaceBitPhase = 180
spaceBitAmplitude = 1
spaceBitTime = 1.0 / (1200)
# NOTE: the bit times may not align with the frequency, make sure to check
sampleRate = 48000 # sample rate
print (args)
# t = 1.0 / (520 + (5/6))
# f = 2083.33333
samples = np.zeros(0)
def markBit():
# f = 2083.33333
f = markBitFrequency
t = markBitTime
p = markBitPhase*np.pi/180
samples = np.arange(t * sampleRate) / sampleRate
return np.sin(2 * np.pi * f * samples - p) * markBitAmplitude
def spaceBit():
# f = 1562.5
f = spaceBitFrequency
t = spaceBitTime
p = spaceBitPhase*np.pi/180
samples = np.arange(t * sampleRate) / sampleRate
return np.sin(2 * np.pi * f * samples - p) * spaceBitAmplitude
def byte(inByte):
sys.stdout.write(chr(inByte))
sys.stdout.write(" ")
byte_data = np.zeros(0)
for i in range(0, 8):
if inByte >> i & 1:
sys.stdout.write("1")
byte_data = np.append(byte_data, markBit())
else:
sys.stdout.write("0")
byte_data = np.append(byte_data, spaceBit())
sys.stdout.write("\n")
sys.stdout.flush()
return byte_data
if args.string:
data = args.string.encode("utf-8")
elif args.file:
with open(args.file, "rb") as file:
data = file.read()
signal = np.zeros(0)
for octet in data:
signal = np.append(signal, byte(octet))
signal *= 32767
signal = np.int16(signal)
wavfile.write(str("encoded.wav"), sampleRate, signal)