SKKbySSK / coast_audio

Real-Time audio processing library written in Dart.
MIT License
89 stars 11 forks source link

How to convert SampleFormat.int16 to SampleFormat.float32 suitable for FFT #18

Closed ekuleshov closed 4 months ago

ekuleshov commented 9 months ago

I'm truing to add an FftNode to the audio_graph_demo.dart demo app.

So, created the FftNode like below and then linked it in the graphNode between mixerNode and masterVolumeNode.

final fftNode = FftNode(
    format: format, fftSize: 512,
    onFftCompleted: (FftResult result) {
      print(result.complexArray.magnitudes().toList().join(', '));
    });

With that it is giving me all NaN values for FFT magnitudes.

If I change the sampleFormat: SampleFormat.float32 to be used to all FunctionNodes - then FFT works, but it fail with an "unsupported sample format" error in the WavAudioEncoder.

The WavAudioDecoder also usually produces the SampleFormat.int16 output and I can't figure out how to convert that data into the SampleFormat.float32 that FftNode can work with.

Could you please advise how to make the such conversion and to integrate FftNode with int16 audio buffers and WavAudioDecoder. Thank you in advance.

PS: the MabAudioDecoder produces SampleFormat.float32 and it seem to work with the FftNode out of the box, but it seems an overkill for a simple audio processing.

aveia commented 7 months ago

v0.0.4 of coast_audio_miniaudio includes MabAudioConverterNode+MabAudioConverter, which take input and output formats as arguments and perform the conversion.

ekuleshov commented 7 months ago

v0.0.4 of coast_audio_miniaudio includes MabAudioConverterNode+MabAudioConverter, which take input and output formats as arguments and perform the conversion.

I know thatMabAudioConverter does work. However, for my needs to simply decode and process wav files, it is excessive and also brings in additional native code.

But if you use the WavAudioDecoder as the audio data source, it produces data that is not compatible with the FftNode and few other processing nodes.

ekuleshov commented 6 months ago

@SKKbySSK still wasn't been able to resolve this. Do you have any idea if the output of WavAudioDecoder could be transformed to work with the FftNode and other components that expect the float values as an input?

Thank you in advance.

SKKbySSK commented 6 months ago

@ekuleshov WavAudioDecoder produces original samples of the wave data. It is usually int16 format. You can verify the format by printing WavAudioDecoder.outputFormat.sampleFormat.

To convert int16 samples to float32 samples. apply this calculation to each sample

final convertedSample = sample / SampleFormat.int16.max

By the way, int16 consists of 16 bits(2 bytes) and float32 consists of 32 bits(4 bytes). So you need to calculate audio buffer size carefully.

SKKbySSK commented 6 months ago

As @aveia stated above, MabAudioConverter should be simpler and the performance should be better.

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 4 months ago

This issue was closed because it has been inactive for 7 days since being marked as stale.