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

Native Audio/C++ - UploadSample Loading Different Values for the Same Audio Data

Discussion in 'Audio & Video' started by krkumar11, Jul 23, 2015.

  1. krkumar11

    krkumar11

    Joined:
    Jun 26, 2015
    Posts:
    4
    I'm trying to use a copy of the demo convolution reverb plugin code to upload several head-related transfer functions (HRTFs, i.e. very short .wav files) to create a better sense of audio spatialization. I'm using the built-in (in the Native Audio SDK demo) ConvolutionReverb_UploadSample function (see below) to load audio buffers as impulse responses, but the audio content/waveform changes every time I upload the same audio buffer.

    For example, when I try to upload an impulse response recorded at a certain location (5 degrees azimuth in this case), sometimes I get this:

    5aOne.png

    ...other times I get this:

    5aTwo.png

    ...and other times I get this:

    5aThree.png

    It's the exact same audio file, and this same thing happens even if I try loading sample buffers that I hardcode rather than uploading, so I'm fairly confident that my file upload process is not the culprit.

    This is the function I'm using to upload files. It's already included in the original file in the SDK - I did not modify anything within it. It looks fine, and I even tried manually setting some of the necessary data rather than using this function, but to no avail.

    Code (C++):
    1. extern "C" UNITY_AUDIODSP_EXPORT_API bool ConvolutionReverbCopy_UploadSample(int index, float* data, int numsamples, int numchannels, int samplerate, const char* name)
    2. {
    3.     if (index < 0 || index >= ConvolutionReverbCopy::MAXSAMPLE)
    4.         return false;
    5.     MutexScopeLock mutexScope(ConvolutionReverbCopy::sampleMutex);
    6.     ConvolutionReverbCopy::IRSample& s = ConvolutionReverbCopy::GetIRSample(index);
    7.     if (s.allocated)
    8.         delete[] s.data;
    9.     int num = numsamples * numchannels;
    10.     if (num > 0)
    11.     {
    12.         s.data = new float[num];
    13.         s.allocated = 1;
    14.         strcpy_s(s.name, name);
    15.         memcpy(s.data, data, numsamples * numchannels * sizeof(float));
    16.     }
    17.     else
    18.     {
    19.         s.data = NULL;
    20.         s.allocated = 1;
    21.     }
    22.     s.numsamples = numsamples;
    23.     s.numchannels = numchannels;
    24.     s.samplerate = samplerate;
    25.     s.updatecount = ++ConvolutionReverbCopy::globalupdatecount;
    26.     return true;
    27. }

    Summary: Can anyone help me identify why the audio content is manipulated differently every time I upload the same file?
     
  2. krkumar11

    krkumar11

    Joined:
    Jun 26, 2015
    Posts:
    4
    FWIW, I found out that I was calling the UploadSample function incorrectly for audio files with more than one channel. In short, the numsamples parameter should probably be called numframes since that's what it's really looking for. E.g. when trying to upload a 2-channel audio file with 256 samples in the left channel and 256 in the right (i.e., 256 frames), I passed in 512 (numframes * numchannels) instead of 256. Once I changed how I called the function, everything uploaded just fine.
     
  3. stefanm140

    stefanm140

    Joined:
    Aug 31, 2015
    Posts:
    5
    Hi, I was just wondering if you could share how you uploaded your own impulse response into the convolution reverb plugin. I have no experience with C++ and could really use this for a project I am working on. I understand that the necessary variables need to be passed onto ConvolutionReverb_UploadSample in the CreateCallback function, yet I don't know how to simply read a wave file and get its number of samples, channels etc. Any help would be strongly appreciated.
     
  4. stefanm140

    stefanm140

    Joined:
    Aug 31, 2015
    Posts:
    5
    Sorry.. I just found out that there is a C# Script in Assets/Scripts called "ConvolutionReverbUploadIR" that basically does it for you.