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 🙏

© 2026 – Pkg Stats / Ryan Hefner

music-segment-detector

v0.2.2

Published

Audio music segment detection library - Automatically detect music segments from WAV audio files

Readme

Music Segment Detector

Audio music segment detection library - Automatically detect music segments from WAV audio files.

English | 中文

Features

  • Multi-feature analysis (RMS, energy, MFCC, spectral centroid, etc.)
  • Automatic detection of music segment start and end times
  • Customizable detection parameters
  • No external dependencies (no ffmpeg required)

Installation

npm install music-segment-detector

Usage

import { analyzeAudio, detectMusicSegments } from "music-segment-detector";

// 1. Analyze WAV audio file (with progress callback)
const features = await analyzeAudio("audio.wav", 2048, 512, (progress) => {
  console.log(`Analysis progress: ${(progress * 100).toFixed(1)}%`);
});

// 1-2. Use Worker parallel processing (recommended for large files, 2.5-3x faster)
const features = await analyzeAudio(
  "audio.wav",
  2048,
  512,
  (progress) => {
    console.log(`Analysis progress: ${(progress * 100).toFixed(1)}%`);
  },
  {
    useWorkers: true, // Enable Worker parallel processing
    numWorkers: 4, // Optional, defaults to CPU core count
  },
);

// 2. Detect music segments
const segments = detectMusicSegments(features, {
  energyPercentile: 50, // Energy percentile threshold (0-100)
  minSegmentDuration: 25, // Minimum segment duration in seconds
  maxGapDuration: 15, // Maximum gap duration in seconds
  smoothWindowSize: 4, // Smoothing window size in seconds
});

// 3. Use detected segments
segments.forEach((segment) => {
  console.log(`Segment: ${segment.startTime}s - ${segment.endTime}s`);
  console.log(`Duration: ${segment.duration}s`);
  console.log(`Confidence: ${segment.confidence}`);
});

API

analyzeAudio(audioPath, windowSize?, hopSize?, onProgress?, options?)

Analyze a WAV audio file and extract features.

  • audioPath: Path to WAV file
  • windowSize: Analysis window size (default: 2048)
  • hopSize: Window hop size (default: 512)
  • onProgress: Optional progress callback function (progress: number) => void, parameter is progress value between 0-1
  • options: Optional configuration
    • useWorkers: Enable Worker parallel processing (default: false)
    • numWorkers: Number of workers (default: CPU core count, max 8)
  • Returns: Promise<AudioFeatures[]>

detectMusicSegments(features, config?)

Detect music segments from audio features.

  • features: Array of audio features
  • config: Detection configuration (optional)
    • energyPercentile: Energy percentile threshold (0-100, default: 50)
    • minSegmentDuration: Minimum segment duration in seconds (default: 25)
    • maxGapDuration: Maximum gap duration in seconds (default: 15)
    • smoothWindowSize: Smoothing window size in seconds (default: 4)
  • Returns: MusicSegment[]

saveSegmentsToJson(segments, outputPath, mediaFileName)

Save detection results to JSON format.

  • segments: Array of music segments
  • outputPath: Output file path
  • mediaFileName: Media file name
  • Returns: Promise<void>

getFeatureStats(features)

Get statistical information of audio features (for debugging and analysis).

  • features: Array of audio features
  • Returns: Statistics object containing min, max, mean, median for each feature

Type Definitions

interface AudioFeatures {
  timestamp: number;
  rms: number;
  energy: number;
  zcr: number;
  spectralEnergy: number;
  variance: number;
  mfcc: number[];
  spectralCentroid: number;
  spectralRolloff: number;
  spectralFlatness: number;
}

interface AnalyzeAudioOptions {
  useWorkers?: boolean; // Enable Worker parallel processing
  numWorkers?: number; // Number of workers (defaults to CPU core count)
}

interface MusicSegment {
  startTime: number;
  endTime: number;
  duration: number;
  confidence: number;
  name?: string;
}

interface DetectionConfig {
  energyPercentile?: number;
  minSegmentDuration?: number;
  maxGapDuration?: number;
  smoothWindowSize?: number;
}

How It Works

  1. Feature Extraction: Analyze audio using sliding windows to extract multi-dimensional features (energy, MFCC, spectral features, etc.)
  2. Multi-Feature Scoring: Score each time window based on 7 features:
    • Energy intensity
    • Energy stability
    • Spectral centroid (timbre brightness)
    • Spectral flatness (tone vs noise)
    • MFCC continuity (timbre consistency)
    • Zero-crossing rate
    • Spectral rolloff
  3. Post-processing: Smoothing, merging adjacent segments, filtering short segments

Performance Optimization

For large audio files, it's recommended to enable Worker parallel processing for better performance:

  • Single-threaded mode (default): Suitable for small files (< 1 minute) or low-spec environments
  • Worker parallel mode: Suitable for large files, 2.5-3x performance boost (on 4-core CPU)
    • Automatically adjusts worker count based on file size
    • Automatically falls back to single-threaded mode if workers fail
    • Supports progress aggregation and error handling

Notes

  • Only supports WAV format audio files
  • For other formats, use tools like ffmpeg to convert to WAV first
  • Detection accuracy depends on audio quality and configuration parameters
  • Worker mode requires Node.js 16+ version

License

MIT