Sound in game! Using OpenAL. NOWHERE TO RUN

This commit is contained in:
Stiffly
2015-09-15 16:08:52 +02:00
parent ff00e2bf9c
commit 5695a8c068
4 changed files with 69 additions and 490 deletions
+53 -59
View File
@@ -4,61 +4,49 @@
#include "PrecompiledHeader.h"
#include "Core/Sound.h"
void dd::Systems::Sound::Initialize()
dd::Systems::Sound::~Sound()
{
LOG_INFO("Hello im an init func ----------------------------");
/*
LOG_ERROR("Init started");
alDeleteSources(1, m_Source);
}
//initialize OpenAL
void dd::Systems::Sound::Initialize()
{ //initialize OpenAL
ALCdevice* device = alcOpenDevice(NULL);
ALCcontext* context;
if(device)
{
if (device) {
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
}
else
{
else {
LOG_ERROR("OMG OPEN AL FAIL");
}
alGetError();
alSpeedOfSound(340.29f); // Speed of sound m/s
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
//Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Sound::OnKeyDown);
LOG_ERROR("Init complete");
*/
}
void dd::Systems::Sound::Update(double dt)
{
/*const ALfloat pos[3] = {0, 0, 0};
const ALfloat pos[3] = {0, 0, 0};
alListenerfv(AL_POSITION, pos);
alSourcefv(m_source, AL_POSITION, pos);
*/
alSourcefv(m_Source, AL_POSITION, pos);
}
bool dd::Systems::Sound::OnKeyDown(const dd::Events::KeyDown &event)
{
// #define GLFW_KEY_S 83
if(event.KeyCode == 83){
m_source = CreateSource();
if (event.KeyCode == 83) {
m_Source = CreateSource();
ALuint buffer = LoadFile("Sounds/screwed.wav");
ALuint buffer = LoadFile("assets/Sounds/screwed.wav");
alSourcei(m_source, AL_BUFFER, buffer);
alSourcePlay(m_source);
LOG_INFO("Playing sound (I wish LOL)");
alSourcei(m_Source, AL_BUFFER, buffer);
alSourcePlay(m_Source);
}
}
@@ -72,74 +60,80 @@ ALuint dd::Systems::Sound::CreateSource()
ALuint dd::Systems::Sound::LoadFile(std::string path)
{
if (m_BufferCache.find(path) != m_BufferCache.end())
if (m_BufferCache.find(path) != m_BufferCache.end()) {
return m_BufferCache[path];
}
FILE *fp = NULL;
fp = fopen(path.c_str(), "rb");
//Open file
FILE *fp = fopen(path.c_str(), "rb");
if (!fp) {
LOG_ERROR("Failed to open file %s, no such file exists", path.c_str());
return 0;
}
//CHECK FOR VALID WAVE-FILE
fread(m_type, sizeof(char), 4, fp);
if(m_type[0]!='R' || m_type[1]!='I' || m_type[2]!='F' || m_type[3]!='F') {
fread(m_Type, sizeof(char), 4, fp);
if(m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') {
LOG_ERROR("ERROR: No RIFF in WAVE-file");
return 0;
}
fread(&m_size, sizeof(unsigned long), 1, fp);
fread(m_type, sizeof(char), 4, fp);
if(m_type[0]!='W' || m_type[1]!='A' || m_type[2]!='V' || m_type[3]!='E') {
fread(&m_Size, sizeof(unsigned long), 1, fp);
fread(m_Type, sizeof(char), 4, fp);
if (m_Type[0] != 'W' || m_Type[1] != 'A' || m_Type[2] != 'V' || m_Type[3]!= 'E') {
LOG_ERROR("ERROR: Not WAVE-file");
return 0;
}
fread(m_type, sizeof(char), 4, fp);
if(m_type[0]!='f' || m_type[1]!='m' || m_type[2]!='t' || m_type[3]!=' ') {
fread(m_Type, sizeof(char), 4, fp);
if (m_Type[0] != 'f' || m_Type[1] != 'm' || m_Type[2] != 't' || m_Type[3] != ' ') {
LOG_ERROR("ERROR: No fmt in WAVE-file");
return 0;
}
//READ THE DATA FROM WAVE-FILE
fread(&m_chunkSize, sizeof(unsigned long), 1, fp);
fread(&m_formatType, sizeof(short), 1, fp);
fread(&m_channels, sizeof(short), 1, fp);
fread(&m_sampleRate, sizeof(unsigned long), 1, fp);
fread(&m_avgBytesPerSec, sizeof(unsigned long), 1, fp);
fread(&m_bytesPerSample, sizeof(short), 1, fp);
fread(&m_bitsPerSample, sizeof(short), 1, fp);
fread(&m_ChunkSize, sizeof(unsigned long), 1, fp);
fread(&m_FormatType, sizeof(short), 1, fp);
fread(&m_Channels, sizeof(short), 1, fp);
fread(&m_SampleRate, sizeof(unsigned long), 1, fp);
fread(&m_AvgBytesPerSec, sizeof(unsigned long), 1, fp);
fread(&m_BytesPerSample, sizeof(short), 1, fp);
fread(&m_BitsPerSample, sizeof(short), 1, fp);
fread(m_type, sizeof(char), 4, fp);
if(m_type[0]!='d' || m_type[1]!='a' || m_type[2]!='t' || m_type[3]!='a')
{
fread(m_Type, sizeof(char), 4, fp);
if (m_Type[0] !='d' || m_Type[1] != 'a' || m_Type[2] != 't' || m_Type[3] != 'a') {
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
fread(&m_dataSize, sizeof(unsigned long), 1, fp);
fread(&m_DataSize, sizeof(unsigned long), 1, fp);
unsigned char* buf = new unsigned char[m_dataSize];
fread(buf, sizeof(unsigned char), m_dataSize, fp);
unsigned char* buf = new unsigned char[m_DataSize];
fread(buf, sizeof(unsigned char), m_DataSize, fp);
fclose(fp);
// Create buffer
ALuint format = 0;
if(m_bitsPerSample == 8)
{
if(m_channels == 1)
if (m_BitsPerSample == 8) {
if (m_Channels == 1) {
format = AL_FORMAT_MONO8;
else if(m_channels == 2)
}
else if (m_Channels == 2) {
format = AL_FORMAT_STEREO8;
}
}
if(m_bitsPerSample == 16)
{
if (m_channels == 1)
if (m_BitsPerSample == 16) {
if (m_Channels == 1) {
format = AL_FORMAT_MONO16;
else if (m_channels == 2)
}
else if (m_Channels == 2) {
format = AL_FORMAT_STEREO16;
}
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, buf, m_dataSize, m_sampleRate);
alBufferData(buffer, format, buf, m_DataSize, m_SampleRate);
delete[] buf;
m_BufferCache[path] = buffer;