Coder Social home page Coder Social logo

swerner / alexa-voice-service.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miguelmota/alexa-voice-service.js

0.0 3.0 0.0 1.41 MB

Library for interacting with Alexa Voice Service (AVS) in the browser.

Home Page: https://lab.miguelmota.com/alexa-voice-service

License: MIT License

JavaScript 98.92% CSS 0.13% HTML 0.95%

alexa-voice-service.js's Introduction

alexa-voice-service.js

Library for interacting with Alexa Voice Service (AVS) in the browser.

Things you can do with this library:

  • Login with Amazon and get access token
  • Get access token and refresh token from 'code' response type
  • Get access token from refresh token
  • Request user microphone
  • Record user audio using microphone
  • Send user audio to AVS
  • Parse response from AVS
  • Queue and play MP3 responses from AVS.

The audio recorded in this library is mono channel, sampled at 16k Hz, and signed 16 bit PCM encoding which is required by AVS.

Demo

https://lab.miguelmota.com/alexa-voice-service

Install

npm install alexa-voice-service

Example

Follow these steps to run demo locally:

  1. Git clone this repo
git clone [email protected]:miguelmota/alexa-voice-service.js.git

cd alexa-voice-service.js/example/
  1. Install NPM Modules
npm install
  1. Run HTTPS server
npm start
  1. Go to browser url.
open https://localhost:9745

Usage

const AVS = require('alexa-voice-service');

const avs = new AVS(options);

Please check out example as noted above.

Documentation

Most methods return a promise.

AVS(options) - constructor

options:
  debug - {boolean} logs to console
  clientId - {string} AVS client id found in portal
  clientSecret - {string} AVS client secret found in portal. Only needed if using `code` response type.
  deviceId - {string} AVS device Id found in portal
  deviceSerialNumber - {number} serial number for this device (can be made up)
  redirectUri - {string} redirect uri set in portal

avs.login({responseType: 'code, token (default)', newWindow: false}) -> promise(response);
avs.promptUserLogin() -> alias to login();
avs.logout() -> promise();
avs.getTokenFromUrl() -> promise(token);
avs.getCodeFromUrl() -> promise(code);
avs.getTokenFromCode(code) -> promise(response);
avs.getTokenFromRefreshToken(refreshToken) -> promise(token)
avs.refreshToken() -> promise({token, refreshToken})
avs.getToken() -> promise(token)
avs.getRefreshToken() -> promise(refreshToken)
avs.requestMic() -> promise(stream);
avs.connectMediaStream(stream) -> promise;
avs.stopRecording() -> promise;
avs.startRecording() -> promise;
avs.sendAudio(dataView) -> promise(response); - send audio to AVS and get back an object containing response.
The response object is the parsed http message.
avs.audioToBlob(AudioBuffer | DataView) -> promise(blob) - mp3 blob

avs.on(identifier, callback)

identifiers (found under AVS.EventTypes object)
  LOG - when a log occurs.
  ERROR - when an error occurs.
  LOGIN - when user is logged in.
  LOGOUT - when user is logged out.
  RECORD_START - when recording is started.
  RECORD_STOP - when recording is stopped.
  TOKEN_SET - when token is set.
  REFRESH_TOKEN_SET - when refresh token is set.
  TOKEN_INVALID - when token is invalid, usually because it is expired.

example: avs.on(AVS.EventTypes.LOG, callback)

# Player

avs.player.enqueu(dataView|typedArray|arrayBuffer) -> promise(arraybuffer) - add an audio source to play queue. Converts input to AudioBuffer.
avs.player.deque() -> promise() - dequeu an audio source to play
avs.player.play() -> promise() - play next source in queue
avs.player.stop() -> promise() - stop playing
avs.player.replay() -> promise() - replay last audio source played
avs.player.pause() -> promise() - pause playing
avs.player.emptyQueue() -> promise() - empty the queue
avs.player.playBlob(blob) -> promise(blob) - play a blob source
avs.player.playAudioBuffer(audioBuffer) -> promise() - play an AudioBuffer source
avs.player.playUrl(url) -> promise(url) - play mp3 url

avs.player.on(identifier, callback)

identifiers (found under AVS.Player.EventTypes object)
  LOG - when a debug log occurs
  ERROR - when an error occurs
  PLAY - when audio source is played
  REPLAY - when audio source is replayed
  PAUSE - when audio source is paused
  STOP - when audio source is stopped playing
  ENQUEUE - when an audio source is added to queue
  DEQUE - when an audio source is removed from queue

TODO

  • Better documentation

License

MIT

alexa-voice-service.js's People

Contributors

miguelmota avatar zamiang avatar

Watchers

Scott Werner avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.