Closed msjaber closed 3 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.
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?
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.
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;
});
Thanks, @Nadav42. This looks good, the only thing to consider would be waveforms with multiple channels.
Is it possible to export binary format too? waveformData.toArrayBuffer
Currently no, but we can add this if it will be useful. Note that .toJSON()
is now available in v4.0.0.
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.
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.
v4.1.0 is now published.
Thanks!
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.