WebSocket Audio API
Thanks
The project is forked from (ws-audio-api)[https://github.com/Ivan-Feofanov/ws-audio-api]
I just fixed little problem.
Library to broadcast the sound from the microphone through a WebSocket
This library can work in two ways:
Streamer mode:
- Get user audio from microphone (getUserMedia support require)
- Encode it with Opus codec
- Send it to websocket server
Works fine in Chrome, Firefox, Edge. Doesn't work in Safari.
Player mode:
- Get packet from broadcasting server
- Decode it with Opus codec
- Write it to audio queue
- Play audio queue (Web Audio Api support require)
Works fine in all browsers
For Speaker only:
In Chrome browser you should use secure connection cause Chrome does not support getUserMedia in unsecure HTTP connection
How to setup secure HTTP server
Bower
$ bower install ws-audio-api
NPM
$ npm install ws-audio-api
$
Quick start
-
Clone this repository
$ git clone https://github.com/Ivan-Feofanov/ws-audio-api.git$ cd ws-audio-api/example -
Start secure websockets server from server folder
$ cd server && npm i$ npm startThis command will start broadcasting server on port 5000
-
Include scripts in both, speaker and listener page
<script src="scripts/ws-audio-api.min.js"></script> -
On Streamer side create new speaker and make start/stop stream buttons
<script>var streamer =server:host: windowlocationhostname //websockets server addres. In this example - localhostport: 5000 //websockets server port);</script><button onclick="streamer.start()">Start stream</button><button onclick="streamer.stop()">Stop stream</button>Detailed config description placed below
-
On listener side create new listener and make play/stop buttons
<script>var player =server:host: windowlocationhostname //websockets server addres. In this example - localhostport: 5000 //websockets server port);</script><button onclick="player.start()">Play stream</button><button onclick="player.stop()">Stop playing</button> -
Enjoy!
Config
Default config
var defaultConfig = codec: sampleRate: 24000 channels: 1 app: 2048 frameDuration: 20 bufferSize: 4096 server: host: windowlocationhostname port: 5000
You can change any parameter to fine tune your broadcast.
!! Codec settings on both streamer and listener side should be the same !!
I recommend use sample rate 24000 and below to avoid gaps in stream
Opus Quality Settings
App: 2048=voip, 2049=audio, 2051=low-delay
Sample Rate: 8000, 12000, 16000, 24000, or 48000
Frame Duration: 2.5, 5, 10, 20, 40, 60
Buffer Size = sample rate/6000 * 1024
Server-side
Server side script is very simple:
You can use this script for setup standalone broadcasting server or add ws-audio broadcast functionality to your own server.
API
Streamer
Create new streamer:
var streamer = config;
Start stream
streamerstart;
Mute microphone
streamer;
Unmute microphone
streamer;
Stop stream
streamer;
Player
Create new player
var player = config;
Play stream
playerstart;
Get stream volume
player;
Change stream volume
player; //Float 0.00 - 1.00
Volume control example
var volume = document;var volumeIndicator = document;volumeIndicatorvalue = player; { player; volumeIndicatorvalue = player;}
Stop playing
player;
People
USM LLC.
With regards to