Jump to content
  • Advertisement
Sign in to follow this  

[OpenAL] Music not playing

This topic is 3661 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'm trying to play an Ogg Vorbis sound file in a game I'm writing. The code compiles/links fine and the application loads the song and runs, but sound doesn't come from the speakers. Any help? Here's the relevant code.
  alutInit(&argc, argv);
  alGenBuffers(1, &bufferID);
  alGenSources(1, &soundSourceID);
  alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
  alSource3f(soundSourceID, AL_POSITION, 0.0f, 0.0f, 0.0f);
  loadSong(c_filename, songBuffer, soundFormat, frequency);
  alBufferData(bufferID, soundFormat, &songBuffer[0], static_cast <ALsizei> (songBuffer.size()), frequency);
  alSourcei(soundSourceID, AL_BUFFER, bufferID);
void loadSong(std::string filename, std::vector<char> songBuffer, ALenum &soundFormat, ALsizei &freq)
	std::ofstream debug("debug.txt");
	int endian = 0; //0 for litte-endian
	int bitstream;
	long numBytes;
	char array[BUFFER_SIZE];
	FILE *file;
	if (!(file = fopen("stairway.ogg", "rb")))
	  debug << "OH TEH NOES!!11!!!!" << std::endl;
	vorbis_info *info;
	OggVorbis_File soundFile;
	ov_open_callbacks(file, &soundFile, NULL, 0, OV_CALLBACKS_DEFAULT);
	info = ov_info(&soundFile, -1);
	if (info->channels == 1)
		soundFormat = AL_FORMAT_MONO16;
		soundFormat = AL_FORMAT_STEREO16;

	freq = info->rate;

		numBytes = ov_read(&soundFile, array, BUFFER_SIZE, endian, 2, 1, &bitstream);
		songBuffer.insert(songBuffer.end(), array, array + numBytes);
	} while (numBytes > 0);



Share this post

Link to post
Share on other sites
The problem is that you are passing songBuffer by value, so loadSong() operates on a copy of songBuffer and doesn't change the original. To fix this, pass it by non-const reference.

Also, you should pass fileName by const reference (not because it will fix anything, but because it's more efficient). Finally, loadSong() doesn't even use fileName?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!