Jump to content
  • Advertisement
Sign in to follow this  
Starfox

Problem with simple OpenAL program

This topic is 3810 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

Hi, I'm trying to run a simple OpenAL program, just to make sure things are running right. I should be hearing some random noise form my speakers, but that isn't happening. Any idea why?
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>			

#include <al/al.h>
#include <al/alc.h>

#include <vector>
#include <fstream>

using namespace std;

ALuint Buffer;

ALuint Source;



ALboolean LoadALData()
{

	// Load wav data into a buffer.

	alGenBuffers(1, &Buffer);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;

	const int DataSize = 1024*1024*40;

	unsigned char* data = new unsigned char [DataSize];
	memset(data, 0xAAAAAAAA, DataSize);
	alBufferData(Buffer, AL_FORMAT_MONO8, data, DataSize, 16000);

	// Bind the buffer with the source.

	alGenSources(1, &Source);

	if(alGetError() != AL_NO_ERROR)
		return AL_FALSE;

	alSourcei (Source, AL_BUFFER,   Buffer   );
	alSourcei (Source, AL_LOOPING,  AL_TRUE     );

	// Do another error check and return.

	if(alGetError() == AL_NO_ERROR)
		return AL_TRUE;

	return AL_FALSE;
}

void KillALData()
{
	alDeleteBuffers(1, &Buffer);
	alDeleteSources(1, &Source);
}




int main(int argc, char *argv[])
{
    printf("MindCode's OpenAL Lesson 1: Single Static Source\n\n");
	printf("Controls:\n");
	printf("p) Play\n");
	printf("s) Stop\n");
	printf("h) Hold (pause)\n");
	printf("q) Quit\n\n");

	ALCdevice* Device = alcOpenDevice(NULL);
	if(!Device)
	{
		printf("No device");
	}
	ALCcontext* pContext;

	pContext = alcCreateContext(Device, NULL);
	if(!pContext)
	{
		printf("No context");
	}

	alcMakeContextCurrent(pContext);
	if(alGetError() != AL_NO_ERROR)
	{
		printf("AL Error!");
	}

	// Load the wav data.

	if(LoadALData() == AL_FALSE)
	{
	    printf("Error loading data.");
		return 0;
	}

	atexit(KillALData);
	printf("X\n");

	// Loop.

	ALubyte c = ' ';

	while(c != 'q')
	{
		if(kbhit())
		{
			c = getche();
		}

		switch(c)
		{
			// Pressing 'p' will begin playing the sample.

			case 'p': alSourcePlay(Source); break;

			// Pressing 's' will stop the sample from playing.

			case 's': alSourceStop(Source); break;

			// Pressing 'h' will pause the sample.

			case 'h': alSourcePause(Source); break;
		};

	}

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I dont think you can play anything,you didnt initialize inputcapture,and if you want to play stream you have to use queuing buffers.So just read openal
guide carefully,good luck.

sorry for my english.

Share this post


Link to post
Share on other sites
I didn't check all the OpenAL commands, but I think the way you fill data is the problem. You set all the data so the same value (on the memset line). That means that all the bytes hold the same frequency, and you just listen the difference of frequency from sample to sample. Since all samples are the same, you can't hear anything. Instead of that, try looping trhough the data setting some random values.

Share this post


Link to post
Share on other sites
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!