Jump to content
  • Advertisement
Sign in to follow this  
Vectorg

OpenAL recording a wav

This topic is 3649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using some sample code from Creative Labs that shows how to record sounds using OpenAL. It's working somewhat, because I can hear a buzzing sound when I start it running, although the buzzing only happens when I play a looping sound. The sample plays back whatever is currently playing, but doesn't record any sound to anything other than air. How does one record this sound? Should a wav file be created from the buffers captured with OpenAL? My code is below. Thanks.
void start_recording(int flag)
{
	//flag 1 = start recording
	//flag 2 = continue recording each frame
	//flag 3 = stop recording
	static ALCdevice     *pCaptureDevice;
	static const ALCchar *szDefaultCaptureDevice;
	static ALuint ulUnqueueCount = 0;
	static ALuint ulQueueCount = 0;
	static ALint lFormat, lFrequency, lBlockAlignment;
	static ALboolean bPlay = AL_FALSE;
	static ALuint SourceID;
	static ALint lLoop;
	static ALuint BufferID[QUEUEBUFFERCOUNT];
	static ALint lSamplesAvailable;
	static ALboolean  bPlaying = AL_FALSE;
	static ALint lProcessed, lPlaying;
	static ALuint TempBufferID;
	static ALuint ulBuffersAvailable = QUEUEBUFFERCOUNT;
	static ALchar Buffer[QUEUEBUFFERSIZE];
	static long lTotalProcessed;
	static long lOldSamplesAvailable;
	static long lOldTotalProcessed;
	static int fh;

	if ( flag == 1 ) {
		// Generate a Source and QUEUEBUFFERCOUNT Buffers for Queuing
		alGetError();
		alGenSources(1, &SourceID);
		for (lLoop = 0; lLoop < QUEUEBUFFERCOUNT; lLoop++) alGenBuffers(1, &BufferID[lLoop]);
		if (alGetError() != AL_NO_ERROR) {
			Log("Failed to generate Source or Buffers\n");
			return;
		}
		const ALchar *pDeviceList = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
		if (pDeviceList) {
			Log("Available Capture Devices are:\n");
			while (*pDeviceList) {
				Log("**%s**\n", pDeviceList);
				pDeviceList += strlen(pDeviceList) + 1;
			}
		}
		szDefaultCaptureDevice = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
		Log("Default Capture Device is '%s'\n", szDefaultCaptureDevice);
		lFormat = AL_FORMAT_MONO8;
		lFrequency = 44100;
		lBlockAlignment = 2;
		lTotalProcessed = 0;
		lOldSamplesAvailable = 0;
		lOldTotalProcessed = 0;
		pCaptureDevice = alcCaptureOpenDevice(szDefaultCaptureDevice, lFrequency, lFormat, lFrequency);
		if (pCaptureDevice) {
			Log("Starting **%s** Capture Device\n", alcGetString(pCaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER));
			alcCaptureStart(pCaptureDevice);
			bPlay = AL_TRUE;
			fh = _open("recwav.wav", _O_BINARY | _O_CREAT | _O_TRUNC | _O_RDWR, _S_IWRITE);
		}
	}
	if ( flag == 2 ) {
		alGetError();
		alcGetIntegerv(pCaptureDevice, ALC_CAPTURE_SAMPLES, 1, &lSamplesAvailable);
		if ((lOldSamplesAvailable != lSamplesAvailable) || (lOldTotalProcessed != lTotalProcessed)) {
			lOldSamplesAvailable = lSamplesAvailable;
			lOldTotalProcessed = lTotalProcessed;
		}
		// If the Source is (or should be) playing, get number of buffers processed and check play status
		if (bPlaying) {
			alGetSourcei(SourceID, AL_BUFFERS_PROCESSED, &lProcessed);
			while (lProcessed) {
				lTotalProcessed++;
				alSourceUnqueueBuffers(SourceID, 1, &TempBufferID);
				if (++ulUnqueueCount == QUEUEBUFFERCOUNT) ulUnqueueCount = 0;
				ulBuffersAvailable++;
				lProcessed--;
			}
			// If the Source has stopped (been starved of data) it will need to be restarted
			alGetSourcei(SourceID, AL_SOURCE_STATE, &lPlaying);
			if (lPlaying == AL_STOPPED) {
				bPlay = AL_TRUE;
			}
		}
		if ((lSamplesAvailable > (QUEUEBUFFERSIZE / lBlockAlignment)) && !(ulBuffersAvailable)) {
			//OutputDebugString("underrun!\n");
		}
		else {
			// When we have enough data to fill our QUEUEBUFFERSIZE byte buffer, grab the samples
			if ((lSamplesAvailable > (QUEUEBUFFERSIZE / lBlockAlignment)) && (ulBuffersAvailable)) {
				alcCaptureSamples(pCaptureDevice, Buffer, QUEUEBUFFERSIZE / lBlockAlignment);
				alBufferData(BufferID[ulQueueCount], lFormat, Buffer, QUEUEBUFFERSIZE, lFrequency);
				_write(fh, Buffer, QUEUEBUFFERSIZE);
				alSourceQueueBuffers(SourceID, 1, &BufferID[ulQueueCount]);
				if (++ulQueueCount == QUEUEBUFFERCOUNT) ulQueueCount = 0;
				ulBuffersAvailable--;
				// If we need to start the Source do it now IF AND ONLY IF we have queued at least 2 buffers
				if ((bPlay) && (ulBuffersAvailable <= (QUEUEBUFFERCOUNT - 2))) {
					alSourcePlay(SourceID);
					Log("Buffer Starting\n");
					bPlaying = AL_TRUE;
					bPlay = AL_FALSE;
				}
			}
		}
	}
	if ( flag == 3 ) {
		_close(fh);
		alcCaptureCloseDevice(pCaptureDevice);
		alcCaptureStop(pCaptureDevice);
		alSourceStop(SourceID);
		alDeleteSources(1, &SourceID);
		for (lLoop = 0; lLoop < QUEUEBUFFERCOUNT; lLoop++)
		alDeleteBuffers(1, &BufferID[lLoop]);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!