-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacker.py
More file actions
998 lines (826 loc) · 34.5 KB
/
packer.py
File metadata and controls
998 lines (826 loc) · 34.5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
import math
import numpy as np
import struct
from typing import List, Optional, Tuple
from enum import IntEnum
class CompressionMode(IntEnum):
FLOAT32 = 0
INT16 = 1
INT8 = 2
class SBRDataPacker:
"""Pack SBR band energies with delta encoding and compression."""
def __init__(
self,
min_db: float = -75.0,
max_db: float = 0.0,
delta_threshold: float = 0.5
):
"""
Initialize packer with expected dB range.
Args:
min_db: Minimum expected dB value (floor)
max_db: Maximum expected dB value
delta_threshold: Minimum change in dB to encode (higher = more compression)
"""
self.min_db = min_db
self.max_db = max_db
self.db_range = max_db - min_db
self.delta_threshold = delta_threshold
# Store previous frame for delta encoding
self.prev_energies: Optional[List[float]] = None
self.prev_noises: Optional[List[bool]] = None
def pack(
self,
band_energies: List[float],
is_transient: bool,
shouldUseNoises: Optional[List[bool]] = None,
mode: CompressionMode = CompressionMode.FLOAT32,
use_delta: bool = True
) -> bytes:
"""
Pack band energies with delta encoding and noise flags.
Args:
band_energies: List of band energy values in dB
is_transient: Transient detection flag
shouldUseNoises: Optional list of noise flags per band (True = use noise)
mode: Compression mode (FLOAT32, INT16, or INT8)
use_delta: Enable delta encoding (only send changed values)
Returns:
Packed bytes with header and data
"""
num_bands = len(band_energies)
# Validate shouldUseNoises if provided
if shouldUseNoises is not None and len(shouldUseNoises) != num_bands:
raise ValueError(f"shouldUseNoises length ({len(shouldUseNoises)}) must match band_energies ({num_bands})")
# Force full frame on transients or if no previous data
if is_transient or self.prev_energies is None or not use_delta:
is_delta = False
values_to_encode = band_energies
changed_indices = list(range(num_bands))
else:
# Delta encoding: only send changed values
is_delta = True
changed_indices = []
values_to_encode = []
for i, (curr, prev) in enumerate(zip(band_energies, self.prev_energies)):
# Check if energy changed
energy_changed = abs(curr - prev) >= self.delta_threshold
# Check if noise flag changed (if using noise flags)
noise_changed = False
if shouldUseNoises is not None and self.prev_noises is not None:
noise_changed = shouldUseNoises[i] != self.prev_noises[i]
# Include if either changed
if energy_changed or noise_changed:
changed_indices.append(i)
values_to_encode.append(curr)
# Store for next frame
self.prev_energies = band_energies.copy()
self.prev_noises = shouldUseNoises.copy() if shouldUseNoises is not None else None
# Header: version(1) + flags(1) + mode(1) + num_bands(2) + num_changed(2)
version = 1
has_noise_flags = shouldUseNoises is not None
flags = (int(is_transient) << 0) | (int(is_delta) << 1) | (int(has_noise_flags) << 2)
num_changed = len(changed_indices)
header = struct.pack(
'=BBBHH',
version,
flags,
mode,
num_bands,
num_changed
)
# Pack changed indices (only if delta encoding)
if is_delta and num_changed > 0:
# Use 1 byte per index if num_bands <= 255, else 2 bytes
if num_bands <= 255:
indices_data = struct.pack(f'{num_changed}B', *changed_indices)
else:
indices_data = struct.pack(f'{num_changed}H', *changed_indices)
else:
indices_data = b''
# Pack values based on compression mode
if mode == CompressionMode.FLOAT32:
values_data = struct.pack(f'{num_changed}f', *values_to_encode)
elif mode == CompressionMode.INT16:
normalized = [
(energy - self.min_db) / self.db_range
for energy in values_to_encode
]
int_values = [
int(max(-32768, min(32767, n * 65535 - 32768)))
for n in normalized
]
values_data = struct.pack(f'{num_changed}h', *int_values)
elif mode == CompressionMode.INT8:
normalized = [
(energy - self.min_db) / self.db_range
for energy in values_to_encode
]
int_values = [
int(max(-128, min(127, n * 255 - 128)))
for n in normalized
]
values_data = struct.pack(f'{num_changed}b', *int_values)
else:
raise ValueError(f"Unsupported compression mode: {mode}")
# Pack noise flags if present
if shouldUseNoises is not None:
if is_delta:
# Only pack flags for changed indices
noise_flags = [shouldUseNoises[i] for i in changed_indices]
else:
# Pack all flags
noise_flags = shouldUseNoises
# Pack as bits (8 flags per byte)
noise_data = self._pack_bool_flags(noise_flags)
else:
noise_data = b''
return header + indices_data + values_data + noise_data
def _pack_bool_flags(self, flags: List[bool]) -> bytes:
"""Pack boolean flags into bytes (8 flags per byte)."""
num_bytes = (len(flags) + 7) // 8
result = bytearray(num_bytes)
for i, flag in enumerate(flags):
if flag:
byte_idx = i // 8
bit_idx = i % 8
result[byte_idx] |= (1 << bit_idx)
return bytes(result)
def _unpack_bool_flags(self, data: bytes, num_flags: int) -> List[bool]:
"""Unpack boolean flags from bytes."""
flags = []
for i in range(num_flags):
byte_idx = i // 8
bit_idx = i % 8
if byte_idx < len(data):
flags.append(bool(data[byte_idx] & (1 << bit_idx)))
else:
flags.append(False)
return flags
def reset(self):
"""Reset encoder state (call when starting new stream)."""
self.prev_energies = None
self.prev_noises = None
def get_packed_size(
self,
num_bands: int,
num_changed: int,
mode: CompressionMode,
has_noise_flags: bool = False
) -> int:
"""Get the size of packed data in bytes."""
header_size = 7
# Index size
if num_changed > 0:
idx_size = num_changed if num_bands <= 255 else num_changed * 2
else:
idx_size = 0
# Value size
if mode == CompressionMode.FLOAT32:
val_size = num_changed * 4
elif mode == CompressionMode.INT16:
val_size = num_changed * 2
elif mode == CompressionMode.INT8:
val_size = num_changed * 1
else:
raise ValueError(f"Unsupported mode: {mode}")
# Noise flags size (if present)
noise_size = 0
if has_noise_flags:
noise_size = (num_changed + 7) // 8 # Ceiling division for bit packing
return header_size + idx_size + val_size + noise_size
def get_compression_ratio(
self,
num_bands: int,
num_changed: int,
mode: CompressionMode,
has_noise_flags: bool = False
) -> float:
"""Calculate compression ratio vs full FLOAT32 frame."""
full_size = 7 + num_bands * 4
if has_noise_flags:
full_size += (num_bands + 7) // 8
packed_size = self.get_packed_size(num_bands, num_changed, mode, has_noise_flags)
return full_size / packed_size
class SBRDataUnpacker:
"""Unpack SBR band energies with delta decoding and optional interpolation."""
def __init__(
self,
min_db: float = -75.0,
max_db: float = 0.0,
use_interpolation: bool = True
):
"""
Initialize unpacker with expected dB range.
Args:
min_db: Minimum expected dB value (floor)
max_db: Maximum expected dB value
use_interpolation: Enable interpolation for missing values
"""
self.min_db = min_db
self.max_db = max_db
self.db_range = max_db - min_db
self.use_interpolation = use_interpolation
def unpack(
self,
data: bytes,
prev_frame: Optional[List[float]] = None,
prev_noises: Optional[List[bool]] = None
) -> Tuple[List[float], bool, CompressionMode, Optional[List[bool]]]:
"""
Unpack bytes back to band energies with delta decoding.
Args:
data: Packed bytes
prev_frame: Previous frame data (required for delta frames)
prev_noises: Previous noise flags (required for delta frames with noise)
Returns:
Tuple of (band_energies, is_transient, mode, shouldUseNoises)
"""
if len(data) < 7:
raise ValueError("Data too short, invalid format")
# Unpack header
version, flags, mode_val, num_bands, num_changed = struct.unpack(
'=BBBHH',
data[:7]
)
if version != 1:
raise ValueError(f"Unsupported version: {version}")
mode = CompressionMode(mode_val)
is_transient = bool(flags & 0x01)
is_delta = bool(flags & 0x02)
has_noise_flags = bool(flags & 0x04)
offset = 7
# Read changed indices
if is_delta and num_changed > 0:
if num_bands <= 255:
idx_size = num_changed
changed_indices = list(struct.unpack(
f'{num_changed}B',
data[offset:offset + idx_size]
))
else:
idx_size = num_changed * 2
changed_indices = list(struct.unpack(
f'{num_changed}H',
data[offset:offset + idx_size]
))
offset += idx_size
else:
changed_indices = list(range(num_bands))
# Read values
if mode == CompressionMode.FLOAT32:
val_size = num_changed * 4
elif mode == CompressionMode.INT16:
val_size = num_changed * 2
elif mode == CompressionMode.INT8:
val_size = num_changed
else:
raise ValueError(f"Unsupported compression mode: {mode}")
payload = data[offset:offset + val_size]
offset += val_size
if mode == CompressionMode.FLOAT32:
if len(payload) != val_size:
raise ValueError(f"Expected {val_size} bytes, got {len(payload)}")
changed_values = list(struct.unpack(f'{num_changed}f', payload))
elif mode == CompressionMode.INT16:
if len(payload) != val_size:
raise ValueError(f"Expected {val_size} bytes, got {len(payload)}")
int_values = struct.unpack(f'{num_changed}h', payload)
changed_values = [
(val + 32768) / 65535 * self.db_range + self.min_db
for val in int_values
]
elif mode == CompressionMode.INT8:
if len(payload) != val_size:
raise ValueError(f"Expected {val_size} bytes, got {len(payload)}")
int_values = struct.unpack(f'{num_changed}b', payload)
changed_values = [
(val + 128) / 255 * self.db_range + self.min_db
for val in int_values
]
# Read noise flags if present
shouldUseNoises = None
if has_noise_flags:
noise_bytes = (num_changed + 7) // 8
noise_data = data[offset:offset + noise_bytes]
changed_noise_flags = self._unpack_bool_flags(noise_data, num_changed)
# Reconstruct full noise array if delta
if is_delta:
if prev_noises is None:
raise ValueError("Delta frame with noise flags requires prev_noises")
if len(prev_noises) != num_bands:
raise ValueError(f"Previous noise size mismatch: expected {num_bands}, got {len(prev_noises)}")
shouldUseNoises = prev_noises.copy()
for idx, flag in zip(changed_indices, changed_noise_flags):
shouldUseNoises[idx] = flag
else:
shouldUseNoises = changed_noise_flags
# Reconstruct full frame
if is_delta:
if prev_frame is None:
raise ValueError("Delta frame requires previous frame data")
if len(prev_frame) != num_bands:
raise ValueError(f"Previous frame size mismatch: expected {num_bands}, got {len(prev_frame)}")
# Start with previous frame
band_energies = prev_frame.copy()
# Update changed values
for idx, val in zip(changed_indices, changed_values):
band_energies[idx] = val
# Optional: Interpolate unchanged values for smoothness
if self.use_interpolation and len(changed_indices) < num_bands:
band_energies = self._interpolate_missing(
band_energies,
changed_indices,
num_bands
)
else:
# Full frame
band_energies = changed_values
return band_energies, is_transient, mode, shouldUseNoises
def _unpack_bool_flags(self, data: bytes, num_flags: int) -> List[bool]:
"""Unpack boolean flags from bytes."""
flags = []
for i in range(num_flags):
byte_idx = i // 8
bit_idx = i % 8
if byte_idx < len(data):
flags.append(bool(data[byte_idx] & (1 << bit_idx)))
else:
flags.append(False)
return flags
def _interpolate_missing(
self,
values: List[float],
changed_indices: List[int],
num_bands: int
) -> List[float]:
"""
Interpolate unchanged values between changed values for smoothness.
"""
if len(changed_indices) <= 1:
return values
result = values.copy()
changed_set = set(changed_indices)
for i in range(num_bands):
if i in changed_set:
continue
# Find nearest changed indices before and after
prev_idx = None
next_idx = None
for idx in changed_indices:
if idx < i:
prev_idx = idx
elif idx > i and next_idx is None:
next_idx = idx
break
# Interpolate between neighbors
if prev_idx is not None and next_idx is not None:
# Linear interpolation
t = (i - prev_idx) / (next_idx - prev_idx)
result[i] = values[prev_idx] * (1 - t) + values[next_idx] * t
elif prev_idx is not None:
# Extrapolate from previous
result[i] = values[prev_idx] * 0.7 + result[i] * 0.3
elif next_idx is not None:
# Extrapolate from next
result[i] = values[next_idx] * 0.7 + result[i] * 0.3
return result
def pack_stereo_metadata(pan_values, ipd_values, ic_values, min_freq, max_freq, point, n_bands):
"""
Pack PS metadata:
- pan_values: list of floats [-1, 1]
- ipd_values: list of floats [-pi, pi]
- ic_values: list of bools
- min_freq: int (minimum frequency)
- max_freq: int (maximum frequency)
- point: int
- n_bands: int (number of bands)
Returns: bytes
"""
n = len(pan_values)
if not (len(ipd_values) == len(ic_values) == n):
raise ValueError("All input lists must have same length")
if n != n_bands:
raise ValueError(f"n_bands ({n_bands}) must match length of input lists ({n})")
packed_bytes = bytearray()
# Pack 4 integers at the beginning (4 bytes each = 16 bytes total)
packed_bytes.extend(struct.pack('<i', min_freq))
packed_bytes.extend(struct.pack('<i', max_freq))
packed_bytes.extend(struct.pack('<i', point))
packed_bytes.extend(struct.pack('<i', n_bands))
# Pack PAN and IPD as int8
for pan, ipd in zip(pan_values, ipd_values):
pan_byte = int(round(pan * 127))
ipd_byte = int(round(ipd / math.pi * 127))
pan_byte = max(-128, min(127, pan_byte))
ipd_byte = max(-128, min(127, ipd_byte))
packed_bytes.append(pan_byte & 0xFF)
packed_bytes.append(ipd_byte & 0xFF)
# Pack IC as bits (1 bit per band)
ic_byte = 0
bit_count = 0
for ic in ic_values:
ic_byte = (ic_byte << 1) | (1 if ic else 0)
bit_count += 1
if bit_count == 8:
packed_bytes.append(ic_byte & 0xFF)
ic_byte = 0
bit_count = 0
# Remaining bits
if bit_count > 0:
ic_byte = ic_byte << (8 - bit_count)
packed_bytes.append(ic_byte & 0xFF)
return bytes(packed_bytes)
def unpack_stereo_metadata(packed_bytes):
"""
Unpack PS metadata
Returns: (pan_values, ipd_values, ic_values, min_freq, max_freq, point, n_bands)
"""
# Unpack 4 integers from the beginning
min_freq = struct.unpack('<i', packed_bytes[0:4])[0]
max_freq = struct.unpack('<i', packed_bytes[4:8])[0]
point = struct.unpack('<i', packed_bytes[8:12])[0]
n_bands = struct.unpack('<i', packed_bytes[12:16])[0]
pan_values = []
ipd_values = []
ic_values = []
# PAN/IPD start after the header (16 bytes)
header_size = 16
for i in range(n_bands):
offset = header_size + i * 2
pan_byte = struct.unpack('b', packed_bytes[offset:offset + 1])[0]
ipd_byte = struct.unpack('b', packed_bytes[offset + 1:offset + 2])[0]
pan = pan_byte / 127.0
ipd = ipd_byte / 127.0 * math.pi
pan_values.append(pan)
ipd_values.append(ipd)
# IC bits start after PAN/IPD
ic_start = header_size + n_bands * 2
total_ic_bits = n_bands
bits_read = 0
for b in packed_bytes[ic_start:]:
for i in range(7, -1, -1):
if bits_read >= total_ic_bits:
break
bit = (b >> i) & 1
ic_values.append(bool(bit))
bits_read += 1
return pan_values, ipd_values, ic_values, min_freq, max_freq, point
def quantize_coefficients(coeffs, num_bits=8):
"""
Simple uniform scalar quantization
Parameters:
-----------
coeffs : np.array
MDCT coefficients to quantize
num_bits : int
Number of bits for quantization (default: 8)
Returns:
--------
quantized : np.array (int)
Quantized coefficients
scale : float
Scale factor for dequantization
"""
if len(coeffs) == 0:
return np.array([]), 0.0
# Find max absolute value for scaling
max_val = np.max(np.abs(coeffs))
if max_val == 0:
return np.zeros_like(coeffs, dtype=np.int16), 0.0
# Calculate quantization levels
max_level = (2 ** (num_bits - 1)) - 1 # Leave one bit for sign
scale = max_val / max_level
# Quantize
quantized = np.round(coeffs / scale).astype(np.int16)
return quantized, scale
def dequantize_coefficients(quantized, scale):
"""
Dequantize coefficients
Parameters:
-----------
quantized : np.array (int)
Quantized coefficients
scale : float
Scale factor from quantization
Returns:
--------
coeffs : np.array (float)
Dequantized coefficients
"""
return quantized.astype(np.float32) * scale
import struct
import math
import numpy as np
class HarmonicPacker:
"""
Packs harmonic data with predictive compression.
Prediction modes:
- 'none': No prediction
- 'delta': Store differences from previous frame
- 'linear': Linear extrapolation from previous 2 frames
"""
def __init__(self, sample_rate, window_size,
amp_dtype='uint8', phase_dtype='uint8',
scale_mode='log', min_amp_db=-80,
prediction_mode='delta'):
self.sr = sample_rate
self.win = window_size
self.bin_size = sample_rate / window_size
self.amp_dtype = amp_dtype
self.phase_dtype = phase_dtype
self.scale_mode = scale_mode
self.min_amp_db = min_amp_db
self.min_amp_linear = 10 ** (min_amp_db / 20)
self.prediction_mode = prediction_mode
# Store previous frames for prediction
self.prev_frames = []
self.dtype_formats = {
'uint8': 'B', 'uint16': 'H', 'int8': 'b',
'int16': 'h', 'float16': None, 'float32': 'f'
}
self.dtype_ranges = {
'uint8': (0, 255), 'uint16': (0, 65535),
'int8': (-128, 127), 'int16': (-32768, 32767),
'float16': (None, None), 'float32': (None, None)
}
def _encode_amplitude(self, amp, max_amp):
"""Encode amplitude based on data type and scale mode."""
if self.amp_dtype in ['float16', 'float32']:
return amp
if max_amp < self.min_amp_linear:
max_amp = self.min_amp_linear
if self.scale_mode == 'linear':
norm = amp / max_amp
elif self.scale_mode == 'log':
amp_safe = max(amp, self.min_amp_linear)
max_safe = max(max_amp, self.min_amp_linear)
try:
norm = math.log(amp_safe / self.min_amp_linear) / math.log(max_safe / self.min_amp_linear)
except (ZeroDivisionError, ValueError):
norm = 0.0
norm = np.clip(norm, 0, 1)
elif self.scale_mode == 'db':
amp_safe = max(amp, self.min_amp_linear)
max_safe = max(max_amp, self.min_amp_linear)
amp_db = 20 * math.log10(amp_safe)
max_db = 20 * math.log10(max_safe)
norm = (amp_db - self.min_amp_db) / (max_db - self.min_amp_db)
norm = np.clip(norm, 0, 1)
else:
norm = amp / max_amp
vmin, vmax = self.dtype_ranges[self.amp_dtype]
return int(norm * (vmax - vmin) + vmin)
def _encode_phase(self, phase):
"""Encode phase to selected data type."""
if self.phase_dtype in ['float16', 'float32']:
return phase
# Normalize phase from [-π, π] to [0, 1]
norm = (phase + math.pi) / (2 * math.pi)
vmin, vmax = self.dtype_ranges[self.phase_dtype]
return int(norm * (vmax - vmin) + vmin)
def _pack_value(self, value, dtype):
"""Pack a single value according to its data type."""
if dtype == 'float16':
return np.float16(value).tobytes()
elif dtype == 'float32':
return struct.pack('f', value)
else:
fmt = self.dtype_formats[dtype]
return struct.pack(fmt, value)
def _predict_frame(self, current_frame):
"""Generate prediction for current frame based on history."""
if self.prediction_mode == 'none' or len(self.prev_frames) == 0:
return None
if self.prediction_mode == 'delta':
# Predict using previous frame
return self.prev_frames[-1]
elif self.prediction_mode == 'linear' and len(self.prev_frames) >= 2:
# Linear extrapolation from last 2 frames
prev1 = self.prev_frames[-1]
prev2 = self.prev_frames[-2]
predicted = []
for i, obj in enumerate(current_frame):
if i < len(prev1) and i < len(prev2):
# Match by frequency
p1 = prev1[i]
p2 = prev2[i]
if abs(p1['freq'] - obj['freq']) < self.bin_size * 2:
pred_harms = []
for j in range(min(len(p1['harmonics']), len(obj['harmonics']))):
h1 = p1['harmonics'][j]
h2 = p2['harmonics'][j]
# Extrapolate amplitude and phase
pred_amp = 2 * h1['amp'] - h2['amp']
pred_phase = 2 * h1['phase'] - h2['phase']
pred_harms.append({
'amp': max(pred_amp, 0),
'phase': pred_phase
})
predicted.append({
'freq': obj['freq'],
'harmonics': pred_harms
})
continue
predicted.append(None)
return predicted
# Fallback to delta
return self.prev_frames[-1] if self.prev_frames else None
def _compute_residual(self, current, predicted):
"""Compute residual between current and predicted frame."""
if predicted is None:
return current, False
residuals = []
has_prediction = False
for i, obj in enumerate(current):
pred = predicted[i] if i < len(predicted) else None
if pred and abs(pred['freq'] - obj['freq']) < self.bin_size * 2:
# Compute residual
res_harms = []
for j, h in enumerate(obj['harmonics']):
if j < len(pred['harmonics']):
ph = pred['harmonics'][j]
res_harms.append({
'amp': h['amp'] - ph['amp'],
'phase': h['phase'] - ph['phase']
})
has_prediction = True
else:
res_harms.append(h)
residuals.append({
'freq': obj['freq'],
'harmonics': res_harms
})
else:
residuals.append(obj)
return residuals, has_prediction
def pack_chunk(self, harmonic_objects, use_prediction=True):
"""Pack harmonic objects into bytes with optional prediction."""
prediction = self._predict_frame(harmonic_objects) if use_prediction else None
residuals, has_pred = self._compute_residual(harmonic_objects, prediction)
# Header: prediction flag (1 byte)
payload = bytearray([1 if has_pred else 0])
for obj in residuals:
freq = obj["freq"]
harms = obj["harmonics"]
hcount = len(harms)
freq_bin = int(freq / self.bin_size)
max_amp_obj = max(abs(h['amp']) for h in harms) if harms else self.min_amp_linear
# Pack header: freq_bin (uint16), hcount (uint8), scale_factor (float32)
payload += struct.pack('<HB', freq_bin, hcount)
payload += struct.pack('f', max_amp_obj)
# Pack each harmonic
for h in harms:
encoded_amp = self._encode_amplitude(abs(h["amp"]), max_amp_obj)
encoded_phase = self._encode_phase(h["phase"])
payload += self._pack_value(encoded_amp, self.amp_dtype)
payload += self._pack_value(encoded_phase, self.phase_dtype)
# Update history
self.prev_frames.append(harmonic_objects)
if len(self.prev_frames) > 2:
self.prev_frames.pop(0)
return bytes(payload)
def reset(self):
"""Reset prediction history."""
self.prev_frames = []
def get_bytes_per_harmonic(self):
"""Calculate bytes per harmonic for current configuration."""
amp_size = 2 if self.amp_dtype == 'float16' else struct.calcsize(self.dtype_formats.get(self.amp_dtype, 'B'))
phase_size = 2 if self.phase_dtype == 'float16' else struct.calcsize(self.dtype_formats.get(self.phase_dtype, 'B'))
return amp_size + phase_size
class HarmonicUnpacker:
"""
Unpacks harmonic data with predictive decompression.
"""
def __init__(self, sample_rate, window_size,
amp_dtype='uint8', phase_dtype='uint8',
scale_mode='log', min_amp_db=-80):
self.sr = sample_rate
self.win = window_size
self.bin_size = sample_rate / window_size
self.amp_dtype = amp_dtype
self.phase_dtype = phase_dtype
self.scale_mode = scale_mode
self.min_amp_db = min_amp_db
self.min_amp_linear = 10 ** (min_amp_db / 20)
# Store previous frames for prediction reconstruction
self.prev_frames = []
self.dtype_formats = {
'uint8': 'B', 'uint16': 'H', 'int8': 'b',
'int16': 'h', 'float16': None, 'float32': 'f'
}
self.dtype_ranges = {
'uint8': (0, 255), 'uint16': (0, 65535),
'int8': (-128, 127), 'int16': (-32768, 32767),
'float16': (None, None), 'float32': (None, None)
}
def _decode_amplitude(self, encoded_val, max_amp):
"""Decode amplitude from encoded value."""
if self.amp_dtype in ['float16', 'float32']:
return encoded_val
vmin, vmax = self.dtype_ranges[self.amp_dtype]
norm = (encoded_val - vmin) / (vmax - vmin)
if self.scale_mode == 'linear':
return norm * max_amp
elif self.scale_mode == 'log':
max_safe = max(max_amp, self.min_amp_linear)
log_ratio = math.log(max_safe / self.min_amp_linear)
return self.min_amp_linear * math.exp(norm * log_ratio)
elif self.scale_mode == 'db':
max_safe = max(max_amp, self.min_amp_linear)
max_db = 20 * math.log10(max_safe)
amp_db = norm * (max_db - self.min_amp_db) + self.min_amp_db
return 10 ** (amp_db / 20)
else:
return norm * max_amp
def _decode_phase(self, encoded_val):
"""Decode phase from encoded value."""
if self.phase_dtype in ['float16', 'float32']:
return encoded_val
vmin, vmax = self.dtype_ranges[self.phase_dtype]
norm = (encoded_val - vmin) / (vmax - vmin)
return norm * (2 * math.pi) - math.pi
def _unpack_value(self, data, pos, dtype):
"""Unpack a single value and return (value, new_position)."""
if dtype == 'float16':
value = np.frombuffer(data[pos:pos + 2], dtype=np.float16)[0]
return float(value), pos + 2
elif dtype == 'float32':
value = struct.unpack_from('f', data, pos)[0]
return value, pos + 4
else:
fmt = self.dtype_formats[dtype]
size = struct.calcsize(fmt)
value = struct.unpack_from(fmt, data, pos)[0]
return value, pos + size
def _reconstruct_from_residual(self, residuals, has_prediction):
"""Reconstruct full frame from residuals using prediction."""
if not has_prediction or len(self.prev_frames) == 0:
return residuals
predicted = self.prev_frames[-1]
reconstructed = []
for i, res_obj in enumerate(residuals):
pred = predicted[i] if i < len(predicted) else None
if pred and abs(pred['freq'] - res_obj['freq']) < self.bin_size * 2:
# Add residuals to prediction
rec_harms = []
for j, res_h in enumerate(res_obj['harmonics']):
if j < len(pred['harmonics']):
ph = pred['harmonics'][j]
rec_harms.append({
'amp': res_h['amp'] + ph['amp'],
'phase': res_h['phase'] + ph['phase']
})
else:
rec_harms.append(res_h)
reconstructed.append({
'freq': res_obj['freq'],
'n_harmonic': len(rec_harms),
'main_amp': rec_harms[0]['amp'] if rec_harms else 0.0,
'harmonics': rec_harms
})
else:
reconstructed.append({
'freq': res_obj['freq'],
'n_harmonic': len(res_obj['harmonics']),
'main_amp': res_obj['harmonics'][0]['amp'] if res_obj['harmonics'] else 0.0,
'harmonics': res_obj['harmonics']
})
return reconstructed
def unpack_chunk(self, data):
"""Unpack bytes back into harmonic objects."""
pos = 0
length = len(data)
# Read prediction flag
has_prediction = data[pos] == 1
pos += 1
objects = []
while pos < length:
# Read header
freq_bin, hcount = struct.unpack_from('<HB', data, pos)
pos += 3
max_amp_obj = struct.unpack_from('f', data, pos)[0]
pos += 4
freq = freq_bin * self.bin_size
harmonics = []
# Read harmonics
for _ in range(hcount):
encoded_amp, pos = self._unpack_value(data, pos, self.amp_dtype)
encoded_phase, pos = self._unpack_value(data, pos, self.phase_dtype)
amp = self._decode_amplitude(encoded_amp, max_amp_obj)
phase = self._decode_phase(encoded_phase)
harmonics.append({'amp': amp, 'phase': phase})
objects.append({
"freq": freq,
"harmonics": harmonics
})
# Reconstruct from residuals if prediction was used
reconstructed = self._reconstruct_from_residual(objects, has_prediction)
# Update history
self.prev_frames.append(reconstructed)
if len(self.prev_frames) > 2:
self.prev_frames.pop(0)
return reconstructed
def reset(self):
"""Reset prediction history."""
self.prev_frames = []