📄 hwctxt.cpp
字号:
BOOL HardwareContext::InitInterruptThread()
{
BOOL bSuccess;
m_hAudioInterrupt = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!m_hAudioInterrupt)
{
return(FALSE);
}
bSuccess = InterruptInitialize(m_IntrAudio, m_hAudioInterrupt, NULL, 0);
m_hAudioInterruptThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL,
0,
(LPTHREAD_START_ROUTINE)CallInterruptThread,
this,
0,
NULL);
if (!m_hAudioInterruptThread)
{
return(FALSE);
}
// Bump up the priority since the interrupt must be serviced immediately.
CeSetThreadPriority(m_hAudioInterruptThread, GetInterruptThreadPriority());
return(TRUE);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: PowerUp()
Description: Powers up the audio codec chip.
Notes: Currently, this function is unimplemented because
the audio codec chip is ONLY powered up when the
user wishes to play or record. The AudioMute() function
handles the powerup sequence.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
void HardwareContext::PowerUp()
{
// SPI_Init();
I2S_Init();
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: PowerDown()
Description: Powers down the audio codec chip.
Notes: Even if the input/output channels are muted, this
function powers down the audio codec chip in order
to conserve battery power.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
void HardwareContext::PowerDown()
{
StopOutputDMA();
AudioMute((DMA_CH_OUT | DMA_CH_MIC), TRUE);
}
//############################################ Helper Functions #############################################
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: TransferOutputBuffer()
Description: Retrieves the next "mixed" audio buffer of data to
DMA into the output channel.
Returns: Number of bytes needing to be transferred.
-------------------------------------------------------------------*/
ULONG HardwareContext::TransferOutputBuffer(ULONG NumBuf)
{
ULONG BytesTransferred = 0;
PBYTE pBufferStart = m_Output_pbDMA_PAGES[NumBuf];
PBYTE pBufferEnd = pBufferStart + AUDIO_DMA_PAGE_SIZE;
PBYTE pBufferLast;
__try
{
pBufferLast = m_OutputDeviceContext.TransferBuffer(pBufferStart, pBufferEnd,NULL);
BytesTransferred = m_OutBytes[NumBuf] = pBufferLast-pBufferStart;
// Enable if you need to clear the rest of the DMA buffer
StreamContext::ClearBuffer(pBufferLast,pBufferEnd);
if(NumBuf == OUT_BUFFER_A) // Output Buffer A
{
m_OutputDMAStatus &= ~DMA_DONEA;
m_OutputDMAStatus |= DMA_STRTA;
}
else // Output Buffer B
{
m_OutputDMAStatus &= ~DMA_DONEB;
m_OutputDMAStatus |= DMA_STRTB;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
DEBUGMSG(ZONE_ERROR, (TEXT("WAVDEV2.DLL:TransferOutputBuffer() - EXCEPTION: %d"), GetExceptionCode()));
}
return BytesTransferred;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: TransferOutputBuffers()
Description: Determines which output buffer (A or B) needs to
be filled with sound data. The correct buffer is
then populated with data and ready to DMA to the
output channel.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
ULONG HardwareContext::TransferOutputBuffers(DWORD dwDCSR)
{
ULONG BytesTransferred = 0;
ULONG BytesTotal;
DWORD Bits = dwDCSR & (DMA_DONEA|DMA_DONEB|DMA_BIU);
// RETAILMSG(1,(TEXT("%x\n"),Bits));
switch (Bits)
{
case 0:
case DMA_BIU:
// No done bits set- must not be my interrupt
return 0;
case DMA_DONEA|DMA_DONEB|DMA_BIU:
// Load B, then A
BytesTransferred = TransferOutputBuffer(OUT_BUFFER_B);
// fall through
case DMA_DONEA: // This should never happen!
case DMA_DONEA|DMA_BIU:
BytesTransferred += TransferOutputBuffer(OUT_BUFFER_A); // charlie, A => B
break;
case DMA_DONEA|DMA_DONEB:
// Load A, then B
BytesTransferred = TransferOutputBuffer(OUT_BUFFER_A);
#if DMA_FLAG
// charlie
BytesTransferred += TransferOutputBuffer(OUT_BUFFER_B);
break; // charlie
#endif
// fall through
case DMA_DONEB|DMA_BIU: // This should never happen!
case DMA_DONEB:
// Load B
BytesTransferred += TransferOutputBuffer(OUT_BUFFER_B); // charlie, B => A
break;
}
// If it was our interrupt, but we weren't able to transfer any bytes
// (e.g. no full buffers ready to be emptied)
// and all the output DMA buffers are now empty, then stop the output DMA
BytesTotal = m_OutBytes[OUT_BUFFER_A]+m_OutBytes[OUT_BUFFER_B];
if (BytesTotal==0)
{
StopOutputDMA();
}
return BytesTransferred;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: TransferInputBuffer()
Description: Retrieves the chunk of recorded sound data and inputs
it into an audio buffer for potential "mixing".
Returns: Number of bytes needing to be transferred.
-------------------------------------------------------------------*/
ULONG HardwareContext::TransferInputBuffer(ULONG NumBuf)
{
ULONG BytesTransferred = 0;
PBYTE pBufferStart = m_Input_pbDMA_PAGES[NumBuf];
PBYTE pBufferEnd = pBufferStart + AUDIO_DMA_PAGE_SIZE;
PBYTE pBufferLast;
__try
{
pBufferLast = m_InputDeviceContext.TransferBuffer(pBufferStart, pBufferEnd,NULL);
BytesTransferred = m_InBytes[NumBuf] = pBufferLast-pBufferStart;
if(NumBuf == IN_BUFFER_A) // Input Buffer A
{
m_InputDMAStatus &= ~DMA_DONEA;
m_InputDMAStatus |= DMA_STRTA;
}
else // Input Buffer B
{
m_InputDMAStatus &= ~DMA_DONEB;
m_InputDMAStatus |= DMA_STRTB;
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
DEBUGMSG(ZONE_ERROR, (TEXT("WAVDEV2.DLL:TransferInputBuffer() - EXCEPTION: %d"), GetExceptionCode()));
}
return BytesTransferred;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: TransferInputBuffers()
Description: Determines which input buffer (A or B) needs to
be filled with recorded sound data. The correct
buffer is then populated with recorded sound data
from the input channel.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
ULONG HardwareContext::TransferInputBuffers(DWORD dwDCSR)
{
ULONG BytesTransferred=0;
DWORD Bits = dwDCSR & (DMA_DONEA|DMA_DONEB|DMA_BIU);
switch (Bits)
{
case 0:
case DMA_BIU:
// No done bits set- must not be my interrupt
return 0;
case DMA_DONEA|DMA_DONEB|DMA_BIU:
// Load B, then A
BytesTransferred = TransferInputBuffer(IN_BUFFER_B);
// fall through
case DMA_DONEA: // This should never happen!
case DMA_DONEA|DMA_BIU:
// Load A
BytesTransferred += TransferInputBuffer(IN_BUFFER_A);
break;
case DMA_DONEA|DMA_DONEB:
// Load A, then B
BytesTransferred = TransferInputBuffer(IN_BUFFER_A);
#if DMA_FLAG
BytesTransferred += TransferInputBuffer(IN_BUFFER_B);
break;
#endif
// fall through
case DMA_DONEB|DMA_BIU: // This should never happen!
case DMA_DONEB:
// Load B
BytesTransferred += TransferInputBuffer(IN_BUFFER_B);
break;
}
// If it was our interrupt, but we weren't able to transfer any bytes
// (e.g. no empty buffers ready to be filled)
// Then stop the input DMA
if (BytesTransferred==0)
{
StopInputDMA();
}
return BytesTransferred;
}
void HardwareContext::InterruptThread()
{
ULONG InputTransferred, OutputTransferred;
BOOL dmaInterruptSource = 0;
// Fast way to access embedded pointers in wave headers in other processes.
SetProcPermissions((DWORD)-1);
while(TRUE)
{
WaitForSingleObject(m_hAudioInterrupt, INFINITE);
DEBUGMSG(1,(TEXT("0x%x\n"),s2410INT->rINTMSK));
DEBUGMSG(1,(TEXT("start\n")));
dmaInterruptSource = 0;
//----- 1. Grab the lock -----
Lock();
__try
{
DEBUGMSG(1,(TEXT("try\n")));
//----- 3. Determine the interrupt source (input DMA operation or output DMA operation?) -----
//----- NOTE: Often, platforms use two separate DMA channels for input/output operations but
// have the OAL return SYSINTR_AUDIO as the interrupt source. If this is the case,
// then the interrupt source (input or output DMA channel) must be determined in
// this step.
// charlie, determine the interrupt source
// s2410INT->rSRCPND
if( s2410INT->rINTMSK & BIT_DMA1 ){
dmaInterruptSource |= DMA_CH_MIC; // Input DMA is supported...
}
if( s2410INT->rINTMSK & BIT_DMA2 ){
dmaInterruptSource |= DMA_CH_OUT; // Output DMA is supported...
}
// For determine the interrupt source
//----- 2. Acknowledge the DMA interrupt -----
InterruptDone(m_IntrAudio);
//----- 4. Handle any interrupts on the input source -----
// NOTE: The InterruptDone() call below automatically clears the interrupt.
// if((m_InputDMARunning) && (dmaInterruptSource == DMA_CH_MIC))
if((dmaInterruptSource == DMA_CH_MIC))
{
//----- Determine which buffer just completed the DMA transfer -----
if(m_InputDMAStatus & DMA_BIU)
{
m_InputDMAStatus &= ~DMA_STRTB; // Buffer B just completed...
m_InputDMAStatus |= DMA_DONEB;
m_InputDMAStatus &= ~DMA_BIU; // Buffer A is in use
#if DMA_FLAG
SELECT_AUDIO_DMA_INPUT_BUFFER_B();
#else
SELECT_AUDIO_DMA_INPUT_BUFFER_A();
#endif
DEBUGMSG(1,(TEXT("1\n")));
}else
{
m_InputDMAStatus &= ~DMA_STRTA; // Buffer A just completed...
m_InputDMAStatus |= DMA_DONEA;
m_InputDMAStatus |= DMA_BIU; // Buffer B is in use
#if DMA_FLAG
SELECT_AUDIO_DMA_INPUT_BUFFER_A();
#else
SELECT_AUDIO_DMA_INPUT_BUFFER_B();
#endif
DEBUGMSG(1,(TEXT("2\n")));
}
#if !DMA_FLAG
//----- 5. Schedule the next DMA transfer -----
AUDIO_IN_DMA_ENABLE();
#endif
//----- 6. Retrieve the next chunk of recorded data from the non-playing buffer -----
InputTransferred = TransferInputBuffers(m_InputDMAStatus);
}
//----- 7. Handle any interrupts on the output source -----
// NOTE: The InterruptDone() call below automatically clears the interrupt.
// if((m_OutputDMARunning) && (dmaInterruptSource == DMA_CH_OUT))
// if((dmaInterruptSource == DMA_CH_OUT))
else
{
//----- Determine which buffer just completed the DMA transfer -----
if(m_OutputDMAStatus & DMA_BIU)
{
m_OutputDMAStatus &= ~DMA_STRTB; // Buffer A just completed...
m_OutputDMAStatus |= DMA_DONEB;
m_OutputDMAStatus &= ~DMA_BIU; // Buffer B is in use
while((g_pDMAregs->rDSTAT2&0xfffff)==0);
#if DMA_FLAG
SELECT_AUDIO_DMA_OUTPUT_BUFFER_B(); // charlie. B => A
#else
SELECT_AUDIO_DMA_OUTPUT_BUFFER_A(); // charlie. B => A
#endif
}else
{
m_OutputDMAStatus &= ~DMA_STRTA; // Buffer B just completed...
m_OutputDMAStatus |= DMA_DONEA;
m_OutputDMAStatus |= DMA_BIU; // Buffer A is in use
while((g_pDMAregs->rDSTAT2&0xfffff)==0);
#if DMA_FLAG
SELECT_AUDIO_DMA_OUTPUT_BUFFER_A(); // charlie. B => A
#else
SELECT_AUDIO_DMA_OUTPUT_BUFFER_B(); // charlie. B => A
#endif
}
#if !DMA_FLAG
//----- 8. Schedule the next DMA transfer -----
AUDIO_OUT_DMA_ENABLE();
#endif
//----- 9. Fill the non-playing buffer with the next chunk of audio data to play -----
OutputTransferred = TransferOutputBuffers(m_OutputDMAStatus);
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
DEBUGMSG(ZONE_ERROR, (TEXT("WAVDEV2.DLL:InterruptThread() - EXCEPTION: %d"), GetExceptionCode()));
}
//----- 10. Give up the lock -----
Unlock();
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: AudioMute()
Description: Mutes/unmutes the specified audio channel.
Notes: If both audio channels are MUTED, then the chip
is powered down to conserve battery life.
Alternatively, if either audio channel is unMUTED,
the chip is powered up.
Returns: Boolean indicating success
-------------------------------------------------------------------*/
BOOL HardwareContext::AudioMute(DWORD channel, BOOL bMute)
{
static DWORD dwActiveChannel = 0;
return(TRUE);
}
void CallInterruptThread(HardwareContext *pHWContext)
{
pHWContext->InterruptThread();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -