⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmaudio.c

📁 WM9713 audio codec driver for WinCE 5.0
💻 C
📖 第 1 页 / 共 3 页
字号:
     * Query our settings.
     */
    if ( pDeviceContext->v_pWMData )
    {
        /*
         * Lock our global data.
         */
        if ( WMLockGlobalData( hDevice ) )
        {
            switch ( audioIF )
            {
                case WM_AUDIOIF_HIFI:
                    if ( pDeviceContext->v_pWMData->audioData.flags & WM_AUDIO_HIFI_CONFIGURED )
                    {
                        status = WMS_RETURN_TRUE;
                    }
                    else
                    {
                        status = WMS_RETURN_FALSE;
                    }
                    break;

#           if WM_VOICE
                case WM_AUDIOIF_VOICE:
                    if ( pDeviceContext->v_pWMData->audioData.flags & WM_AUDIO_VOICE_CONFIGURED )
                    {
                        status = WMS_RETURN_TRUE;
                    }
                    else
                    {
                        status = WMS_RETURN_FALSE;
                    }
                    break;
#           endif /* WM_VOICE */

                default:
                    status = WMS_UNSUPPORTED;
                    break;
            }

            /* Let other threads in again */
            WMUnlockGlobalData( hDevice );
        }
    }
    
    return status;
}

/*-----------------------------------------------------------------------------
 * Function:    WMAudioIsStreamSupported
 *
 * Queries whether the given stream is supported by the WDCL for this device.
 *
 * Parameters:
 *      hDevice         handle to the device (from WMOpenDevice)
 *      stream          The stream to query
 *
 * Returns:     WM_BOOL
 *      TRUE     - stream is supported
 *      FALSE    - stream is not supported
 *---------------------------------------------------------------------------*/
WM_BOOL WMAudioIsStreamSupported( WM_DEVICE_HANDLE hDevice,
                                  WM_STREAM_ID     stream
                                 )
{
    WM_BOOL             supported;
    WM_AUDIO_INTERFACE  audioIF;
    WM_BOOL             checkInterface = FALSE;

    switch ( stream )
    {
        /* The default streams are always supported */
        case WM_STREAM_DEFAULT_INPUT:
        case WM_STREAM_DEFAULT_OUTPUT:
            supported = TRUE;
            break;

        /* For HiFi we should check the table */
        case WM_STREAM_HIFI_IN:
        case WM_STREAM_HIFI_OUT:
            audioIF = WM_AUDIOIF_HIFI;
            checkInterface = TRUE;
            break;
            
        /* For Voice we should check the table */
        case WM_STREAM_VOICE_IN:
        case WM_STREAM_VOICE_OUT:
            audioIF = WM_AUDIOIF_VOICE;
            checkInterface = TRUE;
            break;

        /* Mono uses the HiFi interface at present - check the macro */
        case WM_STREAM_MONO_OUT:
            supported = WM_HAS_MONO_DAC( hDevice ) ? TRUE : FALSE;
            break;

        default:
            supported = FALSE;
    }

    /*
     * If we've got to check our interface table, run through it now.
     */
    if ( checkInterface )
    {
        const WM_CHIPDEF    *pChipDef;
        unsigned int        i;
        WM_BOOL             checkFlag;

        pChipDef = WMGetChipDef( hDevice );
        WM_ASSERT( hDevice, pChipDef );
		if ( !pChipDef )
		{
			supported = FALSE;
			goto finish;
		}

        if ( WM_IS_INPUT_STREAM( stream ) )
            checkFlag = WM_IF_RECORD;
        else
            checkFlag = WM_IF_PLAYBACK;

        supported = FALSE;
        
        for ( i = 0; i < pChipDef->nInterfaces; i++ )
        {
            if ( audioIF == pChipDef->pInterfaces[i].audioIF &&
                 pChipDef->pInterfaces[i].flags & checkFlag
               )
            {
                supported = TRUE;
                break;
            }
        }
    }
    
finish:
    return supported;
}

