Node.js

Installation

Make sure to have node and npm installed.

At the moment, we don’t support npm. Download our nodejs library. Then unzip the file in the root of your project. If you already have a package.json file, make sure to add dependencies contained in the pakage.json of the zip file.

Then go in your root folder and run the following command.

$ npm install

Examples

Analyze audio events in audio files

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const SenseClient = require("SenseNodejs/SenseClient").SenseClient;

const apiKey="SetYourApiKey";
const senseClient = new SenseClient(apiKey);

const buffer = readFileSync("path/to/AudioFile.mp3");
const extension = "mp3";

const fileSense = senseClient.sendFile(buffer, extension);

fileSense.event((err, result) => {
    if(err) console.error(err);
    else console.log(result);
});

Analyze audio events in audio stream

In this example, naudiodon library is used to get the microphone stream.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const senseClient = require("SenseClient").SenseClient;
const SamplingFormat = require("SenseClient").SamplingFormat;
const portAudio = require("naudiodon");

const apiKey="SetYourApiKey";
const senseClient = new SenseClient(apiKey);

const samplingRate = 22050;
const audioInput = new portAudio.AudioIO({
    inOptions: {
        channelCount: 1,
        sampleFormat: portAudio.SampleFormat32Bit,
        sampleRate: samplingRate,
        deviceId: -1,
    }
});

const streamSense = senseClient.sendStream(audioInput, samplingRate, SamplingFormat.Int32Bit);
streamSense.event((err, result) => {
    if(err) console.error(err);
    else console.log(result);
});

audioInput.start()

Syntax

SenseClient

class SenseClient {
    constructor(apiKey: string, host?: string);
    sendFile(buffer: Buffer, extension: string): Sense;
    sendStream(stream: stream.Readable,
        samplingRate: number,
        samplingFormat: SamplingFormat): Sense;
}

SenseClient.constructor

  • arguments
    • apiKey: The api key you received via email. If you don’t have one, you could get one here.
    • host (optional): The Sense API server url.

SenseClient.sendFile

  • arguments
    • buffer: The buffer to be analyzed. It can be from a local file, or any other buffer source.
    • extension: The type of data in wich the buffer is encoded : it can be any music format (wav, mp3, flac…)
  • returned value
    • Return an instance of type Sense, which would be able to communicate with the Sense API backend.

SenseClient.sendStream

  • arguments
    • stream: The stream to be analyzed. It could be any class implementing stream.Writable.
    • samplingRate: The sampling rate of the audio stream (in Hz)
    • samplingFormat: The sampling format of the audio stream (for instance Float32Bit or Int32Bit)
  • returned value
    • Return an instance of type Sense, that would be able to communicate with the Sense API backend.

SamplingFormat

enum SamplingFormat {
    Int64Bit,
    Int32Bit,
    Int16Bit,
    Int8Bit,
    Float64Bit,
    Float32Bit,
    Float16Bit,
    Float8Bit
}

SamplingFormat lists the available formats for an audio stream. It could be encoded with a precision between 8 to 64 bits, and of types int or float. (values will be contained between -1 and 1)

Sense

abstract class Sense {
    abstract event(callback: CallbackType): void;
}

Sense. event

  • arguments
    • callback: When the input is an audio file, callback will be called once, as soon as the result is returned from the backend server. In the case of audio stream, callback will be called every 0.5 seconds recorded.
To understand the meaning of Sense.event, you can read Acoustic event API.

CallbackType

type CallbackType = (error: Error, result: any) => any;

CallbackType follows the usual nodejs callback : the first argument is defined when an error occurs during the async call, and the second argument is defined if the call didn’t have errors. result is a json type variable.