Test Audio, testing CAudio.h
[mines3d.git] / sound / test / CAudio.h
blob505c9a35c5c603d43254470347de6e08bdd5c297
1 // CAudio.h
2 #ifndef __CAudio__
3 #define __CAudio__
4 #define NUM_SOURCES 2
5 #define NUM_BUFFERS 2
6 #define MUSIC_FAST 1
7 #define MUSIC_SLOW 2
8 #define SOUND_BUTTON_CLICK 1
9 #define SOUND_BUTTON_HOVER 2
10 class CAudio
12 public:
13 CAudio();
14 ~CAudio();
15 bool LoadAudioData(char *musicSource, char *buttonSource);
16 void PlayMusic(int id);
17 void PlaySound(int id);
18 private:
19 ALuint Buffers[NUM_BUFFERS]; // Buffers hold sound data.
20 ALuint Sources[NUM_SOURCES]; // Sources are points of emitting sound.
21 ALfloat SourcesPos[NUM_SOURCES][3]; // Position of the source sounds.
22 ALfloat SourcesVel[NUM_SOURCES][3]; // Velocity of the source sounds.
23 ALfloat ListenerPos[3] ;// Position of the listener.
24 ALfloat ListenerVel[3] ;// Velocity of the listener.
25 ALfloat ListenerOri[6] ;// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
28 CAudio::CAudio()
30 // Position of the listener.
31 ListenerPos[0]=0.0; //{ 0.0, 0.0, 0.0 }
32 ListenerPos[1]=0.0;
33 ListenerPos[2]=0.0;
35 // Velocity of the listener.
36 ListenerVel[0]=0.0; //{ 0.0, 0.0, 0.0 }
37 ListenerVel[1]=0.0;
38 ListenerVel[2]=0.0;
40 // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
41 ListenerOri[0]=0.0;// { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }
42 ListenerOri[1]=0.0;
43 ListenerOri[2]=-1.0;
44 ListenerOri[3]=0.0;
45 ListenerOri[4]=1.0;
46 ListenerOri[5]=0.0;
48 CAudio::~CAudio()
50 alDeleteBuffers(NUM_BUFFERS, Buffers);
51 alDeleteSources(NUM_SOURCES, Sources);
52 alutExit();
55 bool CAudio:: LoadAudioData(char *musicSource, char *buttonSource)
57 // Initialize OpenAL and clear the error bit.
58 alutInit(NULL, 0);
59 alGetError();
61 if(alGetError() != AL_NO_ERROR)
62 return AL_FALSE;
64 // Variables to load into.
65 ALenum format;
66 ALsizei size;
67 ALvoid* data;
68 ALsizei freq;
69 ALboolean loop;
71 // Load wav data into buffers.
72 alGenBuffers(NUM_BUFFERS, Buffers);
74 if(alGetError() != AL_NO_ERROR)
75 return AL_FALSE;
77 // Load music file
78 alutLoadWAVFile(musicSource, &format, &data, &size, &freq, &loop);
79 alBufferData(Buffers[MUSIC], format, data, size, freq);
80 alutUnloadWAV(format, data, size, freq);
82 // Load button audio file
83 alutLoadWAVFile(buttonSource, &format, &data, &size, &freq, &loop);
84 alBufferData(Buffers[BUTTON], format, data, size, freq);
85 alutUnloadWAV(format, data, size, freq);
87 // Bind buffers into audio sources.
89 alGenSources(NUM_SOURCES, Sources);
91 if(alGetError() != AL_NO_ERROR)
92 return AL_FALSE;
94 alSourcei (Sources[MUSIC], AL_BUFFER, Buffers[MUSIC] );
95 alSourcef (Sources[MUSIC], AL_PITCH, 1.0f );
96 alSourcef (Sources[MUSIC], AL_GAIN, 1.0f );
97 alSourcefv(Sources[MUSIC], AL_POSITION, SourcesPos[MUSIC]);
98 alSourcefv(Sources[MUSIC], AL_VELOCITY, SourcesVel[MUSIC]);
99 alSourcei (Sources[MUSIC], AL_LOOPING, AL_TRUE );
101 alSourcei (Sources[BUTTON], AL_BUFFER, Buffers[BUTTON] );
102 alSourcef (Sources[BUTTON], AL_PITCH, 1.0f );
103 alSourcef (Sources[BUTTON], AL_GAIN, 1.0f );
104 alSourcefv(Sources[BUTTON], AL_POSITION, SourcesPos[BUTTON]);
105 alSourcefv(Sources[BUTTON], AL_VELOCITY, SourcesVel[BUTTON]);
106 alSourcei (Sources[BUTTON], AL_LOOPING, AL_FALSE );
108 // Do another error check and return.
109 if(alGetError() != AL_NO_ERROR)
110 return AL_FALSE;
112 alListenerfv(AL_POSITION, ListenerPos);
113 alListenerfv(AL_VELOCITY, ListenerVel);
114 alListenerfv(AL_ORIENTATION, ListenerOri);
116 return AL_TRUE;
119 void CAudio::PlayMusic(void)
121 alSourcePlay(Sources[MUSIC]);
124 void CAudio::PlayButton(void)
126 alSourcePlay(Sources[BUTTON]);
128 #endif