Example #1
0
/**************************************************************************
 * 				mmioSetInfo  		[MMSYSTEM.1216]
 */
MMRESULT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16* lpmmioinfo, UINT16 uFlags)
{
    MMIOINFO            mmioinfo;
    MMRESULT            ret;

    TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);

    ret = mmioGetInfo(HMMIO_32(hmmio), &mmioinfo, 0);
    if (ret != MMSYSERR_NOERROR) return ret;

    /* check if seg and lin buffers are the same */
    if (mmioinfo.cchBuffer != lpmmioinfo->cchBuffer  ||
            mmioinfo.pchBuffer != MapSL((DWORD)lpmmioinfo->pchBuffer))
        return MMSYSERR_INVALPARAM;

    /* check pointers coherence */
    if (lpmmioinfo->pchNext < lpmmioinfo->pchBuffer ||
            lpmmioinfo->pchNext > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
            lpmmioinfo->pchEndRead < lpmmioinfo->pchBuffer ||
            lpmmioinfo->pchEndRead > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
            lpmmioinfo->pchEndWrite < lpmmioinfo->pchBuffer ||
            lpmmioinfo->pchEndWrite > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer)
        return MMSYSERR_INVALPARAM;

    mmioinfo.pchNext     = mmioinfo.pchBuffer + (lpmmioinfo->pchNext     - lpmmioinfo->pchBuffer);
    mmioinfo.pchEndRead  = mmioinfo.pchBuffer + (lpmmioinfo->pchEndRead  - lpmmioinfo->pchBuffer);
    mmioinfo.pchEndWrite = mmioinfo.pchBuffer + (lpmmioinfo->pchEndWrite - lpmmioinfo->pchBuffer);

    return mmioSetInfo(HMMIO_32(hmmio), &mmioinfo, uFlags);
}
Example #2
0
UINT WaveFile::Read (BYTE * pbDest, UINT cbSize)
{
    MMIOINFO mmioinfo;
    UINT cb;

    DOUT ("WaveFile::Read\n\r");

    // Use direct buffer access for reads to maximize performance
    if (m_mmr = mmioGetInfo (m_hmmio, &mmioinfo, 0))
    {
      goto READ_ERROR;
    }

    // Limit read size to chunk size
    cbSize = (cbSize > m_mmckiData.cksize) ? m_mmckiData.cksize : cbSize;

    // Adjust chunk size
    m_mmckiData.cksize -= cbSize;

    // Copy bytes from MMIO buffer
    for (cb = 0; cb < cbSize; cb++)
    {
      // Advance buffer if necessary
      if (mmioinfo.pchNext == mmioinfo.pchEndRead)
      {
        if (m_mmr = mmioAdvance (m_hmmio, &mmioinfo, MMIO_READ))
        {
          goto READ_ERROR;
        }

				if (mmioinfo.pchNext == mmioinfo.pchEndRead)
        {
          m_mmr = MMIOERR_CANNOTREAD;
          goto READ_ERROR;
        }
      }

      // Actual copy
//      *((BYTE*)pbDest+cb) = *((BYTE*)mmioinfo.pchNext)++;
			if( g_bBGMPlaying ) *(pbDest+cb) = *(mmioinfo.pchNext)++;
    }

    // End direct buffer access
    if (m_mmr = mmioSetInfo (m_hmmio, &mmioinfo, 0))
    {
      goto READ_ERROR;
    }

    // Successful read, keep running total of number of data bytes read
    m_nBytesPlayed += cbSize;
    goto READ_DONE;
    
READ_ERROR:
    cbSize = 0;

READ_DONE:
    return (cbSize);
}
Example #3
0
int WaveReadFile(
        HMMIO hmmioIn,                          // IN
        UINT cbRead,                            // IN           
        BYTE *pbDest,                           // IN
        MMCKINFO *pckIn,                        // IN.
        UINT *cbActualRead                      // OUT.        
        )
{
    MMIOINFO    mmioinfoIn;         // current status of <hmmioIn>
    int         nError=0;
    UINT        cT, cbDataIn;

    if ((nError = mmioGetInfo(hmmioIn, &mmioinfoIn, 0)) != 0)
    {
        goto ERROR_CANNOT_READ;
    }

    cbDataIn = cbRead;
    if (cbDataIn > pckIn->cksize) 
        cbDataIn = pckIn->cksize;       

    pckIn->cksize -= cbDataIn;

    for (cT = 0; cT < cbDataIn; cT++)
    {
        /* Copy the bytes from the io to the buffer. */
        if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
        {
            if ((nError = mmioAdvance(hmmioIn, &mmioinfoIn, MMIO_READ)) != 0)
            {
                goto ERROR_CANNOT_READ;
            } 
            if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
            {
                nError = ER_CORRUPTWAVEFILE;
                goto ERROR_CANNOT_READ;
            }
        }

        // Actual copy.
        *((BYTE*)pbDest+cT) = *((BYTE*)mmioinfoIn.pchNext)++;                                                                                                   
    }

    if ((nError = mmioSetInfo(hmmioIn, &mmioinfoIn, 0)) != 0)
    {
        goto ERROR_CANNOT_READ;
    }

    *cbActualRead = cbDataIn;
    goto FINISHED_READING;

ERROR_CANNOT_READ:
    *cbActualRead = 0;

FINISHED_READING:
    return(nError);

}
Example #4
0
//-----------------------------------------------------------------------------
// Name: CWaveFile::Close()
// Desc: Closes the wave file
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Close()
{
    if( m_dwFlags == WAVEFILE_READ )
    {
        mmioClose( m_hmmio, 0 );
        m_hmmio = NULL;

		if( !m_pResourceBuffer )
		{
			delete[] m_pResourceBuffer;
			m_pResourceBuffer = NULL;
		}

    }
    else
    {
        m_mmioinfoOut.dwFlags |= MMIO_DIRTY;

        if( m_hmmio == NULL )
            return CO_E_NOTINITIALIZED;

        if( 0 != mmioSetInfo( m_hmmio, &m_mmioinfoOut, 0 ) )
            return E_FAIL;

        // Ascend the output file out of the 'data' chunk -- this will cause
        // the chunk size of the 'data' chunk to be written.
        if( 0 != mmioAscend( m_hmmio, &m_ck, 0 ) )
            return E_FAIL;

        // Do this here instead...
        if( 0 != mmioAscend( m_hmmio, &m_ckRiff, 0 ) )
            return E_FAIL;

        mmioSeek( m_hmmio, 0, SEEK_SET );

        if( 0 != (INT)mmioDescend( m_hmmio, &m_ckRiff, NULL, 0 ) )
            return E_FAIL;

        m_ck.ckid = mmioFOURCC('f', 'a', 'c', 't');

        if( 0 == mmioDescend( m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK ) )
        {
            DWORD dwSamples = 0;
            mmioWrite( m_hmmio, (HPSTR)&dwSamples, sizeof(DWORD) );
            mmioAscend( m_hmmio, &m_ck, 0 );
        }

        // Ascend the output file out of the 'RIFF' chunk -- this will cause
        // the chunk size of the 'RIFF' chunk to be written.
        if( 0 != mmioAscend( m_hmmio, &m_ckRiff, 0 ) )
            return E_FAIL;

        mmioClose( m_hmmio, 0 );
        m_hmmio = NULL;
    }

    return S_OK;
}
Example #5
0
	~mmio_stream_raii() {

				data_info.dwFlags |= MMIO_DIRTY;
				CHECKED(mmioSetInfo( mmio, &data_info, 0 ));

			CHECKED(mmioAscend( mmio, &data_chunk, 0 ));

		CHECKED(mmioAscend( mmio, &WAVE_chunk, 0 ));

		CHECKED(mmioClose( mmio, 0 ));
		mmio = NULL;

	}
