Jump to content
  • Advertisement
Sign in to follow this  
Solid_Spy

OpenAL sound wont work!

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

Hello, I've been following some tutorials on how to get Openal to work in C++, however, I cannot get the sound to play, i'm certain I have the file loaded into memory, however it just wont play:

 


#include <iostream>
#include <al.h>
#include <alc.h>
#include <fstream>

#pragma comment(lib, "OpenAl32.lib")

struct WavHeader
{
	unsigned char riff[4];
	unsigned int chunkSize;
	unsigned char format[4];
	unsigned char subChunk1Id[4];
	unsigned int subChunk1Size;
	unsigned short audioFormat;
	unsigned short numChannels;
	unsigned int Samplerate;
	unsigned int byteRate;
	unsigned short blockAlign;
	unsigned short bitsPerSample;
	unsigned char subChunk2Id[4];
	unsigned int subChunk2Size;
};

int main()
{
	ALCdevice * dev;
	ALCcontext * conte;
	WavHeader wav;
	std::ifstream ifile;
	unsigned int fileSize;
	ALuint source;
	ALint type;
	ALuint buffer;
	ALuint frequency;
	ALenum format;
	alGenBuffers(1, &buffer);
	alGenSources(1, &source);

	unsigned char * soundFile;

	dev = alcOpenDevice(NULL);
	conte = alcCreateContext(dev, NULL);
	alcMakeContextCurrent(conte);
	ifile.open("untitled.wav", std::ios::in | std::ios::binary);

	ifile.read((char*)&wav, sizeof(wav));
	fileSize = wav.subChunk2Size;

	soundFile = new unsigned char[fileSize];
	ifile.seekg(sizeof(WavHeader));
	ifile.read(reinterpret_cast<char*>(&soundFile), sizeof(fileSize));
	ifile.close();
	alListener3f(AL_POSITION, 0, 0, 0);
	alListener3f(AL_VELOCITY, 0, 0, 0);
	alListener3f(AL_ORIENTATION, 0, 0, -1);

	if(wav.bitsPerSample == 8)
	{
		if(wav.numChannels == 1)
		{
			format = AL_FORMAT_MONO8;
		}
		else
		{
			format = AL_FORMAT_STEREO8;
		}
	}
	else
	{
		if(wav.numChannels == 1)
		{
			format = AL_FORMAT_MONO16;
		}
		else
		{
			format = AL_FORMAT_STEREO16;
		}
	}

	alSourcef(source, AL_PITCH, 1);
	alSourcef(source, AL_GAIN, 1);
	alSource3f(source, AL_POSITION, 0, 0, 0);
	alSource3f(source, AL_VELOCITY, 0, 0, 0);
	alSourcei(source, AL_LOOPING, AL_FALSE);

	std::cout << wav.riff[0] << wav.riff[1] << wav.riff[2] << wav.riff[3];
	std::cout << wav.subChunk2Size << std::endl;

	alBufferData(buffer, format, soundFile, fileSize, wav.Samplerate);
	alSourcei(source, AL_BUFFER, buffer);
	alSourcePlay(source);

	alGetSourcei(source, AL_SOURCE_STATE, &type);
	while(type == AL_PLAYING)
	{
		alGetSourcei(source, AL_SOURCE_STATE, &type);
	}

	while(true)
	{
	}

	return 0;
}


If anyone knows what i'm doing wrong, can anyone help me? All help be appreciated.

Edited by Solid_Spy

Share this post


Link to post
Share on other sites
Advertisement
alGenBuffers(1, &buffer);
alGenSources(1, &source);
 
These calls should occur after alcMakeContextCurrent
 
ifile.read(reinterpret_cast(&soundFile), sizeof(fileSize));

Here, you're reading sizeof(fileSize) bytes, which is the size of an unsigned int (4 bytes on my computer). You should be passing fileSize directly as the last parameter.

 

Share this post


Link to post
Share on other sites

 

alGenBuffers(1, &buffer);
alGenSources(1, &source);
 
These calls should occur after alcMakeContextCurrent
 
ifile.read(reinterpret_cast(&soundFile), sizeof(fileSize));

Here, you're reading sizeof(fileSize) bytes, which is the size of an unsigned int (4 bytes on my computer). You should be passing fileSize directly as the last parameter.

 

That fixed it! thanks :3

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!