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

audiostreamsource.js

v0.1.2

Published

Provides a streamed audio source for WebAudio across browsers

Downloads

3

Readme

audiostreamsource.js

This is a tiny library to provide a streamed audio source for the WebAudio API cross browser.

Specifically at least up to iOS9 and Android 6 you can't stream audio to the WebAudio API >:( In that case it's not streamed.

Usage

<script src="audiostreamsource.min.js"><script>
<script>
var context = new (window.AudioContext || window.webkitAudioContext)();
var streamSource = audioStreamSource.create({
  context: context,                    // a WebAudio context
  loop: true,                          // true or false if you want it to loop
  autoPlay: false,                     // true to autoplay (you don't want this. See below)
  crossOrigin: false,                  // true to try to get crossOrigin permission
});

streamSource.on('newSource', function(source) {
  streamSource.play();
  source.connect(context.destination);
});

streamSource.on('error', function(err) {
  // got an error, bad URL? cross origin permission error?
});

var src     = 'url-to-some.mp3';          // file to play
var lofiSrc = 'url-to-some-smaller.mp3';  // for shitty browsers those on iOS/Android

streamSource.setSource(src, lofiSrc);
</script>

API

audioStreamSource.create, it takes options. After that you setup event listeners, newSource and error using the streamSource.on function. You then call streamSource.setSource with 1 or 2 URLs. The first URL is the source to your audio. The second URL is an optional second source for if you're on iOS since as of iOS9 iOS and Android Chrome as of 52 still do not support createMediaElementSource.

When the audio is ready to play you'll get a newSource event. At that point you can call streamSource.play and connect the source node wherever you need it.

Because mobile does not support createMediaElementSource it can't stream audio into Web Audio. That means (a) it can not start playing audio until the entire file is downloaded and (b) it generally can not get access to cross origin audio data.

  • streamSource.setSource

    lets you set a new source. "mic" = microphone

  • streamSource.getSource

    Gets the source node for the stream. This will be null until the first newSource event arrives.

  • streamSource.play()

    starts playing the source

  • streamSource.stop()

    stops playing the source

  • streamSource.isPlaying()

    return true if the stream is playing.

Options

The options passed into audioStreamSource.create are as follows

context:      a WebAudio context (required)
loop:         whether or not to loop. Default false
autoPlay:     whether or not to start playing automatically. default false ([don't set to true](#autoplay-issues))
crossOrigin:  whether or not to request crossOrigin permissions

autoPlay issues

Mobile browsers can not autoplay audio. Audio must be started by user gesture. Because of this autoPlay can not work on mobile period. You must ask the user to click or touch something.

Example:

var isMobile = window.navigator.userAgent.match(/Android|iPhone|iPad|iPod|Windows Phone/i);
var context = new (window.AudioContext || window.webkitAudioContext)();
var clickToStartElem = document.getElementById("clickMe");  // some element to click

function startAudio() {
  streamSource.play();
  source.connect(context.destination);
}

var streamSource = audioStreamSource.create({
  context: context,                    // a WebAudio context
  loop: true,                          // true or false if you want it to loop
});

streamSource.on('newSource', function(source) {
  if (isMobile) {
    clickToStartElem.style.display = "block"; // make this element visible
    clickToStartElem.addEventListener('click', function() {
      clickToStartElem.style.display = "none"; // hide it
      startAudio();
    });
  } else {
    startAudio();
  }
});

streamSource.on('error', function(err) {
  // got an error, bad URL? cross origin permission error?
});

var src     = 'url-to-some.mp3';          // file to play
var lofiSrc = 'url-to-some-smaller.mp3';  // for shitty browsers like Safari on iOS

streamSource.setSource(src, lofiSrc);

Example

There's an example here.

License

MIT