Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I get data from a single channel of Audioclip

Discussion in 'Audio & Video' started by Reguluz, Dec 26, 2020.

  1. Reguluz

    Reguluz

    Joined:
    Nov 9, 2018
    Posts:
    5
    Some times before, I was trying to visualize the waveform of an audioclip in the scene. However the data will be too large to display totally in one time. So I used Audioclip.GetData to get a short sample clips by calculating the total samples( sample rates multiply the time clip I will shown).

    Code (CSharp):
    1. float currentTime = _audioSource.time;
    2. int currentSample = (int)(currentTime * 44100);
    3. int sampleClipLength = Mathf.Min(_samples.Length - currentSample, 44100);
    4. List<float> totalSampleList = new List<float>(_samples);
    5. List<float> samplesClip = new List<float>();
    6. samplesClip = totalSampleList.GetRange((int)(currentTime * 44100), sampleClipLength);
    I have used these code to get one second samples of a 44100hz stereo audioclips after current time.

    And I found that the waveform shows incorrectly(seems that it has been wrong scaled). After setting the audio to open "Force to mono" operation( the song is stereo before), the peak seems to be correctly matching the audio on play. It's so clearly that I've got the wrong sample clips.
    How could I get samples from a single channel so that I don't need to open "Force to mono" operation and still shows the waveform correctly?