Realtime Audio Analyzer Bars Demo

Demonstrates how to create a JavaScript Frequency / Audio Analyzer Bars with Fourier Transform (Frequency spectra) and a real-time frequency history using heatmaps. Note: this example requires microphone permissions to run.

src/drawExample.ts
import { AudioDataProvider } from "./AudioDataProvider";
import { Radix2FFT } from "./Radix2FFT";
import { appTheme } from "./appTheme";
import {
    XyDataSeries,
    TextAnnotation,
    ECoordinateMode,
    EHorizontalAnchorPoint,
    EVerticalAnchorPoint,
    SciChartSurface,
    NumericAxis,
    EAutoRange,
    NumberRange,
    FastLineRenderableSeries,
    EColumnYMode,
    EColumnMode,
    FastRectangleRenderableSeries,
    parseColorToUIntArgb,
    EFillPaletteMode,
    EDataPointWidthMode,
    XyyDataSeries,
    DefaultPaletteProvider,
} from "scichart";

const AUDIO_STREAM_BUFFER_SIZE = 2048;

export const getChartsInitializationApi = () => {
    const dataProvider = new AudioDataProvider();

    const bufferSize = dataProvider.bufferSize;

    const fft = new Radix2FFT(bufferSize);

    const fftCount = 200;

    let audioDS: XyDataSeries;
    let historyDS: XyDataSeries;
    const updateFunctions: Array<(value: number, label: string) => void> = [];

    let hasAudio: boolean;

    const helpText = new TextAnnotation({
        x1: 0,
        y1: 0,
        xAxisId: "history",
        xCoordinateMode: ECoordinateMode.Relative,
        yCoordinateMode: ECoordinateMode.Relative,
        horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
        verticalAnchorPoint: EVerticalAnchorPoint.Top,
        text: "This example requires microphone permissions.  Please click Allow in the popup.",
        textColor: "#FFFFFF88",
    });

    function updateAnalysers(): void {
        // Make sure Audio is initialized
        if (dataProvider.initialized === false) {
            return;
        }

        // Get audio data
        const audioData = dataProvider.next();

        // Update Audio Chart. When fifoCapacity is set, data automatically scrolls
        audioDS.appendRange(audioData.xData, audioData.yData);

        // Update History. When fifoCapacity is set, data automatically scrolls
        historyDS.appendRange(audioData.xData, audioData.yData);

        // Perform FFT
        const fftData = fft.run(audioData.yData);

        // Average nearby FFT bins into ten frequency bands, from bass to treble.
        const bandLevels = [
            (fftData[1] + fftData[2] + fftData[3]) / 3,
            (fftData[4] + fftData[5] + fftData[6]) / 3,
            (fftData[10] + fftData[11] + fftData[12]) / 3,
            (fftData[21] + fftData[22] + fftData[23] + fftData[24]) / 4,
            (fftData[44] + fftData[45] + fftData[46] + fftData[47] + fftData[48]) / 5,
            (fftData[90] + fftData[91] + fftData[92] + fftData[93] + fftData[94]) / 5,
            (fftData[183] + fftData[184] + fftData[185] + fftData[186] + fftData[187]) / 5,
            (fftData[369] + fftData[370] + fftData[371] + fftData[372] + fftData[373]) / 5,
            (fftData[740] + fftData[741] + fftData[742] + fftData[743] + fftData[744]) / 5,
            (fftData[1019] + fftData[1020] + fftData[1021] + fftData[1022] + fftData[1023]) / 5,
        ];

        const frequencies = ["62Hz", "125Hz", "250Hz", "500Hz", "1Khz", "2Khz", "4Khz", "8Khz", "16Khz", "22Khz"];

        bandLevels
            .map((d) => d / 2 - 10)
            .forEach((d, i) => {
                updateFunctions[i](d, frequencies[i]);
            });
    }

    // AUDIO CHART
    const initAudioChart = async (rootElement: string | HTMLDivElement) => {
        // Create a chart for the audio
        const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
            theme: appTheme.SciChartJsTheme,
        });

        // Create an XAxis for the live audio
        const xAxis = new NumericAxis(wasmContext, {
            id: "audio",
            autoRange: EAutoRange.Always,
            drawLabels: false,
            drawMinorTickLines: false,
            drawMajorTickLines: false,
            drawMajorBands: false,
            drawMinorGridLines: false,
            drawMajorGridLines: false,
        });
        sciChartSurface.xAxes.add(xAxis);

        // Create an XAxis for the history of the audio on the same chart
        const xhistAxis = new NumericAxis(wasmContext, {
            id: "history",
            autoRange: EAutoRange.Always,
            drawLabels: false,
            drawMinorGridLines: false,
            drawMajorTickLines: false,
        });
        sciChartSurface.xAxes.add(xhistAxis);

        // Create a YAxis for the audio data
        const yAxis = new NumericAxis(wasmContext, {
            autoRange: EAutoRange.Never,
            visibleRange: new NumberRange(-32768 * 0.8, 32767 * 0.8), // [short.MIN. short.MAX]
            drawLabels: false,
            drawMinorTickLines: false,
            drawMajorTickLines: false,
            drawMajorBands: false,
            drawMinorGridLines: false,
            drawMajorGridLines: false,
        });
        sciChartSurface.yAxes.add(yAxis);

        // Initializing a series with fifoCapacity enables scrolling behaviour and auto discarding old data
        audioDS = new XyDataSeries(wasmContext, { fifoCapacity: AUDIO_STREAM_BUFFER_SIZE });

        // Fill the data series with zero values
        for (let i = 0; i < AUDIO_STREAM_BUFFER_SIZE; i++) {
            audioDS.append(0, 0);
        }

        // Add a line series for the live audio data
        // using XAxisId=audio for the live audio trace scaling
        const rs = new FastLineRenderableSeries(wasmContext, {
            xAxisId: "audio",
            stroke: "#4FBEE6",
            strokeThickness: 2,
            dataSeries: audioDS,
        });

        sciChartSurface.renderableSeries.add(rs);

        // Initializing a series with fifoCapacity enables scrolling behaviour and auto discarding old data.
        historyDS = new XyDataSeries(wasmContext, { fifoCapacity: AUDIO_STREAM_BUFFER_SIZE * fftCount });
        for (let i = 0; i < AUDIO_STREAM_BUFFER_SIZE * fftCount; i++) {
            historyDS.append(0, 0);
        }

        // Add a line series for the historical audio data
        // using the XAxisId=history for separate scaling for this trace
        const histrs = new FastLineRenderableSeries(wasmContext, {
            stroke: "#208EAD33",
            strokeThickness: 1,
            opacity: 0.5,
            xAxisId: "history",
            dataSeries: historyDS,
        });
        sciChartSurface.renderableSeries.add(histrs);

        // Add instructions
        sciChartSurface.annotations.add(helpText);

        hasAudio = await dataProvider.initAudio();

        return { sciChartSurface };
    };

    // FFT CHART
    const initFftChart = async (rootElement: string | HTMLDivElement) => {
        const GRADIENT_COLORS = [
            "#1C5727",
            "#277B09",
            "#2C8A26",
            "#3CAC45",
            "#58FF80",
            "#59FD03",
            "#7FFC09",
            "#98FA96",
            "#AEFE2E",
            "#FEFCD2",
            "#FBFF09",
            "#FBD802",
            "#F9A700",
            "#F88B01",
            "#F54602",
            "#F54702",
            "#F50E02",
            "#DA153D",
            "#B22122",
            "#B22122",
        ];

        const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
            theme: appTheme.SciChartJsTheme,
        });

        const growByX = new NumberRange(0.01, 0.01);
        const growByY = new NumberRange(0.1, 0.05);

        const xAxis = new NumericAxis(wasmContext, {
            isVisible: false,
            growBy: growByX,
        });

        const yAxis = new NumericAxis(wasmContext, {
            growBy: growByY,
        });
        sciChartSurface.xAxes.add(xAxis);
        sciChartSurface.yAxes.add(yAxis);

        class RectangleFillPaletteProvider extends DefaultPaletteProvider {
            public readonly fillPaletteMode: EFillPaletteMode = EFillPaletteMode.SOLID;

            private readonly colors: number[];

            constructor(colorStrings: string[]) {
                super();
                // Convert hex color strings to ARGB numbers
                this.colors = colorStrings.map((color) => parseColorToUIntArgb(color));
            }

            public overrideFillArgb(
                xValue: number,
                yValue: number,
                index: number,
                opacity?: number,
                metadata?: any
            ): number | undefined {
                let color = this.colors[index - 1];

                // Return different color based on index
                return color;
            }
        }

        const createGauge = (value: number, width: number, position: number, label: string) => {
            const dataSeries = new XyyDataSeries(wasmContext);

            const backgroundRectangle = new FastRectangleRenderableSeries(wasmContext, {
                dataSeries: new XyyDataSeries(wasmContext, {
                    xValues: [-2 + position * width * 2],
                    yValues: [-10.5],
                    y1Values: [10.5],
                }),
                columnXMode: EColumnMode.Start,
                columnYMode: EColumnYMode.TopBottom,
                dataPointWidth: width + 4,
                dataPointWidthMode: EDataPointWidthMode.Range,
                fill: appTheme.DarkIndigo,
                strokeThickness: 2,
                stroke: "gray",
            });

            const rectangleSeries = new FastRectangleRenderableSeries(wasmContext, {
                dataSeries,
                columnXMode: EColumnMode.Start,
                columnYMode: EColumnYMode.TopBottom,
                dataPointWidth: width,
                dataPointWidthMode: EDataPointWidthMode.Range,
                stroke: appTheme.DarkIndigo, // Thick stroke same color as background gives gaps between rectangles
                strokeThickness: 4,
                paletteProvider: new RectangleFillPaletteProvider(GRADIENT_COLORS),
                fill: appTheme.ForegroundColor + "00",
            });

            sciChartSurface.renderableSeries.add(backgroundRectangle, rectangleSeries);

            const annotation = new TextAnnotation({
                x1: 5.5 + position * width * 2,
                y1: -11,
                fontSize: 12,
                textColor: "#FFFFFF",
                horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
                verticalAnchorPoint: EVerticalAnchorPoint.Top,
            });
            sciChartSurface.annotations.add(annotation);

            const updateGaugeData = (value: number, label: string) => {
                dataSeries.clear();
                const columnYValues = [
                    -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                ].filter((y) => y <= value);
                const xValues = columnYValues.map((d) => position * width * 2);
                const yValues = columnYValues.map((d, i) => (i === 0 ? columnYValues[0] : columnYValues[i - 1]));
                dataSeries.appendRange(xValues, yValues, columnYValues);
                annotation.text = label;
            };
            updateGaugeData(value, label);

            return updateGaugeData;
        };

        for (let i = 0; i < 10; i++) {
            updateFunctions.push(createGauge(0, 10, i, "0"));
        }
        return { sciChartSurface };
    };

    const onAllChartsInit = () => {
        if (!hasAudio) {
            console.log("dataProvider", dataProvider);
            if (dataProvider.permissionError) {
                helpText.text =
                    "We were not able to access your microphone.  This may be because you did not accept the permissions.  Open your browser security settings and remove the block on microphone permissions from this site, then reload the page.";
            } else if (!window.isSecureContext) {
                helpText.text = "Cannot get microphone access if the site is not localhost or on https";
            } else {
                helpText.text = "There was an error trying to get microphone access.  Check the console";
            }

            return { startUpdate: () => {}, stopUpdate: () => {}, cleanup: () => {} };
        } else {
            helpText.text = "This example uses your microphone to generate waveforms. Say something!";

            // START ANIMATION

            const updateChart = () => {
                if (!dataProvider.isDeleted) {
                    updateAnalysers();
                }
            };

            let timerId: NodeJS.Timeout;

            const startUpdate = () => {
                timerId = setInterval(updateChart, 20);
            };

            const stopUpdate = () => {
                clearInterval(timerId);
            };

            const cleanup = () => {
                dataProvider.closeAudio();
            };

            return { startUpdate, stopUpdate, cleanup };
        }
    };

    return { initAudioChart, initFftChart, onAllChartsInit };
};

JavaScript Audio Spectral Analyzer

Overview

The Audio Analyzer Bars example demonstrates real-time audio analysis using the Web Audio API in JavaScript. It captures microphone input via the browser by using navigator.mediaDevices.getUserMedia (MDN getUserMedia) and processes the audio signal in real time. The processed audio data is visualized using three charts: an audio waveform, an FFT spectrum, and a spectrogram heatmap.

Technical Implementation

The implementation captures audio from the user and applies real-time buffering with a FIFO mechanism to ensure smooth scrolling updates. This approach is similar to techniques discussed in articles like Creating an Interactive Audio Visualizer with JavaScript.

A key aspect of the example is the FFT analysis, which is implemented via a Radix2FFT algorithm that utilizes bit reversal and butterfly calculations. For a deeper understanding of these processes, one can refer to FFT: Bit Reversing and Butterfly.

Features and Capabilities

Real-time Visualization: The example renders an audio waveform using a FastLineRenderableSeries, an FFT spectrum via a FastMountainRenderableSeries with a gradient palette created by PaletteFactory.createGradient(), and a scrolling real-time spectrogram using a UniformHeatmapDataSeries. The spectrogram implementation leverages the concepts outlined in The Uniform Heatmap Chart Type.

Performance Optimizations: Smooth real-time updates are achieved through efficient animation loops using setInterval. Additionally, the application considers performance enhancements for handling complex arithmetic operations within the FFT computation, aligning with strategies found in Performance Optimisation of JavaScript Charts.

Integration and Best Practices

The code follows best practices for secure and efficient real-time audio processing in JavaScript. It ensures that microphone permissions are obtained securely and that the audio context is managed properly. The modular design allows for extensibility and customization, while the careful handling of data buffering and real-time updates provides a responsive user experience. This example is ideal for developers looking to integrate the Web Audio API with advanced charting libraries such as SciChart.js to build high-performance, interactive audio visualizations.