All Projects → kevinsqi → React Piano

kevinsqi / React Piano

Licence: mit
An interactive piano keyboard for React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Piano

Jiosaavnapi
An unofficial API for JioSaavn written in Python 3
Stars: ✭ 123 (-5.38%)
Mutual labels:  music
Music Beat Detector
music-beat-detector is a library that analyzes a music stream and detects any beat. It can be used to control lights or any magic effect by the music wave.
Stars: ✭ 127 (-2.31%)
Mutual labels:  music
108
A minimal beat machine played in the browser.
Stars: ✭ 128 (-1.54%)
Mutual labels:  music
Xsound
Web Audio API Library for Synthesizer, Effects, Visualization, Multi-Track Recording, Audio Streaming, Visual Audio Sprite ...
Stars: ✭ 123 (-5.38%)
Mutual labels:  music
Om Sharp
OM#: Visual Programming | Computer-Assisted Music Compositon
Stars: ✭ 124 (-4.62%)
Mutual labels:  music
Music Bar
🎶 Control Apple Music right from your macOS menu bar.
Stars: ✭ 128 (-1.54%)
Mutual labels:  music
Androidequalizer
Android Equalizer View that can also manage the audio track frequencies
Stars: ✭ 123 (-5.38%)
Mutual labels:  music
Discord Bot
A discord bot that can play music, moderate, log events and more
Stars: ✭ 127 (-2.31%)
Mutual labels:  music
Musical Creativity
Models of Musical Creativity (in Clojure)
Stars: ✭ 125 (-3.85%)
Mutual labels:  music
Playlistfromsong
Create an offline music playlist from a single song 🎶
Stars: ✭ 128 (-1.54%)
Mutual labels:  music
Android Youtubemp3
Download videos as mp3 directly from Youtube Android App
Stars: ✭ 124 (-4.62%)
Mutual labels:  music
Aurora.music
Aurora Music
Stars: ✭ 125 (-3.85%)
Mutual labels:  music
Deezer Downloader
Download music from Deezer with a nice front end
Stars: ✭ 127 (-2.31%)
Mutual labels:  music
Notation
Simplified music notation
Stars: ✭ 124 (-4.62%)
Mutual labels:  music
Reel2bits
Self-hosted Soundtracks and Podcasts sharing, with ActivityPub federation.
Stars: ✭ 128 (-1.54%)
Mutual labels:  music
Dmusic
DMusic Player for Android - An online music player based on Component + MVP Base + MVP Customization + greenDAO + OkHttp3 + Retrofit + RxJava2; supports local and network music playback; can play Baidu music, Netease cloud music; support online music list , radio, MV, music online, download, local and online lyrics; Lyrics, music cache; self-built song list, song management, collection, sorting, sorting, skinning, sleep timing, mode switching, more settings, etc. 基于 Component + MVP Base + MVP Customization + greenDAO + OkHttp3 + Retrofit + RxJava2 的在线音乐播放器; 支持本地及网络音乐播放; 可播放百度音乐,网易云音乐; 支持在线音乐榜单, 电台, MV, 音乐在线播放,下载, 本地及在线歌词; 歌词, 音乐缓存; 自建歌单, 歌曲管理, 收藏, 排序, 分类, 换肤, 睡眠定时, 模式切换, 更多设置等
Stars: ✭ 121 (-6.92%)
Mutual labels:  music
Sysdash
A simple and clean system monitoring skin for Rainmeter.
Stars: ✭ 128 (-1.54%)
Mutual labels:  music
Mudeo
mudeo - A collaborative music app built with Flutter
Stars: ✭ 130 (+0%)
Mutual labels:  music
Voc
A physical model of the human vocal tract using literate programming, based on Pink Trombone.
Stars: ✭ 129 (-0.77%)
Mutual labels:  music
Toa
シンプルで可愛いミュージックプレイヤー「兎亜」
Stars: ✭ 128 (-1.54%)
Mutual labels:  music

react-piano

npm version build status bundle size

An interactive piano keyboard for React. Supports custom sounds, touch/click/keyboard events, and fully configurable styling. Try it out on CodeSandbox.

react-piano screenshot

Installing

yarn add react-piano

