Home > Software engineering >  How to raises or lowers the pitch of an audio file (.wav format) without using external libraries?
How to raises or lowers the pitch of an audio file (.wav format) without using external libraries?

Time:11-07

I want to raises or lowers the pitch of an audio file (.wav format) without using external libraries. Could everyone suggest to me some solution? Many thanks.

CodePudding user response:

The basic steps are as follows:

  1. Convert the audio data to PCM (I prefer using signed normalized floats).

  2. Create a cursor variable that will advance through the PCM array. At each cursor location you will take that PCM and place it in an array for playback.

  3. The increment by which the cursor advances will determine the new playback rate. Examples: if you simply iterate by one through the PCM array, the new PCM array will have the same data and will play back at the same rate. If you increment by 2, the new PCM array will have half the data points and play at twice the rate. If you advance by 1.5, the new PCM array will be 1.5 X's faster than the original.

  4. Use linear interpolation when your cursor lands in between two data points. Example: if your cursor increments by 1.75, and the first three data points on the PCM are 0, 0.2, 0.5, your second "new PCM array" value will be derived from calculating a point in between the [1] and [2] PCM array values of 0.25 and 0.4. (I am counting the array as starting at [0].) In this example, the new PCM value would be the following:

    newPCM = (PCM[round(cursor)] * (1 - decimalPartOfCursor))   PCM[round(cursor)   1] * (decimalPartOfCursor)
    

    or

    newPCM = (0.25 * 0.2)   (0.75 * 0.5)
    
  5. Convert the new PCM array back to a byte stream for playback.

CodePudding user response:

You can play it faster to get a higher pitch. But if you want to keep the same playback speed and change the pitch, this is MUCH more complicated and you need an "Alvin and the Chipmunks" like sound processing algorithm.

https://en.wikipedia.org/wiki/Audio_time_stretching_and_pitch_scaling

  • Related