npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chibitronics-ltc-modulate

v1.2.1

Published

Modulator code for Chibitronics Love-to-Code

Downloads

9

Readme

Chibitronics Love-to-Code Audio Modulator

A software modulator to turn code into audio for the Love-to-Code Chibi Chip.

Usage

First, get some data. The usual way to do this is via some base64 encoded data that came back as a JSON result. Convert it into a Uint8Array:

var data = atob(results.output);
var dataU8 = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++) {
    dataU8[i] = data.charCodeAt(i);
}

Next, initialize a modulation controller. If you want to be able to visualize the output waveform, give it a canvas to work with.

You can also specify the output audio format for the audio tag. Currently-supported values are 'wav' and 'mp3'.

Additionally, two strategies are avilable for encoding data for the browser: Blob and Data URI. Blob uses less memory, but is not compatible with older browsers. The two options encoded as strings as 'blob' and 'data'. If unspecified, the encoder will default to using a 'data' URI.

If you want a more resiliant "low bitrate" mode, specify "true" to lbr.

var ModulationController = require('chibitronics-ltc-modulate');
var modController = new ModulationController({
    canvas: getCanvas(),
    lbr: lbrEnable,
    uriType: 'data',
    format: 'wav',
    endCallback: function () {
        getWaveFooter().style.display = 'none';
    }
});

Finally, transcode the audio into an audio tag:

modController.transcodeToAudioTag(dataU8, document.getElementById('audio_tag'));

Parameters

Most parameters are arguments to the ModulationController object. All are optional.

  • lbr boolean true for low-bitrate mode, false for high-bitrate mode. Defaults to high bitrate. Useful for noisy situations.
  • format string What format to render the output as. Defaults to 'wav'. If lamejs is present, 'mp3' is also an option.
  • loops number The number of iterations of the song to play. Useful because there is no error correction on the device, aside from playing the file again. Defaults to 3 plays.
  • canvas canvas element Pass an HTML canvas element to the modulator, so that it can draw the waveform onto it.
  • endCallback function A function to call after all loops have finished and the wave has played completely.
  • version number Version of the modulation encoding to use. Version 1 was used on preproduction units. Version 2 is much more reliable. Defaults to version 2.

Unblocking Audio

Many devices "block" audio, and can only allow playback when called from a touch or click event. A quick way around this is to play some silence, which will make audio available to you in the future:

var audioTag = document.getElementById('audio_tag');
if (useMP3) {
    audioTag.src = 'data:audio/mp3;base64,' +
        'SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2Z' +
        'jU2LjM2LjEwMAAAAAAAAAAAAAAA//OEAA' +
        'AAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8' +
        'AAAAEAAABIADAwMDAwMDAwMDAwMDAwMDA' +
        'wMDAwMDAwMDV1dXV1dXV1dXV1dXV1dXV1' +
        'dXV1dXV1dXV6urq6urq6urq6urq6urq6u' +
        'rq6urq6urq6v/////////////////////' +
        '///////////8AAAAATGF2YzU2LjQxAAAA' +
        'AAAAAAAAAAAAJAAAAAAAAAAAASDs90hvA' +
        'AAAAAAAAAAAAAAAAAAA//MUZAAAAAGkAA' +
        'AAAAAAA0gAAAAATEFN//MUZAMAAAGkAAA' +
        'AAAAAA0gAAAAARTMu//MUZAYAAAGkAAAA' +
        'AAAAA0gAAAAAOTku//MUZAkAAAGkAAAAA' +
        'AAAA0gAAAAANVVV';
}
else {
    audioTag.src = 'data:audio/wav;base64,' +
        'UklGRigAAABXQVZFZm10IBIAAAABAAEAR' +
        'KwAAIhYAQACABAAAABkYXRhAgAAAAEA';
}
audioTag.play();

if (modController) {
    modController.stop();
}

Internet Explorer Support

If you want to use this with Internet Explorer, you'll need to use MP3 encoding rather than WAV encoding. Use Lamejs, which is a complete MP3 encoding solution written entirely in Javascript.

Because Lamejs is LGPL, it cannot be compiled into this module. Load it sparately.

Also note that you'll need to polyfill Math.log10 on IE11.

Load lame.min.js on your server, and then do something like this:

document.write('<script language="javascript" type="text/javascript" src="js/lame.min.js"></script>');
Math.log10 = function (x) { return Math.log(x) / Math.LN10; };

Finally, specify 'mp3' as the format.