Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Audio Resampling samples array

Discussion in 'Audio & Video' started by video21region, Apr 5, 2020.

  1. video21region

    video21region

    Joined:
    Sep 5, 2019
    Posts:
    10
    Hi. How I can resampling my samples array in realtime? My audio file has 44100 sample rate, but Unity uses 48000 sample rate by default. I khow that I can use OnAudioFilterRead method. But I don't know which formulas I need used.

    Code (CSharp):
    1. private void OnAudioFilterRead(float[] data, int channels)
    2.     {
    3.         if (playing == false) return;
    4.  
    5.         for (var i = 0; i < data.Length; i++)
    6.         {
    7.             if (lastId > audioData.SampleData.Length - 1)
    8.             {
    9.                 lastId = 0;
    10.             }
    11.      
    12.             data[i] = audioData.SampleData[lastId];
    13.             lastId++;
    14.         }
    15.     }
     
  2. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    I'm also running into a similar issue. If anyone finds the solution, please post. If I find a solution, I will repost here.
     
  3. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    266
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    290
    Resampling can be quite straightforward of you are going for a simple linear interpolation. The idea is basically to measure where (in time) the resampled value lands between two original samples and to set its value proportionally. Here's a likely valid ChatGPT generated example:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3.  
    4. public class AudioResampler
    5. {
    6.     public static float[] Resample(float[] inputBuffer, int inputSampleRate, int outputSampleRate)
    7.     {
    8.         double sampleRateRatio = (double)outputSampleRate / inputSampleRate;
    9.         int outputBufferLength = (int)(inputBuffer.Length * sampleRateRatio);
    10.  
    11.         float[] outputBuffer = new float[outputBufferLength];
    12.  
    13.         for (int i = 0; i < outputBufferLength; i++)
    14.         {
    15.             double position = i / sampleRateRatio;
    16.             int leftIndex = (int)Math.Floor(position);
    17.             int rightIndex = leftIndex + 1;
    18.  
    19.             double fraction = position - leftIndex;
    20.  
    21.             if (rightIndex >= inputBuffer.Length)
    22.             {
    23.                 outputBuffer[i] = inputBuffer[leftIndex];
    24.             }
    25.             else
    26.             {
    27.                 outputBuffer[i] = (float)(inputBuffer[leftIndex] * (1 - fraction) + inputBuffer[rightIndex] * fraction);
    28.             }
    29.         }
    30.  
    31.         return outputBuffer;
    32.     }
    33. }
    More elaborate algorithms would take into account a few samples before or after to make things smoother and more reliable, but this is a good start. There is plenty of interesting resources if you Google "audio resampling" or "sample rate conversion"

    Cheers!