Commit 0e1ee7cd authored by Dominik Weckbecker's avatar Dominik Weckbecker 💬
Browse files

add standalone ambisonics converter program ambi_converter.c

parent 123f3f51
Loading
Loading
Loading
Loading
Loading

apps/ambi_converter.c

0 → 100644
+110 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2024 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "tinywavein_c.h"
#include "tinywaveout_c.h"
#include "ambi_convert.h"

#define L_FRAME48k 960
#define MAX_CHANNELS 16

int main(int argc, char* argv[])
{

    WAVEFILEIN *wavFile_in;
    WAVEFILEOUT *wavFile_out;
    char* fileName_in, *fileName_out;
   
    uint32_t samplingRate;
    uint32_t samplesInFile;
    uint32_t numSamples = L_FRAME48k;
    uint32_t numSamplesRead32 = 0;
    
    int16_t bps;
    int16_t samples[L_FRAME48k*MAX_CHANNELS];
    int16_t order = 0;
    int16_t numChannels;

    AMBI_FMT in_format, out_format;

    float samples_f_in[L_FRAME48k*MAX_CHANNELS];
    float samples_f_out[L_FRAME48k*MAX_CHANNELS];
    float *in[MAX_CHANNELS], *out[MAX_CHANNELS];

    for(int16_t j=0;j<MAX_CHANNELS;j++)
    {
        in[j]=&samples_f_in[j*L_FRAME48k];
        out[j]=&samples_f_out[j*L_FRAME48k];
    }

    fileName_in = argv[1];
    fileName_out = argv[2];
    in_format = atoi(argv[3]);
    out_format = atoi(argv[4]);

    wavFile_in  = OpenWav( fileName_in, &samplingRate, &numChannels, &samplesInFile, &bps );
    wavFile_out = CreateWav( fileName_out, samplingRate, numChannels, 16 );

    order = (int16_t)sqrtf(numChannels) - 1;
    assert( order > 0 && order <=3 );

    while ( ReadWavShort( wavFile_in, samples, numSamples, &numSamplesRead32 ) != __TWI_SUCCESS )
    {
        int16_t err = 0;
        for (int16_t i=0;i<numSamples;i++)
        {
            for(int16_t j=0;j<numChannels;j++)
            {
                in[i][j]= (float) samples[i*numChannels + j];
            }
        }

        err = convert_ambi_format( in, out, order, in_format, out_format );
        
        for (int16_t i=0;i<numSamples;i++)
        {
            for(int16_t j=0;j<numChannels;j++)
            {
                samples[i*numChannels + j]= (int16_t) out[j][i];
            }
        }

        err = WriteWavShort( wavFile_out, samples, numSamples );
    }

    CloseWav( wavFile_out );
    CloseWavIn( wavFile_in );

    return 0;
}