OpenAL sound wont work!

Started by
1 comment, last by SolDirix 10 years, 6 months ago

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.

View my game dev blog here!

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.


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

View my game dev blog here!

This topic is closed to new replies.

Advertisement