OpenAL.cpp 8.7 KB

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