|
@@ -5,102 +5,40 @@
|
|
|
#include <al.h>
|
|
#include <al.h>
|
|
|
#include <alc.h>
|
|
#include <alc.h>
|
|
|
|
|
|
|
|
-OpenAL::OpenAL()
|
|
|
|
|
|
|
+SoundSystem::SoundSystem():
|
|
|
|
|
+ device(nullptr),
|
|
|
|
|
+ context(nullptr)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
|
|
+ device = alcOpenDevice(nullptr);
|
|
|
|
|
+ if (!device)
|
|
|
|
|
+ return;
|
|
|
|
|
+ context = alcCreateContext(device, nullptr);
|
|
|
|
|
+ if (!context)
|
|
|
|
|
+ return;
|
|
|
|
|
+ alcMakeContextCurrent(context);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int OpenAL::EndWithError(char* msg)
|
|
|
|
|
|
|
+SoundSystem::~SoundSystem()
|
|
|
{
|
|
{
|
|
|
- //Display error message in console
|
|
|
|
|
- int error = 0;
|
|
|
|
|
- std::cout << msg << "\n";
|
|
|
|
|
- system("PAUSE");
|
|
|
|
|
- return error;
|
|
|
|
|
|
|
+ alcMakeContextCurrent(nullptr); //Make no context current
|
|
|
|
|
+ alcDestroyContext(context); //Destroy the OpenAL Context
|
|
|
|
|
+ alcCloseDevice(device); //Close the OpenAL Device
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-int OpenAL::playMusic(void) {
|
|
|
|
|
- //start nieuwe thread
|
|
|
|
|
- //speel sound;
|
|
|
|
|
- isPlaying = true;
|
|
|
|
|
- //Loading of the WAVE file
|
|
|
|
|
- FILE *fp = NULL; //Create FILE pointer for the WAVE file
|
|
|
|
|
- fp = fopen("WAVE/Sound.wav", "rb"); //Open the WAVE file
|
|
|
|
|
- if (!fp) return EndWithError("Failed to open file"); //Could not open file
|
|
|
|
|
-
|
|
|
|
|
- //Variables to store info about the WAVE file (all of them is not needed for OpenAL)
|
|
|
|
|
- char type[4];
|
|
|
|
|
- DWORD size, chunkSize;
|
|
|
|
|
- short formatType, channels;
|
|
|
|
|
- DWORD sampleRate, avgBytesPerSec;
|
|
|
|
|
- short bytesPerSample, bitsPerSample;
|
|
|
|
|
- DWORD dataSize;
|
|
|
|
|
|
|
|
|
|
- //Check that the WAVE file is OK
|
|
|
|
|
- fread(type, sizeof(char), 4, fp); //Reads the first bytes in the file
|
|
|
|
|
- if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F') //Should be "RIFF"
|
|
|
|
|
- return EndWithError("No RIFF"); //Not RIFF
|
|
|
|
|
|
|
|
|
|
- fread(&size, sizeof(DWORD), 1, fp); //Continue to read the file
|
|
|
|
|
- fread(type, sizeof(char), 4, fp); //Continue to read the file
|
|
|
|
|
- if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E') //This part should be "WAVE"
|
|
|
|
|
- return EndWithError("not WAVE"); //Not WAVE
|
|
|
|
|
|
|
|
|
|
- fread(type, sizeof(char), 4, fp); //Continue to read the file
|
|
|
|
|
- if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') //This part should be "fmt "
|
|
|
|
|
- return EndWithError("not fmt "); //Not fmt
|
|
|
|
|
-
|
|
|
|
|
- //Now we know that the file is a acceptable WAVE file
|
|
|
|
|
- //Info about the WAVE data is now read and stored
|
|
|
|
|
- fread(&chunkSize, sizeof(DWORD), 1, fp);
|
|
|
|
|
- fread(&formatType, sizeof(short), 1, fp);
|
|
|
|
|
- fread(&channels, sizeof(short), 1, fp);
|
|
|
|
|
- fread(&sampleRate, sizeof(DWORD), 1, fp);
|
|
|
|
|
- fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
|
|
|
|
|
- fread(&bytesPerSample, sizeof(short), 1, fp);
|
|
|
|
|
- fread(&bitsPerSample, sizeof(short), 1, fp);
|
|
|
|
|
-
|
|
|
|
|
- fread(type, sizeof(char), 4, fp);
|
|
|
|
|
- if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') //This part should be "data"
|
|
|
|
|
- return EndWithError("Missing DATA"); //not data
|
|
|
|
|
-
|
|
|
|
|
- fread(&dataSize, sizeof(DWORD), 1, fp); //The size of the sound data is read
|
|
|
|
|
-
|
|
|
|
|
- //Display the info about the WAVE file
|
|
|
|
|
- std::cout << "Chunk Size: " << chunkSize << "\n";
|
|
|
|
|
- std::cout << "Format Type: " << formatType << "\n";
|
|
|
|
|
- std::cout << "Channels: " << channels << "\n";
|
|
|
|
|
- std::cout << "Sample Rate: " << sampleRate << "\n";
|
|
|
|
|
- std::cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
|
|
|
|
|
- std::cout << "Bytes Per Sample: " << bytesPerSample << "\n";
|
|
|
|
|
- std::cout << "Bits Per Sample: " << bitsPerSample << "\n";
|
|
|
|
|
- std::cout << "Data Size: " << dataSize << "\n";
|
|
|
|
|
-
|
|
|
|
|
- unsigned char* buf = new unsigned char[dataSize]; //Allocate memory for the sound data
|
|
|
|
|
- std::cout << fread(buf, sizeof(BYTE), dataSize, fp) << " bytes loaded\n"; //Read the sound data and display the
|
|
|
|
|
- //number of bytes loaded.
|
|
|
|
|
- //Should be the same as the Data Size if OK
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- //Now OpenAL needs to be initialized
|
|
|
|
|
- ALCdevice *device; //Create an OpenAL Device
|
|
|
|
|
- ALCcontext *context; //And an OpenAL Context
|
|
|
|
|
- device = alcOpenDevice(NULL); //Open the device
|
|
|
|
|
- if (!device) return EndWithError("no sound device"); //Error during device oening
|
|
|
|
|
- context = alcCreateContext(device, NULL); //Give the device a context
|
|
|
|
|
- alcMakeContextCurrent(context); //Make the context the current
|
|
|
|
|
- if (!context) return EndWithError("no sound context"); //Error during context handeling
|
|
|
|
|
-
|
|
|
|
|
- ALuint source; //Is the name of source (where the sound come from)
|
|
|
|
|
- ALuint buffer; //Stores the sound data
|
|
|
|
|
- ALuint frequency = sampleRate;; //The Sample Rate of the WAVE file
|
|
|
|
|
- ALenum format = 0; //The audio format (bits per sample, number of channels)
|
|
|
|
|
|
|
|
|
|
|
|
+void SoundSystem::Play(ESoundID inID)
|
|
|
|
|
+{
|
|
|
|
|
+ ALuint buffer;
|
|
|
alGenBuffers(1, &buffer); //Generate one OpenAL Buffer and link to "buffer"
|
|
alGenBuffers(1, &buffer); //Generate one OpenAL Buffer and link to "buffer"
|
|
|
|
|
+ ALuint source;
|
|
|
alGenSources(1, &source); //Generate one OpenAL Source and link to "source"
|
|
alGenSources(1, &source); //Generate one OpenAL Source and link to "source"
|
|
|
- if (alGetError() != AL_NO_ERROR) return EndWithError("Error GenSource"); //Error during buffer/source generation
|
|
|
|
|
|
|
+ if (alGetError() != AL_NO_ERROR) return ; //Error during buffer/source generation
|
|
|
|
|
|
|
|
//Figure out the format of the WAVE file
|
|
//Figure out the format of the WAVE file
|
|
|
|
|
+ ALenum format = 0; //The audio format (bits per sample, number of channels)
|
|
|
if (bitsPerSample == 8)
|
|
if (bitsPerSample == 8)
|
|
|
{
|
|
{
|
|
|
if (channels == 1)
|
|
if (channels == 1)
|
|
@@ -115,8 +53,9 @@ int OpenAL::playMusic(void) {
|
|
|
else if (channels == 2)
|
|
else if (channels == 2)
|
|
|
format = AL_FORMAT_STEREO16;
|
|
format = AL_FORMAT_STEREO16;
|
|
|
}
|
|
}
|
|
|
- if (!format) return EndWithError("Wrong BitPerSample"); //Not valid format
|
|
|
|
|
|
|
+ if (!format) return; //Not valid format
|
|
|
|
|
|
|
|
|
|
+ ALuint frequency = sampleRate; //The Sample Rate of the WAVE file
|
|
|
alBufferData(buffer, format, buf, dataSize, frequency); //Store the sound data in the OpenAL Buffer
|
|
alBufferData(buffer, format, buf, dataSize, frequency); //Store the sound data in the OpenAL Buffer
|
|
|
if (alGetError() != AL_NO_ERROR)
|
|
if (alGetError() != AL_NO_ERROR)
|
|
|
return EndWithError("Error loading ALBuffer"); //Error during buffer loading
|
|
return EndWithError("Error loading ALBuffer"); //Error during buffer loading
|
|
@@ -151,19 +90,91 @@ int OpenAL::playMusic(void) {
|
|
|
delete[] buf; //Delete the sound data buffer
|
|
delete[] buf; //Delete the sound data buffer
|
|
|
alDeleteSources(1, &source); //Delete the OpenAL Source
|
|
alDeleteSources(1, &source); //Delete the OpenAL Source
|
|
|
alDeleteBuffers(1, &buffer); //Delete the OpenAL Buffer
|
|
alDeleteBuffers(1, &buffer); //Delete the OpenAL Buffer
|
|
|
- alcMakeContextCurrent(NULL); //Make no context current
|
|
|
|
|
- alcDestroyContext(context); //Destroy the OpenAL Context
|
|
|
|
|
- alcCloseDevice(device); //Close the OpenAL Device
|
|
|
|
|
|
|
|
|
|
- isPlaying = false;
|
|
|
|
|
- return EXIT_SUCCESS;
|
|
|
|
|
|
|
+ return ;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-bool OpenAL::isMusicPlaying()
|
|
|
|
|
|
|
+
|
|
|
|
|
+void SoundSystem::Pause(ESoundID inID)
|
|
|
{
|
|
{
|
|
|
- return isPlaying;
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-OpenAL::~OpenAL()
|
|
|
|
|
|
|
+
|
|
|
|
|
+void SoundSystem::Stop(ESoundID inID)
|
|
|
{
|
|
{
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+const char* SoundSystem::GetWaveFile(ESoundID inID)
|
|
|
|
|
+{
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+bool SoundSystem::LoadWave(ESoundID inID)
|
|
|
|
|
+{
|
|
|
|
|
+ const char* path = GetWaveFile(inID);
|
|
|
|
|
+
|
|
|
|
|
+ FILE *fp = fopen(path, "rb"); //Open the WAVE file
|
|
|
|
|
+ if (!fp)
|
|
|
|
|
+ return false; // Could not open file
|
|
|
|
|
+
|
|
|
|
|
+ //Check that the WAVE file is OK
|
|
|
|
|
+ char type[4];
|
|
|
|
|
+ fread(type, sizeof(char), 4, fp); // Reads the first bytes in the file
|
|
|
|
|
+ if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F') // Should be "RIFF"
|
|
|
|
|
+ return false; // Not RIFF
|
|
|
|
|
+
|
|
|
|
|
+ DWORD size;
|
|
|
|
|
+ fread(&size, sizeof(DWORD), 1, fp); // Continue to read the file
|
|
|
|
|
+ fread(type, sizeof(char), 4, fp); // Continue to read the file
|
|
|
|
|
+ if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E') //This part should be "WAVE"
|
|
|
|
|
+ return false; //Not WAVE
|
|
|
|
|
+
|
|
|
|
|
+ fread(type, sizeof(char), 4, fp); //Continue to read the file
|
|
|
|
|
+ if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') //This part should be "fmt "
|
|
|
|
|
+ return false; //Not fmt
|
|
|
|
|
+
|
|
|
|
|
+ //Now we know that the file is a acceptable WAVE file
|
|
|
|
|
+ //Info about the WAVE data is now read and stored
|
|
|
|
|
+ DWORD chunkSize;
|
|
|
|
|
+ fread(&chunkSize, sizeof(DWORD), 1, fp);
|
|
|
|
|
+ short formatType;
|
|
|
|
|
+ fread(&formatType, sizeof(short), 1, fp);
|
|
|
|
|
+ short channels;
|
|
|
|
|
+ fread(&channels, sizeof(short), 1, fp);
|
|
|
|
|
+ DWORD sampleRate;
|
|
|
|
|
+ fread(&sampleRate, sizeof(DWORD), 1, fp);
|
|
|
|
|
+ DWORD avgBytesPerSec;
|
|
|
|
|
+ fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
|
|
|
|
|
+ short bytesPerSample;
|
|
|
|
|
+ fread(&bytesPerSample, sizeof(short), 1, fp);
|
|
|
|
|
+ short bitsPerSample;
|
|
|
|
|
+ fread(&bitsPerSample, sizeof(short), 1, fp);
|
|
|
|
|
+
|
|
|
|
|
+ fread(type, sizeof(char), 4, fp);
|
|
|
|
|
+ if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') //This part should be "data"
|
|
|
|
|
+ return false; //not data
|
|
|
|
|
+
|
|
|
|
|
+ DWORD dataSize;
|
|
|
|
|
+ fread(&dataSize, sizeof(DWORD), 1, fp); //The size of the sound data is read
|
|
|
|
|
+
|
|
|
|
|
+ //Display the info about the WAVE file
|
|
|
|
|
+ std::cout << "Chunk Size: " << chunkSize << "\n";
|
|
|
|
|
+ std::cout << "Format Type: " << formatType << "\n";
|
|
|
|
|
+ std::cout << "Channels: " << channels << "\n";
|
|
|
|
|
+ std::cout << "Sample Rate: " << sampleRate << "\n";
|
|
|
|
|
+ std::cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
|
|
|
|
|
+ std::cout << "Bytes Per Sample: " << bytesPerSample << "\n";
|
|
|
|
|
+ std::cout << "Bits Per Sample: " << bitsPerSample << "\n";
|
|
|
|
|
+ std::cout << "Data Size: " << dataSize << "\n";
|
|
|
|
|
+
|
|
|
|
|
+ unsigned char* buf = new unsigned char[dataSize]; //Allocate memory for the sound data
|
|
|
|
|
+ std::cout << fread(buf, sizeof(BYTE), dataSize, fp) << " bytes loaded\n"; //Read the sound data and display the
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+}
|
|
|
|
|
+
|