OpenAL tutorial


SUBMITTED BY: Guest

DATE: Nov. 12, 2013, 2:48 a.m.

FORMAT: Text only

SIZE: 4.1 kB

HITS: 986

  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Playing 3D sound with OpenAL, and loading a wav file manually
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstring>
  6. #include <AL/al.h>
  7. #include <AL/alc.h>
  8. #include <SDL/SDL.h>
  9. bool isBigEndian()
  10. {
  11. int a=1;
  12. return !((char*)&a)[0];
  13. }
  14. int convertToInt(char* buffer,int len)
  15. {
  16. int a=0;
  17. if(!isBigEndian())
  18. for(int i=0;i<len;i++)
  19. ((char*)&a)[i]=buffer[i];
  20. else
  21. for(int i=0;i<len;i++)
  22. ((char*)&a)[3-i]=buffer[i];
  23. return a;
  24. }
  25. char* loadWAV(const char* fn,int& chan,int& samplerate,int& bps,int& size)
  26. {
  27. char buffer[4];
  28. std::ifstream in(fn,std::ios::binary);
  29. in.read(buffer,4);
  30. if(strncmp(buffer,"RIFF",4)!=0)
  31. {
  32. std::cout << "this is not a valid WAVE file" << std::endl;
  33. return NULL;
  34. }
  35. in.read(buffer,4);
  36. in.read(buffer,4); //WAVE
  37. in.read(buffer,4); //fmt
  38. in.read(buffer,4); //16
  39. in.read(buffer,2); //1
  40. in.read(buffer,2);
  41. chan=convertToInt(buffer,2);
  42. in.read(buffer,4);
  43. samplerate=convertToInt(buffer,4);
  44. in.read(buffer,4);
  45. in.read(buffer,2);
  46. in.read(buffer,2);
  47. bps=convertToInt(buffer,2);
  48. in.read(buffer,4); //data
  49. in.read(buffer,4);
  50. size=convertToInt(buffer,4);
  51. char* data=new char[size];
  52. in.read(data,size);
  53. return data;
  54. }
  55. int main(int argc,char** argv)
  56. {
  57. int channel,sampleRate,bps,size;
  58. char* data=loadWAV("test.wav",channel,sampleRate,bps,size);
  59. ALCdevice* device=alcOpenDevice(NULL);
  60. if(device==NULL)
  61. {
  62. std::cout << "cannot open sound card" << std::endl;
  63. return 0;
  64. }
  65. ALCcontext* context=alcCreateContext(device,NULL);
  66. if(context==NULL)
  67. {
  68. std::cout << "cannot open context" << std::endl;
  69. return 0;
  70. }
  71. alcMakeContextCurrent(context);
  72. unsigned int bufferid,format;
  73. alGenBuffers(1,&bufferid);
  74. if(channel==1)
  75. {
  76. if(bps==8)
  77. {
  78. format=AL_FORMAT_MONO8;
  79. }else{
  80. format=AL_FORMAT_MONO16;
  81. }
  82. }else{
  83. if(bps==8)
  84. {
  85. format=AL_FORMAT_STEREO8;
  86. }else{
  87. format=AL_FORMAT_STEREO16;
  88. }
  89. }
  90. alBufferData(bufferid,format,data,size,sampleRate);
  91. unsigned int sourceid;
  92. alGenSources(1,&sourceid);
  93. alSourcei(sourceid,AL_BUFFER,bufferid);
  94. SDL_Init(SDL_INIT_EVERYTHING);
  95. SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  96. SDL_Event event;
  97. Uint32 start;
  98. bool running;
  99. bool b[4]={0,0,0,0};
  100. float x=0,z=0;
  101. while(running)
  102. {
  103. start=SDL_GetTicks();
  104. while(SDL_PollEvent(&event))
  105. {
  106. switch(event.type)
  107. {
  108. case SDL_QUIT:
  109. running=false;
  110. break;
  111. case SDL_KEYDOWN:
  112. switch(event.key.keysym.sym)
  113. {
  114. case SDLK_UP:
  115. b[0]=true;
  116. break;
  117. case SDLK_RIGHT:
  118. b[1]=true;
  119. break;
  120. case SDLK_DOWN:
  121. b[2]=true;
  122. break;
  123. case SDLK_LEFT:
  124. b[3]=true;
  125. break;
  126. case SDLK_SPACE:
  127. alSourcePlay(sourceid);
  128. break;
  129. }
  130. break;
  131. case SDL_KEYUP:
  132. switch(event.key.keysym.sym)
  133. {
  134. case SDLK_UP:
  135. b[0]=false;
  136. break;
  137. case SDLK_RIGHT:
  138. b[1]=false;
  139. break;
  140. case SDLK_DOWN:
  141. b[2]=false;
  142. break;
  143. case SDLK_LEFT:
  144. b[3]=false;
  145. break;
  146. }
  147. break;
  148. }
  149. }
  150. if(b[0])
  151. z+=0.3;
  152. if(b[1])
  153. x+=0.3;
  154. if(b[2])
  155. z-=0.3;
  156. if(b[3])
  157. x-=0.3;
  158. alSource3f(sourceid,AL_POSITION,x,0,z);
  159. alSourcei(sourceid,AL_LOOPING,AL_TRUE);
  160. float f[]={1,0,0,0,1,0};
  161. alListenerfv(AL_ORIENTATION,f);
  162. if(1000/30.0>SDL_GetTicks()-start)
  163. SDL_Delay(1000/30.0-(SDL_GetTicks()-start));
  164. }
  165. alDeleteSources(1,&sourceid);
  166. alDeleteBuffers(1,&bufferid);
  167. alcDestroyContext(context);
  168. alcCloseDevice(device);
  169. delete[] data;
  170. return 0;
  171. }

comments powered by Disqus