OpenAL - How to play a .wav file?

Hi!

I downloaded & installed OpenAL SDK some time ago, thought it could be very useful, cross-platform audio API to be used in my game engine, which it is, but so far, but there is one problem: I cannot play the requested .wav file. To what I've understood, the code I've written manages to read the requested .wav file normally (chunk & data size, frequency, etc), but for some reason, playback is not working.

Here is the part of my source code, which I'm trying to test:

IDE: Code::Blocks
Compiler: GNU GCC

audio.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// ---------------------
/// @author God
#ifndef __TIM_AUDIO_HPP__
#define __TIM_AUDIO_HPP__
#include "tim_main.hpp"
/// ---------------------
namespace tim{
	class AudioSound{
	public:
        enum AUDIO{AUDIO_BUFFERS = 3};
		enum AUDIO_FILE{AUDIO_FILE_OGG, AUDIO_FILE_WAV};
		explicit AudioSound(const char* file, uint file_type);
		~AudioSound(void);
		void set_changes(void);
		AudioSound& set_orientation(float x = 0, float y = 0, float z = 0);
		AudioSound& set_position(float x = 0, float y = 0, float z = 0);
		AudioSound& set_velocity(float x = 0, float y = 0, float z = 0);
		void play(void);
	private:
		static bool load_file(tim::AudioSound& sound);
        ::ALboolean m_bLoop;
		byte* m_ucpBuffer;
		::ALenum m_iFormat;
		::ALfloat m_fProps[9];
		::ALuint m_uiBuffer, m_uiSource;
		FILE* m_opFile;
		const char* m_cpFile;
		uint m_uiTypeFile;
	};
}
#endif // __TIM_AUDIO_HPP__ 


audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "audio.hpp"

tim::AudioSound::AudioSound(const char* file, uint file_type) : m_bLoop(false), m_opFile(::fopen(file, "rb")), m_uiTypeFile(file_type){
	m_fProps[0] = m_fProps[1] = m_fProps[2] = m_fProps[3] = m_fProps[4] = m_fProps[5] = m_fProps[6] = m_fProps[7] = m_fProps[8] = 0.0f;
}
tim::AudioSound::~AudioSound(void){
	delete[] m_ucpBuffer;
	::fclose(m_opFile);
	::alDeleteSources(1, &m_uiSource);
    ::alDeleteBuffers(1, &m_uiBuffer);
}
bool tim::AudioSound::load_file(tim::AudioSound& sound){
	if(sound.m_uiTypeFile == tim::AudioSound::AUDIO_FILE_WAV){
		// Variable declarations & definitions:
		char __string[4] = {NULL};
		short __bits_psample, __bytes_psample, __channels, __type_format;
		ulong __avg__bytes__psec, __rate_sample, __size, __size_chunk, __size_data;

		// Check requested .wav file's data validness:
		::fread(__string, sizeof(char), 4, sound.m_opFile);
		if(!::strcmp(__string, "RIFF")){
            return false;
		}
		::fread(&__size, sizeof(ulong), 1, sound.m_opFile);
		::fread(__string, sizeof(char), 4, sound.m_opFile);
		if(!::strcmp(__string, "WAVE")){
            return false;
		}
		::fread(__string, sizeof(char), 4, sound.m_opFile);
		if(!::strcmp(__string, "fmt ")){
            return false;
		}

		// Read data from requested .wav file:
		::fread(&__size_chunk, sizeof(ulong), 1, sound.m_opFile);
		::fread(&__type_format, sizeof(short), 1, sound.m_opFile);
		::fread(&__channels, sizeof(short), 1, sound.m_opFile);
		::fread(&__rate_sample, sizeof(ulong), 1, sound.m_opFile);
		::fread(&__avg__bytes__psec, sizeof(ulong), 1, sound.m_opFile);
		::fread(&__bytes_psample, sizeof(short), 1, sound.m_opFile);
		::fread(&__bits_psample, sizeof(short), 1, sound.m_opFile);

		// Check requested .wav file's data:
		::fread(__string, sizeof(char), 4, sound.m_opFile);
		if(!::strcmp(__string, "data")){
            return false;
		}
		::fread(&__size_data, sizeof(ulong), 1, sound.m_opFile);
		
		// Memory allocation & handling for sound buffer:
		sound.m_ucpBuffer = new byte[__size_data];
		::fread(sound.m_ucpBuffer, sizeof(byte), __size_data, sound.m_opFile);

		// Sound generation & format check:
		alGenBuffers(1, &sound.m_uiBuffer);
		alGenSources(1, &sound.m_uiSource);
		if(__bits_psample == 8){
			if(__channels == 1){
				__type_format = AL_FORMAT_MONO8;
			}
			else if(__channels == 2){
				__type_format = AL_FORMAT_STEREO8;
			}
		}
		else if(__bits_psample == 16){
			if(__channels == 1){
				__type_format = AL_FORMAT_MONO16;
			}
			else if(__channels == 2){
				__type_format = AL_FORMAT_STEREO16;
			}
		}
		::ALuint __freq = __rate_sample;
		::alBufferData(sound.m_uiBuffer, __type_format, sound.m_ucpBuffer, __size_data, __freq);

		// Set source properties:
		::alSourcei(sound.m_uiSource, AL_BUFFER, sound.m_uiBuffer);
		::alSourcef(sound.m_uiSource, AL_PITCH, 1.0f);
		::alSourcef(sound.m_uiSource, AL_GAIN, 1.0f);
		::alSource3f(sound.m_uiSource, AL_POSITION, sound.m_fProps[3], sound.m_fProps[4], sound.m_fProps[5]);
		::alSource3f(sound.m_uiSource, AL_VELOCITY, sound.m_fProps[6], sound.m_fProps[7], sound.m_fProps[8]);
		::alSourcei(sound.m_uiSource, AL_LOOPING, sound.m_bLoop);
	}
	else{
		return false;
	}
	return true;
}
void tim::AudioSound::play(void){
	::alSourcePlay(m_uiSource);
}
void tim::AudioSound::set_changes(void){
	load_file(*this);
}
tim::AudioSound& tim::AudioSound::set_orientation(float x, float y, float z){
	m_fProps[0] = x;
	m_fProps[1] = y;
	m_fProps[2] = z;
	return *this;
}
tim::AudioSound& tim::AudioSound::set_position(float x, float y, float z){
	m_fProps[3] = x;
	m_fProps[4] = y;
	m_fProps[5] = z;
	return *this;
}
tim::AudioSound& tim::AudioSound::set_velocity(float x, float y, float z){
	m_fProps[6] = x;
	m_fProps[7] = y;
	m_fProps[8] = z;
	return *this;
}