Example #6
0
void CWaveFile::CloseFile()
{
	// Close the file
	//

	mmioinfoOut.dwFlags |= MMIO_DIRTY;
	mmioSetInfo(hmmioOut, &mmioinfoOut, 0);

	mmioAscend(hmmioOut, &ckOut, 0);
	mmioAscend(hmmioOut, &ckOutRIFF, 0);

	mmioSeek(hmmioOut, 0, SEEK_SET); 
	mmioDescend(hmmioOut, &ckOutRIFF, NULL, 0);

	mmioClose(hmmioOut, 0);
}
Example #7
0
HRESULT WaveDecoder::Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead )
{
	MMIOINFO mmioinfoIn; // current status of m_hmmio

    if( m_hmmio == NULL )
        return CO_E_NOTINITIALIZED;
    if( pBuffer == NULL || pdwSizeRead == NULL )
        return E_INVALIDARG;

    if( pdwSizeRead != NULL )
        *pdwSizeRead = 0;

    if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) )
        return E_FAIL;

    UINT cbDataIn = dwSizeToRead;
    if( cbDataIn > m_ck.cksize )
        cbDataIn = m_ck.cksize;

    m_ck.cksize -= cbDataIn;

    for( DWORD cT = 0; cT < cbDataIn; cT++ )
    {
        // Copy the bytes from the io to the buffer.
        if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
        {
            if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) )
                return E_FAIL;

            if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
                return E_FAIL;
        }

        // Actual copy.
        *( ( BYTE* )pBuffer + cT ) = *( ( BYTE* )mmioinfoIn.pchNext );
        mmioinfoIn.pchNext++;
    }

    if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) )
        return E_FAIL;

    if( pdwSizeRead != NULL )
        *pdwSizeRead = cbDataIn;

    return S_OK;
}
Example #8
0
	/** ストリームを閉じる
	 *
	 * @author SAM (T&GG, Org.)<*****@*****.**>
	 * @date 2004/01/21 3:32:04
	 * Copyright (C) 2001,2002,2003,2004 SAM (T&GG, Org.). All rights reserved.
	 */
	HRslt WavFile::close() {
		if( !hmmio_ ) return CO_E_NOTINITIALIZED;
		if( flags_ & READ ) {
			mmioClose( hmmio_, 0 );
			hmmio_ = NULL;
		}
		else {
			write_mmioinfo_.dwFlags |= MMIO_DIRTY;
			if( 0 != mmioSetInfo( hmmio_, &write_mmioinfo_, 0 ) )
				return DXTRACE_ERR( TEXT("mmioSetInfo"), E_FAIL );
    
			// Ascend the output file out of the 'data' chunk -- this will cause
			// the chunk size of the 'data' chunk to be written.
			if( 0 != mmioAscend( hmmio_, &ck_, 0 ) )
				return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );
    
			// Do this here instead...
			if( 0 != mmioAscend( hmmio_, &ckriff_, 0 ) )
				return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );
        
			mmioSeek( hmmio_, 0, SEEK_SET );

			if( 0 != (int)mmioDescend( hmmio_, &ckriff_, NULL, 0 ) )
				return DXTRACE_ERR( TEXT("mmioDescend"), E_FAIL );
    
			ck_.ckid = mmioFOURCC('f','a','c','t');

			if( 0 == mmioDescend( hmmio_, &ck_, &ckriff_, MMIO_FINDCHUNK ) ) {
				DWORD dwSamples = 0;
				mmioWrite( hmmio_, (HPSTR)&dwSamples, sizeof(dwSamples) );
				mmioAscend( hmmio_, &ck_, 0 ); 
			}
    
			// Ascend the output file out of the 'RIFF' chunk -- this will cause
			// the chunk size of the 'RIFF' chunk to be written.
			if( 0 != mmioAscend( hmmio_, &ckriff_, 0 ) )
				return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );
    
			mmioClose( hmmio_, 0 );
			hmmio_ = NULL;
		}
		SAFE_FREE( buffer_ );
		SAFE_FREE( wfx_ );

		return S_OK;
	}
