Skip to content

Instantly share code, notes, and snippets.

@rutvik110
Last active March 27, 2022 12:08
Embed
What would you like to do?
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