game.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// ---------------------
/// @author God
#ifndef __TIM_GAME_HPP__
#define __TIM_GAME_HPP__
#include "tim_main.hpp"
/// ---------------------
namespace tim{
	class Game{
	public:
        Game(void);
		~Game(void);
	private:
		static void init_audio(void);
		static void uninit_audio(void);
		static ::ALCdevice* ms_opDevice;
		static ::ALCcontext* ms_opContext;
		static ::ALfloat ms_fProps[3][9];
	};
}
#endif // __TIM_GAME_HPP__ 


game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "game.hpp"

::ALCdevice* tim::Game::ms_opDevice;
::ALCcontext* tim::Game::ms_opContext;
::ALfloat tim::Game::ms_fProps[3][9] = {{0.0f}, {0.0f}};

tim::Game::Game(void){
	init_audio();
}
tim::Game::~Game(void){
	uninit_audio();
}
void tim::Game::init_audio(void){
    // Create context & device:
    ms_opDevice = ::alcOpenDevice(NULL);
    ms_opContext = ::alcCreateContext(ms_opDevice, NULL);
    ::alcMakeContextCurrent(ms_opContext);

    // Set listener properties:
    alListenerfv(AL_POSITION,ms_fProps[0]);
    alListenerfv(AL_VELOCITY, ms_fProps[1]);
    alListenerfv(AL_ORIENTATION, ms_fProps[2]);
}
void tim::Game::uninit_audio(void){
	::alcMakeContextCurrent(NULL);
	::alcDestroyContext(ms_opContext);
	::alcCloseDevice(ms_opDevice);
}


tim_main.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// ----------------------------------------------------------------------------------------------
/// @author God
#ifndef __TIM_MAIN_HPP__
#define __TIM_MAIN_HPP__
/// --------------------------------------
/// @include C standard library (C++-way):
/// --------------------------------------
#include <cmath>
#include <cstdio>
#include <stdlib.h>
#include <time.h>
/// ------------------------------
/// @include C++ standard library:
/// ------------------------------
#include <fstream>
#ifdef __TIM_DEBUG__
    #include <iostream>
#endif // __TIM_DEBUG__
#include <string>
#include <vector>
/// --------------------------------------
/// @include External headers & libraries:
/// --------------------------------------
/// OpenAL:
#include <AL/al.h>
#include <AL/alc.h>
/// ------------------------------------------------------------
/// @include Platform specific definitions, headers & libraries:
/// ------------------------------------------------------------
/// Windows
#elif (defined __WIN32__) || (defined _WIN32) || (defined _WIN64)
	#define __TIM_WINDOWS__
	#define WIN32_LEAN_AND_MEAN // Disable MFC.
	#include <windows.h>
	#include <d3d9.h>
	#include <XInput.h> // For Xbox 360 controller management.
	#if _MSC_VER >= 1 // Microsoft Visual C++ -specific library inclusions:
		#pragma comment(lib, "XInput.lib")
	#endif // _MSC_VER
#endif
/// ----------------------------------------------------------------------------------------------
namespace tim{
	/// -------------------------------------------
	/// @brief	General declarations & definitions:
	/// -------------------------------------------
	typedef unsigned char byte;
	typedef unsigned int uint;
	typedef unsigned long ulong;
	template<typename T>
	inline T& limit_into_range(T& variable, T const min, T const max){
		if(variable < min)
			variable = min;
		else if(variable > max)
			variable = max;
		return variable;
	}
#endif // __TIM_MAIN_HPP__
Last edited on
Topic archived. No new replies allowed.