Skip to content

Instantly share code, notes, and snippets.

@rutvik110
Last active December 13, 2023 01:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rutvik110/31a588244d288e89368e8704c1437d34 to your computer and use it in GitHub Desktop.
Save rutvik110/31a588244d288e89368e8704c1437d34 to your computer and use it in GitHub Desktop.
Audio Data parse
import 'dart:convert';
import 'dart:math' as math;
List<double> loadparseJson(String jsonBody) {
final data = jsonDecode(jsonBody);
final List<int> points = List.castFrom(data['data']);
List<int> filteredData = [];
// Change this value to number of audio samples you want.
// Values between 256 and 1024 are good for showing [RectangleWaveform] and [SquigglyWaveform]
// While the values above them are good for showing [PolygonWaveform]
const int samples = 256;
final double blockSize = points.length / samples;
for (int i = 0; i < samples; i++) {
final double blockStart =
blockSize * i; // the location of the first sample in the block
int sum = 0;
for (int j = 0; j < blockSize; j++) {
sum = sum +
points[(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) => math.max(a.abs(), b.abs()));
final double multiplier = math.pow(maxNum, -1).toDouble();
return filteredData.map<double>((e) => (e * multiplier)).toList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment