OpenAL.cpp 7.3 KB

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