-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.py
More file actions
145 lines (114 loc) · 5.73 KB
/
Copy pathencoder.py
File metadata and controls
145 lines (114 loc) · 5.73 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import math
import argparse
from PIL import Image
from scipy.io import wavfile
import common
class SSTVEncoder:
def __init__(self, image_source, width=320, height=240, sample_rate=common.SAMPLE_RATE):
self.image_source = image_source
self.config = common.get_mode_config(width, height, sample_rate)
self.phase = 0.0
self.audio_buffer = []
def load_and_process_image(self):
if isinstance(self.image_source, str):
img = Image.open(self.image_source).convert('RGB')
elif isinstance(self.image_source, Image.Image):
img = self.image_source.convert('RGB')
elif isinstance(self.image_source, np.ndarray):
img = Image.fromarray(self.image_source).convert('RGB')
else:
raise ValueError("Unsupported image source type")
img = img.resize((self.config.width, self.config.height), Image.Resampling.LANCZOS)
return np.array(img)
def add_tone(self, freq, duration_samples):
# Generate tone with continuous phase
t = np.arange(duration_samples)
phase_increment = 2 * np.pi * freq / self.config.sample_rate
phases = self.phase + phase_increment * t
# Update phase for next segment
self.phase = phases[-1] + phase_increment
tone = np.sin(phases)
self.audio_buffer.append(tone)
def add_scan_line_tone(self, pixels, duration_samples, channel_idx):
# pixels: array of pixel values for the channel
# Map pixel values to frequency range 1500Hz - 2300Hz
freqs = common.BLACK_FREQ + (pixels * (common.WHITE_FREQ - common.BLACK_FREQ) / 255.0)
# Generate samples with varying frequency (FM)
t_indices = np.linspace(0, len(pixels) - 1, duration_samples)
interpolated_freqs = np.interp(t_indices, np.arange(len(pixels)), freqs)
# Integrate frequency to get phase
# phase[n] = phase[n-1] + 2*pi*f[n]/Fs
phase_increments = 2 * np.pi * interpolated_freqs / self.config.sample_rate
cumulative_phases = np.cumsum(phase_increments) + self.phase
self.phase = cumulative_phases[-1]
tone = np.sin(cumulative_phases)
self.audio_buffer.append(tone)
def generate_header(self):
# Calibration Header
# Leader tone
self.add_tone(common.LEADER_TONE_FREQ, self.config.leader_tone_samples)
# Break
self.add_tone(common.BREAK_FREQ, self.config.break_samples)
# Leader tone
self.add_tone(common.LEADER_TONE_FREQ, self.config.leader_tone_samples)
# VIS Start bit
self.add_tone(common.BREAK_FREQ, self.config.vis_bit_samples)
# VIS Code 8 (Robot36): 00001000 (LSB first) -> 0, 0, 0, 1, 0, 0, 0, 0
vis_code = 8
parity = 0
for i in range(7):
bit = (vis_code >> i) & 1
parity ^= bit
freq = common.VIS_BIT_1_FREQ if bit else common.VIS_BIT_0_FREQ
self.add_tone(freq, self.config.vis_bit_samples)
# Parity bit
freq = common.VIS_BIT_1_FREQ if parity else common.VIS_BIT_0_FREQ
self.add_tone(freq, self.config.vis_bit_samples)
# Stop bit
self.add_tone(common.BREAK_FREQ, self.config.vis_bit_samples)
def write_wav(self, file_object):
img_array = self.load_and_process_image()
# Convert to YUV
r = img_array[:,:,0].astype(float)
g = img_array[:,:,1].astype(float)
b = img_array[:,:,2].astype(float)
y, u, v = common.rgb_to_yuv(r, g, b)
# Generate Audio
self.generate_header()
for line in range(self.config.height):
# Sync
self.add_tone(common.SYNC_FREQ, self.config.sync_samples)
# Sync Porch
self.add_tone(common.PORCH_FREQ, self.config.sync_porch_samples)
# Y Scan
self.add_scan_line_tone(y[line], self.config.y_scan_samples, 0)
if line % 2 == 0:
# Even Line: Separator (1500) -> Porch -> V Scan
self.add_tone(common.EVEN_SEPARATOR_FREQ, self.config.separator_samples)
self.add_tone(common.PORCH_SEPARATOR_FREQ, self.config.porch_samples)
self.add_scan_line_tone(v[line], self.config.uv_scan_samples, 2)
else:
# Odd Line: Separator (2300) -> Porch -> U Scan
self.add_tone(common.ODD_SEPARATOR_FREQ, self.config.separator_samples)
self.add_tone(common.PORCH_SEPARATOR_FREQ, self.config.porch_samples)
self.add_scan_line_tone(u[line], self.config.uv_scan_samples, 1)
# Concatenate and save
full_signal = np.concatenate(self.audio_buffer)
# Normalize to 16-bit PCM range
# full_signal is -1.0 to 1.0
audio_data = (full_signal * 32767).astype(np.int16)
wavfile.write(file_object, self.config.sample_rate, audio_data)
def encode(self, output_path):
with open(output_path, 'wb') as f:
self.write_wav(f)
print(f"SSTV Audio saved to {output_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Python Robot36 SSTV Encoder")
parser.add_argument("input_image", help="Path to input image")
parser.add_argument("output_wav", help="Path to output WAV file")
parser.add_argument("--width", type=int, default=320, help="Output width (default: 320)")
parser.add_argument("--height", type=int, default=240, help="Output height (default: 240)")
args = parser.parse_args()
encoder = SSTVEncoder(args.input_image, args.width, args.height)
encoder.encode(args.output_wav)