📄 aaci_dma.c
字号:
#include <windows.h>
#include <pkfuncs.h>
#include <ceddk.h>
#include <oalfuncs.h>
#include <dma.h>
#include "oalintr.h"
#include "wavemdd.h"
#include "pdd_audio.h"
#define DebugMsg_Normal 0x01
#define DebugMsg_Important 0x02
#define DebugMsg_Trace 0x04
#define DebugMsgMask (DebugMsg_Important)
int ReleasePlayDMA(void);
int InitPlayDMA();
int AllocPlayDMA(void);
void GetPlayBuffer( unsigned int *buf );
int InitAll();
int StartPlayDMA( unsigned int bufphy, int size , int* actualbuffered );
int ReleaseAll();
int AllocRecordDMA(void);
int InitRecordDMA(void);
int StartRecordDMA( LPSTR buffer , DWORD size , int* actualbuffered);
DWORD AudioDMAThread(LPVOID p);
static HANDLE DmaPlayEvent = 0;
static HANDLE PlayCmdEvent = 0;
static volatile int WaitPlayBuffer = 0;
static HANDLE DmaRecordEvent = 0;
static HANDLE RecordCmdEvent = 0;
static HANDLE RemoveCmdEvent = 0;
static volatile int PlayBufferToRemove = 0;
static volatile int WaitRecordBuffer = 0;
static volatile int DMAPlayCmd;
static volatile int DMARecordCmd;
static int TotalPlayBufferCnt = 0;
static volatile int PlayBufferPlayIndex = 0;
static volatile int PlayBufferStoreIndex = 0;
static int PlayDMAChannelNumber = -1;
static int PlayDmaIntID = 0;
static int RecordDMAChannelNumber = -1;
static int RecordDmaIntID = 0;
static HANDLE DmaThread = 0;
static HANDLE DmaRecordThread = 0;
static HANDLE RemoveThread = 0;
#define RecordTempCnt 4
#define RecordTempSize 0x1000
#define MinRecordSize 160
static char RecordTemp[RecordTempCnt][RecordTempSize];
static int RecordSize[RecordTempCnt];
static int RecordIndex = 0;
static int RecordUsedIndex = 0;
extern void set_pa_on(void);
extern void set_pa_off(void);
WAVEFORMATEX RecordFormat;
static int playcount = 0;
static int playedcount = 0;
int InRemove = 0;
static int recordcount = 0;
static int recordedcount = 0;
#define DMA_PLAY_STOP 0
#define DMA_PLAY_START 1
#define DMA_PLAY_EXIT 2
#define DMA_RECORD_STOP 0
#define DMA_RECORD_START 1
#define DMA_RECORD_EXIT 2
//junxz
//#define SET_SLOT12_FALSE AACI_RegWrite32( AACIReg_Slot12Tx, 0x00000);
//#define SET_SLOT12_TRUE AACI_RegWrite32( AACIReg_Slot12Tx, 0xfffff);
//#define SET_SLOT12_FALSE AACI_RegWrite32( AACIReg_Slot12Tx, (AACI_CodecRead16(0x54)&(~0x06))<<4);
//#define SET_SLOT12_TRUE AACI_RegWrite32( AACIReg_Slot12Tx, (AACI_CodecRead16(0x54)|(0x06))<<4);
static int RecordOver = 0;
static char DebugMsg[1024*512];
static int MsgLength = 0;
CRITICAL_SECTION Critical;
static void StartDebugMsg()
{
MsgLength = 0;
memset( DebugMsg , 0 , sizeof(DebugMsg) );
}
static void EndDebugMsg()
{
return;
EnterCriticalSection( &Critical );
if( MsgLength )
{
char temp[256];
FILE* fp;
sprintf( temp , "save %d" , MsgLength );
fp = fopen( temp , "wb" );
if( fp )
{
fwrite( DebugMsg + MsgLength , 1 , sizeof(DebugMsg)-MsgLength , fp );
fwrite( DebugMsg , 1 , MsgLength , fp );
fclose( fp );
}
MsgLength = 0;
}
LeaveCriticalSection( &Critical );
}
static void AddDebugMsg( char* fmt, ... )
{
char temp[256];
int length;
va_list args;
return;
EnterCriticalSection( &Critical );
va_start(args, fmt);
vsprintf( temp , fmt , args);
length = strlen( temp );
if( MsgLength + length >= sizeof(DebugMsg) )
MsgLength = 0;
{
memcpy( DebugMsg+MsgLength , temp , length );
MsgLength += length;
DebugMsg[MsgLength++] = '\0';
}
LeaveCriticalSection( &Critical );
}
int OnInit()
{
int error = 0;
if( InitAll() != 0 )
error = 1;
if( error )
{
DMAPlayCmd = DMA_PLAY_EXIT;
DMARecordCmd = DMA_RECORD_EXIT;
}
else
{
DMAPlayCmd = DMA_PLAY_STOP;
DMARecordCmd = DMA_RECORD_STOP;
}
InitializeCriticalSection( &Critical );
return error;
}
int OnDeinit()
{
int error = 0;
ReleaseAll();
return error;
}
int OnRecordOpen()
{
int error = 0;
DMARecordCmd = DMA_RECORD_STOP;
recordcount = recordedcount = 0;
RecordIndex = RecordOver = 0;
StartDebugMsg();
RETAILMSG( (DebugMsgMask&DebugMsg_Normal) , (TEXT( "OnRecordOpen\r\n" ) ));
return error;
}
int OnRecordStart()
{
int error = 0;
DMARecordCmd = DMA_RECORD_START;
RecordIndex = RecordUsedIndex = 0;
SetEvent( RecordCmdEvent );
return error;
}
int OnRecordStop()
{
int error = 0;
DMARecordCmd = DMA_RECORD_STOP;
return error;
}
int OnRecordClose()
{
int error = 0;
EndDebugMsg();
DMARecordCmd = DMA_RECORD_STOP;
SetEvent( RecordCmdEvent );
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnRecordClose\r\n" ) ));
return error;
}
/*
int OnPlayOpen()
{
int error = 0;
PlayBufferPlayIndex = PlayBufferStoreIndex = 0;
DMAPlayCmd = DMA_PLAY_START;
playcount = playedcount = 0;
StartDebugMsg();
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayOpen\r\n" ) ));
return error;
}
int OnPlayStart()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_START;
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayStart\r\n" ) ));
return error;
}
int OnPlayStop()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_STOP;
MsgLength = 0;
SetEvent( PlayCmdEvent );
return error;
}
int OnPlayClose()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_STOP;
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayClose\r\n" ) ));
return error;
}
*/
int OnPlayOpen()
{
int error = 0;
PlayBufferPlayIndex = PlayBufferStoreIndex = 0;
DMAPlayCmd = DMA_PLAY_START;
playcount = playedcount = 0;
StartDebugMsg();
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayOpen\r\n" ) ));
if(lout)//如果当前为喇叭输出
AACI_CodecWrite16( 0x02, Volume&0x7fff );//控制喇叭寄存器(通过0x02控制音量)
else
AACI_CodecWrite16( 0x04, Volume&0x7fff);//控制耳机音量
playflags=TRUE;
if(lout==TRUE)
{
RETAILMSG(1, (TEXT( "OnPlayOpen---lout\r\n" ) ));
//SET_SLOT12_TRUE;
set_pa_on();
}
else
{
//SET_SLOT12_FALSE;
set_pa_off();
}
return error;
}
int OnPlayStart()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_START;
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayStart\r\n" ) ));
if(lout)//如果当前为喇叭输出
AACI_CodecWrite16( 0x02, Volume&0x7fff );//控制喇叭寄存器(通过0x02控制音量)
else
AACI_CodecWrite16( 0x04, Volume&0x7fff);//控制耳机音量
playflags=TRUE;
return error;
}
int OnPlayStop()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_STOP;
MsgLength = 0;
SetEvent( PlayCmdEvent );
AACI_CodecWrite16( 0x02, Volume|0x8000 );//控制喇叭寄存器(通过0x02控制音量)
AACI_CodecWrite16( 0x04, Volume|0x8000);//控制耳机音量
playflags=FALSE;
return error;
}
int OnPlayClose()
{
int error = 0;
DMAPlayCmd = DMA_PLAY_STOP;
RETAILMSG( (DebugMsgMask&DebugMsg_Normal), (TEXT( "OnPlayClose\r\n" ) ));
AACI_CodecWrite16( 0x02, Volume|0x8000 );//控制喇叭寄存器(通过0x02控制音量)
AACI_CodecWrite16( 0x04, Volume|0x8000);//控制耳机音量
playflags=FALSE;
if(lout==TRUE)
{
RETAILMSG(1, (TEXT( "OnPlayClose---lout\r\n" ) ));
//SET_SLOT12_FALSE;
set_pa_off();
}
return error;
}
int RemoveRecordBuffer( int toremove )
{
PWAVEHDR g_pwh;
int error = 0;
LOCK_GSI(WAPI_IN);
if( gsi[WAPI_IN].bStarted == TRUE )
{
g_pwh = gsi[WAPI_IN].pwh;
while( g_pwh && g_pwh->dwBytesRecorded == g_pwh->dwBufferLength )
g_pwh = g_pwh->lpNext;
while( toremove-- && g_pwh )
{
memcpy( g_pwh->lpData + g_pwh->dwBytesRecorded ,
RecordTemp[ RecordUsedIndex ] ,
RecordSize[ RecordUsedIndex ] );
g_pwh->dwBytesRecorded += RecordSize[ RecordUsedIndex ];
RecordUsedIndex = (RecordUsedIndex+1)%RecordTempCnt;
if( g_pwh->dwBytesRecorded == g_pwh->dwBufferLength )
{
MARK_BUFFER_DONE(g_pwh);
g_pwh->reserved = 0;
g_pwh = g_pwh->lpNext;
recordedcount ++;
if( recordcount == recordedcount )
RETAILMSG( 1 , (TEXT("record buffer over")));
}
}
RemoveCompleteBlocks(WAPI_IN);
}
else
{
error = 1;
}
UNLOCK_GSI(WAPI_IN);
return error;
}
int RemovePlayBuffer( int toremove )
{
PWAVEHDR g_pwh;
int error = 0;
LOCK_GSI(WAPI_OUT);
if( gsi[WAPI_OUT].bStarted == TRUE )
{
g_pwh = gsi[WAPI_OUT].pwh;
while( g_pwh && g_pwh->dwBytesRecorded == g_pwh->dwBufferLength)
g_pwh = g_pwh->lpNext;
while( toremove-- && g_pwh )
{
g_pwh->dwBytesRecorded = g_pwh->dwBufferLength;
g_pwh = g_pwh->lpNext;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -