static int perform_law_test(int full, int law, const char *name)
{
    gsm0610_state_t *gsm0610_enc_state;
    gsm0610_state_t *gsm0610_dec_state;
    int i;
    int xxx;
    int mismatches;

    if (law == 'a')
        printf("Performing A-law test '%s'\n", name);
    else
        printf("Performing u-law test '%s'\n", name);
        
    get_law_test_vector(full, law, name);

    if (full)
    {
        if ((gsm0610_enc_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
        {
            fprintf(stderr, "    Cannot create encoder\n");
            exit(2);
        }
        if (law == 'a')
        {
            for (i = 0;  i < vector_len;  i++)
                in_vector[i] = alaw_to_linear(law_in_vector[i]);
        }
        else
        {
            for (i = 0;  i < vector_len;  i++)
                in_vector[i] = ulaw_to_linear(law_in_vector[i]);
        }
        xxx = gsm0610_encode(gsm0610_enc_state, code_vector, in_vector, vector_len);

        printf("Check code vector of length %d\n", xxx);
        for (i = 0, mismatches = 0;  i < xxx;  i++)
        {
            if (code_vector[i] != ref_code_vector[i])
            {
                printf("%8d/%3d: %6d %6d %6d\n", i/76, i%76, code_vector[i], ref_code_vector[i], decoder_code_vector[i]);
                mismatches++;
            }
        }
        if (mismatches)
        {
            printf("Test failed: %d of %d samples mismatch\n", mismatches, xxx);
            exit(2);
        }
        printf("Test passed\n");
        gsm0610_release(gsm0610_enc_state);
    }

    if ((gsm0610_dec_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
    {
        fprintf(stderr, "    Cannot create decoder\n");
        exit(2);
    }
    xxx = gsm0610_decode(gsm0610_dec_state, out_vector, decoder_code_vector, vector_len);
    if (law == 'a')
    {
        for (i = 0;  i < vector_len;  i++)
            law_out_vector[i] = linear_to_alaw(out_vector[i]);
    }
    else
    {
        for (i = 0;  i < vector_len;  i++)
            law_out_vector[i] = linear_to_ulaw(out_vector[i]);
    }
    printf("Check output vector of length %d\n", vector_len);
    for (i = 0, mismatches = 0;  i < vector_len;  i++)
    {
        if (law_out_vector[i] != ref_law_out_vector[i])
        {
            printf("%8d: %6d %6d\n", i, law_out_vector[i], ref_law_out_vector[i]);
            mismatches++;
        }
    }
    if (mismatches)
    {
        printf("Test failed: %d of %d samples mismatch\n", mismatches, vector_len);
        exit(2);
    }
    gsm0610_release(gsm0610_dec_state);
    printf("Test passed\n");
    return 0;
}
int main(int argc, char *argv[])
{
    SNDFILE *inhandle;
    SNDFILE *outhandle;
    int frames;
    int bytes;
    int16_t pre_amp[HIST_LEN];
    int16_t post_amp[HIST_LEN];
    uint8_t gsm0610_data[HIST_LEN];
    gsm0610_state_t *gsm0610_enc_state;
    gsm0610_state_t *gsm0610_dec_state;
    int opt;
    int etsitests;
    int packing;

    etsitests = TRUE;
    packing = GSM0610_PACKING_NONE;
    while ((opt = getopt(argc, argv, "lp:")) != -1)
    {
        switch (opt)
        {
        case 'l':
            etsitests = FALSE;
            break;
        case 'p':
            packing = atoi(optarg);
            break;
        default:
            //usage();
            exit(2);
        }
    }

    if (etsitests)
    {
        etsi_compliance_tests();
    }
    else
    {
        if ((inhandle = sf_open_telephony_read(IN_FILE_NAME, 1)) == NULL)
        {
            fprintf(stderr, "    Cannot open audio file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
        {
            fprintf(stderr, "    Cannot create audio file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
    
        if ((gsm0610_enc_state = gsm0610_init(NULL, packing)) == NULL)
        {
            fprintf(stderr, "    Cannot create encoder\n");
            exit(2);
        }
            
        if ((gsm0610_dec_state = gsm0610_init(NULL, packing)) == NULL)
        {
            fprintf(stderr, "    Cannot create decoder\n");
            exit(2);
        }

        while ((frames = sf_readf_short(inhandle, pre_amp, 2*BLOCK_LEN)))
        {
            bytes = gsm0610_encode(gsm0610_enc_state, gsm0610_data, pre_amp, frames);
            gsm0610_decode(gsm0610_dec_state, post_amp, gsm0610_data, bytes);
            sf_writef_short(outhandle, post_amp, frames);
        }
    
        if (sf_close_telephony(inhandle))
        {
            fprintf(stderr, "    Cannot close audio file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if (sf_close_telephony(outhandle))
        {
            fprintf(stderr, "    Cannot close audio file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
        gsm0610_release(gsm0610_enc_state);
        gsm0610_release(gsm0610_dec_state);
    }
    return 0;
}
예제 #3
0
int main(int argc, char *argv[])
{
    AFfilehandle inhandle;
    AFfilehandle outhandle;
    int frames;
    int outframes;
    int bytes;
    int16_t pre_amp[HIST_LEN];
    int16_t post_amp[HIST_LEN];
    uint8_t gsm0610_data[HIST_LEN];
    gsm0610_state_t *gsm0610_enc_state;
    gsm0610_state_t *gsm0610_dec_state;
    int opt;
    int etsitests;
    int packing;

    etsitests = TRUE;
    packing = GSM0610_PACKING_NONE;
    while ((opt = getopt(argc, argv, "lp:")) != -1)
    {
        switch (opt)
        {
        case 'l':
            etsitests = FALSE;
            break;
        case 'p':
            packing = atoi(optarg);
            break;
        default:
            //usage();
            exit(2);
        }
    }

    if (etsitests)
    {
        perform_linear_test(TRUE, 1, "Seq01");
        perform_linear_test(TRUE, 1, "Seq02");
        perform_linear_test(TRUE, 1, "Seq03");
        perform_linear_test(TRUE, 1, "Seq04");
        perform_linear_test(FALSE, 1, "Seq05");
        perform_law_test(TRUE, 'a', "Seq01");
        perform_law_test(TRUE, 'a', "Seq02");
        perform_law_test(TRUE, 'a', "Seq03");
        perform_law_test(TRUE, 'a', "Seq04");
        perform_law_test(FALSE, 'a', "Seq05");
        perform_law_test(TRUE, 'u', "Seq01");
        perform_law_test(TRUE, 'u', "Seq02");
        perform_law_test(TRUE, 'u', "Seq03");
        perform_law_test(TRUE, 'u', "Seq04");
        perform_law_test(FALSE, 'u', "Seq05");
        /* This is not actually an ETSI test */
        perform_pack_unpack_test();

        printf("Tests passed.\n");
    }
    else
    {
        if ((inhandle = afOpenFile_telephony_read(IN_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
        {
            fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
        {
            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
    
        if ((gsm0610_enc_state = gsm0610_init(NULL, packing)) == NULL)
        {
            fprintf(stderr, "    Cannot create encoder\n");
            exit(2);
        }
            
        if ((gsm0610_dec_state = gsm0610_init(NULL, packing)) == NULL)
        {
            fprintf(stderr, "    Cannot create decoder\n");
            exit(2);
        }

        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, 2*BLOCK_LEN)))
        {
            bytes = gsm0610_encode(gsm0610_enc_state, gsm0610_data, pre_amp, frames);
            gsm0610_decode(gsm0610_dec_state, post_amp, gsm0610_data, bytes);
            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, frames);
        }
    
        if (afCloseFile(inhandle) != 0)
        {
            fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME);
            exit(2);
        }
        if (afCloseFile(outhandle) != 0)
        {
            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
            exit(2);
        }
        gsm0610_release(gsm0610_enc_state);
        gsm0610_release(gsm0610_dec_state);
    }
    return 0;
}