//-----------------------------------------------------------------------------
// Name: WaveCloseWriteFile()
// Desc: This routine will close a wave file used for writing.  
//-----------------------------------------------------------------------------
HRESULT WaveCloseWriteFile( HMMIO hmmioOut,       
                            MMCKINFO *pckOut,       
                            MMCKINFO *pckOutRIFF,   
                            MMIOINFO *pmmioinfoOut, 
                            DWORD dwSamples )
{    
    pmmioinfoOut->dwFlags |= MMIO_DIRTY;

    if( 0 != mmioSetInfo( hmmioOut, pmmioinfoOut, 0 ) )
        return E_FAIL;
    
    // Ascend the output file out of the 'data' chunk -- this will cause
    // the chunk size of the 'data' chunk to be written.
    if( 0 != mmioAscend( hmmioOut, pckOut, 0 ) )
        return E_FAIL;
    
    // Do this here instead...
    if( 0 != mmioAscend( hmmioOut, pckOutRIFF, 0 ) )
        return E_FAIL;
        
    mmioSeek( hmmioOut, 0, SEEK_SET );

    if( 0 != (INT)mmioDescend( hmmioOut, pckOutRIFF, NULL, 0 ) )
        return E_FAIL;
    
    pckOut->ckid = mmioFOURCC('f', 'a', 'c', 't');

    if( 0 == mmioDescend( hmmioOut, pckOut, pckOutRIFF, MMIO_FINDCHUNK ) ) 
    {
        mmioWrite( hmmioOut, (HPSTR)&dwSamples, sizeof(DWORD) );
        mmioAscend( hmmioOut, pckOut, 0 ); 
    }
    
    // Ascend the output file out of the 'RIFF' chunk -- this will cause
    // the chunk size of the 'RIFF' chunk to be written.
    if( 0 != mmioAscend( hmmioOut, pckOutRIFF, 0 ) )
        return E_FAIL;
    
    mmioClose( hmmioOut, 0 );

    return S_OK;   
}
Example #10
0
//-----------------------------------------------------------------------------
// Name: WaveReadFile()
// Desc: Reads wave data from the wave file. Make sure we're descended into
//       the data chunk before calling this function.
//          hmmioIn      - Handle to mmio.
//          cbRead       - # of bytes to read.   
//          pbDest       - Destination buffer to put bytes.
//          cbActualRead - # of bytes actually read.
//-----------------------------------------------------------------------------
HRESULT WaveReadFile( HMMIO hmmioIn, UINT cbRead, BYTE* pbDest,
                      MMCKINFO* pckIn, UINT* cbActualRead )
{
    MMIOINFO mmioinfoIn;         // current status of <hmmioIn>

    *cbActualRead = 0;

    if( 0 != mmioGetInfo( hmmioIn, &mmioinfoIn, 0 ) )
        return E_FAIL;
                
    UINT cbDataIn = cbRead;
    if( cbDataIn > pckIn->cksize ) 
        cbDataIn = pckIn->cksize;       

    pckIn->cksize -= cbDataIn;
    
    for( DWORD cT = 0; cT < cbDataIn; cT++ )
    {
        // Copy the bytes from the io to the buffer.
        if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
        {
            if( 0 != mmioAdvance( hmmioIn, &mmioinfoIn, MMIO_READ ) )
                return E_FAIL;

            if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
                return E_FAIL;
        }

        // Actual copy.
        *((BYTE*)pbDest+cT) = *((BYTE*)mmioinfoIn.pchNext);
        mmioinfoIn.pchNext++;
    }

    if( 0 != mmioSetInfo( hmmioIn, &mmioinfoIn, 0 ) )
        return E_FAIL;

    *cbActualRead = cbDataIn;
    return S_OK;
}
Example #11
0
	BOOL TinyWaveFile::Read(BYTE* lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead)
	{
		if (hmmio == NULL) return FALSE;
		*lpNumberOfBytesRead = 0;
		MMIOINFO mmioinfo;
		if (MMSYSERR_NOERROR != mmioGetInfo(hmmio, &mmioinfo, 0)) return FALSE;
		UINT cbDataIn = nNumberOfBytesToRead;
		if (cbDataIn > dwSize) cbDataIn = dwLSize;
		dwLSize -= cbDataIn;
		for (DWORD cT = 0; cT < cbDataIn; cT++)
		{
			if (mmioinfo.pchNext == mmioinfo.pchEndRead)
			{
				if (MMSYSERR_NOERROR != mmioAdvance(hmmio, &mmioinfo, MMIO_READ)) return FALSE;
				if (mmioinfo.pchNext == mmioinfo.pchEndRead) return FALSE;
			}
			*((BYTE*)lpBuffer + cT) = *((BYTE*)mmioinfo.pchNext);
			mmioinfo.pchNext++;
		}
		if (MMSYSERR_NOERROR != mmioSetInfo(hmmio, &mmioinfo, 0)) return FALSE;
		*lpNumberOfBytesRead = cbDataIn;
		return TRUE;
	}
