Skip to content

Instantly share code, notes, and snippets.

@slime73
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slime73/6704f90af9011b99967a to your computer and use it in GitHub Desktop.
Save slime73/6704f90af9011b99967a to your computer and use it in GitHub Desktop.
/**
BUG:
changing the listener gain while a source is paused will not change the
final output volume when that source is unpaused.
STEPS TO REPRODUCE:
Run the program and press "P" to pause the source. Press "V" to change the
listener gain while the source is paused, and press "P" again to unpause the
source.
**/
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <AL/al.h>
#include <AL/alc.h>
static ALCdevice *device = NULL;
static ALCcontext *context = NULL;
static ALuint source = 0;
static int HandleKeyPress(const SDL_Event *e)
{
ALint ivalue = 0;
switch (e->key.keysym.sym) {
case SDLK_ESCAPE:
return 1;
case SDLK_p:
alGetSourcei(source, AL_SOURCE_STATE, &ivalue);
if (ivalue == AL_PLAYING) {
alSourcePause(source);
printf("Paused source\n");
} else {
alSourcePlay(source);
printf("Resumed source\n");
}
break;
case SDLK_v:
alListenerf(AL_GAIN, 0.1);
printf("Set listener gain to 0.1\n");
break;
default:
break;
}
return 0;
}
static int PollEvents()
{
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_MOUSEBUTTONDOWN:
case SDL_QUIT:
return 1;
case SDL_KEYDOWN:
if (HandleKeyPress(&e) != 0)
return 1;
break;
default:
break;
}
}
return 0;
}
static void InitAL()
{
device = alcOpenDevice(NULL);
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
}
static void QuitAL()
{
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
context = NULL;
device = NULL;
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Window *window = SDL_CreateWindow("Test", 200, 200, 640, 480, 0);
InitAL();
ALuint buffer = 0;
alGenBuffers(1, &buffer);
const int samples = 44100 / 2;
const int sampleRate = 44100 / 2;
Sint16 *data = (Sint16 *) malloc(sizeof(Sint16) * samples);
for (int i = 0; i < samples; i++) {
data[i] = (Sint16) (0x7FFF * sin(120 * (2.0 * M_PI) * i / sampleRate));
}
alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(Sint16) * samples, sampleRate);
free(data);
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_TRUE);
alSourcePlay(source);
while (1) {
if (PollEvents() != 0) {
break;
}
SDL_Delay(16);
}
alSourceStop(source);
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
QuitAL();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment