Trouble recording sound

This is a discussion on Trouble recording sound within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys, hopefully you're not getting sick of me yet. basically, I'm trying to record sound coming in from the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    52

    Trouble recording sound

    Hi guys,

    hopefully you're not getting sick of me yet. basically, I'm trying to record sound coming in from the line-in socket. I've seen quite a few C++ examples but I haven't seen any C ones and I don't particularly won't to go delving into C++ just for the sake of one thing I want to do. I'm using Microsoft Visual Studio 6 Professional edition and running Windows XP SP2.

    Below is the code I currently have. When startRecording() is called, "wim open" is displayed but I'm a bit stuck there, am I supposed to load all the data coming through into memory and if so, how do I go about doing that, I've tried looking through the FAQ, all the forums, on MSDN and I guess I'm just stupid (or very sleep deprived). Basically, once it finished recording I want it saved as a .wav file, but I'll try to figure that out later, baby steps is the key

    By the way, I've used waveInGetDevCaps() to store the data of the input device already.

    Code:
    /* in sound.h */
    WAVEFORMATEX pcmWaveFormat;
    HWAVEIN m_hRecord;
    LPWAVEHDR pHdr;
    
    /* part of sound.c */
    
    char buf[512];
    
    void CALLBACK m_ThreadID(HWAVEIN hwi, UINT uMessage, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
    {
    	switch(uMessage) {
    		case MM_WIM_OPEN:
    			sprintf(buf, "wim open");
    			break;
    		case MM_WIM_CLOSE:
    			sprintf(buf, "wim close");
    			break;
    		case MM_WOM_DONE:
    			sprintf(buf, "wom done");
    			break;
    		case MM_WIM_DATA:
    			sprintf(buf, "wim data");
    			break;
    		default:
    			break;
    	}
    	MessageBox(NULL, buf, "Debug", MB_OK);
    }
    
    void startRecording(void)
    {
    	char err[256] = "";
    	int i = 0;
    
    	pHdr = (LPWAVEHDR) GlobalLock(GlobalAlloc(GHND, sizeof(WAVEHDR)));
    
    //	if(isPlaying) {
    //		MessageBox(hwndMain, "You are currently playing a file", "Error", MB_ICONERROR|MB_OK);
    //		return;
    //	}
    //	if(isRecording) {
    //		MessageBox(hwndMain, "You are already recording a file", "Error", MB_ICONERROR|MB_OK);
    //		return;
    //	}
    
    	isRecording = 1;
    
    	m_hRecord = NULL;
    
    	pcmWaveFormat.wFormatTag	= WAVE_FORMAT_PCM;
    	pcmWaveFormat.cbSize		= 0;
    	pcmWaveFormat.wBitsPerSample = 16;
    	pcmWaveFormat.nSamplesPerSec = 22050;
    	pcmWaveFormat.nChannels = 2;
    	pcmWaveFormat.nAvgBytesPerSec	= pcmWaveFormat.nSamplesPerSec*(pcmWaveFormat.wBitsPerSample/8);
    	pcmWaveFormat.nBlockAlign		= pcmWaveFormat.nChannels     *(pcmWaveFormat.wBitsPerSample/8);
    
    	if(i = waveInOpen(&m_hRecord, WAVE_MAPPER, &pcmWaveFormat, (DWORD)m_ThreadID, 0, CALLBACK_FUNCTION))
    	{
    		waveInGetErrorText(i, err, 255);
    		sprintf(buf, "WAVEIN Open (%d): %s", i, err);
    		MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
    		return;
    	}
    
    	if(i = waveInPrepareHeader(m_hRecord, pHdr, sizeof(WAVEHDR)))
    	{
    		waveInGetErrorText(i, err, 255);
    		sprintf(buf, "WAVEIN PrepareHeader (%d): %s", i, err);
    		MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
    		return;
    	}
    
    	if(i = waveInAddBuffer(m_hRecord, pHdr, sizeof(WAVEHDR)))
    	{
    		waveInGetErrorText(i, err, 255);
    		sprintf(buf, "WAVEIN AddBudder (%d): %s", i, err);
    		MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
    		return;
    	}
    
    	if(i = waveInStart(m_hRecord))
    	{
    		waveInGetErrorText(i, err, 255);
    		sprintf(buf, "WAVEIN InStart (%d): %s", i, err);
    		MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
    		return;
    	}
    
            /* save file */
    	/* clean up */
    	/* let them know it's done */
    	return;
    }
    I hope I'm going through the right process. There should be a library for this :P

    Thanks in advance,

    Daniel Wallace
    Last edited by Longie; 05-08-2005 at 10:18 PM. Reason: typo
    - Daniel Wallace

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    52
    Never mind guys, I took a look at FMOD and realised that it can be used for recording also.

    All sorted, including saving to a wav file (go wotsit)
    - Daniel Wallace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 12:58 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 03:33 PM
  4. recording sound from your computer
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-09-2003, 11:36 AM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21