Example #12
0
	/** 読み込み
	 *
	 * @author SAM (T&GG, Org.)<*****@*****.**>
	 * @date 2004/01/21 3:20:24
	 * Copyright (C) 2001,2002,2003,2004 SAM (T&GG, Org.). All rights reserved.
	 */
	HRslt WavFile::read( void *dst, size_t size, size_t * const readsize ) {
		if( !hmmio_ ) return CO_E_NOTINITIALIZED;
		if( !dst ) return E_INVALIDARG;

		if( readsize ) *readsize = 0;

		MMIOINFO mmioinfo; // current status of hmmio_
		if( 0 != mmioGetInfo( hmmio_, &mmioinfo, 0 ) )
			return DXTRACE_ERR( TEXT("mmioGetInfo"), E_FAIL );

		DWORD cbDataIn = (DWORD)size;
		if( cbDataIn > ck_.cksize ) cbDataIn = ck_.cksize;

		ck_.cksize -= cbDataIn;

		for( size_t cT = 0; cT < cbDataIn; cT++ ) {
			// Copy the bytes from the io to the buffer.
			if( mmioinfo.pchNext == mmioinfo.pchEndRead ) {
				if( 0 != mmioAdvance( hmmio_, &mmioinfo, MMIO_READ ) )
					return DXTRACE_ERR( TEXT("mmioAdvance"), E_FAIL );

				if( mmioinfo.pchNext == mmioinfo.pchEndRead )
					return DXTRACE_ERR( TEXT("mmioinfo.pchNext"), E_FAIL );
			}

			// Actual copy.
			*((uint8_t *)dst+cT) = *mmioinfo.pchNext;
			mmioinfo.pchNext++;
		}

		if( 0 != mmioSetInfo( hmmio_, &mmioinfo, 0 ) )
			return DXTRACE_ERR( TEXT("mmioSetInfo"), E_FAIL );

		if( readsize ) *readsize = cbDataIn;
		return S_OK;
	}
Example #13
0
//-----------------------------------------------------------------------------
// Name: CWaveFile::Read()
// Desc: Reads section of data from a wave file into pBuffer and returns 
//       how much read in pdwSizeRead, reading not more than dwSizeToRead.
//       This uses m_ck to determine where to start reading from.  So 
//       subsequent calls will be continue where the last left off unless 
//       Reset() is called.
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead )
{
    if( m_bIsReadingFromMemory )
    {
        if( m_pbDataCur == NULL )
            return CO_E_NOTINITIALIZED;
        if( pdwSizeRead != NULL )
            *pdwSizeRead = 0;

        if( (BYTE*)(m_pbDataCur + dwSizeToRead) > 
            (BYTE*)(m_pbData + m_ulDataSize) )
        {
            dwSizeToRead = m_ulDataSize - (DWORD)(m_pbDataCur - m_pbData);
        }
        
        CopyMemory( pBuffer, m_pbDataCur, dwSizeToRead );
        
        if( pdwSizeRead != NULL )
            *pdwSizeRead = dwSizeToRead;

        return S_OK;
    }
    else 
    {
        MMIOINFO mmioinfoIn; // current status of m_hmmio

        if( m_hmmio == NULL )
            return CO_E_NOTINITIALIZED;
        if( pBuffer == NULL || pdwSizeRead == NULL )
            return E_INVALIDARG;

        if( pdwSizeRead != NULL )
            *pdwSizeRead = 0;

        if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) )
            return DXTRACE_ERR( TEXT("mmioGetInfo"), E_FAIL );
                
        UINT cbDataIn = dwSizeToRead;
        if( cbDataIn > m_ck.cksize ) 
            cbDataIn = m_ck.cksize;       

        m_ck.cksize -= cbDataIn;
    
        for( DWORD cT = 0; cT < cbDataIn; cT++ )
        {
            // Copy the bytes from the io to the buffer.
            if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
            {
                if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) )
                    return DXTRACE_ERR( TEXT("mmioAdvance"), E_FAIL );

                if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
                    return DXTRACE_ERR( TEXT("mmioinfoIn.pchNext"), E_FAIL );
            }

            // Actual copy.
            *((BYTE*)pBuffer+cT) = *((BYTE*)mmioinfoIn.pchNext);
            mmioinfoIn.pchNext++;
        }

        if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) )
            return DXTRACE_ERR( TEXT("mmioSetInfo"), E_FAIL );

        if( pdwSizeRead != NULL )
            *pdwSizeRead = cbDataIn;

        return S_OK;
    }
}
//-----------------------------------------------------------------------------
// Name: CWaveFile::Read()
// Desc: Reads section of data from a wave file into pBuffer and returns
//       how much read in pdwSizeRead, reading not more than dwSizeToRead.
//       This uses m_ck to determine where to start reading from.  So
//       subsequent calls will be continue where the last left off unless
//       Reset() is called.
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead )
{
    if( m_bIsReadingFromMemory )
    {
        if( m_pbDataCur == NULL )
            return CO_E_NOTINITIALIZED;
        if( pdwSizeRead != NULL )
            *pdwSizeRead = 0;

        if( ( BYTE* )( m_pbDataCur + dwSizeToRead ) >
            ( BYTE* )( m_pbData + m_ulDataSize ) )
        {
            dwSizeToRead = m_ulDataSize - ( DWORD )( m_pbDataCur - m_pbData );
        }

#pragma warning( disable: 4616 )    // disable warning about warning number '22104' being out of range
#pragma warning( disable: 22104 )   // disable PREfast warning during static code analysis
        CopyMemory( pBuffer, m_pbDataCur, dwSizeToRead );
#pragma warning( default: 22104 )
#pragma warning( default: 4616 )

        if( pdwSizeRead != NULL )
            *pdwSizeRead = dwSizeToRead;

        return S_OK;
    }
    else
    {
        MMIOINFO mmioinfoIn; // current status of m_hmmio

        if( m_hmmio == NULL )
            return CO_E_NOTINITIALIZED;
        if( pBuffer == NULL || pdwSizeRead == NULL )
            return E_INVALIDARG;

        *pdwSizeRead = 0;

        if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) )
            return DXTRACE_ERR( L"mmioGetInfo", E_FAIL );

        UINT cbDataIn = dwSizeToRead;
        if( cbDataIn > m_ck.cksize )
            cbDataIn = m_ck.cksize;

        m_ck.cksize -= cbDataIn;

        for( DWORD cT = 0; cT < cbDataIn; cT++ )
        {
            // Copy the bytes from the io to the buffer.
            if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
            {
                if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) )
                    return DXTRACE_ERR( L"mmioAdvance", E_FAIL );

                if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
                    return DXTRACE_ERR( L"mmioinfoIn.pchNext", E_FAIL );
            }

            // Actual copy.
            *( ( BYTE* )pBuffer + cT ) = *( ( BYTE* )mmioinfoIn.pchNext );
            mmioinfoIn.pchNext++;
        }

        if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) )
            return DXTRACE_ERR( L"mmioSetInfo", E_FAIL );

        *pdwSizeRead = cbDataIn;

        return S_OK;
    }
}
Example #15
0
int SoundEvent::PlaySound(char *filenames,int newbuffer, double offset)
{
    HMMIO m_hmmio;
    HRESULT hr;

    TRACESETUP("PLAYSOUND");

	if(newbuffer)
	{

	m_hmmio = mmioOpen( filenames, NULL, MMIO_ALLOCBUF | MMIO_READ );

	if( NULL == m_hmmio )
	{
		TRACE ("DIRECT SOUND ERROR MMIOOPEN");
		return (false);
	}

    MMCKINFO        ckIn;           // chunk info. for general use.
    PCMWAVEFORMAT   pcmWaveFormat;  // Temp PCM structure to load in.

	WAVEFORMATEX* m_pwfx;        // Pointer to WAVEFORMATEX structure
    MMCKINFO      m_ck;          // Multimedia RIFF chunk
    MMCKINFO      m_ckRiff;      // Use in opening a WAVE file
    DWORD         m_dwSize;      // The size of the wave file
    MMIOINFO      m_mmioinfoOut;
    DWORD         m_dwFlags;

#define WAVEFILE_READ   1
#define WAVEFILE_WRITE  2
	
	
	   m_pwfx = NULL;

    if( ( 0 != mmioDescend( m_hmmio, &m_ckRiff, NULL, 0 ) ) )
	{
        TRACE ("DIRECT SOUND ERROR MMIODESCEND");
		return (false);
	}

    // Check to make sure this is a valid wave file
    if( (m_ckRiff.ckid != FOURCC_RIFF) ||
        (m_ckRiff.fccType != mmioFOURCC('W', 'A', 'V', 'E') ) )
	{
		return(false);
        TRACE ("DIRECT SOUND ERROR MMIOFOURCC");
	}

    // Search the input file for for the 'fmt ' chunk.
    ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
    if( 0 != mmioDescend( m_hmmio, &ckIn, &m_ckRiff, MMIO_FINDCHUNK ) )
	{
        TRACE("DIRECT SOUND ERROR MMIDESCENT FIND CHUNK");
		return(false);
	}

    // Expect the 'fmt' chunk to be at least as large as <PCMWAVEFORMAT>;
    // if there are extra parameters at the end, we'll ignore them
    if( ckIn.cksize < (LONG) sizeof(PCMWAVEFORMAT) )
	{
        TRACE ("DIRECT SOUND ERROR CHUNKSIZE");
		return(false);
	}

    // Read the 'fmt ' chunk into <pcmWaveFormat>.
    if( mmioRead( m_hmmio, (HPSTR) &pcmWaveFormat,
                  sizeof(pcmWaveFormat)) != sizeof(pcmWaveFormat) )
	{
        TRACE ("DIRECT SOUND ERROR MMIOREAD");
		return(false);
	}

    // Allocate the waveformatex, but if its not pcm format, read the next
    // word, and thats how many extra bytes to allocate.
    if( pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM )
    {
        m_pwfx = (WAVEFORMATEX*)new CHAR[ sizeof(WAVEFORMATEX) ];
        if( NULL == m_pwfx )
		{
            TRACE("DIRECT SOUND ERROR ALLOC");
			return(false);
		}

        // Copy the bytes from the pcm structure to the waveformatex structure
        memcpy( m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat) );
        m_pwfx->cbSize = 0;
    }
    else
    {
        // Read in length of extra bytes.
        WORD cbExtraBytes = 0L;
        if( mmioRead( m_hmmio, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD) )
		{
            TRACE ("DIRECT SOUND ERROR M_HMMIO");
			return(false);
		}

        m_pwfx = (WAVEFORMATEX*)new CHAR[ sizeof(WAVEFORMATEX) + cbExtraBytes ];
        if( NULL == m_pwfx )
		{
            TRACE("DIRECT SOUND ERREOR ALLOC 2");
			return(false);
		}

        // Copy the bytes from the pcm structure to the waveformatex structure
        memcpy( m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat) );
        m_pwfx->cbSize = cbExtraBytes;

        // Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
        if( mmioRead( m_hmmio, (CHAR*)(((BYTE*)&(m_pwfx->cbSize))+sizeof(WORD)),
                      cbExtraBytes ) != cbExtraBytes )
        {
            //SAFE_DELETE( m_pwfx );
            TRACE("DIRECT SOUND ERROR MMIOREAD2");
			return(false);
        }
    }

    // Ascend the input file out of the 'fmt ' chunk.
    if( 0 != mmioAscend( m_hmmio, &ckIn, 0 ) )
    {
        //SAFE_DELETE( m_pwfx );
        TRACE("DIRECT SOUND ERROR MMIOASCEND");
		return(false);
    }

	TRACE("DIRECTSOUND READMMIO OK");



    m_dwFlags = WAVEFILE_READ;

        if( m_dwFlags == WAVEFILE_READ )
        {
            // Seek to the data
            if( -1 == mmioSeek( m_hmmio, m_ckRiff.dwDataOffset + sizeof(FOURCC),
                            SEEK_SET ) )
			{
                TRACE ("DIRECT SOUND ERROR MMIOSEEK");
				return(false);
			}

            // Search the input file for the 'data' chunk.
            m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
            if( 0 != mmioDescend( m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK ) )
			{
                 TRACE ("DIRECT SOUND ERROR MMIODESCEND");
				 return(false);
			}
        }
        else
        {
            // Create the 'data' chunk that holds the waveform samples.
            m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
            m_ck.cksize = 0;

            if( 0 != mmioCreateChunk( m_hmmio, &m_ck, 0 ) )
			{
                TRACE("DIRECT SOUND ERROR MMIOCREATECHUNK");
				return(false);
			}

            if( 0 != mmioGetInfo( m_hmmio, &m_mmioinfoOut, 0 ) )
			{
                TRACE ("DIRECT SOUND ERROR MMMIOGETINFO");
				return(false);
			}
		}
  

	TRACE("DIRECTSOUND RESETFILE OK");

 
    // After the reset, the size of the wav file is m_ck.cksize so store it now
    m_dwSize = m_ck.cksize;


    char buffers[80];

	TRACE ("DIRECTSOUND TAILLE BUFFER");
	sprintf(buffers,"%d", m_dwSize);
	TRACE (buffers);


    DWORD                dwDSBufferSize = NULL;



    apDSBuffer = new LPDIRECTSOUNDBUFFER[1];
    if( apDSBuffer == NULL )
    {
        TRACE("ERROR DIRECTSOUND NEW BUFFER");
		return(false);
    }

    // Make the DirectSound buffer the same size as the wav file
    dwDSBufferSize = m_dwSize;

    // Create the direct sound buffer, and only request the flags needed
    // since each requires some overhead and limits if the buffer can
    // be hardware accelerated
    DSBUFFERDESC dsbd2;
    ZeroMemory( &dsbd2, sizeof(DSBUFFERDESC) );
    dsbd2.dwSize          = sizeof(DSBUFFERDESC);
    dsbd2.dwFlags         = 0;
    dsbd2.dwBufferBytes   = dwDSBufferSize;
    dsbd2.guid3DAlgorithm = GUID_NULL;
    dsbd2.lpwfxFormat     = m_pwfx;

