Coder Social home page Coder Social logo

sbrl / soundbox Goto Github PK

View Code? Open in Web Editor NEW
39.0 3.0 4.0 431 KB

:speaker: :notes: A super simple JS library for playing sound effects :headphones: :musical_keyboard:

Home Page: https://sbrl.github.io/soundbox/example.html

License: MIT License

HTML 7.52% JavaScript 50.11% Shell 42.37%
html javascript js-library web-audio-library sound

soundbox's Introduction

soundbox

A super simple JS library for playing sound effects and other audio.

npm package size | minified + gzipped size

Demo

Check it out in action with the Online Demo (see also the Real-World Usage section :D)!

Running Demo Locally

The demo can also be run locally. Clone this repository, and then run the following from the root of this repo:

npm install
npm run demo

Quick example:

var soundbox = new SoundBox();
soundbox.load("beep-a", "beep-a.wav")
    .then(
        () => console.log("Loaded beep a!"),
        () => console.error("Failed to load keep a :-(")
    );
soundbox.load("beep-b", "beep-b.wav");
soundbox.load("beep-c", "beep-c.wav");
soundbox.load("victory", "victory.mp3");

soundbox.play("beep-a")
    .then(() => soundbox.play("beep-b"))
    .then(() => soundbox.play("beep-c"))
    .then(() => soundbox.play("victory"));
    
// The 3rd parameter is the volume: The callback can be omitted to enable promise mode
soundbox.play("beep", null, 0.8)
    .then(() => soundbox.play("victory"))

(Full documentation below)

Download

The recommended way to obtain soundbox is via npm here: sound-box - To install it simply type npm install sound-box. Then you can include any of the 4 files described below from the ./node_modules/sound-box/ directory.

If npm isn't for you, then the latest master should be perfectly stable. If you want something that you know will work, try the latest release.

The following files are available via all release methods:

Usage

Create a new instance:

var soundbox = new SoundBox();

Load a sound

Sound loading is done as follows. If a callback is not specified, promise mode is activated.

// Promise mode
soundbox.load("alias", "path/to/filename").then(
    () => console.log("Loaded beep!"),
    () => console.error("Failed to load beep :-(")
);
soundbox.load("victory", "./sounds/yay.wav").then( /* .... */);
// Callback mode
soundbox.load("alias", "path/to/filename", function() {
    console.log("Loaded beep!");
});

Play a sound

Similarly, playing a sound takes 2 forms: callback and promise.

// Promise mode
soundbox.play("beep-a")
    .then(() => soundbox.play("beep-b")) /* The .then() is optional of course */
    .then(() => soundbox.play("beep-c"))
    .then(() => soundbox.play("victory"));
// Callback mode
soundbox.play("beep", function() {
	// Do stuff
});

Specify the volume

The volume can be specified when playing a sound, too. The volume should be a floating-point number between 0 (silent) and 1 (full volume, default).

Here's how:

// Promise mode
soundbox.play("beep-a", null, 0.8)
    .then(() => /* .... */);
// Callback mode
soundbox.play("beep", function() {
	// Do stuff
}, 0.5);

Specify the default volume

If you'd like to change the default volume that sounds are played at, do this:

soundbox.default_volume = 0.45; // Sets the default volume to 45%

Looping again and again

As of v0.3.7, looping support has been added. Use it like this:

// Promise mode
soundbox.play("rocket", null, 0.8, true)
    .then(() => /* .... */);
// Callback mode
soundbox.play("rocket", function() {
	// Do stuff
}, null, true); // A null volume = use the default volume. Supported in both modes of operation

Stop all sounds

You can stop all currently playing sounds like this:

soundbox.stop_all();

Unload a sound

If you want to, you can unload a sound:

soundbox.remove("sound_alias");
soundbox.remove("oops");

Get the current version

Get the current version of SoundBox:

console.log(`Soundbox is at ${SoundBox.version}`);

Changelog

  • v1.0.0: This has to be stable by now! Also switch to alive-server in dev dependencies to fix security issues - long live forks!
  • v0.3.10: Bugfix: Don't skip playback if the volume is set to 0 (#10, thanks @matthewoates!) - v0.3.9: Documentation release. Use badges to show size.
  • v0.3.8: Add optional loop parameter to .play()
  • v0.3.7: Update changelog in README
  • v0.3.6: Fix main definition in package.json
  • v0.3.5: Update from .jsm to .mjs for file extension
  • v0.3.4: Fix another bug in .stop_all()
  • v0.3.3: Fix a bug in .stop_all()
  • v0.3.2: Added .stop_all() and default_volume
  • v0.3.1: Added non-es6 version and build system to automate minification.
  • v0.3: Added volume support! Converted for use as an ES6 module. If this doesn't suit you, then just remove the import and export statements to make it the way it was before.
  • v0.2: Now with button mashing support!

Real-World Usage

Contributing

Contributions are welcome! Please mention that you release your contribution under the MIT license (see below) in your PR message.

Edits should be made to soundbox.js - the other 3 are automatically generated by the bash-based build script build, which is located in the root of this repository.

License

SoundBox.js is licensed under MIT:

The MIT License (MIT)

Copyright (c) 2015 Starbeamrainbowlabs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

soundbox's People

Contributors

dependabot[bot] avatar gnuns avatar kdallas avatar matthewoates avatar sbrl avatar vdeville avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

soundbox's Issues

stop_all() callback

Can you add the possibility to call the callback of all song played when call stop_all() ?

For example if song XXX.mp3 and YYYY.mp3 is play, and i call soundbox.stop_all(), can you terminate each song that is currently playing and call the callback of play() method ?

Or add killed callback for each song ?

Thanks ;)

Add support for play the a sound again while it is already playing

Currently, we stop the sound object and play it again if the .play() method is called more than once in quick succession. It would be desirable to 'clone' the Audio object and play that instead. It would also be good if we could store them in a sort of 'pool', and locate the first once that isn't playing and use that.

demo does not work

https://sbrl.github.io/soundbox/example.html
.jsm file is not loaded properly, and throws error in Chrome:
soundbox.jsm:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

Firefox reports:
Loading failed for the module with source “https://sbrl.github.io/soundbox/soundbox.jsm”.

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.