bridge.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import sys
  3. import time
  4. import socket
  5. import struct
  6. import numpy as np
  7. import fcntl
  8. # Loosen the lobotomy slightly to 8192 bytes.
  9. try:
  10. F_SETPIPE_SZ = 1031
  11. fcntl.fcntl(sys.stdin.fileno(), F_SETPIPE_SZ, 8192)
  12. except Exception:
  13. pass
  14. # Network settings
  15. UDP_IP = os.environ.get("UDP_IP", "239.0.0.1")
  16. UDP_PORT = int(os.environ.get("UDP_PORT", "11988"))
  17. # Audio stream constants optimized for 24kHz downsampling
  18. SAMPLE_RATE = 24000
  19. CHUNK_SIZE = 512
  20. CHUNK_DURATION = CHUNK_SIZE / SAMPLE_RATE
  21. FRAME_BYTES = CHUNK_SIZE * 4
  22. MAX_SILENT_FRAMES = 43
  23. # Hardcoded DSP tuning for violent clipping
  24. FREQ_MIN = 44.0
  25. FREQ_MAX = 12000.0
  26. SILENCE_THRESHOLD = 0.5
  27. GAIN_MIN = 800.0
  28. GAIN_MAX = 1500.0
  29. TILT_EXPONENT = 0.45
  30. DECAY_RATE = 0.40
  31. CONTRAST_EXPONENT = 4.5
  32. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  33. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  34. # Pre-compute Hanning window
  35. HANNING_WINDOW = np.hanning(CHUNK_SIZE).astype(np.float32)
  36. # Compute 16 logarithmic frequency bands
  37. FREQ_EDGES = np.logspace(np.log10(FREQ_MIN), np.log10(FREQ_MAX), 17)
  38. fft_freqs = np.fft.rfftfreq(CHUNK_SIZE, 1.0 / SAMPLE_RATE)
  39. # Pre-compute logarithmic treble tilt weights
  40. band_centers = np.sqrt(FREQ_EDGES[:-1] * FREQ_EDGES[1:])
  41. TILT_WEIGHTS = ((band_centers / FREQ_MIN) ** TILT_EXPONENT).astype(np.float32)
  42. # Build a C-optimized Matrix for dot-product binning
  43. BIN_MATRIX = np.zeros((16, len(fft_freqs)), dtype=np.float32)
  44. for i in range(16):
  45. low = FREQ_EDGES[i]
  46. high = FREQ_EDGES[i + 1]
  47. idx = np.where((fft_freqs >= low) & (fft_freqs < high) & (fft_freqs > 0))[0]
  48. if len(idx) == 0:
  49. non_zero_bins = np.where(fft_freqs > 0)[0]
  50. closest = non_zero_bins[np.argmin(np.abs(fft_freqs[non_zero_bins] - low))]
  51. idx = [closest]
  52. BIN_MATRIX[i, idx] = (1.0 / len(idx)) * TILT_WEIGHTS[i]
  53. # State variables
  54. sample_smth = 0.0
  55. silence_frames = 0
  56. running_peak = 0.05
  57. STRUCT_FMT_V2 = "<6s2xffB3x16sd"
  58. def read_exact_chunk(fd, size):
  59. buf = bytearray(size)
  60. view = memoryview(buf)
  61. pos = 0
  62. while pos < size:
  63. chunk = fd.readinto(view[pos:])
  64. if not chunk:
  65. return None
  66. pos += chunk
  67. return bytes(buf)
  68. start_time = None
  69. frames_processed = 0
  70. while True:
  71. t0 = time.perf_counter()
  72. raw_data = read_exact_chunk(sys.stdin.buffer, FRAME_BYTES)
  73. read_duration = time.perf_counter() - t0
  74. if not raw_data:
  75. break
  76. if read_duration > 0.015:
  77. start_time = time.perf_counter()
  78. frames_processed = 0
  79. running_peak = 0.05
  80. if start_time is None:
  81. start_time = time.perf_counter()
  82. audio = np.frombuffer(raw_data, dtype=np.int16).astype(np.float32)
  83. left = audio[0::2]
  84. right = audio[1::2]
  85. mono = (left + right) * (1.0 / 65536.0)
  86. raw_mag = float(np.max(np.abs(mono)) * 255.0)
  87. sample_smth = 0.7 * sample_smth + 0.3 * raw_mag
  88. sample_peak = 1 if raw_mag > (sample_smth * 1.5 + 20) else 0
  89. if raw_mag < SILENCE_THRESHOLD:
  90. silence_frames += 1
  91. else:
  92. silence_frames = 0
  93. if silence_frames <= MAX_SILENT_FRAMES:
  94. windowed = mono * HANNING_WINDOW
  95. fft_vals = np.abs(np.fft.rfft(windowed)) * (2.0 / CHUNK_SIZE)
  96. tilted_energies = BIN_MATRIX @ fft_vals
  97. current_max = float(np.max(tilted_energies))
  98. if current_max > running_peak:
  99. running_peak = current_max
  100. else:
  101. running_peak = max(0.005, running_peak * DECAY_RATE)
  102. dynamic_gain = np.clip(1.0 / running_peak, GAIN_MIN / 255.0, GAIN_MAX / 255.0)
  103. normalized = np.clip(tilted_energies * dynamic_gain, 0.0, 1.0)
  104. contrasted = (normalized ** CONTRAST_EXPONENT) * 255.0
  105. fft_result = bytes(np.clip(contrasted, 0, 255).astype(np.uint8))
  106. payload = struct.pack(
  107. STRUCT_FMT_V2,
  108. b"00002\x00",
  109. float(raw_mag),
  110. float(sample_smth),
  111. sample_peak,
  112. fft_result,
  113. float(raw_mag)
  114. )
  115. try:
  116. sock.sendto(payload, (UDP_IP, UDP_PORT))
  117. except Exception:
  118. pass
  119. # Hybrid PLL Metronome to provide flawless backpressure
  120. frames_processed += 1
  121. target_time = start_time + (frames_processed * CHUNK_DURATION)
  122. sleep_time = target_time - time.perf_counter()
  123. if sleep_time > 0.002:
  124. time.sleep(sleep_time - 0.001)
  125. while time.perf_counter() < target_time:
  126. pass
  127. if sleep_time < -1.0:
  128. start_time = time.perf_counter()
  129. frames_processed = 0