int audioCapturerSetFormat()

in source/T31/T31AudioCapturer.c [100:188]


int audioCapturerSetFormat(AudioCapturerHandle handle, const AudioFormat format, const AudioChannel channel, const AudioSampleRate sampleRate,
                           const AudioBitDepth bitDepth)
{
    T31_HANDLE_NULL_CHECK(handle);
    T31_HANDLE_GET(handle);

    T31_HANDLE_STATUS_CHECK(t31Handle, AUD_CAP_STATUS_STREAM_OFF);

    IMPAudioEncChnAttr chnAttr = {
        .bufSize = 2,
    };

    IMPAudioIOAttr ioAttr = {
        .frmNum = 2,
        .numPerFrm = 400,
        .chnCnt = 1,
    };

    switch (format) {
        case AUD_FMT_PCM:
            chnAttr.type = PT_PCM;
            break;

        case AUD_FMT_G711A:
            chnAttr.type = PT_G711A;
            break;

        case AUD_FMT_G711U:
            chnAttr.type = PT_G711U;
            break;

        default:
            LOG("Unsupported format %d", format);
            return -EINVAL;
    }

    switch (channel) {
        case AUD_CHN_MONO:
            ioAttr.soundmode = AUDIO_SOUND_MODE_MONO;
            break;

        default:
            LOG("Unsupported channel num %d", channel);
            return -EINVAL;
    }

    switch (sampleRate) {
        case AUD_SAM_8K:
            ioAttr.samplerate = AUDIO_SAMPLE_RATE_8000;
            break;

        case AUD_SAM_16K:
            ioAttr.samplerate = AUDIO_SAMPLE_RATE_16000;
            break;

        default:
            LOG("Unsupported sample rate %d", sampleRate);
            return -EINVAL;
    }

    switch (bitDepth) {
        case AUD_BIT_16:
            ioAttr.bitwidth = AUDIO_BIT_WIDTH_16;
            break;

        default:
            LOG("Unsupported bit depth %d", bitDepth);
            return -EINVAL;
    }

    if (IMP_AI_SetPubAttr(T31_MIC_DEV_ID, &ioAttr)) {
        LOG("IMP_AI_SetPubAttr failed");
        return -EAGAIN;
    }

    // Make sure T31_MIC_ENC_CHN_ID is not occupied
    IMP_AENC_DestroyChn(T31_MIC_ENC_CHN_ID);
    if (IMP_AENC_CreateChn(T31_MIC_ENC_CHN_ID, &chnAttr)) {
        LOG("IMP_AENC_CreateChn failed");
        return -EAGAIN;
    }

    t31Handle->format = format;
    t31Handle->channel = channel;
    t31Handle->sampleRate = sampleRate;
    t31Handle->bitDepth = bitDepth;

    return 0;
}