예제 #1
0
void A2MPHandleReadLocalAMPInfo(const BtEvent *Event)
{
    BtA2MPGetInfoRsp result;
    U8 *parm;
    U8 controllerId;
    U8 i=0;

	A2MP_MAIN_CONN *main_channel;

    parm = (U8 *)Event->p.hciAmpEvent.parms;
    result.controllerId = Event->p.hciAmpEvent.controllerId;
    if(Event->errCode ==0)
        result.status = 0x00; /* AMP get info response success*/
    else
        result.status = 0x01; /* AMP get info response failed */
    result.total_bandwidth = LEtoHost32(parm+2);
    result.max_guarantee_bandwidth = LEtoHost32(parm+6);
    result.min_latency = LEtoHost32(parm+10);
    result.max_pdu_size = LEtoHost32(parm+14);
    result.controller_type = parm[18];
    result.pal_capability = LEtoHost16(parm+19);
    result.max_amp_assoc_length = LEtoHost16(parm+21);
    result.max_flush_timeout = LEtoHost32(parm+23);
    result.best_effort_flush_timeout= LEtoHost32(parm+27);
    for(i=0;i<NUM_BT_DEVICES; i++)
    {
        if(A2MPC(a2mp_main_channel)[i].state == A2MP_STATE_ALLOCATED)
        {
            main_channel = &A2MPC(a2mp_main_channel)[i];
            if(main_channel->last_rxCmdopcode == A2MP_CODE_GET_INFO_REQ)
            {
                BTA2MP_SendGetInfoResponse(main_channel->remDev, &result);
                main_channel->last_rxCmdopcode = A2MP_CODE_INVALID;
            }
        }
    }


}
예제 #2
0
int encodec_sbc(
        U8 min_bit_pool,
        U8 block_len, // b0: 16, b1: 12, b2: 8, b3: 4
        U8 subband_num, // b0: 8, b1: 4
        U8 alloc_method, // b0: loudness, b1: SNR
        U8 sample_rate, // b0: 48000, b1: 44100, b2: 32000, b3: 16000
        U8 channel_mode // b0: joint stereo, b1: stereo, b2: dual channel, b3: mono
        )
{
    FILE       *wavIn, *sbcOut;
    SbcPcmData  pcmData;
    SbcEncoder  encoder;
    U16         bytesToRead;
    U16         bytesToEncode;
    U16         bytesEncoded;
    XaStatus    status;
    U8          *fNameIn;

    U8          *fNameOut;
    U32         sampleFreq = 0;
    U32         chunk2Size = 0;
    U16         sbcLen;
    U8 pathBuf[_MAX_PATH] = {0};
    int         encodedFrameLen;

    /* Initialize default encoder settings */
    SBC_InitEncoder(&encoder);
    fNameOut = BT_A2DP_SBC_FILE;
    switch(sample_rate)
    {
        case 0x01: /* 48.0K*/
            fNameIn = (U8 *)&BT_A2DP_SRC48_FILE;
            break;
        case 0x02:
            fNameIn = (U8 *)&BT_A2DP_SRC44_FILE;
            break;
        case 0x04:
            fNameIn = (U8 *)&BT_A2DP_SRC32_FILE;
            break;
        case 0x08:
            fNameIn = (U8 *)&BT_A2DP_SRC16_FILE;
            break;

    }
    switch(block_len)
    {
        case 0x01:
            encoder.streamInfo.numBlocks = 16;
            break;
        case 0x02:
            encoder.streamInfo.numBlocks = 12;
            break;
        case 0x04:
            encoder.streamInfo.numBlocks = 8;
            break;
        case 0x08:
            encoder.streamInfo.numBlocks = 4;
            break;
    }
    switch(subband_num)
    {
        case 0x01:
            encoder.streamInfo.numSubBands = 8;
            break;
        case 0x02:
            encoder.streamInfo.numSubBands = 4;
            break;
    }
    switch(alloc_method)
    {
        case 0x01:
            encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_LOUDNESS;
            break;
        case 0x02:
            encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_SNR;
            break;
    }
    switch(channel_mode)
    {
        case 0x01:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_JOINT_STEREO;
            break;
        case 0x02:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_STEREO;
            break;
        case 0x04:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_DUAL_CHNL;
            break;
        case 0x08:
            encoder.streamInfo.channelMode = SBC_CHNL_MODE_MONO;
            break;
    }

    encoder.streamInfo.bitPool = min_bit_pool;    
#if 0        
    encoder.streamInfo.numBlocks = 16;
    encoder.streamInfo.numSubBands = 8;
    encoder.streamInfo.allocMethod = SBC_ALLOC_METHOD_LOUDNESS;
    encoder.streamInfo.channelMode = SBC_CHNL_MODE_JOINT_STEREO;
    encoder.streamInfo.bitPool = 35;
#endif

    /* Open the WAV file */
    memset(pathBuf, 0, sizeof(pathBuf));
    translateFilePath(fNameIn, pathBuf);
    wavIn = fopen(pathBuf, "rb");
    if(wavIn == NULL)
        return -1;
    memset(pathBuf, 0, sizeof(pathBuf));
    OS_MemSet(pathBuf, 0, sizeof(pathBuf));
    translateFilePath(fNameOut, pathBuf);
    sbcOut = fopen(pathBuf, "wb");
    if(sbcOut == NULL)
        return -1;    

    /**** Parse the WAV header ****/
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "RIFF", 4) != 0) {
            printf("Invalid RIFF file format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Read format */
    fseek(wavIn, 8, 0);
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "WAVE", 4) != 0) {
            printf("Invalid WAV format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /**** Start subchunk 1 ****/
    fseek(wavIn, 12, 0);
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {
        if (memcmp(pcmBuffer, "fmt", 3) != 0) {
            printf("Invalid format chunk\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }
    
    /* PCM */
    fseek(wavIn, 20, 0);
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        if (LEtoHost16(pcmBuffer) != 1) {
            printf("Unsupported audio format\n");
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Channels */
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        encoder.streamInfo.numChannels = pcmBuffer[0];
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }

    /* Sample frequency */
    if (fread(pcmBuffer, 1, 4, wavIn) == 4) {

        sampleFreq = LEtoHost32(pcmBuffer);

        switch (sampleFreq) {
        case 48000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_48;
            break;
        case 44100:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_44_1;
            break;
        case 32000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_32;
            break;
        case 16000:
            encoder.streamInfo.sampleFreq = SBC_CHNL_SAMPLE_FREQ_16;
            break;
        default:
            printf("Unsupported sampling frequency %d\n", sampleFreq);
            return -1;
        }
    } else {
        printf("Error reading WAV file\n");
        return -1;
    }
    /* Bits per sample */
    fseek(wavIn, 34, 0);
    if (fread(pcmBuffer, 1, 2, wavIn) == 2) {
        if (LEtoHost32(pcmBuffer) != 16) {
            printf("Unsupported sample size\n");
        }
    }

    /**** Data chunk ****/
    fseek(wavIn, 44, 0);

    /* Print out SBC format */
    printf("Start\n");
    printf("Sampling Frequency = %d\n", sampleFreq);

    if (encoder.streamInfo.numChannels == 1) {
        if (encoder.streamInfo.channelMode != SBC_CHNL_MODE_MONO) {
            printf("Switching to mono mode...\n");
        }
        encoder.streamInfo.channelMode = SBC_CHNL_MODE_MONO;
        printf("Mode = Mono\n");
    } else {
        switch (encoder.streamInfo.channelMode) {
        case SBC_CHNL_MODE_JOINT_STEREO:
            printf("Mode = Joint Stereo\n");
            break;
        case SBC_CHNL_MODE_STEREO:
            printf("Mode = Stereo\n");
            break;
        case SBC_CHNL_MODE_DUAL_CHNL:
            printf("Mode = Dual Channel\n");
            break;
        default:
            printf("Mode = Unknown\n");
        }
    }

    printf("Blocks = %d\n", encoder.streamInfo.numBlocks);
    printf("Subbands = %d\n", encoder.streamInfo.numSubBands);

    if (encoder.streamInfo.allocMethod == SBC_ALLOC_METHOD_SNR) {
        printf("Allocation = SNR\n");
    } else if(encoder.streamInfo.allocMethod == SBC_ALLOC_METHOD_LOUDNESS) {
        printf("Allocation = LOUDNESS\n");
    } else {
        printf("Allocation = Unknown\n");
    }

    printf("Bitpool = %d\n", encoder.streamInfo.bitPool);

    /* Initialize PCM data structure */
    pcmData.data = pcmBuffer;
    bytesToRead = encoder.streamInfo.numChannels * 
                  encoder.streamInfo.numSubBands * 
                  encoder.streamInfo.numBlocks * 2;
    bytesToRead = (PCM_BUFF_SIZE / bytesToRead) * bytesToRead;

    /* Start reading the SBC data and encoding */
    while ((bytesToEncode = fread(pcmBuffer, 1, bytesToRead, wavIn))) {

        pcmData.data = pcmBuffer;
        pcmData.dataLen = bytesToEncode;

        while (bytesToEncode) {

            /* Encode the PCM buffer into the SBC buffer */
            status = SBC_EncodeFrames(&encoder, &pcmData, &bytesEncoded, 
                                      sbcBuffer, &sbcLen, SBC_BUFF_SIZE);

            bytesToEncode -= bytesEncoded;
            pcmData.data += bytesEncoded;
            pcmData.dataLen -= bytesEncoded;

            if (status == XA_STATUS_SUCCESS) {

                /* Block encoded */
                if (fwrite(sbcBuffer, 1, sbcLen, sbcOut) != sbcLen) {
                    printf("Error writing SBC data\n");
                    return -1;
                }

            } else if (status != XA_STATUS_CONTINUE) {
                /* Parsing error */
                printf("Error encoding SBC stream\n");
                return -1;
            } else {
                /* Not enough data left to encode a frame */
                break;
            }
        }
    }

    printf("Done\n");
    fclose(wavIn);
    fclose(sbcOut);
    encodedFrameLen = SBC_FrameLen(&encoder.streamInfo);
    return encodedFrameLen;
}