OpenAL.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "OpenAL.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <al.h>
  6. #include <alc.h>
  7. int OpenAL::endWithError(char* msg, int error = 0)
  8. {
  9. //Display error message in console
  10. std::cout << msg << "\n";
  11. system("PAUSE");
  12. return error;
  13. }
  14. OpenAL::OpenAL()
  15. {
  16. Test();
  17. }
  18. OpenAL::~OpenAL()
  19. {
  20. }
  21. boolean OpenAL::Test()
  22. {
  23. //Loading of the WAVE file
  24. FILE *fp = NULL; //Create FILE pointer for the WAVE file
  25. fp = fopen("WAVE/Sound.wav", "rb"); //Open the WAVE file
  26. if (!fp) return endWithError("Failed to open file"); //Could not open file
  27. //Variables to store info about the WAVE file (all of them is not needed for OpenAL)
  28. char type[4];
  29. DWORD size, chunkSize;
  30. short formatType, channels;
  31. DWORD sampleRate, avgBytesPerSec;
  32. short bytesPerSample, bitsPerSample;
  33. DWORD dataSize;
  34. //Check that the WAVE file is OK
  35. fread(type, sizeof(char), 4, fp); //Reads the first bytes in the file
  36. if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F') //Should be "RIFF"
  37. return endWithError("No RIFF"); //Not RIFF
  38. fread(&size, sizeof(DWORD), 1, fp); //Continue to read the file
  39. fread(type, sizeof(char), 4, fp); //Continue to read the file
  40. if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E') //This part should be "WAVE"
  41. return endWithError("not WAVE"); //Not WAVE
  42. fread(type, sizeof(char), 4, fp); //Continue to read the file
  43. if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') //This part should be "fmt "
  44. return endWithError("not fmt "); //Not fmt
  45. //Now we know that the file is a acceptable WAVE file
  46. //Info about the WAVE data is now read and stored
  47. fread(&chunkSize, sizeof(DWORD), 1, fp);
  48. fread(&formatType, sizeof(short), 1, fp);
  49. fread(&channels, sizeof(short), 1, fp);
  50. fread(&sampleRate, sizeof(DWORD), 1, fp);
  51. fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
  52. fread(&bytesPerSample, sizeof(short), 1, fp);
  53. fread(&bitsPerSample, sizeof(short), 1, fp);
  54. fread(type, sizeof(char), 4, fp);
  55. if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') //This part should be "data"
  56. return endWithError("Missing DATA"); //not data
  57. fread(&dataSize, sizeof(DWORD), 1, fp); //The size of the sound data is read
  58. //Display the info about the WAVE file
  59. std::cout << "Chunk Size: " << chunkSize << "\n";
  60. std::cout << "Format Type: " << formatType << "\n";
  61. std::cout << "Channels: " << channels << "\n";
  62. std::cout << "Sample Rate: " << sampleRate << "\n";
  63. std::cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
  64. std::cout << "Bytes Per Sample: " << bytesPerSample << "\n";
  65. std::cout << "Bits Per Sample: " << bitsPerSample << "\n";
  66. std::cout << "Data Size: " << dataSize << "\n";
  67. unsigned char* buf = new unsigned char[dataSize]; //Allocate memory for the sound data
  68. std::cout << fread(buf, sizeof(BYTE), dataSize, fp) << " bytes loaded\n"; //Read the sound data and display the
  69. //number of bytes loaded.
  70. //Should be the same as the Data Size if OK
  71. //Now OpenAL needs to be initialized
  72. ALCdevice *device; //Create an OpenAL Device
  73. ALCcontext *context; //And an OpenAL Context
  74. device = alcOpenDevice(NULL); //Open the device
  75. if (!device) return endWithError("no sound device"); //Error during device oening
  76. context = alcCreateContext(device, NULL); //Give the device a context
  77. alcMakeContextCurrent(context); //Make the context the current
  78. if (!context) return endWithError("no sound context"); //Error during context handeling
  79. ALuint source; //Is the name of source (where the sound come from)
  80. ALuint buffer; //Stores the sound data
  81. ALuint frequency = sampleRate;; //The Sample Rate of the WAVE file
  82. ALenum format = 0; //The audio format (bits per sample, number of channels)
  83. alGenBuffers(1, &buffer); //Generate one OpenAL Buffer and link to "buffer"
  84. alGenSources(1, &source); //Generate one OpenAL Source and link to "source"
  85. if (alGetError() != AL_NO_ERROR) return endWithError("Error GenSource"); //Error during buffer/source generation
  86. //Figure out the format of the WAVE file
  87. if (bitsPerSample == 8)
  88. {
  89. if (channels == 1)
  90. format = AL_FORMAT_MONO8;
  91. else if (channels == 2)
  92. format = AL_FORMAT_STEREO8;
  93. }
  94. else if (bitsPerSample == 16)
  95. {
  96. if (channels == 1)
  97. format = AL_FORMAT_MONO16;
  98. else if (channels == 2)
  99. format = AL_FORMAT_STEREO16;
  100. }
  101. if (!format) return endWithError("Wrong BitPerSample"); //Not valid format
  102. alBufferData(buffer, format, buf, dataSize, frequency); //Store the sound data in the OpenAL Buffer
  103. if (alGetError() != AL_NO_ERROR)
  104. return endWithError("Error loading ALBuffer"); //Error during buffer loading
  105. //Sound setting variables
  106. ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; //Position of the source sound
  107. ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the source sound
  108. ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; //Position of the listener
  109. ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the listener
  110. ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; //Orientation of the listener
  111. //First direction vector, then vector pointing up)
  112. //Listener
  113. alListenerfv(AL_POSITION, ListenerPos); //Set position of the listener
  114. alListenerfv(AL_VELOCITY, ListenerVel); //Set velocity of the listener
  115. alListenerfv(AL_ORIENTATION, ListenerOri); //Set orientation of the listener
  116. //Source
  117. alSourcei(source, AL_BUFFER, buffer); //Link the buffer to the source
  118. alSourcef(source, AL_PITCH, 1.0f); //Set the pitch of the source
  119. alSourcef(source, AL_GAIN, 1.0f); //Set the gain of the source
  120. alSourcefv(source, AL_POSITION, SourcePos); //Set the position of the source
  121. alSourcefv(source, AL_VELOCITY, SourceVel); //Set the velocity of the source
  122. alSourcei(source, AL_LOOPING, AL_FALSE); //Set if source is looping sound
  123. //PLAY
  124. alSourcePlay(source); //Play the sound buffer linked to the source
  125. if (alGetError() != AL_NO_ERROR) return endWithError("Error playing sound"); //Error when playing sound
  126. system("PAUSE"); //Pause to let the sound play
  127. //Clean-up
  128. fclose(fp); //Close the WAVE file
  129. delete[] buf; //Delete the sound data buffer
  130. alDeleteSources(1, &source); //Delete the OpenAL Source
  131. alDeleteBuffers(1, &buffer); //Delete the OpenAL Buffer
  132. alcMakeContextCurrent(NULL); //Make no context current
  133. alcDestroyContext(context); //Destroy the OpenAL Context
  134. alcCloseDevice(device); //Close the OpenAL Device
  135. return EXIT_SUCCESS;
  136. }