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

📄 wmaudiotests.c

📁 WM9713 audio codec driver for WinCE 5.0
💻 C
字号:
 /*-----------------------------------------------------------------------------
 * Copyright (c) Wolfson Microelectronics plc.  All rights reserved.
 *
 * This software as well as any related documentation is furnished under 
 * license and may only be used or copied in accordance with the terms of the 
 * license. The information in this file is furnished for informational use 
 * only, is subject to change without notice, and should not be construed as 
 * a commitment by Wolfson Microelectronics plc. Wolfson Microelectronics plc
 * assumes no responsibility or liability for any errors or inaccuracies that
 * may appear in this document or any software that may be provided in
 * association with this document. 
 *
 * Except as permitted by such license, no part of this document may be 
 * reproduced, stored in a retrieval system, or transmitted in any form or by 
 * any means without the express written consent of Wolfson Microelectronics plc. 
 *
 * $Id: WMAudioTests.c 2819 2006-03-27 08:03:10Z fb $
 *
 * This file contains tests for the Audio interface to the library.
 *
 * Warning:
 *  This driver is specifically written for Wolfson Codecs. It is not a 
 *  general CODEC device driver.
 *
 * -----------------------------------------------------------------------------*/

/*
 * Include files
 */
#include "WMCommon.h"
#include "WMDevice.h"
#include "WMControlLink.h"
#include "WMAudio.h"
#include "WMAudioPaths.h"
#include "Test/WMTestCommon.h"
#include "Test/WMAudioTests.h"
#include "Test/WMTestHelpers.h"
#include "Test/WMAudioTestHelpers.h"

/*
 * Only build this if we are asked to.
 */
#if WM_AUDIO && WM_TESTING

/*
 * Global definitions
 */
#define TEST_SAMPLE_RATE_HIFI   48000
#define TEST_SAMPLE_RATE_VOICE  8000
#define TEST_SAMPLE_RATE_MONO   8000

/* The maximum time (in ms) we're prepared to wait before we decide DMA is broke */
#define MAX_PATIENCE    1000

/*
 * Private data
 */

/*
 * Function prototypes
 */

/*-----------------------------------------------------------------------------
 * Function:    WMTestAudio
 *
 * Audio tests - plays sound via the various DACs using the library.
 *
 * Parameters:
 *      hDevice     handle to the device (from WMOpenDevice)
 *
 * Returns:     WM_BOOL
 *      TRUE if it passed, FALSE if it failed in a way it could detect.
 *---------------------------------------------------------------------------*/
WMTEST_START( WMTestAudio( WM_DEVICE_HANDLE hDevice ) )
{
    savedRegister   savedRegs[MAX_REGISTER_COUNT];
    WM_REGVAL       savedPowerReg[ MAX_GLOBAL_POWER_REGS ];
    WM_POWERFLAG    savedPowerRequirements[ WM_MAX_DRIVERS ];
    WM_POWERFLAG    savedWmPower = 0;
    
    /* Save our state */
    WMTEST_RUN( WMTest_SaveState( hDevice, 
                                  savedRegs,
                                  savedPowerReg,
                                  savedPowerRequirements,
                                  &savedWmPower ) );

    
#if WM_AUDIO_STREAM && WM_AUDIO_WAVEGEN
    /* Now run the tests */
    WMTEST_RUN( WMTestPlaySineWave( hDevice,
                                    WM_STREAM_HIFI_OUT,
                                    TEST_SAMPLE_RATE_HIFI
                                  ) );

    WMTEST_RUN( WMTestPlaySineWave( hDevice,
                                    WM_STREAM_VOICE_OUT,
                                    TEST_SAMPLE_RATE_VOICE
                                  ) );

    WMTEST_RUN( WMTestPlaySineWave( hDevice,
                                    WM_STREAM_MONO_OUT,
                                    TEST_SAMPLE_RATE_MONO
                                  ) );

#else /* WM_AUDIO_STREAM && WM_AUDIO_WAVEGEN */
	WMTEST_TRACE(( "There are no non-streaming audio tests" ));
#endif /* WM_AUDIO_STREAM */
    
    /* And set things back again. */
    WMTEST_RUN( WMTest_RestoreState( hDevice, 
                                     savedRegs,
                                     savedPowerReg,
                                     savedPowerRequirements,
                                     &savedWmPower ) );
}
WMTEST_END

#if WM_AUDIO_STREAM && WM_AUDIO_WAVEGEN
/*-----------------------------------------------------------------------------
 * Function:    WMTestPlaySineWave
 *
 * Plays a sound to the specified DAC, using the library.
 *
 * Parameters:
 *      hDevice     handle to the device (from WMOpenDevice)
 *      stream      stream to play to
 *      sampleRate  sample rate to stream at
 *
 * Returns:     WM_BOOL
 *      TRUE if it passed, FALSE if it failed in a way it could detect.
 *---------------------------------------------------------------------------*/
WMTEST_START( WMTestPlaySineWave( WM_DEVICE_HANDLE  hDevice,
                                  WM_STREAM_ID      stream,
                                  WM_SAMPLE_RATE    sampleRate
                                ) )
{
    unsigned int                    repeat;
    unsigned int                    repeats;
    unsigned int                    samplesPerBuffer;
    WM_WAVEGEN_CTX                  wavegenCtx = WM_START_OF_WAVE;
    WMAUDIO_INTSTATE                intState;
    WM_STREAM_HANDLE                hStream;
    const WM_TEST_SOUND             *pTestSound = WMTestAudio_GetSound();
	WMSTATUS						expectedResult;
	WM_BOOL							isStereo=TRUE;

    /* 
	 * The following values are put into variables so they can be
     * altered in a debugger. 
	 */
    volatile int                    streamControlLoopForever = FALSE;
    volatile int                    audioPlayLoopForever  = FALSE;
	volatile WM_SAMPLE_RATE         setSampleRate = sampleRate;
	volatile int                    bitsPerSample = 16;
    volatile unsigned short         frequency = pTestSound->frequency;
    volatile unsigned short         amplitude = pTestSound->amplitude;
    volatile unsigned int           duration = pTestSound->duration;

	/* 
	 * Disable the DMA Interrupts until we have finished.
	 */
	WMAudioDisableDMAInterrupts( hDevice );

	/*
	 * External loop - enabling sample rate and frequency changes 
	 * to be repeated when under debugger control
	 */
	do
	{
		/* 
		 * Open the stream.
		 * If the stream is unsupported, exit the test.
		 */
		if ( !WM_HAS_VOICE_DAC( hDevice ) &&
			 ( WM_STREAM_VOICE_OUT == stream )
		   )
		{
			expectedResult = WMS_UNSUPPORTED;
		}
		else if ( !WM_HAS_MONO_DAC( hDevice ) &&
				  ( WM_STREAM_MONO_OUT == stream )
				)
		{
			expectedResult = WMS_UNSUPPORTED;
		}
		else
		{
			expectedResult = WMS_SUCCESS;
		}

		if ( WM_STREAM_MONO_OUT == stream || WM_STREAM_VOICE_OUT == stream )
		{
			isStereo = FALSE;
		}

		WMTEST_CALL_STATUS( WMAudioOpenStream( hDevice,
											   stream,
											   &hStream,
											   setSampleRate,
											   bitsPerSample,	/* bits per sample */
											   isStereo			/* stereo ? */
											 ),
							expectedResult
						  );

		if ( WM_ERROR( expectedResult ) )
		{
			goto exit;
		}

        /*
         * Power up, enable and unmute the output path.
         */
        WMTEST_CALL( WMAudioPowerUp( hDevice,WM_POWER_AUDIO_PLAYBACK ) );

        WMTEST_CALL( WMAudioEnableOutputPaths( hDevice, stream, TRUE ) );

        WMTEST_CALL( WMAudioMuteAllOutputs( hDevice, FALSE ) );


		/*
		 * Work out the number of repeats to get our duration.
		 */
		samplesPerBuffer = WMAudioSamplesPerBuffer( hDevice, hStream );
		repeats = WM_TEST_REPEATS( duration, setSampleRate, samplesPerBuffer );

		/*
		 * And now actually play the sound.
		 * can be repeated when under debugger control.
		 */
		do
		{
			/* Play the sound the requested number of times */
			for ( repeat = 0; repeat < repeats; repeat++ )
			{
				WM_BOOL filled = FALSE;
                unsigned int startTime = GetMillisecondTimestamp( hDevice );
                unsigned int endTime;

				/* Fill buffer until they're all full */
				do
				{
					filled = WMAudioPlaySineWave( hDevice,
												  hStream,
												  frequency,
												  amplitude,
												  &wavegenCtx
												);
				} while ( filled );
                                 
				/* Wait for the current buffer to finish */
				do
				{
					MilliSleep( hDevice, 1 );
					intState = WMAudioCheckStreamInterrupts( hDevice, hStream );
                    endTime = GetMillisecondTimestamp( hDevice );
				}
				while ( 0 == intState && (endTime - startTime) < MAX_PATIENCE );

                if ( (endTime - startTime) >= MAX_PATIENCE )
                {
                    WM_TRACE( hDevice, ( "Timed out waiting for DMA to finish - skipping test" ) );
                    WMTEST_SKIP();
                }

				/* Check the end interrupt is set, and nothing else */
				WMTEST_BOOLEAN( WMAUDIO_INT_END == intState );
            
				/* Now handle the interrupt */
				WMAudioHandleInterrupt( hDevice, hStream );
			}
		}
		while ( audioPlayLoopForever  );

		/* 
		 * Clean up now that the test is finished.
		 */
        WMAudioStop( hDevice, hStream );

        WMAudioMuteAllOutputs( hDevice, TRUE );

        WMAudioEnableOutputPaths( hDevice, stream, FALSE );

        WMAudioPowerUp( hDevice,WM_POWER_AUDIO_PLAYBACK );

        WMAudioCloseStream( hDevice, hStream );
	}
	while ( streamControlLoopForever );

exit:
    /*
     * Enable the DMA Interrupts now we have finished.
     */
    WMAudioEnableDMAInterrupts( hDevice );
}
WMTEST_END
#endif /* WM_AUDIO_STREAM && WM_AUDIO_WAVEGEN */

#endif  /* WM_AUDIO && WM_TESTING */
/*------------------------------ END OF FILE ---------------------------------*/

⌨️ 快捷键说明

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