<commit_before>/*
 * spectral.js - Uses the Web Audio API to provide frequency and waveform data
 * from the specified HTML5 <audio> element.
 */

const AudioContext = window.AudioContext;

export default function Spectral(audio, bufsize) {
    if (!(audio instanceof HTMLMediaElement)) {
        throw new Error("audio element not an instance of HTMLMediaElement");
    }

    const context = new AudioContext();
    const analyser = context.createAnalyser();
    const script = context.createScriptProcessor(bufsize, 1, 1);
    const gain = context.createGain();
    const source = context.createMediaElementSource(audio);

    analyser.fftSize = bufsize;
    analyser.smoothingTimeConstant = 0.7;

    const scriptListeners = [];
    script.onaudioprocess = (event) => {
        for (let callback of scriptListeners) {
            callback(event);
        }
    }

    source.connect(analyser);
    source.connect(gain);
    analyser.connect(script);
    script.connect(context.destination);
    gain.connect(context.destination);

    Object.defineProperties(audio, {
        addScriptListener: {
            value: (callback) => scriptListeners.push(callback)
        },
        waveformSize: {
            get: () => analyser.frequencyBinCount
        },
        spectrumSize: {
            get: () => analyser.frequencyBinCount
        },
        getWaveform: {
            value: (out) => analyser.getFloatTimeDomainData(out)
        },
        getSpectrum: {
            value: (out) => analyser.getFloatFrequencyData(out)
        }
    });

    return audio;
}<commit_msg>Fixed webkit GC bug<commit_after>/*
 * spectral.js - Uses the Web Audio API to provide frequency and waveform data
 * from the specified HTML5 <audio> element.
 */

const AudioContext = window.AudioContext;

export default function Spectral(audio, bufsize) {
    if (!(audio instanceof HTMLMediaElement)) {
        throw new Error("audio element not an instance of HTMLMediaElement");
    }

    const context = new AudioContext();
    const analyser = context.createAnalyser();
    const script = context.createScriptProcessor(bufsize, 1, 1);
    const gain = context.createGain();
    const source = context.createMediaElementSource(audio);

    analyser.fftSize = bufsize;
    analyser.smoothingTimeConstant = 0.7;

    const scriptListeners = [];
    script.onaudioprocess = window.onaudioprocess = (event) => {
        for (let callback of scriptListeners) {
            callback(event);
        }
    }

    source.connect(analyser);
    source.connect(gain);
    analyser.connect(script);
    script.connect(context.destination);
    gain.connect(context.destination);

    Object.defineProperties(audio, {
        addScriptListener: {
            value: (callback) => scriptListeners.push(callback)
        },
        waveformSize: {
            get: () => analyser.frequencyBinCount
        },
        spectrumSize: {
            get: () => analyser.frequencyBinCount
        },
        getWaveform: {
            value: (out) => analyser.getFloatTimeDomainData(out)
        },
        getSpectrum: {
            value: (out) => analyser.getFloatFrequencyData(out)
        }
    });

    return audio;
}
{'fraction_non_alphanumeric': 0.10770194113963683, 'fraction_numerical': 0.0031308703819661866, 'mean_word_length': 2.6514285714285712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3920173', 'n_tokens_mistral': 885, 'n_tokens_neox': 868, 'n_words': 200}