Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audioDataList WIth No Json #25

Open
AimanKyo97 opened this issue Jul 6, 2022 · 8 comments
Open

audioDataList WIth No Json #25

AimanKyo97 opened this issue Jul 6, 2022 · 8 comments
Labels
discussion discussion abt the topic/issue

Comments

@AimanKyo97
Copy link

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

@rutvik110

@rutvik110
Copy link
Owner

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.

@t-m-z
Copy link

t-m-z commented Dec 26, 2022

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;
}

@rutvik110
Copy link
Owner

rutvik110 commented Jan 2, 2023

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?

@t-m-z
Copy link

t-m-z commented Jan 14, 2023

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.

@rutvik110
Copy link
Owner

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?

@t-m-z
Copy link

t-m-z commented Jan 25, 2023 via email

@rutvik110
Copy link
Owner

rutvik110 commented Feb 13, 2023

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 rutvik110 added the discussion discussion abt the topic/issue label Feb 13, 2023
@lucasjinreal
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion discussion abt the topic/issue
Projects
None yet
Development

No branches or pull requests

4 participants