TRACE ("APPEL CREATE SOUND BUFFER");

    // DirectSound is only guarenteed to play PCM data.  Other
    // formats may or may not work depending the sound card driver.
    hr = m_pDS->CreateSoundBuffer( &dsbd2, &apDSBuffer[0], NULL );
    if (hr != DS_OK)
	    TRACE ("ERROR DIRECTSOUND CREATE SOUND BUFFER")
	else TRACE("DIRECTSOUND CREATE SOUND BUFFER OK");


	

    // Make sure we have focus, and we didn't just switch in from
    // an app which had a DirectSound device

//    hr = RestoreBuffer( &apDSBuffer[0], NULL );
//    if (hr != DS_OK)
//	    TRACE ("ERROR DIRECTSOUND RESTORE SOUND BUFFER")
//	else TRACE("DIRECTSOUND RESTORE SOUND BUFFER OK");



    // Lock the buffer down
    hr = apDSBuffer[0]->Lock( 0, m_dwSize,
                     &pDSLockedBuffer, &dwDSLockedBufferSize,
                     NULL, NULL, 0L );
    if (hr != DS_OK)
		 TRACE ("ERROR DIRECTSOUND LOCK")
	else TRACE ("DIRECTSOUND LOCK OK");

    // Reset the wave file to the beginning


    // Seek to the data
    if( -1 == mmioSeek( m_hmmio, m_ckRiff.dwDataOffset + sizeof(FOURCC),
                            SEEK_SET ) )
         TRACE ("ERROR DIRECTSOUND MMIOSEEK")
	else TRACE ("DIRECTSOUND MMIOSSEEK OK");

    // Search the input file for the 'data' chunk.
    m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
    if( 0 != mmioDescend( m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK ) )
         TRACE ("ERROR DIRECTSOUND MMIODESCEND")
	else TRACE ("DIRECTSOUND MMIODESCEND OK");



    MMIOINFO mmioinfoIn; // current status of m_hmmio
    DWORD   dwWavDataRead        = 0;    // Amount of data read from the wav file


    dwWavDataRead = 0;

    if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) )
         TRACE ("ERROR DIRECTSOUND MMIOGETINFO")
	else TRACE ("DIRECTSOUND MMIOGETINFO OK");
        

    UINT cbDataIn = m_dwSize;
    if( cbDataIn > m_ck.cksize )
        cbDataIn = m_ck.cksize;

    m_ck.cksize -= cbDataIn;

    for( DWORD cT = 0; cT < cbDataIn; cT++ )
    {
            // Copy the bytes from the io to the buffer.
            if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
            {
                if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) )
                         TRACE ("ERROR DIRECTSOUND MMIOADVANCE")
                  	else /* TRACE ("DIRECTSOUND MMIOADVANCE OK")*/;


                if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
                     TRACE ("ERROR DIRECTSOUND READ FIC")
	            else /* TRACE ("DIRECTSOUND READ FIC OK") */;
            }

            // Actual copy.
            *((BYTE*)pDSLockedBuffer+cT) = *((BYTE*)mmioinfoIn.pchNext);
            mmioinfoIn.pchNext++;
    }

    if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) )
           TRACE ("ERROR DIRECTSOUND MMIOSETINFO")
	  else TRACE ("DIRECTSOUND MMIOSETINFO OK");

    mmioClose( m_hmmio, 0 );

    dwWavDataRead = cbDataIn;
    } // end of newbuffer part


