You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

200 lines
5.2 KiB

#include <stdio.h>
#include <stdlib.h>
//#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <string.h>
#include <stdint.h>
#include "audio.h"
#define NUM_BUFFERS 5
#define NUM_SOURCES 6
#define NUM_ENVIRONMENTS 1
ALCdevice *dev;
ALCcontext *ctx;
//There is only one listener, and these arrays define the initial set up of the listener for its position, direction, and velocity. The listener is not moving at the start, but can move around any other sound source.
ALfloat listenerPos[]={0.0,0.0,4.0};
ALfloat listenerVel[]={0.0,0.0,0.0};
ALfloat listenerOri[]={0.0,0.0,1.0, 0.0,1.0,0.0};
//Each sound source has similar properties that the listener has. The source0Pos and source0Vel arrays show the position and velocity of the sound source.
ALfloat source0Pos[]={ -2.0, 0.0, 0.0};
ALfloat source0Vel[]={ 0.0, 0.0, 0.0};
//Sounds need to be stored in an array, similar to textures. Several buffers are needed to contain the sounds and other necessary information. The size, freq, format, and data variables are used when loading the sound files.
ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];
int file_read_int32_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 4, file);
uint32_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
ret |= (uint8_t)buffer[2] << 16;
ret |= (uint8_t)buffer[3] << 24;
return ret;
}
int file_read_int16_le(char *buffer, FILE *file)
{
int r = fread(buffer, sizeof(char), 2, file);
uint16_t ret = (uint8_t)buffer[0];
ret |= (uint8_t)buffer[1] << 8;
return ret;
}
void file_ignore_bytes(FILE *file, int bytes)
{
fseek(file, bytes, SEEK_CUR);
}
void *file_allocate_and_read_bytes(FILE *file, size_t bytes)
{
void *buffer = malloc(bytes);
fread(buffer, 1, bytes, file);
return buffer;
}
static inline ALenum GetFormatFromInfo(short channels, short bitsPerSample) {
if (channels == 1)
return AL_FORMAT_MONO16;
return AL_FORMAT_STEREO16;
}
void audio_load(const char *filename, ALuint buffer)
{
FILE* file = fopen(filename, "rb");
char xbuffer[5];
memset(xbuffer, 0, sizeof xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "RIFF") != 0)
printf("Not a WAV file: %s\n", xbuffer);
file_read_int32_le(xbuffer, file);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "WAVE") != 0)
printf("Not a WAV file: %s\n", xbuffer);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "fmt ") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int size = file_read_int32_le(xbuffer, file);
short audioFormat = file_read_int16_le(xbuffer, file);
short channels = file_read_int16_le(xbuffer, file);
int sampleRate = file_read_int32_le(xbuffer, file);
int byteRate = file_read_int32_le(xbuffer, file);
file_read_int16_le(xbuffer, file);
short bitsPerSample = file_read_int16_le(xbuffer, file);
size -= 16;
file_ignore_bytes(file, size);
if (fread(xbuffer, sizeof(char), 4, file) != 4 || strcmp(xbuffer, "data") != 0)
printf("Invalid WAV file: %s\n", xbuffer);
int dataChunkSize = file_read_int32_le(xbuffer, file);
unsigned char* bufferData = file_allocate_and_read_bytes(file, (size_t) dataChunkSize);
float duration = (float)(dataChunkSize) / byteRate;
alBufferData(buffer, GetFormatFromInfo(channels, bitsPerSample), bufferData, dataChunkSize, sampleRate);
free(bufferData);
fclose(file);
}
void audio_init(void)
{
dev = alcOpenDevice(NULL);
if (!dev)
{
fprintf(stderr, "Oops\n");
return;
}
ctx = alcCreateContext(dev, NULL);
alcMakeContextCurrent(ctx);
if (!ctx)
{
fprintf(stderr, "Oops2\n");
return;
}
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);
alGetError(); // clear any error messages
// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);
if (alGetError() != AL_NO_ERROR) {
printf("- Error creating buffers !!\n");
exit(1);
}
audio_load("off.wav", buffer[FX_OFF]);
audio_load("on.wav", buffer[FX_ON]);
audio_load("bing.wav", buffer[FX_BING]);
audio_load("bip.wav", buffer[FX_BIP]);
audio_load("bop.wav", buffer[FX_BOP]);
alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);
if (alGetError() != AL_NO_ERROR)
{
printf("- Error creating sources !!\n");
exit(2);
}
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_PITCH, 1.0f);
alSourcef(source[ii], AL_GAIN, 1.0f);
alSourcefv(source[ii], AL_POSITION, source0Pos);
alSourcefv(source[ii], AL_VELOCITY, source0Vel);
//alSourcei(source[0], AL_BUFFER, buffer[0]);
alSourcei(source[ii], AL_LOOPING, AL_FALSE);
}
}
void audio_deinit(void)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourceStop(source[ii]);
}
alDeleteSources(NUM_SOURCES, source);
alDeleteBuffers(NUM_BUFFERS, buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(dev);
}
void audio_volume(float f)
{
int ii;
for (ii = 0; ii < NUM_SOURCES; ii++)
{
alSourcef(source[ii], AL_GAIN, f);
}
}
float frand(float mult)
{
return ((float)rand() / (float)RAND_MAX) * mult;
}
void audio_play(int s, int b)
{
alSourceStop(source[s]);
alSourcei(source[s], AL_BUFFER, buffer[b]);
alSourcePlay(source[s]);
}