debug_test.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """
  2. Audio Recorder — Debug Diagnostic Tool
  3. =======================================
  4. Run this DIRECTLY with Python (not as EXE) to see exactly what's failing.
  5. python debug_test.py
  6. It will print a full report and pause at any error so you can read it.
  7. """
  8. import sys
  9. import os
  10. import time
  11. import socket
  12. import traceback
  13. import subprocess
  14. print("=" * 60)
  15. print(" Audio Recorder — Startup Diagnostic")
  16. print("=" * 60)
  17. print(f" Python: {sys.version}")
  18. print(f" EXE: {sys.executable}")
  19. print(f" CWD: {os.getcwd()}")
  20. print(f" Script: {os.path.abspath(__file__)}")
  21. print("=" * 60)
  22. print()
  23. errors = []
  24. # ── 1. Check required packages ────────────────────────────────────────────────
  25. print("[1] Checking installed packages...")
  26. packages = {
  27. "flask": "flask",
  28. "sounddevice": "sounddevice",
  29. "soundfile": "soundfile",
  30. "numpy": "numpy",
  31. "PIL": "pillow",
  32. "pystray": "pystray",
  33. }
  34. for mod, pip_name in packages.items():
  35. try:
  36. __import__(mod)
  37. print(f" OK {mod}")
  38. except ImportError as e:
  39. print(f" MISSING {mod} → pip install {pip_name}")
  40. errors.append(f"Missing package: {mod} (install with: pip install {pip_name})")
  41. print()
  42. # ── 2. Check port 5000 availability ───────────────────────────────────────────
  43. print("[2] Checking port 5000...")
  44. try:
  45. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  46. s.settimeout(1)
  47. result = s.connect_ex(('127.0.0.1', 5000))
  48. s.close()
  49. if result == 0:
  50. print(" WARNING: Port 5000 is already IN USE!")
  51. print(" Something else is already listening on port 5000.")
  52. print(" Either stop that app, or change the port in recorder_config.json")
  53. errors.append("Port 5000 already in use")
  54. else:
  55. print(" OK Port 5000 is free")
  56. except Exception as e:
  57. print(f" ERROR checking port: {e}")
  58. print()
  59. # ── 3. Try importing audio_recorder_server ────────────────────────────────────
  60. print("[3] Importing audio_recorder_server...")
  61. # Add current dir to path (simulates what the launcher does)
  62. script_dir = os.path.dirname(os.path.abspath(__file__))
  63. if script_dir not in sys.path:
  64. sys.path.insert(0, script_dir)
  65. try:
  66. # Set sys.argv as the launcher would
  67. sys.argv = [
  68. "audio_recorder_server.py",
  69. "--port", "5000",
  70. "--outdir", "./recordings",
  71. "--samplerate", "44100",
  72. "--channels", "2",
  73. "--format", "WAV",
  74. ]
  75. import audio_recorder_server as srv
  76. print(" OK audio_recorder_server imported")
  77. try:
  78. srv.init_from_args()
  79. print(f" OK init_from_args() called")
  80. print(f" PORT = {srv.PORT}")
  81. print(f" OUTPUT_DIR = {srv.OUTPUT_DIR}")
  82. print(f" DEVICE = {srv.DEVICE}")
  83. print(f" SAMPLERATE = {srv.SAMPLE_RATE}")
  84. except Exception as e:
  85. tb = traceback.format_exc()
  86. print(f" ERROR in init_from_args():\n{tb}")
  87. errors.append(f"init_from_args() failed: {tb}")
  88. except Exception as e:
  89. tb = traceback.format_exc()
  90. print(f" ERROR importing audio_recorder_server:\n{tb}")
  91. errors.append(f"Import failed: {tb}")
  92. print()
  93. # ── 4. Check sounddevice / audio devices ─────────────────────────────────────
  94. print("[4] Checking audio devices...")
  95. try:
  96. import sounddevice as sd
  97. devices = sd.query_devices()
  98. inputs = [d for d in devices if d['max_input_channels'] > 0]
  99. print(f" OK Found {len(inputs)} input device(s):")
  100. for i, d in enumerate(inputs[:5]):
  101. idx = list(devices).index(d)
  102. print(f" [{idx}] {d['name']}")
  103. if len(inputs) == 0:
  104. print(" WARNING: No audio input devices found!")
  105. errors.append("No audio input devices found")
  106. except Exception as e:
  107. tb = traceback.format_exc()
  108. print(f" ERROR querying audio devices:\n{tb}")
  109. errors.append(f"sounddevice error: {tb}")
  110. print()
  111. # ── 5. Try starting Flask briefly ────────────────────────────────────────────
  112. print("[5] Trying to start Flask server for 5 seconds...")
  113. flask_error = None
  114. def run_flask():
  115. global flask_error
  116. try:
  117. import audio_recorder_server as srv
  118. srv.app.run(host="0.0.0.0", port=5000, debug=False,
  119. threaded=True, use_reloader=False)
  120. except Exception as e:
  121. flask_error = traceback.format_exc()
  122. import threading
  123. t = threading.Thread(target=run_flask, daemon=True)
  124. t.start()
  125. time.sleep(3)
  126. if flask_error:
  127. print(f" ERROR starting Flask:\n{flask_error}")
  128. errors.append(f"Flask startup error: {flask_error}")
  129. else:
  130. # Try hitting the API
  131. try:
  132. import urllib.request
  133. with urllib.request.urlopen("http://localhost:5000/api/status", timeout=2) as r:
  134. data = r.read()
  135. print(f" OK Flask responded: {data.decode()[:80]}")
  136. except Exception as e:
  137. print(f" WARNING: Flask started but /api/status failed: {e}")
  138. if not flask_error:
  139. errors.append(f"Flask not responding: {e}")
  140. print()
  141. # ── 6. Check firewall hint ────────────────────────────────────────────────────
  142. print("[6] Checking local IP...")
  143. try:
  144. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  145. s.connect(("8.8.8.8", 80))
  146. ip = s.getsockname()[0]
  147. s.close()
  148. print(f" OK Local IP: {ip}")
  149. print(f" ESP32 should connect to: http://{ip}:5000")
  150. except Exception as e:
  151. print(f" ERROR: {e}")
  152. print()
  153. # ── Summary ───────────────────────────────────────────────────────────────────
  154. print("=" * 60)
  155. if errors:
  156. print(f" FOUND {len(errors)} PROBLEM(S):")
  157. for i, e in enumerate(errors, 1):
  158. print(f"\n [{i}] {e[:200]}")
  159. else:
  160. print(" ALL CHECKS PASSED!")
  161. print(" The server should work. Try running AudioRecorder.exe again.")
  162. print()
  163. print(" If the EXE still fails, run BUILD.bat again to rebuild it")
  164. print(" with the latest files.")
  165. print()
  166. print("=" * 60)
  167. input("\nPress Enter to exit...")