Need help with ws-audio-api?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

Ivan-Feofanov
186 Stars 38 Forks MIT License 26 Commits 11 Opened issues

Description

WebSocket Audio API: library to broadcast the sound from the microphone through a WebSocket

Services available

!
?

Need anything else?

Contributors list

# 315,266
HTML
django-...
Django
python3
22 commits

WebSocket Audio API

Library to broadcast the sound from the microphone through a WebSocket

This library can work in two ways:

Streamer mode:
1. Get user audio from microphone (getUserMedia support require)
2. Encode it with Opus codec
3. Send it to websocket server

Works fine in Chrome, Firefox, Edge. Doesn't work in Safari.

Player mode:
1. Get packet from broadcasting server
2. Decode it with Opus codec
3. Write it to audio queue
4. 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
$ import 'ws-audio-api'

Quick start

  1. Clone this repository

    bash
    $ git clone https://github.com/Ivan-Feofanov/ws-audio-api.git
    $ cd ws-audio-api
    $ npm i
    $ cd ws-audio-api/example
    
  2. Start secure websockets server from server folder

    bash
    $ cd server && npm i
    $ npm start
    
    This command will start broadcasting server on port 5000
  3. Include scripts in both, speaker and listener page

    js
    
    
  4. On Streamer side create new speaker and make start/stop stream buttons ```js

    Start stream Stop stream ``` Detailed config description placed below

  5. On listener side create new listener and make play/stop buttons

    js
    
    Play stream
    Stop playing
    
  6. Enjoy!

Config

Default config

var defaultConfig = { 
    codec: {
        sampleRate: 24000,
        channels: 1,
        app: 2048,
        frameDuration: 20,
        bufferSize: 4096
    },
    server: 'ws://' + window.location.hostname + ':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:

js
var streamer = new WSAudioAPI.Streamer(config);

Start stream

js
streamer.start();

Mute microphone

js
streamer.mute();

Unmute microphone

js
streamer.unMute();

Stop stream

js
streamer.stop();

Player

Create new player

js
var player = new WSAudioAPI.Player(config);

Play stream

js
player.start();

Get stream volume

js
player.getVolume();

Change stream volume

js
player.setVolume(level); //Float 0.00 - 1.00

Volume control example

html


```js var volume = document.querySelector('#volume'); var volumeIndicator = document.querySelector('#volumeIndicator'); volumeIndicator.value = player.getVolume();

function setVol(val){ player.setVolume(val); volumeIndicator.value = player.getVolume(); } ```

Stop playing

js
player.stop();

People

USM LLC.
With regards to * Kazuki Oikawa * F1LT3R

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.