In my Flutter project for Windows, I use the compute
function to run some code in an Isolate
, in order to avoid freezing the user interface.
But I'm facing a problem: the code I run in the Isolate
returns a very long list of double
(a few millions of items), and because of that, the overall execution takes a ridiculously big amount of time, compare to if I don't use Isolate
at all.
Here is the function that makes many calculations and returns a big list:
static Future<List<double>> extractWavSamples(File fileWav) async
{
// read the audio file:
Uint8List bytes = await fileWAV.readAsBytes();
bytes = bytes.sublist(44);
// convert to a list of doubles:
List<double> samples = [];
Uint8List samplesBytes = Uint8List(2);
int sample = 0;
for (int a = 0 ; a < bytes.length - 2 ; a += 2)
{
samplesBytes[0] = bytes.sublist(a + 1, a + 2)[0];
samplesBytes[1] = bytes.sublist(a, a + 1)[0];
sample = ByteData.view(samplesBytes.buffer).getInt16(0);
samples.add(sample / 32768.0);
}
return samples;
}
Here is how I run that function without compute
(it takes 5-6 seconds to run, but it freezes the user interface):
List<double> samples = await extractWavSamples(myFileWav);
Here is how I run that function with compute
(it takes about 20 seconds to run):
List<double> samples = await compute(extractWavSamples, myFileWav);
And I found out that, because the extractWavSamples()
function returns a big list, that's why the code takes a way too long time to run. If I make it return an empty list, it's much faster.
Tha thing is: I need that list in the main thread, so I can display it (it allows me to display the waveform of the audio file).
So what should I fix to make the code work in a reasonable amount of time?
Thanks.