Play a waveform (floating point array) as sound

I have a waveform represented as a floating point array from -1 to 1. Can this waveform be reproduced as a repeating sound?

I found many examples of reproducing sound from an array, but all of them relate to byte arrays and require very complex code.

+7
source share
2 answers

Without knowing this, why don't you just assign a range to the values ​​and reproduce this tonal range by tone.

-1 ... 1 50Hz ... 20,000Hz 

You can easily calculate it as follows:

 //input is the float array int minPitch = 50; int maxPitch = 20000; int pitch = (int)((input[idx] + 1) * ((maxPitch - minPitch) / 2) + minPitch); 

This will give you the value step in the array.

+1
source

Assuming your float array contains PCM data and you want to play it in 8-bit mode, it is easy to convert it to a byte array:

  int off=(signed!=0 ? 0 : 128); for(int i=0; i<samples; i++){ val=(int)(pcm[i]*128. + 0.5); if(val>127) val=127; else if(val<-128) val=-128; buffer[index++]=(byte)(val+off); } } 

This code is a slightly modified code from JOrbis, here pcm is your float array, and buffer is an array of bytes.

+1
source

All Articles