
/************************************************************
**
**     Object name : 
**
**     Description
**     -- -- -- -- -- -- -- --
**     固定音源位置の出力モジュール
**
**
**     History
**     -- -- -- -- -- -- -- --
**     2013.05.13 M.Takigahira Build有無指定に対応
**     2013.03.22 K.Takagi     初期座標計算をマージ
**     2012.12.02 K.Takagi     新規作成
**
**     Copyright (c) 2012
**     System In Frontier,Inc.
**     All rights reserved. 
**
**     当社の許可なしに複製および転載を禁ず
**
*/

///////////////////////////////////////////////////////////////
//  Include Files
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>

#include "build_modules.h"
#include "HA_log.h"
#include "android_hark.h"

#include "ConstantLocalizationFilter.h"

///////////////////////////////////////////////////////////////
//  Define Macros

#if !defined BUILD_MODULE_CONSTANT_LOCALIZATION_FILTER
///////////////////////////////////////////////////////////////
//  Dummy Functions for build empty methods.
int HASetParamConstantLocalizationFilter(Param_ConstantLocalization_t *param){ return 0; }
int HAInitConstantLocalizationFilter(void){ return 0; }
int HAGetConstantLocalizationFilter(int count, SOURCE_DATA_ARY_t *data_ary){ return 0; }
#ifdef STANDALON_TEST
int main(void){ return 0; }
#endif // STANDALON_TEST
#else // #if defined BUILD_MODULE_CONSTANT_LOCALIZATION_FILTER
///////////////////////////////////////////////////////////////
//  Parameter
static Param_ConstantLocalization_t filter_location_info;
static SOURCE_DATA_ARY_t filter_source_data;

///////////////////////////////////////////////////////////////
//  Local Buffer

///////////////////////////////////////////////////////////////
//  Clear

///////////////////////////////////////////////////////////////
//  Initialize
/******************************************************************************
**  History :
**    2013.03.22 K.Takagi M_PI/180.0の計算をループの外へ
**    2012.12.02 K.Takagi 新規作成
*/
// Public : 初期化
int HAInitConstantLocalizationFilter(void)
{
    int index;
    int source_num;
    
    double theta;
    double phi;
    double cos_phi;
    // deg to rad coef. TAKIGAHIRA
    double rad_coef;
    
    source_num = filter_location_info.source_num;
    if((source_num <= 0) || (source_num > 16)){
        return -1;
    }
    bzero(&filter_source_data, sizeof(SOURCE_DATA_ARY_t));
    
    /* 固定音源位置の算出 */
    filter_source_data.count = 0;
    filter_source_data.source_num = source_num;
    
    for(index=0, rad_coef=M_PI/180.0; index<source_num; index++){
        theta   = filter_location_info.angle[index] * rad_coef;
        phi     = filter_location_info.elevations[index] * rad_coef;
        cos_phi = cos(phi);
        
        filter_source_data.data[index].x[0] = (float)(cos(theta) * cos_phi);
        filter_source_data.data[index].x[1] = (float)(sin(theta) * cos_phi);
        filter_source_data.data[index].x[2] = (float)(sin(phi));
        
        filter_source_data.data[index].id    = index;
        filter_source_data.data[index].power = 1.0;
    }
    return 0;
}

///////////////////////////////////////////////////////////////
//  SetParam
/******************************************************************************
**  History :
**    2012.12.02 K.Takagi 新規作成
*/
// Public : パラメータ設定　初期化の前に実行すること
int HASetParamConstantLocalizationFilter(Param_ConstantLocalization_t *param)
{
    int index;

    if((param->source_num <= 0) || (param->source_num > 16)){
        return -1;
    }
    filter_location_info.source_num = param->source_num;
    for(index=0; index<param->source_num; index++){
        filter_location_info.angle[index] = param->angle[index];
        filter_location_info.elevations[index] = param->elevations[index];
    }
    return 0;
}

///////////////////////////////////////////////////////////////
//  MAIN
/******************************************************************************
**  History :
**    2012.12.02 K.Takagi 新規作成
*/
// Public : 固定音源位置の取得
int HAGetConstantLocalizationFilter(int count, SOURCE_DATA_ARY_t *data_ary)
{
    int index;
    int source_num;
    
    source_num = filter_source_data.source_num;
    if((source_num <= 0) || (source_num > 16)){
        return -1;
    }
    
    /* 音源位置情報をコピー */
    data_ary->count = count;
    data_ary->source_num = source_num;
    
    for(index=0; index<source_num; index++){
        data_ary->data[index].x[0] = filter_source_data.data[index].x[0];
        data_ary->data[index].x[1] = filter_source_data.data[index].x[1];
        data_ary->data[index].x[2] = filter_source_data.data[index].x[2];
        
        data_ary->data[index].id    = index;
        data_ary->data[index].power = 1.0;
    }
    return 0;
}

#ifdef STANDALON_TEST
/************************************************************
**  History :
**    2012.12.02 K.Takagi 新規作成
*/
// Public : 単体テストのメイン
int main(void)
{
    int stat;
    int count;
    int index;
    
    Param_ConstantLocalization_t filter_info;
    SOURCE_DATA_ARY_t source_data;
    
    filter_info.source_num = 3;
    filter_info.angle[0] = 0.0;
    filter_info.angle[1] = 30.0;
    filter_info.angle[2] = 45.0;
    filter_info.elevations[0] = 0.0;
    filter_info.elevations[1] = 0.0;
    filter_info.elevations[2] = 0.0;

    stat = HASetParamConstantLocalizationFilter(&filter_info);
    printf("HASetParamConstantLocalizationFilter : stat = %d\n", stat);
    stat = HAInitConstantLocalizationFilter();
    printf("HAInitParamConstantLocalizationFilter : stat = %d\n", stat);
    for(count=0; count<5; count++){
        HAGetConstantLocalizationFilter(count, &source_data);
        printf("[%d/%d] HAGetParamConstantLocalizationFilter : stat = %d\n", count, source_data.count, stat);
        for(index=0; index<source_data.source_num; index++){
            printf("index(id) = %d(%d)\n", index, source_data.data[index].id);
            printf("power = %f\n", source_data.data[index].power);
            printf("%f, %f, %f\n", source_data.data[index].x[0],
                                   source_data.data[index].x[1],
                                   source_data.data[index].x[2]);
        }
    }
    return 0;
}
#endif // STANDALON_TEST
#endif // BUILD_MODULE_CONSTANT_LOCALIZATION_FILTER

/* EOF */