Alternatively, you can download the UMD build from unpkg.

Usage

You can view or fork the CodeSandbox demo to get a live version of the component in action.

Import the component and styles:

import { Piano, KeyboardShortcuts, MidiNumbers } from 'react-piano';
import 'react-piano/dist/styles.css';

Importing CSS requires a CSS loader (if you're using create-react-app, this is already set up for you). If you don't have a CSS loader, you can alternatively copy the CSS file into your project from src/styles.css.

Then to use the component:

function App() {
  const firstNote = MidiNumbers.fromNote('c3');
  const lastNote = MidiNumbers.fromNote('f5');
  const keyboardShortcuts = KeyboardShortcuts.create({
    firstNote: firstNote,
    lastNote: lastNote,
    keyboardConfig: KeyboardShortcuts.HOME_ROW,
  });

  return (
    <Piano
      noteRange={{ first: firstNote, last: lastNote }}
      playNote={(midiNumber) => {
        // Play a given note - see notes below
      }}
      stopNote={(midiNumber) => {
        // Stop playing a given note - see notes below
      }}
      width={1000}
      keyboardShortcuts={keyboardShortcuts}
    />
  );
}

Implementing audio playback

react-piano does not implement audio playback of each note, so you have to implement it with playNote and stopNote props. This gives you the ability to use any sounds you'd like with the rendered piano. The react-piano demo page uses @danigb's excellent soundfont-player to play realistic-sounding soundfont samples. Take a look at the CodeSandbox demo to see how you can implement that yourself.

Props

Name Type Description
noteRange Required object An object with format { first: 48, last: 77 } where first and last are MIDI numbers that correspond to natural notes. You can use MidiNumbers.NATURAL_MIDI_NUMBERS to identify whether a number is a natural note or not.
playNote Required function (midiNumber) => void function to play a note specified by MIDI number.
stopNote Required function (midiNumber) => void function to stop playing a note.
width Conditionally required number Width in pixels of the component. While this is not strictly required, if you omit it, the container around the <Piano> will need to have an explicit width and height in order to render correctly.
activeNotes Array of numbers An array of MIDI numbers, e.g. [44, 47, 54], which allows you to programmatically play notes on the piano.
keyWidthToHeight Number Ratio of key width to height. Used to specify the dimensions of the piano key.
renderNoteLabel Function ({ keyboardShortcut, midiNumber, isActive, isAccidental }) => node function to render a label on piano keys that have keyboard shortcuts
className String A className to add to the component.
disabled Boolean Whether to show disabled state. Useful when audio sounds need to be asynchronously loaded.
keyboardShortcuts Array of object An array of form [{ key: 'a', midiNumber: 48 }, ...], where key is a keyEvent.key value. You can generate this using KeyboardShortcuts.create, or use your own method to generate it. You can omit it if you don't want to use keyboard shortcuts. Note: this shouldn't be generated inline in JSX because it can cause problems when diffing for shortcut changes.
onPlayNoteInput Function (midiNumber, { prevActiveNotes }) => void function that fires whenever a play-note event is fired. Can use prevActiveNotes to record notes.
onStopNoteInput Function (midiNumber, { prevActiveNotes }) => void function that fires whenever a stop-note event is fired. Can use prevActiveNotes to record notes.

Recording/saving notes

You can "record" notes that are played on a <Piano> by using onPlayNoteInput or onStopNoteInput, and you can then play back the recording by using activeNotes. See this CodeSandbox which demonstrates how to set that up.

demo of recording

Customizing styles

You can customize many aspects of the piano using CSS. In javascript, you can override the base styles by creating your own set of overrides:

import 'react-piano/dist/styles.css';
import './customPianoStyles.css';  // import a set of overrides

In the CSS file you can do things like:

.ReactPiano__Key--active {
  background: #f00;  /* Change the default active key color to bright red */
}

.ReactPiano__Key--accidental {
  background: #000;  /* Change accidental keys to be completely black */
}

See styles.css for more detail on what styles can be customized.

Upgrading versions

See the CHANGELOG which contains migration guides for instructions on upgrading to each major version.

Browser compatibility

To support IE, you'll need to provide an Array.find polyfill.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].