/*-----------------------------------------------------------------------------
 * Function:    WMAudioIsInterfaceSupported
 *
 * Queries whether the given audio interface is supported by the WDCL for
 * this device.
 *
 * Parameters:
 *      hDevice         handle to the device (from WMOpenDevice)
 *      audioIF         The interface to query
 *
 * Returns:     WM_BOOL
 *      TRUE     - interface is supported
 *      FALSE    - interface is not supported
 *---------------------------------------------------------------------------*/
WM_BOOL WMAudioIsInterfaceSupported( WM_DEVICE_HANDLE   hDevice,
                                     WM_AUDIO_INTERFACE audioIF
                                    )
{
    WM_BOOL             supported;
    const WM_CHIPDEF    *pChipDef;
    unsigned int        i;

    pChipDef = WMGetChipDef( hDevice );
    WM_ASSERT( hDevice, pChipDef );
    if ( !pChipDef )
    {
        supported = FALSE;
        goto finish;
    }

    supported = FALSE;
    
    for ( i = 0; i < pChipDef->nInterfaces; i++ )
    {
        if ( audioIF == pChipDef->pInterfaces[i].audioIF )
        {
            supported = TRUE;
            break;
        }
    }
    
finish:
    return supported;
}

/*-----------------------------------------------------------------------------
 * Function:    WMAudioEnableStream
 *
 * Called to enable the device to handle the given stream.  This powers up
 * and configures the interface according to the default values.
 *
 * Parameters:
 *      hDevice         handle to the device (from WMOpenDevice)
 *      stream          The stream to enable
 *
 * Returns:     WMSTATUS
 *      See WMStatus.h.
 *---------------------------------------------------------------------------*/
