Commit fc61fe54 authored by Jan Brouwer's avatar Jan Brouwer
Browse files

add script text_to_binary_payload.py to read a text-based configuration file...

add script text_to_binary_payload.py to read a text-based configuration file and generate the equivalent binary payload
parent b060ebc2
Loading
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -31,13 +31,13 @@
"""

#
#   Generate binary render configuration output files for testing purposes
#   The binary code generation is based on the MPEG-I audio standard
#   which defines functions to decode raw bitstream into internal parameters
#   Generate binary render configuration output files for testing purposes.
#   The binary code generation is based on the MPEG-I audio standard,
#   which defines functions to decode raw bitstream into internal parameters.
#


from bitarray import bitarray, test as bitarray_test
from bitarray import bitarray
import math
from enum import Enum
import numpy as np
@@ -238,6 +238,11 @@ class fgdMethod(Enum):
    Default_Banding        = '10'


def get_default_grid_nr_bands(code):
    assert 0 <= code <= 8
    return [10, 10, 31, 5, 6, 3, 41, 21, 25][code]


def get_distance_code(distance, isSmallScene = True):
    # 0, 1, ... 99
    metersCode = [
+151 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

"""
   (C) 2022-2023 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.
"""

#
#   Read a text-based configuration file and generate the equivalent binary payload.
#   Makes use of the pyhton configuration file parser configparser,
#   and of the payload configuration functions in generate_acoustic_environments_metadata.
#   The configuration file format is as follows:
#
#   [<section>:<index>]
#   <option>=<value>
#   # a comment
#
#   <section> is one of the supported section names (frequencyGrid, acousticEnvironment).
#   <index> is an integer used as acoustic environment ID (revAcEnvID), and to refer to (revFreqGridIdx).
#   There is no leading white space for sections and options, and no trailing ';'.


from bitarray import bitarray
from configparser import ConfigParser
import ast
from generate_acoustic_environments_metadata import *


# convert text containing an option value to a Python value, if possible
def eval_option(text):
    try:
        value = ast.literal_eval(text)
    except ValueError:
        value = text
    return value


def get_bool_code(b):
    assert(isinstance(b, bool))
    return format(b, '01b')


def parse_reverb_text_configuration_and_generate_binary_payload(file):

    # parse file
    config = ConfigParser()
    files_parsed = config.read(file)
    assert len(files_parsed) == 1, 'file {} not successfully parsed'.format(file)

    # collect dicts of frequency grid and acoustic environment sections
    sections = { key : {} for key in ['frequencyGrid', 'acousticEnvironment' ] }
    for section_name in config.sections():
        section, index = section_name.split(':')
        assert section in sections, 'unknown section name'
        sections[section][index] = config[section_name]

    # parse frequency grids
    nr_bands = []
    data = bitarray(
          '1'                                                                       # hasAcEnv
        + get_count_or_index_code(len(sections['frequencyGrid'])),                  # fgdNrGrids
        endian='big')
    for _, fg in sections['frequencyGrid'].items():
        if fg['method'] == 'individualFrequencies':
            nr_bands.append(len(eval_option(fg['frequencies'])))
            data += bitarray(
                  fgdMethod.Individual_Frequencies.value                            # fgdMethod
                + get_count_or_index_code(len(eval_option(fg['frequencies'])))      # fgdNrBands
                + concatenate(get_frequency_code, eval_option(fg['frequencies'])))  # fgdCenterFreq
        elif fg['method'] == 'startHopAmount':
            nr_bands.append(eval_option(fg['nrBands']))
            data += bitarray(
                  fgdMethod.Start_Hop_Amount.value                                  # fgdMethod
                + get_count_or_index_code(eval_option(fg['nrBands']))               # fgdNrBands
                + get_frequency_code(eval_option(fg['centerFrequency']))            # fgdCenterFreq
                + get_frequency_hop_code(eval_option(fg['hop'])))                   # frequencyHop
        elif fg['method'] == 'defaultBanding':
            nr_bands.append(get_default_grid_nr_bands(eval_option(fg['defaultGrid'])))
            data += bitarray(
                  fgdMethod.Default_Banding.value                                   # fgdMethod
                + format(eval_option(fg['defaultGrid']), '04b')                     # fgdDefaultGrid
                + get_bool_code('defaultGridOffset' in fg))                         # fgdIsSubGrid
            if 'defaultGridOffset' in fg:
                data += bitarray(
                      format(eval_option(fg['defaultGridOffset']), '03b')           # fgdDefaultGridOffset   ## only 3 bits used iso 6 (to offset into max 41 bands), correct?
                    + format(eval_option(fg['defaultGridNrBands']), '06b'))         # fgdDefaultGridNrBands
        else:
            assert False, 'unknow frequency grid method'

    # parse acoustic environments
    for index, ae in sections['acousticEnvironment'].items():
        data += bitarray(
              get_count_or_index_code(len(sections['acousticEnvironment']))         # revNrElements 
            + get_id_code(eval_option(index))                                       # revAcEnvID 
            + get_count_or_index_code(eval_option(ae['frequencyGridIndex']))        # revFreqGridIdx
            + get_duration_code(eval_option(ae['predelay']))                        # revPredelay
            + concatenate(get_duration_code, eval_option(ae['rt60']))               # revRT60
            + concatenate(get_dsr_code, eval_option(ae['dsr']))                     # revDSR
            + get_bool_code('earlyReflectionsSize' in ae))                          # hasEarlyReflections
        if 'earlyReflectionsSize' in ae:
            assert len(eval_option(ae['absorptionCoefficients'])) == nr_bands[eval_option(ae['frequencyGridIndex'])] * 6, 'wrong number of absorption coefficients'
            data += bitarray(
                  concatenate(lambda d : get_distance_code(d, True), eval_option(ae['earlyReflectionsSize']))  # erSize
                + get_count_or_index_code(eval_option(ae['earlyReflectionsfrequencyGridIndex']))               # erFreqGridIdx
                + concatenate(get_absorption_code, eval_option(ae['absorptionCoefficients']))                  # erAbsCoeff
                + get_bool_code('listenerOrigin' in ae))                                                       # hasListenerOrigin
            if 'listenerOrigin' in ae:
                xyz = eval_option(ae['listenerOrigin'])
                assert len(xyz) == 3, 'wrong number of listener origin coordinates'
                data += bitarray(
                      '1' if xyz[0] >= 0 else '0'                                   # isPositiveX
                    + '1' if xyz[1] >= 0 else '0'                                   # isPositiveY
                    + concatenate(get_distance_code, xyz))                          # erListenerOrigin

    # generate binary file
    data.tofile(open(file.split('.')[0] + '.dat', 'wb'))


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='Reads a text-based configuration file and generates the equivalent binary payload file (<file>.dat)')
    parser.add_argument("configuration_file")
    args = parser.parse_args()
    parse_reverb_text_configuration_and_generate_binary_payload(args.configuration_file)

    print("\nNote: the conversion algorithm uses quantization, which may lead to quantization errors.")