How kengonakajima/nodephone works
Main Components
• Client Application:
– The core “phone.js” acts as the main client connecting to the server.
– Other client variants (recplay.js for microphone-speaker testing; cancel.js, which adds echo cancellation on top of recplay).
– A suite of supporting modules (for echo cancellation, e.g., aec3.js with its associated WASM and native modules, along with additional audio processing scripts like fft_ifft.js, matched.js, etc.).
– Audio I/O dependency components (e.g., “node-record-lpcm16” for recording and “speaker” for playback, as indicated in the README).
• Server Application:
– The “sv.js” script and accompanying files (like cl.js, package.json) indicate that the server uses Node.js with WebSockets listening on TCP port 13478.
– This server is responsible for relaying audio data among connected clients.
• External Dependencies & Resources:
– NPM modules (as referenced in the installation instructions).
– Platform dependencies (the use of x86_64 Rosetta2 on macOS and the Homebrew-installed sox).
– Audio files in the “sounds” directory for demo and testing.
Relationships and Data Flow
• Clients connect to the server via WebSockets (TCP port 13478) and send/receive audio data.
• On the client side:
– The microphone captures audio which then gets possibly processed by the echo cancellation module (or bypassed, depending on the app variant being executed).
– Processed audio is sent over the network to the server.
– Incoming audio data from other clients is played back via the speaker.
• The server acts as a broadcaster – routing every connected user’s audio to all other clients (or acting as a relay for voice communication).
• There is dependency on low-level native modules and WebAssembly (e.g., aec3.wasm and NativeAudioMac.node) for performance-critical audio processing.
Architectural Patterns and Design Principles
• Modularization: The separation into an “app” (client code), “server” (server code), and “sounds” (assets) directories shows a clear division of concerns.
• Real-time data streaming: The use of WebSockets ensures low-latency communication between clients and server.
• Platform-specific optimizations: Native modules (.node files like PAmac.node, opusmac.node, and opuswin.node) along with compiled WASM code indicate an emphasis on performance for audio processing.
• Extensibility: By offering multiple client entry points (phone.js, recplay.js, cancel.js), the architecture provides flexibility for testing versus production use.