WMSTATUS WMAudioEnableStream( WM_DEVICE_HANDLE hDevice,
                              WM_STREAM_ID     ifStream
                            )
{
    WM_DEVICE_CONTEXT   *pDeviceContext = WMHANDLE_TO_DEVICE( hDevice );
    WMSTATUS            status;
    WM_POWERFLAG        powerSections;
    const WM_CHIPDEF    *pChipDef;
    WM_AUDIO_INTERFACE  audioIF;
    WM_STREAM_ID        stream = _WMAudioGetRealStream( hDevice, ifStream );

    /*
     * Check our stream.
     */
    if ( !WMAudioIsStreamSupported( hDevice, stream ) )
    {
        status = WMS_UNSUPPORTED;
        goto error0;
    }
               
    /*
     * Power up.
     */
    switch ( stream )
    {
        case WM_STREAM_HIFI_IN:
            powerSections = WM_POWER_AUDIO_HIFI_ADC;
            audioIF = WM_AUDIOIF_HIFI;
            break;
        case WM_STREAM_VOICE_IN:
            powerSections = WM_POWER_AUDIO_ADCS;
            audioIF = WM_AUDIOIF_VOICE;
            break;

        case WM_STREAM_HIFI_OUT:
            powerSections = WM_POWER_AUDIO_HIFI_DAC;
            audioIF = WM_AUDIOIF_HIFI;
            break;
        case WM_STREAM_VOICE_OUT:
            powerSections = WM_POWER_AUDIO_VOICE_DAC;
            audioIF = WM_AUDIOIF_VOICE;
            break;
        case WM_STREAM_MONO_OUT:
            powerSections = WM_POWER_AUDIO_MONO_DAC;
            audioIF = (WM_AUDIO_INTERFACE) WM_AUDIOIF_MONO;
            break;

        default:
			/* We checked we had a valid stream above */
            WM_ASSERT( hDevice, WM_IS_VALID_STREAM( stream ) );
    }
    
    status = WMAudioPowerUp( hDevice, powerSections );
    if ( WM_ERROR( status ) )
    {
        WM_TRACE( hDevice, ( "WMAudioEnableStream(%d): WMAudioPowerUp failed: %s",
                             ifStream,
                             WMStatusText( status )
                           )
                );
        goto error0;
    }

    /*
     * Enable voice if necessary.
     */
    if ( WM_IS_VOICE_STREAM( stream ) )
    {
        status = WMVoiceEnable( hDevice );
        if ( WM_ERROR( status ) )
        {
            goto error0;
        }
    }

    /*
     * Look up our chipdef and see if we've got the function.
     * If not (e.g. standard AC'97), we don't need to do anything extra.
     */
    pChipDef = WMGetChipDef( hDevice );
    WM_ASSERT( hDevice, pChipDef );
    if ( !pChipDef )
    {
        status = WMS_NO_SUPPORTED_DEVICE;
        WM_TRACE( hDevice, ( "WMAudioEnableStream(%d): Chip Def not found: %s",
                             ifStream,
                             WMStatusText( status )
                           )
                );
        goto error1;
    }
    if ( pChipDef->vtable.fnEnableStream )
    {
        status = pChipDef->vtable.fnEnableStream( hDevice, stream );

        if ( WM_ERROR( status ) )
        {
            WM_TRACE( hDevice, ( "WMAudioEnableStream(%d): device-specific enable failed: %s",
                                 ifStream,
                                 WMStatusText( status )
                               )
                    );
            goto error1;
        }
    }

    /*
     * If it's an I2S device, make sure the interface is configured
     * - use default settings if not.
     * 
     * Note we do this after the chip-specific initialisation in case there
     * is special configuration needed by the chip (for example the WM8753
     * output stream may be either HiFi or Voice depending on the other
     * set-up).
     */
    if ( WM_IS_I2S( hDevice ) )
    {
        WM_BOOL             isMaster;
        WM_AUDIOIF_FORMAT   format;
        WM_AUDIOIF_WIDTH    width;
        WM_AUDIOIF_FLAGS    flags;
        unsigned int        configuredFlag;
    
        if ( WM_AUDIOIF_HIFI == audioIF )
        {
            isMaster = WM_AUDIOIF_DEFAULT_HIFI_IS_MASTER;
            format = WM_AUDIOIF_DEFAULT_HIFI_FORMAT;
            width = WM_AUDIOIF_DEFAULT_HIFI_WIDTH;
            flags = WM_AUDIOIF_DEFAULT_HIFI_FLAGS;
            configuredFlag = WM_AUDIO_HIFI_CONFIGURED;
        }
        else
        {
            isMaster = WM_AUDIOIF_DEFAULT_VOICE_IS_MASTER;
            format = WM_AUDIOIF_DEFAULT_VOICE_FORMAT;
            width = WM_AUDIOIF_DEFAULT_VOICE_WIDTH;
            flags = WM_AUDIOIF_DEFAULT_VOICE_FLAGS;
            configuredFlag = WM_AUDIO_VOICE_CONFIGURED;
        }
    
        if ( !pDeviceContext->v_pWMData || 
             !(pDeviceContext->v_pWMData->audioData.flags & configuredFlag)
           )
        {
            status = WMAudioConfigureInterface( hDevice,
                                                audioIF,
                                                isMaster,
                                                format,
                                                width,
                                                flags
                                              );
            if ( WM_ERROR( status ) )
            {
                WM_TRACE( hDevice,
                          ( "WM8753EnableStream: WMAudioConfigureInterface failed: %s",
                            WMStatusText( status )
                          )
                        );
                goto error2;
            }
            
            WM_ASSERT( hDevice,
                       WMS_RETURN_TRUE == WMAudioIsInterfaceConfigured( hDevice, audioIF )
                     );
        }
    }
    
    /*
     * Update our state.
     */
    if ( pDeviceContext->v_pWMData )
    {
        /*
         * Lock our global data.
         */
        if ( WMLockGlobalData( hDevice ) )
        {
            pDeviceContext->v_pWMData->audioData.streams |= WM_STREAM_FLAG( stream );

            /*
             * Let other threads in.
             */
            WMUnlockGlobalData( hDevice );
        }
    }
    
    /*
     * We're done.
     */
    return WMS_SUCCESS;

error2:
    if ( pChipDef->vtable.fnDisableStream )
    {
        status = pChipDef->vtable.fnDisableStream( hDevice, stream );
    }

error1:
    WMAudioPowerDown( hDevice, powerSections );

error0:
    return status;
}

/*-----------------------------------------------------------------------------
 * Function:    WMAudioDisableStream
 *
 * Called to disable the given stream.
 *
 * Parameters:
 *      hDevice         handle to the device (from WMOpenDevice)
 *      stream          The stream to enable
 *
 * Returns:     WMSTATUS
 *      See WMStatus.h.
 *---------------------------------------------------------------------------*/
