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

@sourcesync-sdk/whisper-web

v0.1.1

Published

## Overview The @sourcesync-sdk/whisper-web enables audio fingerprinting and matching capabilities in web applications. It allows developers to implement real-time audio capture, generate audio fingerprints, and match them against a database.

Downloads

186

Readme

@sourcesync-sdk/whisper-web

Overview

The @sourcesync-sdk/whisper-web enables audio fingerprinting and matching capabilities in web applications. It allows developers to implement real-time audio capture, generate audio fingerprints, and match them against a database.

Installation

To install the package, run the following command in your project directory:

npm install sourcesync-sdk

Or install a submodule as its own dependency:

npm install @sourcesync-sdk/whisper-web @sourcesync-sdk/app

Basic Usage

Here's a quick guide to get started with the Whisper Web SDK:

  1. Import and initialize the SDK:
import { createWhisperFactory } from 'sourcesync-sdk/whisper-web';
import { initializeApp } from 'sourcesync-sdk/app';

const app = await initializeApp({
  appKey: 'your-app-key',
  
  // app config
})

const whisperFactory = await createWhisperFactory(app, {
  // set default options for Whisper instances
  defaultOpts: {
    apiUrl: 'YOUR_API_URL',
    apiToken: 'YOUR_API_TOKEN',
    apiKey: 'YOUR_API_KEY' // optional
  }
});
  1. Create a Whisper instance:
const whisper = whisperFactory.create();
await whisper.init();
  1. Set up fingerprinting:
await whisper.setCallback((fingerprint) => {
  // Handle the generated fingerprint
  console.log('Fingerprint generated:', fingerprint);
  // Request a match
  whisper.requestMatch(fingerprint)
    .then((matchResult) => {
      console.log('Match result:', matchResult);
    })
    .catch((err) => {
      // Handle errors
    });
});

MatchResponse Object

The MatchResponse object contains the following properties:

 
interface MatchResponse {
  // device id from whisper platform if registered
  wdeviceid: number

  // session id from whisper platform if registered
  wsessionid: number

  // pass-through JSON string, you can store timestamps, custom device id's or anything
  // you need to receive back with the match response in the match request
  requestJson: string

  // match object stores the matches grouped by type or an array of matches
  matches: MatchGroups | MatchItem[]
}
 
 type MatchGroups = Record<string, MatchItem[]>
 
 interface MatchItem {
  // reference content id as registered on the whisper platform
  wrefid: number

  // confidence score, 0 - 100, higher is better
  confidence: number

  // unknown time for the match based on the incoming fingerprint timestamp
  unknowntime: string

  // the time within the reference content that matched
  referencetime: string

  // JSON data that was registered with the content on the whisper platform
  // this could be channel id's, custom content id's, title, description, etc.
  refJson: string

  title?: string
}

  1. Setup audio capture:
// attach the microphone
await whisper.attachMicrophone();

// or attach the video element
await whisper.attachVideoElement(videoElement);

// or attach the audio element
await whisper.attachAudioElement(audioElement);
  1. Start and stop audio capture:
// Start capturing
await whisper.start();

// Stop capturing
await whisper.stop();

Limitations and Requirements

Browser Audio/Video Access

For media resources to be accessible, the server must include the following header in its response:

Access-Control-Allow-Origin: *

Note: Using a wildcard (*) allows access from all origins. For production environments, consider specifying allowed origins explicitly for enhanced security.

WebAssembly (WASM) Requirements

When using the WASM build, specific headers are required to enable SharedArrayBuffers, which are necessary for the WASM runtime to support pthreads via WebWorkers.

Client Site (page embedding the WASM code)

The service must return these headers in the page response:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

WASM Host (e.g., CDN)

The service hosting the WASM code must return these headers in OPTIONS requests:

Access-Control-Allow-Origin: *
Cross-Origin-Resource-Policy: cross-origin

Important Notes

  1. These requirements may not cover all scenarios, especially when hosting components on different domains.
  2. Enabling these headers may potentially break existing website functionality.
  3. Without loading the WASM module, fingerprint generation in the browser is not possible.

Explanation of Headers

  1. Cross-Origin-Opener-Policy: 'same-origin'

    • Isolates the browsing context to same-origin documents
    • Enhances security by preventing cross-origin popup manipulation
    • Reduces the risk of side-channel attacks
  2. Cross-Origin-Embedder-Policy: 'require-corp'

    • Ensures cross-origin resources must explicitly grant permission to be loaded
    • Enables the use of SharedArrayBuffer, crucial for multi-threaded WebAssembly
    • Prevents unauthorized data access from other origins

These headers enable "cross-origin isolation", which is important for WebAssembly because it:

  • Allows use of high-precision timers and SharedArrayBuffer, improving performance
  • Creates a more isolated environment for WebAssembly code execution
  • Enables advanced WebAssembly features like threads in most browsers

Trade-offs

  • May break integrations with some third-party services relying on cross-origin access
  • Can complicate embedding your content in other sites or vice versa

For full WebAssembly functionality, ensure all resources (including .wasm files) are served from the same origin or have appropriate CORS headers.

API Reference

WhisperFactory

  • create(): Creates a new Whisper instance

WhisperWeb Instance

  • init(): Initializes the Whisper instance
  • attachMicrophone(): Attaches the microphone for audio capture
  • attachVideoElement(videoElement): Attaches a video element for audio capture
  • attachAudioElement(audioElement): Attaches an audio element for audio capture
  • setCallback(callback): Sets the callback for fingerprint generation
  • start(): Starts audio capture and fingerprinting
  • stop(): Stops audio capture
  • requestMatch(fingerprint): Requests a match for the given fingerprint

Best Practices

  • Ensure proper cleanup by calling stop() when audio capture is no longer needed

License

This SDK is distributed under the Apache License, Version 2.0. The Apache 2.0 License applies to the SDK only and not any other component of the SourceSync Platform.