Giter Site home page Giter Site logo

tatajjj / lite-piano Goto Github PK

View Code? Open in Web Editor NEW

This project forked from echoloyuk/lite-piano

0.0 0.0 0.0 2.19 MB

A mini piano component based on the JavaScript Web Audio API, for playing the basic tone of a piano using the Audio Context in a web app.

JavaScript 100.00%

lite-piano's Introduction

Lite-piano

A mini piano component based on the JavaScript Web Audio API, for playing the basic tone of a piano using the Audio Context in a web app.

The Lite-piano is a basic library, you can use it to do your applications logic or any other piano-like music web apps. This is a Demo, which introduces a musicXML score of "Canon D Major" and parses it into lite-piano recognizable parameters. we uses setTimeout to play it automatically. (The demo did not do extra network optimization. It may take about 3.5Mb to load audio and music scores. Please wait patiently and click Play after loading.)

See Readme with 中文版

How to install

Use source

Download the source code, put /dist/index.js where you need it, and load the contents of /dist/index.js directly into the page.

Use for localhost

  • git clone
  • cd lite-piano
  • npm install
  • npm run start start webpack-dev-server。
  • Open the browser 127.0.0.1:9090/demo/index.html

How to use

You should load index.js into your page. After loading, the constructor will be mounted to the window global variable. So you can use it as follows:

const LitePiano = window.LitePiano;
const piano = new LitePiano();
piano.resume().then(() => {
  piano.initAllSound(); // load all basic on shot sound.
}).then(() => {
  document.getElementById('button').addEventListener('click', () => {
    piano.oneShot('C', 4, 0); // play a DO pitch.
  });
});

Attributes & Functions

resume()

This method returns a Promise. On the IOS, the audio contexts are automatically suspended, requiring the user to manually trigger an event to unlock. Therefore, after executing this function, the logic implementation after the user interaction is unlocked can be directly executed.

piano.resume().then(() => {
  // you can do something after user interact. and the web audio api is ok.
});

initAllSound()

This function returns a Promise. Since the loading and processing of the 7 basic scales is asynchronous, you need to perform this method to ensure that the tones on all pianos are played correctly.

piano.initAllSound().then(() => {
  // all sound is inited, now you can play it.
});

oneShot(step, octave, alter)

This function returns an audio source object, and play a pitch sound. Step indicates the tone name, respectively: C, D, E, F, G, A, B. Octave represents octave, max is 8, and min is 0. eg. The standard piano key C has an octave of 4 And alter = 1 indicates an alter step tone, for example: C#, F#, etc. Usually, The alter tone is the black key on the piano.

piano.oneShot('C', 4, 0);

Compatibility

It is not compatible for IE series, due to using JavaScript Web Audio API to implement. Sorry to see that.

  • Chrome: up to 49
  • FireFox: up to 61
  • Edge: 17
  • Safari: 11.1
  • iOS Safari: up to 10.3
  • Chrome for Android: up to 69

lite-piano's People

Contributors

echoloyuk 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.