WMSTATUS WMAudioDisableStream( WM_DEVICE_HANDLE hDevice,
                               WM_STREAM_ID     ifStream
                             )
{
    WM_DEVICE_CONTEXT   *pDeviceContext = WMHANDLE_TO_DEVICE( hDevice );
    WMSTATUS            status;
    WM_POWERFLAG        powerSections;
    const WM_CHIPDEF    *pChipDef;
    WMSTATUS            enabled;
    WM_AUDIO_INTERFACE  audioIF;
    WM_STREAM_ID        stream = _WMAudioGetRealStream( hDevice, ifStream );

    WM_TRACE( hDevice, ("Disabling stream %d", ifStream));

    /*
     * Check our stream.
     */
    if ( !WMAudioIsStreamSupported( hDevice, stream ) )
    {
        status = WMS_UNSUPPORTED;
        goto error;
    }
               
    /*
     * Power down.
     */
    switch ( stream )
    {
        case WM_STREAM_HIFI_IN:
            powerSections = WM_POWER_AUDIO_HIFI_ADC;
            enabled = WMAudioIsStreamEnabled( hDevice, WM_STREAM_HIFI_OUT );
            if ( WMS_RETURN_FALSE == enabled )
                audioIF = WM_AUDIOIF_HIFI;
            else
                audioIF = WM_AUDIOIF_NONE;
            break;
            
        case WM_STREAM_HIFI_OUT:
            powerSections = WM_POWER_AUDIO_HIFI_DAC;
            enabled = WMAudioIsStreamEnabled( hDevice, WM_STREAM_HIFI_IN );
            if ( WMS_RETURN_FALSE == enabled )
                audioIF = WM_AUDIOIF_HIFI;
            else
                audioIF = WM_AUDIOIF_NONE;
            break;
            
        case WM_STREAM_VOICE_IN:
            powerSections = WM_POWER_AUDIO_ADCS;
            enabled = WMAudioIsStreamEnabled( hDevice, WM_STREAM_VOICE_OUT );
            if ( WMS_RETURN_FALSE == enabled )
                audioIF = WM_AUDIOIF_VOICE;
            else
                audioIF = WM_AUDIOIF_NONE;
            break;

        case WM_STREAM_VOICE_OUT:
            powerSections = WM_POWER_AUDIO_VOICE_DAC;
            enabled = WMAudioIsStreamEnabled( hDevice, WM_STREAM_VOICE_IN );
            if ( WMS_RETURN_FALSE == enabled )
                audioIF = WM_AUDIOIF_VOICE;
            else
                audioIF = WM_AUDIOIF_NONE;
            break;
            
        case WM_STREAM_MONO_OUT:
            powerSections = WM_POWER_AUDIO_MONO_DAC;
            audioIF = (WM_AUDIO_INTERFACE) WM_AUDIOIF_MONO;
            break;

        default:
			/* We checked we had a valid stream above */
            WM_ASSERT( hDevice, WM_IS_VALID_STREAM( stream ) );
    }
    
    /*
     * Look up our chipdef and see if we've got the function.
     * If not (e.g. standard AC'97), we don't need to do anything extra.
     */
    pChipDef = WMGetChipDef( hDevice );
    WM_ASSERT( hDevice, pChipDef );
    if ( !pChipDef )
    {
        status = WMS_NO_SUPPORTED_DEVICE;
        goto error;
    }
    if ( pChipDef->vtable.fnDisableStream )
    {
        status = pChipDef->vtable.fnDisableStream( hDevice, stream );
        if ( WM_ERROR( status ) )
        {
            goto error;
        }
    }

    if ( WM_IS_I2S( hDevice ) )
    {
        /*
         * Unconfigure the interface, if it's not still in use.
         */
        if ( ( WM_AUDIOIF_NONE != audioIF ) &&
             ( WMS_RETURN_TRUE == WMAudioIsInterfaceConfigured( hDevice, audioIF ) )
           )
        {
            status = WMAudioUnconfigureInterface( hDevice, audioIF );
            if ( WM_ERROR( status ) )
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -