bbc / waveform-data.js

Audio Waveform Data Manipulation API – resample, offset and segment waveform data in JavaScript.
https://waveform.prototyping.bbc.co.uk
Other
1.04k stars 103 forks source link

Export to JSON #76

Closed msjaber closed 3 years ago

msjaber commented 4 years ago

Hey,

As I can see this library "can generate waveform data using the Web Audio API."

I'm wondering if it can export the generated wave into a JSON structure or data file just like the C++ library?

Thanks.

chrisn commented 4 years ago

It doesn't have functions to generate the JSON or binary data directly, but these could be added - something like waveformData.toJSON() and waveformData.toArrayBuffer().

Let me know if you're interested in creating a PR to add this feature.

msjaber commented 4 years ago

Yes perfect!

The waveform data is not exposed explicitly to the developers (Could be accessed from waveform._adapter._data.buffer). And I want to export it to JSON since I need to generate the waveform on the client for the first time, upload it to the server, and reuse it later on.

Can I create a PR for it @chrisn?

chrisn commented 4 years ago

Yes, please do create a PR. We'll need to consider how to handle the different adapters - e.g., if the WaveformData object is using the ArrayBuffer adapter and the client requests JSON data, and vice versa.

Nadav42 commented 4 years ago

had the same issue, this work just fine for me: can you think of any possible issues with this?

new Promise((resolve, reject) => {
            WaveformData.createFromAudio(options, (err, waveform) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(waveform);
                }
            });
        }).then((waveform) => {
            console.log(`Waveform has ${waveform.channels} channels`);
            console.log(`Waveform has length ${waveform.length} points`);

            const waveformJson = {
                version: 2,
                channels: 1,
                sample_rate: waveform.sample_rate,
                samples_per_pixel: waveform.scale,
                bits: waveform.bits,
                length: waveform.length,
                data: []
            };

            const channel = waveform.channel(0);

            for (let i = 0; i < waveform.length; i++) {
                waveformJson.data.push(channel.min_sample(i));
                waveformJson.data.push(channel.max_sample(i));
            }

           return waveformJson;
        });
chrisn commented 4 years ago

Thanks, @Nadav42. This looks good, the only thing to consider would be waveforms with multiple channels.

wong2 commented 3 years ago

Is it possible to export binary format too? waveformData.toArrayBuffer

chrisn commented 3 years ago

Currently no, but we can add this if it will be useful. Note that .toJSON() is now available in v4.0.0.

wong2 commented 3 years ago

Yes, I'm using toJSON() currently, saving the result in indexeddb to speedup later usage. Thought it would be better if I can store the binary data.

chrisn commented 3 years ago

Yes, the binary data will be smaller. We can add this in the next release. You are welcome to send a pull request, or I can do it, although I can't promise when that will be.

chrisn commented 3 years ago

v4.1.0 is now published.

wong2 commented 3 years ago

Thanks!