1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include "audio.hpp"
tim::AudioSound::AudioSound(const char* file, uint file_type) : m_bLoop(false), m_opFile(::fopen(file, "rb")), m_uiTypeFile(file_type){
m_fProps[0] = m_fProps[1] = m_fProps[2] = m_fProps[3] = m_fProps[4] = m_fProps[5] = m_fProps[6] = m_fProps[7] = m_fProps[8] = 0.0f;
}
tim::AudioSound::~AudioSound(void){
delete[] m_ucpBuffer;
::fclose(m_opFile);
::alDeleteSources(1, &m_uiSource);
::alDeleteBuffers(1, &m_uiBuffer);
}
bool tim::AudioSound::load_file(tim::AudioSound& sound){
if(sound.m_uiTypeFile == tim::AudioSound::AUDIO_FILE_WAV){
// Variable declarations & definitions:
char __string[4] = {NULL};
short __bits_psample, __bytes_psample, __channels, __type_format;
ulong __avg__bytes__psec, __rate_sample, __size, __size_chunk, __size_data;
// Check requested .wav file's data validness:
::fread(__string, sizeof(char), 4, sound.m_opFile);
if(!::strcmp(__string, "RIFF")){
return false;
}
::fread(&__size, sizeof(ulong), 1, sound.m_opFile);
::fread(__string, sizeof(char), 4, sound.m_opFile);
if(!::strcmp(__string, "WAVE")){
return false;
}
::fread(__string, sizeof(char), 4, sound.m_opFile);
if(!::strcmp(__string, "fmt ")){
return false;
}
// Read data from requested .wav file:
::fread(&__size_chunk, sizeof(ulong), 1, sound.m_opFile);
::fread(&__type_format, sizeof(short), 1, sound.m_opFile);
::fread(&__channels, sizeof(short), 1, sound.m_opFile);
::fread(&__rate_sample, sizeof(ulong), 1, sound.m_opFile);
::fread(&__avg__bytes__psec, sizeof(ulong), 1, sound.m_opFile);
::fread(&__bytes_psample, sizeof(short), 1, sound.m_opFile);
::fread(&__bits_psample, sizeof(short), 1, sound.m_opFile);
// Check requested .wav file's data:
::fread(__string, sizeof(char), 4, sound.m_opFile);
if(!::strcmp(__string, "data")){
return false;
}
::fread(&__size_data, sizeof(ulong), 1, sound.m_opFile);
// Memory allocation & handling for sound buffer:
sound.m_ucpBuffer = new byte[__size_data];
::fread(sound.m_ucpBuffer, sizeof(byte), __size_data, sound.m_opFile);
// Sound generation & format check:
alGenBuffers(1, &sound.m_uiBuffer);
alGenSources(1, &sound.m_uiSource);
if(__bits_psample == 8){
if(__channels == 1){
__type_format = AL_FORMAT_MONO8;
}
else if(__channels == 2){
__type_format = AL_FORMAT_STEREO8;
}
}
else if(__bits_psample == 16){
if(__channels == 1){
__type_format = AL_FORMAT_MONO16;
}
else if(__channels == 2){
__type_format = AL_FORMAT_STEREO16;
}
}
::ALuint __freq = __rate_sample;
::alBufferData(sound.m_uiBuffer, __type_format, sound.m_ucpBuffer, __size_data, __freq);
// Set source properties:
::alSourcei(sound.m_uiSource, AL_BUFFER, sound.m_uiBuffer);
::alSourcef(sound.m_uiSource, AL_PITCH, 1.0f);
::alSourcef(sound.m_uiSource, AL_GAIN, 1.0f);
::alSource3f(sound.m_uiSource, AL_POSITION, sound.m_fProps[3], sound.m_fProps[4], sound.m_fProps[5]);
::alSource3f(sound.m_uiSource, AL_VELOCITY, sound.m_fProps[6], sound.m_fProps[7], sound.m_fProps[8]);
::alSourcei(sound.m_uiSource, AL_LOOPING, sound.m_bLoop);
}
else{
return false;
}
return true;
}
void tim::AudioSound::play(void){
::alSourcePlay(m_uiSource);
}
void tim::AudioSound::set_changes(void){
load_file(*this);
}
tim::AudioSound& tim::AudioSound::set_orientation(float x, float y, float z){
m_fProps[0] = x;
m_fProps[1] = y;
m_fProps[2] = z;
return *this;
}
tim::AudioSound& tim::AudioSound::set_position(float x, float y, float z){
m_fProps[3] = x;
m_fProps[4] = y;
m_fProps[5] = z;
return *this;
}
tim::AudioSound& tim::AudioSound::set_velocity(float x, float y, float z){
m_fProps[6] = x;
m_fProps[7] = y;
m_fProps[8] = z;
return *this;
}
|