flutter_audio_waveforms icon indicating copy to clipboard operation
flutter_audio_waveforms copied to clipboard

audioDataList WIth No Json

Open AimanKyo97 opened this issue 2 years ago • 8 comments

How to made it read from the file audio and not include the json file?

@rutvik110

AimanKyo97 avatar Jul 06 '22 02:07 AimanKyo97

Hey, the package doesn't support directly reading from an audio file due to various reasons, one of them being scaling! You definitely won't be adding/downloading a audio file each time you want to present a waveform for it. Thus, the normalized json data set coze it's easy to move around and manage. You can try justWaveform package if you want to really do that. And get json data from there and pass it to the the waveforms.

rutvik110 avatar Jul 20 '22 14:07 rutvik110

this will only work for uncompressed audio file data like PCM/WAV as depicted here:

List calculateJson( {int PCMformat, int bits, int channels, int sampleRate, Uint8List PCMamplitudesData, int blockSize,}) {

List filteredData = []; List rawSamples = [];

int amplitude = 0;

if (bits == 24) { // https://wiki.multimedia.cx/index.php/PCM int sample = 0; Map<int, List> LPCM = {}; while (sample + 6 * channels < PCMamplitudesData.length) { amplitude = 0; for (int c = 0; c < channels * 2; c++) { List LPCMc = []; LPCMc.add(PCMamplitudesData[sample + c * 2]); LPCMc.add(PCMamplitudesData[sample + c * 2 + 1]); LPCMc.add(PCMamplitudesData[sample + 2 * channels * 2 + c]); LPCM[c] = LPCMc; } for (int c = 0; c < channels * 2; c++) { amplitude = LPCM[c][0] * 256 * 256 + LPCM[c][1] * 256 + LPCM[c][2]; } rawSamples.add(amplitude); sample = sample + 6 * channels; } } else { // 8 Bit, 16 Bit, 32 Bit for (int sample = 0; sample < PCMamplitudesData.length; sample = sample + bits ~/ 8 * channels) { amplitude = 0; switch (bits) { case 8: for (int i = 0; i < channels; i++) amplitude = (ByteData.sublistView(PCMamplitudesData).getInt8(sample + i) - 128); break; case 16: if (sample + 4 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getInt16(sample + i * 2, Endian.little); } break; case 32: if (PCMformat == WAV_FORMAT_PCM) { // PCM if (sample + 8 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getInt32(sample + i * 4, Endian.little); } } else if (PCMformat == WAV_FORMAT_IEEEFLOAT32) { // IEEE Float 32 if (sample + 8 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getFloat32(sample + i * 4, Endian.little).toInt(); } } break; } rawSamples.add(amplitude); }; }

int totalSamples = (rawSamples.length / blockSize).toInt();

for (int i = 0; i < totalSamples; i++) { final double blockStart = (blockSize * i).toDouble(); // the location of the first sample in the block int sum = 0; for (int j = 0; j < blockSize; j++) { sum = sum + rawSamples[(blockStart + j).toInt()] .toInt(); // find the sum of all the samples in the block

}
filteredData.add((sum / blockSize)
    .round() // take the average of the block and add it to the filtered data
    .toInt()); // divide the sum by the block size to get the average

} final maxNum = filteredData.reduce((a, b) => max(a.abs(), b.abs()));

final double multiplier = pow(maxNum, -1).toDouble();

final samples = filteredData.map((e) => (e * multiplier)).toList();

return samples; }

t-m-z avatar Dec 26 '22 18:12 t-m-z

this will only work for uncompressed audio file data like PCM/WAV as depicted here:

List calculateJson( {int PCMformat, int bits, int channels, int sampleRate, Uint8List PCMamplitudesData, int blockSize,}) {

List filteredData = []; List rawSamples = [];

int amplitude = 0;

if (bits == 24) { // https://wiki.multimedia.cx/index.php/PCM int sample = 0; Map<int, List> LPCM = {}; while (sample + 6 * channels < PCMamplitudesData.length) { amplitude = 0; for (int c = 0; c < channels * 2; c++) { List LPCMc = []; LPCMc.add(PCMamplitudesData[sample + c * 2]); LPCMc.add(PCMamplitudesData[sample + c * 2 + 1]); LPCMc.add(PCMamplitudesData[sample + 2 * channels * 2 + c]); LPCM[c] = LPCMc; } for (int c = 0; c < channels * 2; c++) { amplitude = LPCM[c][0] * 256 * 256 + LPCM[c][1] * 256 + LPCM[c][2]; } rawSamples.add(amplitude); sample = sample + 6 * channels; } } else { // 8 Bit, 16 Bit, 32 Bit for (int sample = 0; sample < PCMamplitudesData.length; sample = sample + bits ~/ 8 * channels) { amplitude = 0; switch (bits) { case 8: for (int i = 0; i < channels; i++) amplitude = (ByteData.sublistView(PCMamplitudesData).getInt8(sample + i) - 128); break; case 16: if (sample + 4 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getInt16(sample + i * 2, Endian.little); } break; case 32: if (PCMformat == WAV_FORMAT_PCM) { // PCM if (sample + 8 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getInt32(sample + i * 4, Endian.little); } } else if (PCMformat == WAV_FORMAT_IEEEFLOAT32) { // IEEE Float 32 if (sample + 8 < PCMamplitudesData.length) { for (int i = 0; i < channels; i++) amplitude = ByteData.sublistView(PCMamplitudesData).getFloat32(sample + i * 4, Endian.little).toInt(); } } break; } rawSamples.add(amplitude); }; }

int totalSamples = (rawSamples.length / blockSize).toInt();

for (int i = 0; i < totalSamples; i++) { final double blockStart = (blockSize * i).toDouble(); // the location of the first sample in the block int sum = 0; for (int j = 0; j < blockSize; j++) { sum = sum + rawSamples[(blockStart + j).toInt()] .toInt(); // find the sum of all the samples in the block

}
filteredData.add((sum / blockSize)
    .round() // take the average of the block and add it to the filtered data
    .toInt()); // divide the sum by the block size to get the average

} final maxNum = filteredData.reduce((a, b) => max(a.abs(), b.abs()));

final double multiplier = pow(maxNum, -1).toDouble();

final samples = filteredData.map((e) => (e * multiplier)).toList();

return samples; }

I'm not clearly able to understand ur concern. Could you please elaborate a little on it?

rutvik110 avatar Jan 02 '23 10:01 rutvik110

Sure. Your package is an excellent solution if you have the jason data already. If not you need to create the jason file as a first step. The question was if your package provides a way to create the json data. My answer was no. However if you want to display an uncompressed audio file like wav there is a way to create the json data within the flutter app.

t-m-z avatar Jan 14 '23 08:01 t-m-z

Ah ok. As I said, I don't plan to support json data creation through package. May be in future but nothing planned right now. Any contributions are welcome if you plan to work on something like that.

Also, wdym by "this will only work for uncompressed audio file data like PCM/WAV as depicted here:" in above message? Am I missing something?

rutvik110 avatar Jan 25 '23 04:01 rutvik110

To extract the JSON Data you need to have access to the uncompressed and raw data. This is easy with respect to WAV. MP3 or OGG is a compressed file format. Hence you have to decompress first. This is a rathe complex topic.

Sent from MailDroid

-----Original Message----- From: Rutvik Tak @.> To: rutvik110/flutter_audio_waveforms @.> Cc: Thomas @.>, Comment @.> Sent: Mi., 25 Jan. 2023 5:30 Subject: Re: [rutvik110/flutter_audio_waveforms] audioDataList WIth No Json (Issue #25)

Ah ok. As I said, I don't plan to support json data creation through package. May be in future but nothing planned right now. Any contributions are welcome if you plan to work on something like that.

Also, wdym by "this will only work for uncompressed audio file data like PCM/WAV as depicted here:" in above message? Am I missing something?

-- Reply to this email directly or view it on GitHub: https://github.com/rutvik110/flutter_audio_waveforms/issues/25#issuecomment-1403084355 You are receiving this because you commented.

Message ID: @.***>

t-m-z avatar Jan 25 '23 14:01 t-m-z

ah ohk. This could be critical for some people. Give me some time, I'll have to think abt how we can better support this. Till then I believe u can access json data for any audio files using the just waveforms package. I'vent tried it myself yet but it seems to work fine from what I heard from others. https://pub.dev/packages/just_waveform

rutvik110 avatar Feb 13 '23 14:02 rutvik110

Hi, any consideration add unit8list from url support built-in or any guidance to do this? Really need!

lucasjinreal avatar Jan 06 '24 06:01 lucasjinreal