Speechmatics provides an API for speech to text. This package implements the API making it easier to integrate into Node.js projects.
npm install speechmatics
Read here for more detailed description of the API.
const Speechmatics = require('speechmatics');
const sm = new Speechmatics(userId, apiKey, options);
userId
and apiKey
are required as the first two parameters for Speechmatics client instantiation. options
are...optional. Defaults detailed below.
baseUrl
: string - defaults to 'https://api.speechmatics.com'apiVersion
: number - defaults to 1.0;callbackUrl
: string - URL for notification callbacks- if this option is set,
notification
field for request will automatically be set as 'callback'
- if this option is set,
headers
: object - extra header fields
For each request, opts
are settings that will be passed along to the request
module.
/* User */
sm.getUser(opts, callback);
sm.getPayments(opts, callback);
sm.getJobs(opts, callback);
sm.createJob(opts, callback);
sm.getJob(jobId, opts, callback);
sm.getTranscript(jobId, opts, callback);
sm.getAlignment(jobId, opts, callback);
/* Status */
sm.getStatus(opts, callback);
/* Statics */
Speechmatics.parseAlignment(text);
If the response object has only a single key, the callback object is pared to that value. This provides a more simplified than the actual Speechmatics API. Specifically this applies, getUser
, getPayments
, getJobs
, and getJob
Example:
According the Speechmatics API, a GET on /user/$userId/
wil respond with:
{
"user": {
"balance": 90,
"email": "[email protected]",
"id": 1
}
}
Whereas, this module will simply return the value of the user
key:
{
balance: 90,
email: '[email protected]',
id: userId
}
sm.createJob
has a built-in nicety. Setting opts.audioFilename
or opts.textFilename
(for alignment) will read those files from the supplied paths as a ReadStream, which is then passed through to the request as the correct formData
fields.
You can also use the opts.audioStream
and opts.textStream
parameters to pass in readable streams. This is useful when uploading from a remote source, for example:
var request = https.get("https://example.com/catVideo.webm")
.on('response', function(response) {
sm.createJob({audioStream: response}, callback);
});
Or, if you already happen to have a read stream open:
var existingReadStream = fs.createReadStream("./zero.wav");
//do stuff...
sm.createJob({audioStream: existingReadStream}, callback);
note: "auth_token" request parameter is automatically set based on apiKey