Home | History | Annotate | Download | only in hardware
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 #ifndef ANDROID_AUDIO_EFFECT_H
     19 #define ANDROID_AUDIO_EFFECT_H
     20 
     21 #include <errno.h>
     22 #include <stdint.h>
     23 #include <strings.h>
     24 #include <sys/cdefs.h>
     25 #include <sys/types.h>
     26 
     27 #include <cutils/bitops.h>
     28 
     29 #include <system/audio.h>
     30 
     31 
     32 __BEGIN_DECLS
     33 
     34 
     35 /////////////////////////////////////////////////
     36 //      Common Definitions
     37 /////////////////////////////////////////////////
     38 
     39 //
     40 //--- Effect descriptor structure effect_descriptor_t
     41 //
     42 
     43 // Unique effect ID (can be generated from the following site:
     44 //  http://www.itu.int/ITU-T/asn1/uuid.html)
     45 // This format is used for both "type" and "uuid" fields of the effect descriptor structure.
     46 // - When used for effect type and the engine is implementing and effect corresponding to a standard
     47 // OpenSL ES interface, this ID must be the one defined in OpenSLES_IID.h for that interface.
     48 // - When used as uuid, it should be a unique UUID for this particular implementation.
     49 typedef struct effect_uuid_s {
     50     uint32_t timeLow;
     51     uint16_t timeMid;
     52     uint16_t timeHiAndVersion;
     53     uint16_t clockSeq;
     54     uint8_t node[6];
     55 } effect_uuid_t;
     56 
     57 // Maximum length of character strings in structures defines by this API.
     58 #define EFFECT_STRING_LEN_MAX 64
     59 
     60 // NULL UUID definition (matches SL_IID_NULL_)
     61 #define EFFECT_UUID_INITIALIZER { 0xec7178ec, 0xe5e1, 0x4432, 0xa3f4, \
     62                                   { 0x46, 0x57, 0xe6, 0x79, 0x52, 0x10 } }
     63 static const effect_uuid_t EFFECT_UUID_NULL_ = EFFECT_UUID_INITIALIZER;
     64 const effect_uuid_t * const EFFECT_UUID_NULL = &EFFECT_UUID_NULL_;
     65 const char * const EFFECT_UUID_NULL_STR = "ec7178ec-e5e1-4432-a3f4-4657e6795210";
     66 
     67 // The effect descriptor contains necessary information to facilitate the enumeration of the effect
     68 // engines present in a library.
     69 typedef struct effect_descriptor_s {
     70     effect_uuid_t type;     // UUID of to the OpenSL ES interface implemented by this effect
     71     effect_uuid_t uuid;     // UUID for this particular implementation
     72     uint32_t apiVersion;    // Version of the effect control API implemented
     73     uint32_t flags;         // effect engine capabilities/requirements flags (see below)
     74     uint16_t cpuLoad;       // CPU load indication (see below)
     75     uint16_t memoryUsage;   // Data Memory usage (see below)
     76     char    name[EFFECT_STRING_LEN_MAX];   // human readable effect name
     77     char    implementor[EFFECT_STRING_LEN_MAX];    // human readable effect implementor name
     78 } effect_descriptor_t;
     79 
     80 // CPU load and memory usage indication: each effect implementation must provide an indication of
     81 // its CPU and memory usage for the audio effect framework to limit the number of effects
     82 // instantiated at a given time on a given platform.
     83 // The CPU load is expressed in 0.1 MIPS units as estimated on an ARM9E core (ARMv5TE) with 0 WS.
     84 // The memory usage is expressed in KB and includes only dynamically allocated memory
     85 
     86 // Definitions for flags field of effect descriptor.
     87 //  +---------------------------+-----------+-----------------------------------
     88 //  | description               | bits      | values
     89 //  +---------------------------+-----------+-----------------------------------
     90 //  | connection mode           | 0..2      | 0 insert: after track process
     91 //  |                           |           | 1 auxiliary: connect to track auxiliary
     92 //  |                           |           |  output and use send level
     93 //  |                           |           | 2 replace: replaces track process function;
     94 //  |                           |           |   must implement SRC, volume and mono to stereo.
     95 //  |                           |           | 3 pre processing: applied below audio HAL on input
     96 //  |                           |           | 4 post processing: applied below audio HAL on output
     97 //  |                           |           | 5 - 7 reserved
     98 //  +---------------------------+-----------+-----------------------------------
     99 //  | insertion preference      | 3..5      | 0 none
    100 //  |                           |           | 1 first of the chain
    101 //  |                           |           | 2 last of the chain
    102 //  |                           |           | 3 exclusive (only effect in the insert chain)
    103 //  |                           |           | 4..7 reserved
    104 //  +---------------------------+-----------+-----------------------------------
    105 //  | Volume management         | 6..8      | 0 none
    106 //  |                           |           | 1 implements volume control
    107 //  |                           |           | 2 requires volume indication
    108 //  |                           |           | 4 reserved
    109 //  +---------------------------+-----------+-----------------------------------
    110 //  | Device indication         | 9..11     | 0 none
    111 //  |                           |           | 1 requires device updates
    112 //  |                           |           | 2, 4 reserved
    113 //  +---------------------------+-----------+-----------------------------------
    114 //  | Sample input mode         | 12..13    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
    115 //  |                           |           |   command must specify a buffer descriptor
    116 //  |                           |           | 2 provider: process() function uses the
    117 //  |                           |           |   bufferProvider indicated by the
    118 //  |                           |           |   EFFECT_CMD_CONFIGURE command to request input.
    119 //  |                           |           |   buffers.
    120 //  |                           |           | 3 both: both input modes are supported
    121 //  +---------------------------+-----------+-----------------------------------
    122 //  | Sample output mode        | 14..15    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
    123 //  |                           |           |   command must specify a buffer descriptor
    124 //  |                           |           | 2 provider: process() function uses the
    125 //  |                           |           |   bufferProvider indicated by the
    126 //  |                           |           |   EFFECT_CMD_CONFIGURE command to request output
    127 //  |                           |           |   buffers.
    128 //  |                           |           | 3 both: both output modes are supported
    129 //  +---------------------------+-----------+-----------------------------------
    130 //  | Hardware acceleration     | 16..17    | 0 No hardware acceleration
    131 //  |                           |           | 1 non tunneled hw acceleration: the process() function
    132 //  |                           |           |   reads the samples, send them to HW accelerated
    133 //  |                           |           |   effect processor, reads back the processed samples
    134 //  |                           |           |   and returns them to the output buffer.
    135 //  |                           |           | 2 tunneled hw acceleration: the process() function is
    136 //  |                           |           |   transparent. The effect interface is only used to
    137 //  |                           |           |   control the effect engine. This mode is relevant for
    138 //  |                           |           |   global effects actually applied by the audio
    139 //  |                           |           |   hardware on the output stream.
    140 //  +---------------------------+-----------+-----------------------------------
    141 //  | Audio Mode indication     | 18..19    | 0 none
    142 //  |                           |           | 1 requires audio mode updates
    143 //  |                           |           | 2..3 reserved
    144 //  +---------------------------+-----------+-----------------------------------
    145 
    146 // Insert mode
    147 #define EFFECT_FLAG_TYPE_SHIFT          0
    148 #define EFFECT_FLAG_TYPE_SIZE           3
    149 #define EFFECT_FLAG_TYPE_MASK           (((1 << EFFECT_FLAG_TYPE_SIZE) -1) \
    150                                             << EFFECT_FLAG_TYPE_SHIFT)
    151 #define EFFECT_FLAG_TYPE_INSERT         (0 << EFFECT_FLAG_TYPE_SHIFT)
    152 #define EFFECT_FLAG_TYPE_AUXILIARY      (1 << EFFECT_FLAG_TYPE_SHIFT)
    153 #define EFFECT_FLAG_TYPE_REPLACE        (2 << EFFECT_FLAG_TYPE_SHIFT)
    154 #define EFFECT_FLAG_TYPE_PRE_PROC       (3 << EFFECT_FLAG_TYPE_SHIFT)
    155 #define EFFECT_FLAG_TYPE_POST_PROC      (4 << EFFECT_FLAG_TYPE_SHIFT)
    156 
    157 // Insert preference
    158 #define EFFECT_FLAG_INSERT_SHIFT        (EFFECT_FLAG_TYPE_SHIFT + EFFECT_FLAG_TYPE_SIZE)
    159 #define EFFECT_FLAG_INSERT_SIZE         3
    160 #define EFFECT_FLAG_INSERT_MASK         (((1 << EFFECT_FLAG_INSERT_SIZE) -1) \
    161                                             << EFFECT_FLAG_INSERT_SHIFT)
    162 #define EFFECT_FLAG_INSERT_ANY          (0 << EFFECT_FLAG_INSERT_SHIFT)
    163 #define EFFECT_FLAG_INSERT_FIRST        (1 << EFFECT_FLAG_INSERT_SHIFT)
    164 #define EFFECT_FLAG_INSERT_LAST         (2 << EFFECT_FLAG_INSERT_SHIFT)
    165 #define EFFECT_FLAG_INSERT_EXCLUSIVE    (3 << EFFECT_FLAG_INSERT_SHIFT)
    166 
    167 
    168 // Volume control
    169 #define EFFECT_FLAG_VOLUME_SHIFT        (EFFECT_FLAG_INSERT_SHIFT + EFFECT_FLAG_INSERT_SIZE)
    170 #define EFFECT_FLAG_VOLUME_SIZE         3
    171 #define EFFECT_FLAG_VOLUME_MASK         (((1 << EFFECT_FLAG_VOLUME_SIZE) -1) \
    172                                             << EFFECT_FLAG_VOLUME_SHIFT)
    173 #define EFFECT_FLAG_VOLUME_CTRL         (1 << EFFECT_FLAG_VOLUME_SHIFT)
    174 #define EFFECT_FLAG_VOLUME_IND          (2 << EFFECT_FLAG_VOLUME_SHIFT)
    175 #define EFFECT_FLAG_VOLUME_NONE         (0 << EFFECT_FLAG_VOLUME_SHIFT)
    176 
    177 // Device indication
    178 #define EFFECT_FLAG_DEVICE_SHIFT        (EFFECT_FLAG_VOLUME_SHIFT + EFFECT_FLAG_VOLUME_SIZE)
    179 #define EFFECT_FLAG_DEVICE_SIZE         3
    180 #define EFFECT_FLAG_DEVICE_MASK         (((1 << EFFECT_FLAG_DEVICE_SIZE) -1) \
    181                                             << EFFECT_FLAG_DEVICE_SHIFT)
    182 #define EFFECT_FLAG_DEVICE_IND          (1 << EFFECT_FLAG_DEVICE_SHIFT)
    183 #define EFFECT_FLAG_DEVICE_NONE         (0 << EFFECT_FLAG_DEVICE_SHIFT)
    184 
    185 // Sample input modes
    186 #define EFFECT_FLAG_INPUT_SHIFT         (EFFECT_FLAG_DEVICE_SHIFT + EFFECT_FLAG_DEVICE_SIZE)
    187 #define EFFECT_FLAG_INPUT_SIZE          2
    188 #define EFFECT_FLAG_INPUT_MASK          (((1 << EFFECT_FLAG_INPUT_SIZE) -1) \
    189                                             << EFFECT_FLAG_INPUT_SHIFT)
    190 #define EFFECT_FLAG_INPUT_DIRECT        (1 << EFFECT_FLAG_INPUT_SHIFT)
    191 #define EFFECT_FLAG_INPUT_PROVIDER      (2 << EFFECT_FLAG_INPUT_SHIFT)
    192 #define EFFECT_FLAG_INPUT_BOTH          (3 << EFFECT_FLAG_INPUT_SHIFT)
    193 
    194 // Sample output modes
    195 #define EFFECT_FLAG_OUTPUT_SHIFT        (EFFECT_FLAG_INPUT_SHIFT + EFFECT_FLAG_INPUT_SIZE)
    196 #define EFFECT_FLAG_OUTPUT_SIZE         2
    197 #define EFFECT_FLAG_OUTPUT_MASK         (((1 << EFFECT_FLAG_OUTPUT_SIZE) -1) \
    198                                             << EFFECT_FLAG_OUTPUT_SHIFT)
    199 #define EFFECT_FLAG_OUTPUT_DIRECT       (1 << EFFECT_FLAG_OUTPUT_SHIFT)
    200 #define EFFECT_FLAG_OUTPUT_PROVIDER     (2 << EFFECT_FLAG_OUTPUT_SHIFT)
    201 #define EFFECT_FLAG_OUTPUT_BOTH         (3 << EFFECT_FLAG_OUTPUT_SHIFT)
    202 
    203 // Hardware acceleration mode
    204 #define EFFECT_FLAG_HW_ACC_SHIFT        (EFFECT_FLAG_OUTPUT_SHIFT + EFFECT_FLAG_OUTPUT_SIZE)
    205 #define EFFECT_FLAG_HW_ACC_SIZE         2
    206 #define EFFECT_FLAG_HW_ACC_MASK         (((1 << EFFECT_FLAG_HW_ACC_SIZE) -1) \
    207                                             << EFFECT_FLAG_HW_ACC_SHIFT)
    208 #define EFFECT_FLAG_HW_ACC_SIMPLE       (1 << EFFECT_FLAG_HW_ACC_SHIFT)
    209 #define EFFECT_FLAG_HW_ACC_TUNNEL       (2 << EFFECT_FLAG_HW_ACC_SHIFT)
    210 
    211 // Audio mode indication
    212 #define EFFECT_FLAG_AUDIO_MODE_SHIFT    (EFFECT_FLAG_HW_ACC_SHIFT + EFFECT_FLAG_HW_ACC_SIZE)
    213 #define EFFECT_FLAG_AUDIO_MODE_SIZE     2
    214 #define EFFECT_FLAG_AUDIO_MODE_MASK     (((1 << EFFECT_FLAG_AUDIO_MODE_SIZE) -1) \
    215                                             << EFFECT_FLAG_AUDIO_MODE_SHIFT)
    216 #define EFFECT_FLAG_AUDIO_MODE_IND      (1 << EFFECT_FLAG_AUDIO_MODE_SHIFT)
    217 #define EFFECT_FLAG_AUDIO_MODE_NONE     (0 << EFFECT_FLAG_AUDIO_MODE_SHIFT)
    218 
    219 
    220 #define EFFECT_MAKE_API_VERSION(M, m)  (((M)<<16) | ((m) & 0xFFFF))
    221 #define EFFECT_API_VERSION_MAJOR(v)    ((v)>>16)
    222 #define EFFECT_API_VERSION_MINOR(v)    ((m) & 0xFFFF)
    223 
    224 
    225 
    226 /////////////////////////////////////////////////
    227 //      Effect control interface
    228 /////////////////////////////////////////////////
    229 
    230 // Effect control interface version 2.0
    231 #define EFFECT_CONTROL_API_VERSION EFFECT_MAKE_API_VERSION(2,0)
    232 
    233 // Effect control interface structure: effect_interface_s
    234 // The effect control interface is exposed by each effect engine implementation. It consists of
    235 // a set of functions controlling the configuration, activation and process of the engine.
    236 // The functions are grouped in a structure of type effect_interface_s.
    237 //
    238 // Effect control interface handle: effect_handle_t
    239 // The effect_handle_t serves two purposes regarding the implementation of the effect engine:
    240 // - 1 it is the address of a pointer to an effect_interface_s structure where the functions
    241 // of the effect control API for a particular effect are located.
    242 // - 2 it is the address of the context of a particular effect instance.
    243 // A typical implementation in the effect library would define a structure as follows:
    244 // struct effect_module_s {
    245 //        const struct effect_interface_s *itfe;
    246 //        effect_config_t config;
    247 //        effect_context_t context;
    248 // }
    249 // The implementation of EffectCreate() function would then allocate a structure of this
    250 // type and return its address as effect_handle_t
    251 typedef struct effect_interface_s **effect_handle_t;
    252 
    253 
    254 // Forward definition of type audio_buffer_t
    255 typedef struct audio_buffer_s audio_buffer_t;
    256 
    257 
    258 
    259 
    260 
    261 
    262 // Effect control interface definition
    263 struct effect_interface_s {
    264     ////////////////////////////////////////////////////////////////////////////////
    265     //
    266     //    Function:       process
    267     //
    268     //    Description:    Effect process function. Takes input samples as specified
    269     //          (count and location) in input buffer descriptor and output processed
    270     //          samples as specified in output buffer descriptor. If the buffer descriptor
    271     //          is not specified the function must use either the buffer or the
    272     //          buffer provider function installed by the EFFECT_CMD_CONFIGURE command.
    273     //          The effect framework will call the process() function after the EFFECT_CMD_ENABLE
    274     //          command is received and until the EFFECT_CMD_DISABLE is received. When the engine
    275     //          receives the EFFECT_CMD_DISABLE command it should turn off the effect gracefully
    276     //          and when done indicate that it is OK to stop calling the process() function by
    277     //          returning the -ENODATA status.
    278     //
    279     //    NOTE: the process() function implementation should be "real-time safe" that is
    280     //      it should not perform blocking calls: malloc/free, sleep, read/write/open/close,
    281     //      pthread_cond_wait/pthread_mutex_lock...
    282     //
    283     //    Input:
    284     //          self:       handle to the effect interface this function
    285     //              is called on.
    286     //          inBuffer:   buffer descriptor indicating where to read samples to process.
    287     //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
    288     //
    289     //          outBuffer:   buffer descriptor indicating where to write processed samples.
    290     //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
    291     //
    292     //    Output:
    293     //        returned value:    0 successful operation
    294     //                          -ENODATA the engine has finished the disable phase and the framework
    295     //                                  can stop calling process()
    296     //                          -EINVAL invalid interface handle or
    297     //                                  invalid input/output buffer description
    298     ////////////////////////////////////////////////////////////////////////////////
    299     int32_t (*process)(effect_handle_t self,
    300                        audio_buffer_t *inBuffer,
    301                        audio_buffer_t *outBuffer);
    302     ////////////////////////////////////////////////////////////////////////////////
    303     //
    304     //    Function:       command
    305     //
    306     //    Description:    Send a command and receive a response to/from effect engine.
    307     //
    308     //    Input:
    309     //          self:       handle to the effect interface this function
    310     //              is called on.
    311     //          cmdCode:    command code: the command can be a standardized command defined in
    312     //              effect_command_e (see below) or a proprietary command.
    313     //          cmdSize:    size of command in bytes
    314     //          pCmdData:   pointer to command data
    315     //          pReplyData: pointer to reply data
    316     //
    317     //    Input/Output:
    318     //          replySize: maximum size of reply data as input
    319     //                      actual size of reply data as output
    320     //
    321     //    Output:
    322     //          returned value: 0       successful operation
    323     //                          -EINVAL invalid interface handle or
    324     //                                  invalid command/reply size or format according to command code
    325     //              The return code should be restricted to indicate problems related to the this
    326     //              API specification. Status related to the execution of a particular command should be
    327     //              indicated as part of the reply field.
    328     //
    329     //          *pReplyData updated with command response
    330     //
    331     ////////////////////////////////////////////////////////////////////////////////
    332     int32_t (*command)(effect_handle_t self,
    333                        uint32_t cmdCode,
    334                        uint32_t cmdSize,
    335                        void *pCmdData,
    336                        uint32_t *replySize,
    337                        void *pReplyData);
    338     ////////////////////////////////////////////////////////////////////////////////
    339     //
    340     //    Function:        get_descriptor
    341     //
    342     //    Description:    Returns the effect descriptor
    343     //
    344     //    Input:
    345     //          self:       handle to the effect interface this function
    346     //              is called on.
    347     //
    348     //    Input/Output:
    349     //          pDescriptor:    address where to return the effect descriptor.
    350     //
    351     //    Output:
    352     //        returned value:    0          successful operation.
    353     //                          -EINVAL     invalid interface handle or invalid pDescriptor
    354     //        *pDescriptor:     updated with the effect descriptor.
    355     //
    356     ////////////////////////////////////////////////////////////////////////////////
    357     int32_t (*get_descriptor)(effect_handle_t self,
    358                               effect_descriptor_t *pDescriptor);
    359     ////////////////////////////////////////////////////////////////////////////////
    360     //
    361     //    Function:       process_reverse
    362     //
    363     //    Description:    Process reverse stream function. This function is used to pass
    364     //          a reference stream to the effect engine. If the engine does not need a reference
    365     //          stream, this function pointer can be set to NULL.
    366     //          This function would typically implemented by an Echo Canceler.
    367     //
    368     //    Input:
    369     //          self:       handle to the effect interface this function
    370     //              is called on.
    371     //          inBuffer:   buffer descriptor indicating where to read samples to process.
    372     //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
    373     //
    374     //          outBuffer:   buffer descriptor indicating where to write processed samples.
    375     //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
    376     //              If the buffer and buffer provider in the configuration received by
    377     //              EFFECT_CMD_CONFIGURE_REVERSE are also NULL, do not return modified reverse
    378     //              stream data
    379     //
    380     //    Output:
    381     //        returned value:    0 successful operation
    382     //                          -ENODATA the engine has finished the disable phase and the framework
    383     //                                  can stop calling process_reverse()
    384     //                          -EINVAL invalid interface handle or
    385     //                                  invalid input/output buffer description
    386     ////////////////////////////////////////////////////////////////////////////////
    387     int32_t (*process_reverse)(effect_handle_t self,
    388                                audio_buffer_t *inBuffer,
    389                                audio_buffer_t *outBuffer);
    390 };
    391 
    392 
    393 //
    394 //--- Standardized command codes for command() function
    395 //
    396 enum effect_command_e {
    397    EFFECT_CMD_INIT,                 // initialize effect engine
    398    EFFECT_CMD_CONFIGURE,            // configure effect engine (see effect_config_t)
    399    EFFECT_CMD_RESET,                // reset effect engine
    400    EFFECT_CMD_ENABLE,               // enable effect process
    401    EFFECT_CMD_DISABLE,              // disable effect process
    402    EFFECT_CMD_SET_PARAM,            // set parameter immediately (see effect_param_t)
    403    EFFECT_CMD_SET_PARAM_DEFERRED,   // set parameter deferred
    404    EFFECT_CMD_SET_PARAM_COMMIT,     // commit previous set parameter deferred
    405    EFFECT_CMD_GET_PARAM,            // get parameter
    406    EFFECT_CMD_SET_DEVICE,           // set audio device (see audio.h, audio_devices_t)
    407    EFFECT_CMD_SET_VOLUME,           // set volume
    408    EFFECT_CMD_SET_AUDIO_MODE,       // set the audio mode (normal, ring, ...)
    409    EFFECT_CMD_CONFIGURE_REVERSE,    // configure effect engine reverse stream(see effect_config_t)
    410    EFFECT_CMD_SET_INPUT_DEVICE,     // set capture device (see audio.h, audio_devices_t)
    411    EFFECT_CMD_FIRST_PROPRIETARY = 0x10000 // first proprietary command code
    412 };
    413 
    414 //==================================================================================================
    415 // command: EFFECT_CMD_INIT
    416 //--------------------------------------------------------------------------------------------------
    417 // description:
    418 //  Initialize effect engine: All configurations return to default
    419 //--------------------------------------------------------------------------------------------------
    420 // command format:
    421 //  size: 0
    422 //  data: N/A
    423 //--------------------------------------------------------------------------------------------------
    424 // reply format:
    425 //  size: sizeof(int)
    426 //  data: status
    427 //==================================================================================================
    428 // command: EFFECT_CMD_CONFIGURE
    429 //--------------------------------------------------------------------------------------------------
    430 // description:
    431 //  Apply new audio parameters configurations for input and output buffers
    432 //--------------------------------------------------------------------------------------------------
    433 // command format:
    434 //  size: sizeof(effect_config_t)
    435 //  data: effect_config_t
    436 //--------------------------------------------------------------------------------------------------
    437 // reply format:
    438 //  size: sizeof(int)
    439 //  data: status
    440 //==================================================================================================
    441 // command: EFFECT_CMD_RESET
    442 //--------------------------------------------------------------------------------------------------
    443 // description:
    444 //  Reset the effect engine. Keep configuration but resets state and buffer content
    445 //--------------------------------------------------------------------------------------------------
    446 // command format:
    447 //  size: 0
    448 //  data: N/A
    449 //--------------------------------------------------------------------------------------------------
    450 // reply format:
    451 //  size: 0
    452 //  data: N/A
    453 //==================================================================================================
    454 // command: EFFECT_CMD_ENABLE
    455 //--------------------------------------------------------------------------------------------------
    456 // description:
    457 //  Enable the process. Called by the framework before the first call to process()
    458 //--------------------------------------------------------------------------------------------------
    459 // command format:
    460 //  size: 0
    461 //  data: N/A
    462 //--------------------------------------------------------------------------------------------------
    463 // reply format:
    464 //  size: sizeof(int)
    465 //  data: status
    466 //==================================================================================================
    467 // command: EFFECT_CMD_DISABLE
    468 //--------------------------------------------------------------------------------------------------
    469 // description:
    470 //  Disable the process. Called by the framework after the last call to process()
    471 //--------------------------------------------------------------------------------------------------
    472 // command format:
    473 //  size: 0
    474 //  data: N/A
    475 //--------------------------------------------------------------------------------------------------
    476 // reply format:
    477 //  size: sizeof(int)
    478 //  data: status
    479 //==================================================================================================
    480 // command: EFFECT_CMD_SET_PARAM
    481 //--------------------------------------------------------------------------------------------------
    482 // description:
    483 //  Set a parameter and apply it immediately
    484 //--------------------------------------------------------------------------------------------------
    485 // command format:
    486 //  size: sizeof(effect_param_t) + size of param and value
    487 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
    488 //--------------------------------------------------------------------------------------------------
    489 // reply format:
    490 //  size: sizeof(int)
    491 //  data: status
    492 //==================================================================================================
    493 // command: EFFECT_CMD_SET_PARAM_DEFERRED
    494 //--------------------------------------------------------------------------------------------------
    495 // description:
    496 //  Set a parameter but apply it only when receiving EFFECT_CMD_SET_PARAM_COMMIT command
    497 //--------------------------------------------------------------------------------------------------
    498 // command format:
    499 //  size: sizeof(effect_param_t) + size of param and value
    500 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
    501 //--------------------------------------------------------------------------------------------------
    502 // reply format:
    503 //  size: 0
    504 //  data: N/A
    505 //==================================================================================================
    506 // command: EFFECT_CMD_SET_PARAM_COMMIT
    507 //--------------------------------------------------------------------------------------------------
    508 // description:
    509 //  Apply all previously received EFFECT_CMD_SET_PARAM_DEFERRED commands
    510 //--------------------------------------------------------------------------------------------------
    511 // command format:
    512 //  size: 0
    513 //  data: N/A
    514 //--------------------------------------------------------------------------------------------------
    515 // reply format:
    516 //  size: sizeof(int)
    517 //  data: status
    518 //==================================================================================================
    519 // command: EFFECT_CMD_GET_PARAM
    520 //--------------------------------------------------------------------------------------------------
    521 // description:
    522 //  Get a parameter value
    523 //--------------------------------------------------------------------------------------------------
    524 // command format:
    525 //  size: sizeof(effect_param_t) + size of param
    526 //  data: effect_param_t + param
    527 //--------------------------------------------------------------------------------------------------
    528 // reply format:
    529 //  size: sizeof(effect_param_t) + size of param and value
    530 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
    531 //==================================================================================================
    532 // command: EFFECT_CMD_SET_DEVICE
    533 //--------------------------------------------------------------------------------------------------
    534 // description:
    535 //  Set the rendering device the audio output path is connected to. See audio.h, audio_devices_t
    536 //  for device values.
    537 //  The effect implementation must set EFFECT_FLAG_DEVICE_IND flag in its descriptor to receive this
    538 //  command when the device changes
    539 //--------------------------------------------------------------------------------------------------
    540 // command format:
    541 //  size: sizeof(uint32_t)
    542 //  data: uint32_t
    543 //--------------------------------------------------------------------------------------------------
    544 // reply format:
    545 //  size: 0
    546 //  data: N/A
    547 //==================================================================================================
    548 // command: EFFECT_CMD_SET_VOLUME
    549 //--------------------------------------------------------------------------------------------------
    550 // description:
    551 //  Set and get volume. Used by audio framework to delegate volume control to effect engine.
    552 //  The effect implementation must set EFFECT_FLAG_VOLUME_IND or EFFECT_FLAG_VOLUME_CTRL flag in
    553 //  its descriptor to receive this command before every call to process() function
    554 //  If EFFECT_FLAG_VOLUME_CTRL flag is set in the effect descriptor, the effect engine must return
    555 //  the volume that should be applied before the effect is processed. The overall volume (the volume
    556 //  actually applied by the effect engine multiplied by the returned value) should match the value
    557 //  indicated in the command.
    558 //--------------------------------------------------------------------------------------------------
    559 // command format:
    560 //  size: n * sizeof(uint32_t)
    561 //  data: volume for each channel defined in effect_config_t for output buffer expressed in
    562 //      8.24 fixed point format
    563 //--------------------------------------------------------------------------------------------------
    564 // reply format:
    565 //  size: n * sizeof(uint32_t) / 0
    566 //  data: - if EFFECT_FLAG_VOLUME_CTRL is set in effect descriptor:
    567 //              volume for each channel defined in effect_config_t for output buffer expressed in
    568 //              8.24 fixed point format
    569 //        - if EFFECT_FLAG_VOLUME_CTRL is not set in effect descriptor:
    570 //              N/A
    571 //  It is legal to receive a null pointer as pReplyData in which case the effect framework has
    572 //  delegated volume control to another effect
    573 //==================================================================================================
    574 // command: EFFECT_CMD_SET_AUDIO_MODE
    575 //--------------------------------------------------------------------------------------------------
    576 // description:
    577 //  Set the audio mode. The effect implementation must set EFFECT_FLAG_AUDIO_MODE_IND flag in its
    578 //  descriptor to receive this command when the audio mode changes.
    579 //--------------------------------------------------------------------------------------------------
    580 // command format:
    581 //  size: sizeof(uint32_t)
    582 //  data: audio_mode_e
    583 //--------------------------------------------------------------------------------------------------
    584 // reply format:
    585 //  size: 0
    586 //  data: N/A
    587 //==================================================================================================
    588 // command: EFFECT_CMD_CONFIGURE_REVERSE
    589 //--------------------------------------------------------------------------------------------------
    590 // description:
    591 //  Apply new audio parameters configurations for input and output buffers of reverse stream
    592 //--------------------------------------------------------------------------------------------------
    593 // command format:
    594 //  size: sizeof(effect_config_t)
    595 //  data: effect_config_t
    596 //--------------------------------------------------------------------------------------------------
    597 // reply format:
    598 //  size: sizeof(int)
    599 //  data: status
    600 //==================================================================================================
    601 // command: EFFECT_CMD_SET_INPUT_DEVICE
    602 //--------------------------------------------------------------------------------------------------
    603 // description:
    604 //  Set the capture device the audio input path is connected to. See audio.h, audio_devices_t
    605 //  for device values.
    606 //  The effect implementation must set EFFECT_FLAG_DEVICE_IND flag in its descriptor to receive this
    607 //  command when the device changes
    608 //--------------------------------------------------------------------------------------------------
    609 // command format:
    610 //  size: sizeof(uint32_t)
    611 //  data: uint32_t
    612 //--------------------------------------------------------------------------------------------------
    613 // reply format:
    614 //  size: 0
    615 //  data: N/A
    616 //==================================================================================================
    617 // command: EFFECT_CMD_FIRST_PROPRIETARY
    618 //--------------------------------------------------------------------------------------------------
    619 // description:
    620 //  All proprietary effect commands must use command codes above this value. The size and format of
    621 //  command and response fields is free in this case
    622 //==================================================================================================
    623 
    624 
    625 // Audio buffer descriptor used by process(), bufferProvider() functions and buffer_config_t
    626 // structure. Multi-channel audio is always interleaved. The channel order is from LSB to MSB with
    627 // regard to the channel mask definition in audio.h, audio_channels_t e.g :
    628 // Stereo: left, right
    629 // 5 point 1: front left, front right, front center, low frequency, back left, back right
    630 // The buffer size is expressed in frame count, a frame being composed of samples for all
    631 // channels at a given time. Frame size for unspecified format (AUDIO_FORMAT_OTHER) is 8 bit by
    632 // definition
    633 struct audio_buffer_s {
    634     size_t   frameCount;        // number of frames in buffer
    635     union {
    636         void*       raw;        // raw pointer to start of buffer
    637         int32_t*    s32;        // pointer to signed 32 bit data at start of buffer
    638         int16_t*    s16;        // pointer to signed 16 bit data at start of buffer
    639         uint8_t*    u8;         // pointer to unsigned 8 bit data at start of buffer
    640     };
    641 };
    642 
    643 // The buffer_provider_s structure contains functions that can be used
    644 // by the effect engine process() function to query and release input
    645 // or output audio buffer.
    646 // The getBuffer() function is called to retrieve a buffer where data
    647 // should read from or written to by process() function.
    648 // The releaseBuffer() function MUST be called when the buffer retrieved
    649 // with getBuffer() is not needed anymore.
    650 // The process function should use the buffer provider mechanism to retrieve
    651 // input or output buffer if the inBuffer or outBuffer passed as argument is NULL
    652 // and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_CONFIGURE
    653 // command did not specify an audio buffer.
    654 
    655 typedef int32_t (* buffer_function_t)(void *cookie, audio_buffer_t *buffer);
    656 
    657 typedef struct buffer_provider_s {
    658     buffer_function_t getBuffer;       // retrieve next buffer
    659     buffer_function_t releaseBuffer;   // release used buffer
    660     void       *cookie;                // for use by client of buffer provider functions
    661 } buffer_provider_t;
    662 
    663 
    664 // The buffer_config_s structure specifies the input or output audio format
    665 // to be used by the effect engine. It is part of the effect_config_t
    666 // structure that defines both input and output buffer configurations and is
    667 // passed by the EFFECT_CMD_CONFIGURE or EFFECT_CMD_CONFIGURE_REVERSE command.
    668 typedef struct buffer_config_s {
    669     audio_buffer_t  buffer;     // buffer for use by process() function if not passed explicitly
    670     uint32_t   samplingRate;    // sampling rate
    671     uint32_t   channels;        // channel mask (see audio_channels_t in audio.h)
    672     buffer_provider_t bufferProvider;   // buffer provider
    673     uint8_t    format;          // Audio format  (see see audio_format_t in audio.h)
    674     uint8_t    accessMode;      // read/write or accumulate in buffer (effect_buffer_access_e)
    675     uint16_t   mask;            // indicates which of the above fields is valid
    676 } buffer_config_t;
    677 
    678 // Values for "accessMode" field of buffer_config_t:
    679 //   overwrite, read only, accumulate (read/modify/write)
    680 enum effect_buffer_access_e {
    681     EFFECT_BUFFER_ACCESS_WRITE,
    682     EFFECT_BUFFER_ACCESS_READ,
    683     EFFECT_BUFFER_ACCESS_ACCUMULATE
    684 
    685 };
    686 
    687 // Values for bit field "mask" in buffer_config_t. If a bit is set, the corresponding field
    688 // in buffer_config_t must be taken into account when executing the EFFECT_CMD_CONFIGURE command
    689 #define EFFECT_CONFIG_BUFFER    0x0001  // buffer field must be taken into account
    690 #define EFFECT_CONFIG_SMP_RATE  0x0002  // samplingRate field must be taken into account
    691 #define EFFECT_CONFIG_CHANNELS  0x0004  // channels field must be taken into account
    692 #define EFFECT_CONFIG_FORMAT    0x0008  // format field must be taken into account
    693 #define EFFECT_CONFIG_ACC_MODE  0x0010  // accessMode field must be taken into account
    694 #define EFFECT_CONFIG_PROVIDER  0x0020  // bufferProvider field must be taken into account
    695 #define EFFECT_CONFIG_ALL (EFFECT_CONFIG_BUFFER | EFFECT_CONFIG_SMP_RATE | \
    696                            EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT | \
    697                            EFFECT_CONFIG_ACC_MODE | EFFECT_CONFIG_PROVIDER)
    698 
    699 
    700 // effect_config_s structure describes the format of the pCmdData argument of EFFECT_CMD_CONFIGURE
    701 // command to configure audio parameters and buffers for effect engine input and output.
    702 typedef struct effect_config_s {
    703     buffer_config_t   inputCfg;
    704     buffer_config_t   outputCfg;
    705 } effect_config_t;
    706 
    707 
    708 // effect_param_s structure describes the format of the pCmdData argument of EFFECT_CMD_SET_PARAM
    709 // command and pCmdData and pReplyData of EFFECT_CMD_GET_PARAM command.
    710 // psize and vsize represent the actual size of parameter and value.
    711 //
    712 // NOTE: the start of value field inside the data field is always on a 32 bit boundary:
    713 //
    714 //  +-----------+
    715 //  | status    | sizeof(int)
    716 //  +-----------+
    717 //  | psize     | sizeof(int)
    718 //  +-----------+
    719 //  | vsize     | sizeof(int)
    720 //  +-----------+
    721 //  |           |   |           |
    722 //  ~ parameter ~   > psize     |
    723 //  |           |   |           >  ((psize - 1)/sizeof(int) + 1) * sizeof(int)
    724 //  +-----------+               |
    725 //  | padding   |               |
    726 //  +-----------+
    727 //  |           |   |
    728 //  ~ value     ~   > vsize
    729 //  |           |   |
    730 //  +-----------+
    731 
    732 typedef struct effect_param_s {
    733     int32_t     status;     // Transaction status (unused for command, used for reply)
    734     uint32_t    psize;      // Parameter size
    735     uint32_t    vsize;      // Value size
    736     char        data[];     // Start of Parameter + Value data
    737 } effect_param_t;
    738 
    739 
    740 
    741 /////////////////////////////////////////////////
    742 //      Effect library interface
    743 /////////////////////////////////////////////////
    744 
    745 // Effect library interface version 2.0
    746 #define EFFECT_LIBRARY_API_VERSION EFFECT_MAKE_API_VERSION(2,0)
    747 
    748 #define AUDIO_EFFECT_LIBRARY_TAG ((('A') << 24) | (('E') << 16) | (('L') << 8) | ('T'))
    749 
    750 // Every effect library must have a data structure named AUDIO_EFFECT_LIBRARY_INFO_SYM
    751 // and the fields of this data structure must begin with audio_effect_library_t
    752 
    753 typedef struct audio_effect_library_s {
    754     // tag must be initialized to AUDIO_EFFECT_LIBRARY_TAG
    755     uint32_t tag;
    756     // Version of the effect library API : 0xMMMMmmmm MMMM: Major, mmmm: minor
    757     uint32_t version;
    758     // Name of this library
    759     const char *name;
    760     // Author/owner/implementor of the library
    761     const char *implementor;
    762 
    763     ////////////////////////////////////////////////////////////////////////////////
    764     //
    765     //    Function:        query_num_effects
    766     //
    767     //    Description:    Returns the number of different effects exposed by the
    768     //          library. Each effect must have a unique effect uuid (see
    769     //          effect_descriptor_t). This function together with EffectQueryEffect()
    770     //          is used to enumerate all effects present in the library.
    771     //
    772     //    Input/Output:
    773     //          pNumEffects:    address where the number of effects should be returned.
    774     //
    775     //    Output:
    776     //        returned value:    0          successful operation.
    777     //                          -ENODEV     library failed to initialize
    778     //                          -EINVAL     invalid pNumEffects
    779     //        *pNumEffects:     updated with number of effects in library
    780     //
    781     ////////////////////////////////////////////////////////////////////////////////
    782     int32_t (*query_num_effects)(uint32_t *pNumEffects);
    783 
    784     ////////////////////////////////////////////////////////////////////////////////
    785     //
    786     //    Function:        query_effect
    787     //
    788     //    Description:    Returns the descriptor of the effect engine which index is
    789     //          given as argument.
    790     //          See effect_descriptor_t for details on effect descriptors.
    791     //          This function together with EffectQueryNumberEffects() is used to enumerate all
    792     //          effects present in the library. The enumeration sequence is:
    793     //              EffectQueryNumberEffects(&num_effects);
    794     //              for (i = 0; i < num_effects; i++)
    795     //                  EffectQueryEffect(i,...);
    796     //
    797     //    Input/Output:
    798     //          index:          index of the effect
    799     //          pDescriptor:    address where to return the effect descriptor.
    800     //
    801     //    Output:
    802     //        returned value:    0          successful operation.
    803     //                          -ENODEV     library failed to initialize
    804     //                          -EINVAL     invalid pDescriptor or index
    805     //                          -ENOSYS     effect list has changed since last execution of
    806     //                                      EffectQueryNumberEffects()
    807     //                          -ENOENT     no more effect available
    808     //        *pDescriptor:     updated with the effect descriptor.
    809     //
    810     ////////////////////////////////////////////////////////////////////////////////
    811     int32_t (*query_effect)(uint32_t index,
    812                             effect_descriptor_t *pDescriptor);
    813 
    814     ////////////////////////////////////////////////////////////////////////////////
    815     //
    816     //    Function:        create_effect
    817     //
    818     //    Description:    Creates an effect engine of the specified implementation uuid and
    819     //          returns an effect control interface on this engine. The function will allocate the
    820     //          resources for an instance of the requested effect engine and return
    821     //          a handle on the effect control interface.
    822     //
    823     //    Input:
    824     //          uuid:    pointer to the effect uuid.
    825     //          sessionId:  audio session to which this effect instance will be attached. All effects
    826     //              created with the same session ID are connected in series and process the same signal
    827     //              stream. Knowing that two effects are part of the same effect chain can help the
    828     //              library implement some kind of optimizations.
    829     //          ioId:   identifies the output or input stream this effect is directed to at audio HAL.
    830     //              For future use especially with tunneled HW accelerated effects
    831     //
    832     //    Input/Output:
    833     //          pHandle:        address where to return the effect interface handle.
    834     //
    835     //    Output:
    836     //        returned value:    0          successful operation.
    837     //                          -ENODEV     library failed to initialize
    838     //                          -EINVAL     invalid pEffectUuid or pHandle
    839     //                          -ENOENT     no effect with this uuid found
    840     //        *pHandle:         updated with the effect interface handle.
    841     //
    842     ////////////////////////////////////////////////////////////////////////////////
    843     int32_t (*create_effect)(effect_uuid_t *uuid,
    844                              int32_t sessionId,
    845                              int32_t ioId,
    846                              effect_handle_t *pHandle);
    847 
    848     ////////////////////////////////////////////////////////////////////////////////
    849     //
    850     //    Function:        release_effect
    851     //
    852     //    Description:    Releases the effect engine whose handle is given as argument.
    853     //          All resources allocated to this particular instance of the effect are
    854     //          released.
    855     //
    856     //    Input:
    857     //          handle:         handle on the effect interface to be released.
    858     //
    859     //    Output:
    860     //        returned value:    0          successful operation.
    861     //                          -ENODEV     library failed to initialize
    862     //                          -EINVAL     invalid interface handle
    863     //
    864     ////////////////////////////////////////////////////////////////////////////////
    865     int32_t (*release_effect)(effect_handle_t handle);
    866 
    867     ////////////////////////////////////////////////////////////////////////////////
    868     //
    869     //    Function:        get_descriptor
    870     //
    871     //    Description:    Returns the descriptor of the effect engine which implementation UUID is
    872     //          given as argument.
    873     //
    874     //    Input/Output:
    875     //          uuid:           pointer to the effect uuid.
    876     //          pDescriptor:    address where to return the effect descriptor.
    877     //
    878     //    Output:
    879     //        returned value:    0          successful operation.
    880     //                          -ENODEV     library failed to initialize
    881     //                          -EINVAL     invalid pDescriptor or uuid
    882     //        *pDescriptor:     updated with the effect descriptor.
    883     //
    884     ////////////////////////////////////////////////////////////////////////////////
    885     int32_t (*get_descriptor)(effect_uuid_t *uuid,
    886                               effect_descriptor_t *pDescriptor);
    887 } audio_effect_library_t;
    888 
    889 // Name of the hal_module_info
    890 #define AUDIO_EFFECT_LIBRARY_INFO_SYM         AELI
    891 
    892 // Name of the hal_module_info as a string
    893 #define AUDIO_EFFECT_LIBRARY_INFO_SYM_AS_STR  "AELI"
    894 
    895 __END_DECLS
    896 
    897 #endif  // ANDROID_AUDIO_EFFECT_H
    898