예제 #1
0
파일: record.cpp 프로젝트: naeems/bkvoice
int openMicAndStartRecording(DWORD threadId)
{
	int i;

	WAVEFORMATEX m_WaveFormatEx = getDefaultFormat();

	if( MMSYSERR_NOERROR != waveInOpen(&g_recordHandler,WAVE_MAPPER,&m_WaveFormatEx,threadId,0,CALLBACK_THREAD)){
		printf("ER1!\n");
		return -1;
	}

	for(i=0; i<MAXRECBUFFER; i++){
		rechead[i]=CreateWaveHeader();
		if(MMSYSERR_NOERROR != waveInPrepareHeader(g_recordHandler,rechead[i], sizeof(WAVEHDR)) ){
			printf("ER2!   i=%d\n", i);
			return -1;
		}

		if(MMSYSERR_NOERROR != waveInAddBuffer(g_recordHandler, rechead[i], sizeof(WAVEHDR))){
			printf("ER3!\n");
			return -1;
		}

		if(MMSYSERR_NOERROR != waveInStart(g_recordHandler)){
			printf("ER4!\n");	
			return -1;
		}
	}

	printf("Start recording OK!\n");
	return 0;
}
예제 #2
0
void RecordSound::PreCreateHeader()
{

for(int i=0;i<MAXRECBUFFER;i++)
rechead[i]=CreateWaveHeader();

isallocated=1;
}
예제 #3
0
LRESULT CRecordSound::OnStartRecording(WPARAM wParam, LPARAM lParam)
{
	DWORD mmReturn = 0;

	if(m_bRecording)
		return FALSE;

	if(!m_bRecording)
	{
		// open wavein device
		MMRESULT mmReturn = ::waveInOpen( &m_hRecord, WAVE_MAPPER,
			&m_WaveFormatEx, ::GetCurrentThreadId(), 0, CALLBACK_THREAD);
		if(mmReturn)
		{
			char errorbuffer[MAX_PATH];
			char errorbuffer1[MAX_PATH];
			waveInGetErrorText( mmReturn, 
								errorbuffer,
								MAX_PATH);
			sprintf(errorbuffer1,"WAVEIN:%x:%s",mmReturn,errorbuffer);

			AfxMessageBox(errorbuffer1);  

		}
	
			
		if(!mmReturn)
		{
			for(int i=0; i < m_nMaxInputBuffers; i++)
			{
				LPWAVEHDR lpHdr = CreateWaveHeader();
				mmReturn = ::waveInPrepareHeader(m_hRecord,lpHdr, sizeof(WAVEHDR));
				mmReturn = ::waveInAddBuffer(m_hRecord, lpHdr, sizeof(WAVEHDR));
				m_nInputBuffers++;
			}
			mmReturn = ::waveInStart(m_hRecord);
			if(mmReturn )
		{
			char errorbuffer[MAX_PATH];
			char errorbuffer1[MAX_PATH];
			waveInGetErrorText( mmReturn, 
								errorbuffer,
								MAX_PATH);
			sprintf(errorbuffer1,"WAVEIN:%x:%s",mmReturn,errorbuffer);

			AfxMessageBox(errorbuffer1);  

		}
	
			if(!mmReturn)
			{
				m_bRecording = TRUE;
				if(m_Player)
					m_Player->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING, 0, 0);				
					if(m_Writer)
					{
						char SendFileName[MAX_PATH];
						memset(SendFileName,0,MAX_PATH*sizeof(char));
						pwsf= (PWRITESOUNDFILE) new WRITESOUNDFILE;
						ZeroMemory(pwsf,sizeof(WRITESOUNDFILE));
						char *p = pwsf->lpszFileName;
						CString strdir,fullname;
						strdir.Format("%sXMTvoice",XMTVoiceFile.m_Path);
						CreateFileName(strdir.GetBuffer(0),".wav",SendFileName);
						strcpy(p,SendFileName);
						memcpy(&pwsf->waveFormatEx,&m_WaveFormatEx,sizeof(m_WaveFormatEx));
						m_Writer->PostThreadMessage(WM_WRITESOUNDFILE_FILENAME,0,(LPARAM)pwsf);
					}
		
				return ERROR_SUCCESS;
			}
		}
	

	}
	return TRUE;
}
예제 #4
0
LRESULT CRecordSound::OnSoundData(WPARAM wParam, LPARAM lParam)
{
	LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;

	if(lpHdr)
	{
		short int * lpInt = (short int*) lpHdr->lpData;
		DWORD cbRecorded = lpHdr->dwBytesRecorded;
		::waveInUnprepareHeader(m_hRecord, lpHdr, sizeof(WAVEHDR));

		ProcessSoundData(lpInt, cbRecorded/sizeof(short int));
// 		if ((cbRecorded==0)&&(m_bRecording == FALSE))//无声音缓冲进入
// 		{
// 			TRACE("firsttime =TRUE\n");
// 			firsttime =TRUE;
// 		}
		if(m_Writer)
		{
			WAVEHDR* pWriteHdr = new WAVEHDR;
			if(!pWriteHdr)
				return FALSE;
			memcpy(pWriteHdr,lpHdr,sizeof(WAVEHDR));
			BYTE * pSound = new BYTE[lpHdr->dwBufferLength];
			if(!pSound)
			{
				if (pWriteHdr)
				{
					delete pWriteHdr;
				}
				
				return FALSE;
			}
			memcpy(pSound,lpHdr->lpData,lpHdr->dwBufferLength);
			pWriteHdr->lpData = (char*)pSound;
			m_Writer->PostThreadMessage(WM_WRITESOUNDFILE_WRITEBLOCK,GetCurrentThreadId(),(LPARAM) pWriteHdr);
		}
		

		if(m_Player)
		{
	
			m_Player->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK, GetCurrentThreadId(),(LPARAM) lpHdr);
		
		}
		else
		{
			if (lpHdr->lpData)
			{
				delete lpHdr->lpData;
				lpHdr->lpData = NULL;
			}
			if (lpHdr)
			{
				delete lpHdr;
				lpHdr = NULL;
			}
			
			
		}
		char debugbuffer[256];
		sprintf(debugbuffer, "SOUND BUFFER returned: %d\n",cbRecorded);
		TRACE(debugbuffer);
		if(m_bRecording)
		{
			
			LPWAVEHDR lpHdr = CreateWaveHeader();
			::waveInPrepareHeader(m_hRecord,lpHdr, sizeof(WAVEHDR));
			::waveInAddBuffer(m_hRecord, lpHdr, sizeof(WAVEHDR));
			m_nInputBuffers++;
		}
	}


	return ERROR_SUCCESS;
}
예제 #5
0
/*---------------------------------------------------------------------------------*
   FUNCTION  : main
   Purpose   : program entry point
   Arguments : argc - argument count
   Globals   : none
   Return    : 0 for successful execution
               none-zero for any error
*---------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
    CONVERT *cp=NULL;
    WAVEHEADER *whp1=NULL, *whp2=NULL;
    int fd1=0, fd2=0, i;
    long total_bytes_written=0;
    
    
    cp = ConvertCreate(inputBuffer, outputBuffer);  /* intialize the CONVERT structure */

    ParseCommandLine(argc,argv, cp);        /* parse the command line options */
    CheckFiles(cp);                         /* validate the input/output file */

    fd1=_open(cp->inputFilename,_O_RDONLY | _O_BINARY );
    if(fd1==-1)     Error("Opening INPUT FILE \"%s\" \n",cp->inputFilename);
    
    fd2=_open(cp->outputFilename,_O_WRONLY | _O_CREAT | _O_EXCL | _O_BINARY, _S_IREAD | _S_IWRITE);
    
    if( errno == EEXIST )       /* if OVERWRITE option not specified, give prompt */
    {
        char ch;
        if      (cp->overWrite == FALSE) 
        {
            fprintf(stderr, "Target %s exists. Overwrite? ", cp->outputFilename );
            ch = getch();
            if( (ch == 'y') || (ch == 'Y') )
                fd2 = _open( cp->outputFilename, _O_BINARY | _O_WRONLY |
                                _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE );
            else return 0;
            fprintf(stderr, "\n" );
        }
        else if (cp->overWrite == TRUE)
            fd2 = _open( cp->outputFilename, _O_BINARY | _O_WRONLY |
                            _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE );
    }
    else if(fd2==-1)     Error("Opening OUTPUT FILE \"%s\" \n",cp->outputFilename);
    

    /* if output type is specified as WAV 8/16 bit, set the WAV format header */
    if( (cp->outputType == WAV8) || (cp->outputType == WAV16) )
    {
        long nSamplesPerSec=11025;
        int  wBitsPerSample= (cp->outputType==WAV8)? 8: 16;
        
        switch(cp->outputSampleRate) {
            case 11: nSamplesPerSec = 11025; break;
            case 22: nSamplesPerSec = 22050; break;
            case 44: nSamplesPerSec = 44100; break;
        }
        whp2  = CreateWaveHeader( nSamplesPerSec, wBitsPerSample );
        if( _write(fd2, whp2, sizeof(WAVEHEADER)) == -1 ) Error("Writing OUTPUT FILE %s",cp->outputFilename);
    }

    /* this is done again for input type WAVxx, since we'll be checking the input file */
    /* if input type is specified as WAV 8/16 bit, set the WAV format header, check it against actual input file */    
    if( (cp->inputType == WAV8) || (cp->inputType == WAV16) )
    {
        whp1  = CreateWaveHeader( 0L, 0 );
        if( _read(fd1, whp1, sizeof(WAVEHEADER)) == -1 ) Error("Reading INPUT FILE %s",cp->inputFilename);
        CheckWaveHeader(whp1);
        
        cp->inputType = (whp1->wBitsPerSample==8)? WAV8: WAV16;
        cp->inputSampleRate = (int)(whp1->nSamplesPerSec/1000L);
        
        SetRateType(cp);
//        if (cp->convertRateCb) CVC_ConvertDestroy(cp->convertRateCb);
        
        if(cp->rateType != CVT_CONV01TO01)        
            cp->convertRateCb = CVC_ConvertCreate(cp->rateType);        
        
//        if (cp->inputTypeCb)  CVC_WaveDestroy(cp->inputTypeCb);
        if (cp->inputType == WAV8)
            { cp->inputTypeCb = CVC_WaveCreate(CVT_MONO08); cp->linInpRatio = 2.0; }   
        else if (cp->inputType == WAV16)                  
            { cp->inputTypeCb = CVC_WaveCreate(CVT_MONO16); cp->linInpRatio = 1.0; }  

    }          
        /* calculate the maximum size allow per conversion in the loop */
        cp->maxInpSize = min( (unsigned int)floor(INPBUFLEN/cp->linInpRatio),
                            (unsigned int)floor((double)INPBUFLEN/cp->outputSampleRate*cp->inputSampleRate));

    
    for (i=0; i < 24; i++) putchar('\n');
    printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n");
    printf("º RHETOREX           º          CONVERT UTILITY          º  Version: 1.00     º\n");
    printf("ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n");
    printf(" input  filename     :   %s\n",          cp->inputFilename       );
    printf(" input  type         :   %s\n",          TypeStr(cp->inputType)  );
    printf(" input  sample rate  :   %2d Khz\n",    cp->inputSampleRate     );
    printf("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
    printf(" output filename     :   %s\n",          cp->outputFilename      );
    printf(" output type         :   %s\n",          TypeStr(cp->outputType) );  
    printf(" output sample rate  :   %2d Khz\n",    cp->outputSampleRate    );  
    printf("ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n");
    printf("\n                          CONVERTING PLEASE WAIT \n");  
                    
    /* read each block of data upto maxInpSize, do the conversion & write result to output file */
    while((cp->inpBufSize=_read(fd1, inputBuffer,cp->maxInpSize<<1)) > 0)
    {
        cp->inpBufSize >>=1;
        cp->inpBufPtr=inputBuffer;
        cp->outBufPtr=outputBuffer;
        ConvertToLin(cp);               /* 1st, convert it to LIN */

        cp->inpBufPtr=outputBuffer;     /* switch the input <-> output */
        cp->outBufPtr=inputBuffer;
        cp->inpBufSize = *cp->outBufLen;
        ConvertRate(cp);                /* 2nd, convert the LIN to appropriate rate */

        cp->inpBufPtr=inputBuffer;      /* switch the output <-> input */
        cp->outBufPtr=outputBuffer;
        cp->inpBufSize = *cp->outBufLen;
        ConvertFromLin(cp);             /* finally, convert LIN to destination format */
        
        /* write output buffer to file */
        total_bytes_written += _write(fd2, outputBuffer,sizeof(outputBuffer[0])*(*cp->outBufLen));
        putchar('.');
    }
    
    /* if ouput type is specified as WAV 8/16 bit, must update the  header file size */
    if( (cp->outputType == WAV8) || (cp->outputType == WAV16) )
        UpdateWaveHeaderSize(cp->outputFilename, total_bytes_written);

    printf("\n\n");
    printf("------------------------------------------------------------------------------\n");
    printf(" input  file length  :   %5ld bytes\n", _filelength(fd1) );  
    printf(" output file length  :   %5ld bytes\n", _filelength(fd2) );  
    printf("------------------------------------------------------------------------------\n");

    
    /* close file descriptors used and free all structures */
    _close(fd1); _close(fd2);    
 
    ConvertDestroy(cp);
    if (whp1) DestroyWaveHeader(whp1);
    if (whp2) DestroyWaveHeader(whp2);
    return 0; 
}