//  Unlock the buffer, we don't need it anymore.
//  apDSBuffer[0]->Unlock( pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0 );


    if (offset > 0.)
    {
         DWORD curplay,curwrite;
		 char buffers[80];

         apDSBuffer[0]->GetCurrentPosition(&curplay,&curwrite);
         sprintf(buffers, "POS CURPLAY AVT %d", curplay);
         TRACE(buffers);

         curplay = DWORD (offset * 8000.);
	     hr = apDSBuffer[0]->SetCurrentPosition(curplay);
         if (hr != DS_OK)
		     TRACE ("ERROR DIRECTSOUND SETCURRENT")
	     else TRACE ("DIRECTSOUND SETCURRENT OK");

         apDSBuffer[0]->GetCurrentPosition(&curplay,&curwrite);
         sprintf(buffers, "POS CURPLAY APS %d", curplay);
         TRACE(buffers);
	}
Example #16
0
int WaveCloseWriteFile( 
HMMIO *phmmioOut, // (IN) 
MMCKINFO *pckOut, // (IN) 
MMCKINFO *pckOutRIFF, // (IN) 
MMIOINFO *pmmioinfoOut, // (IN) 
DWORD cSamples // (IN) 
) 
{ 
	int nError; 

	nError = 0; 

	if (*phmmioOut == NULL) return(0); 

	pmmioinfoOut->dwFlags |= MMIO_DIRTY;

	if ((nError = mmioSetInfo(*phmmioOut, pmmioinfoOut, 0)) != 0) 
	{ 
		// cannot flush, probably... 
		goto ERROR_CANNOT_WRITE; 
	} 

	/* Ascend the output file out of the 'data' chunk -- this will cause 
	* the chunk size of the 'data' chunk to be written. 
	*/ 

	if ((nError = mmioAscend(*phmmioOut, pckOut, 0)) != 0) 
		goto ERROR_CANNOT_WRITE; // cannot write file, probably 

	// Do this here instead...

	if ((nError = mmioAscend(*phmmioOut, pckOutRIFF, 0)) != 0) 
		goto ERROR_CANNOT_WRITE; // cannot write file, probably 

	nError = mmioSeek(*phmmioOut, 0, SEEK_SET); 

	if ((nError = (int)mmioDescend(*phmmioOut, pckOutRIFF, NULL, 0)) != 0) 
		goto ERROR_CANNOT_WRITE; 

	nError = 0; 

	pckOut->ckid = mmioFOURCC('f', 'a', 'c', 't'); 

	if ((nError = mmioDescend(*phmmioOut, pckOut, pckOutRIFF, MMIO_FINDCHUNK)) == 0) 
	{ 
		// If it didn't fail, write the fact chunk out, if it failed, not critical, just 
		// assert (below). 

		nError = mmioWrite(*phmmioOut, (HPSTR)&cSamples, sizeof(DWORD)); 
		nError = mmioAscend(*phmmioOut, pckOut, 0); 
		nError = 0; 
	} 
	else 
	{ 
		nError = 0; 
//		ASSERT(FALSE); 
	} 

	/* Ascend the output file out of the 'RIFF' chunk -- this will cause 
	* the chunk size of the 'RIFF' chunk to be written. 
	*/ 

	if ((nError = mmioAscend(*phmmioOut, pckOutRIFF, 0)) != 0) 
		goto ERROR_CANNOT_WRITE; // cannot write file, probably 

ERROR_CANNOT_WRITE: 
	if (*phmmioOut != NULL) 
	{ 
		mmioClose(*phmmioOut, 0); 
		*phmmioOut = NULL; 
	} 

	return(nError); 
}