NodeJS

We offer ODIN, a high-quality cross-platform immersive voice SDK that includes state-of-the-art noise suppression technologies.

Our Web SDK includes fallback code for WebRTC and is compatible with most browsers. While it can also be used in NodeJS, it may not be as performant as our native NodeJS SDK, which is designed for advanced use cases and bots.

Although we strive to keep the API as similar as possible between our SDKs, there are some differences. Since wrapping objects in JavaScript and TypeScript is easier than in C/C++, we only use objects where necessary in our native SDK. For instance, the WebSDK provides an OdinPeer object that encapsulates various methods and properties. However, for NodeJS use cases, you usually only need a peerId and mediaId, so we decided not to overcomplicate things. Of course, you can always create your own wrapper if needed.

The main difference between the two SDKs is that the NodeJS version allows you to access raw audio data from the ODIN server, which enables you to record or stream audio to the room. While the WebSDK has similar capabilities, it relies on Web Audio, which is not available in NodeJS. Rather than creating a complex polyfill, we found it easier and faster to wrap the ODIN native SDK into NodeJS bindings.

By providing two separate libraries, we can optimize each one for its respective platform and use cases.

ODIN is our cross-platform immersive voice SDK with best in class quality and noise suppression technologies.

Source Code

The NodeJS SDK is developed in C/C++ using Node API and is open source. Check out the source in our public NPM repository.

Installation

npm npm npm

Install the web SDK via npm in your project like this:

npm install --save @4players/odin-nodejs

Interoperability

The ODIN web SDK is fully compatible with our other client SDKs so you can easily communicate between your NodeJS script and other clients - even Unity or Unreal Engine clients.

Event Handling

The ODIN server will automatically notify you about relevant updates and the ODIN NodeJS SDK is using the JavaScript EventTarget API to dispatch custom events based on the OdinEvents interface. Use any of the provided the addEventListener methods to set up a function that will be called whenever the specified event is delivered to the target.

These events are available:

Use them like shown in this example:
Listening for events
// Create an odin client instance using our access key and create a room
const odinClient = new OdinClient("__YOUR_ACCESS_KEY__");
const room = odinClient.createRoom(roomName, userName);

// Listen on PeerJoined messages and print the user data of the joined peer
room.addEventListener('PeerJoined', (event) => {
    console.log("Received PeerJoined event", event);
    console.log(JSON.parse(new TextDecoder().decode(event.userData)));
});

// Listen on PeerLeft messages and print the user data of the left peer
room.addEventListener('PeerLeft', (event) => {
    console.log("Received PeerLeft event", event);
});

Using Media objects

ODIN works with the concept of media objects that are attached to a room. A media is either remote (i.e. connected to the mic of someone else device) or a local media, which is linked to your own microphone or other input device.

After joining a room, you can create a media (linked to your local input device) and add to the room. After joining a room, you are just a listener in the room, you don’t send anything to it (i.e. your are muted).

Receiving audio data

Once the script has joined a room it receives events that you add handlers for. If you want to get access to audio data, you’ll need to add an event handler for AudioDataReceived . The data property of the event contains the audio samples in 16-bit format or 32-bit floats (-1 to 1):

Receiving audio data
// Add an event filter for audio data received events
room.addEventListener('AudioDataReceived', (data) => {
    // Getting an array of the sample buffer - use for example to visualize audio
    let ui32 = new Float32Array(data.samples32.buffer);
    console.log(ui32);

    let ui16 = new Int16Array(data.samples16.buffer);
    console.log(ui16);
});

Sending audio data

If you want to your bot to send audio into the room (i.e. text to speech or music and other sounds, you need to attach a media to that room:

Creating a media object
// Send music to the room
const sendMusic = async (media) => {
    // Prepare our MP3 decoder and load the sample file
    const audioBuffer = await decode(fs.readFileSync('./santa.mp3'));

    // Print our some info about the audio file
    console.log(audioBuffer);

    // Create a stream that will match the settings of the file
    const audioBufferStream = new AudioBufferStream({channels: 1, sampleRate: 44100, float: true, bitDepth: 32});

    // Whenever the stream has data, send it to the media stream
    audioBufferStream.on('data', (data) => {
        const floats = new Float32Array(new Uint8Array(data).buffer)
        media.sendAudioData(floats);
    });

    audioBufferStream.write(audioBuffer);
}

// Create a media stream in the room - it will return an OdinMedia instance that we can use to send data to ODIN
const media = room.createAudioStream(44100, 1);

// Start the stream and send the music to ODIN
sendMusic(media).then(() => {
    console.log("Finished sending song");
});

Examples

We have prepared a couple of examples to get you started with the ODIN NodeJS SDK. You can find them in our SDK in the tests folder. After installing the NodeJS SDK check out this folder: node_modules/@4players/odin-nodejs/tests. You’ll find one example on how to record audio and sending them to OpenAIs whisper module for transcription and another sample that shows you how to send data into the channel. Examples are also provided for sending text messages when users join the room.