-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.cpp
64 lines (53 loc) · 1.05 KB
/
audio.cpp
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
#include "audio.h"
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <AL/al.h>
#include <AL/alc.h>
Audio::Audio()
{
dev = alcOpenDevice(NULL);
if (!dev)
{
fprintf(stderr, "no device\n");
return;
}
ctx = alcCreateContext(dev, NULL);
alcMakeContextCurrent(ctx);
if (!ctx)
{
fprintf(stderr, "no context\n");
return;
}
int16_t buf[bufferSize];
for (int i=0; i<bufferSize; i++)
{
buf[i] = 0x7fff*sin(2*M_PI*((double)i/(double)bufferSize));
}
alGenBuffers(1, &buffer);
alGenSources(1, &source);
if (alGetError() != AL_NO_ERROR)
{
fprintf(stderr, "error generating buffers\n");
return;
}
alBufferData(buffer, AL_FORMAT_MONO16, buf, bufferSize, 44100);
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_TRUE);
}
Audio::~Audio()
{
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
alcMakeContextCurrent(NULL);
alcDestroyContext(ctx);
alcCloseDevice(dev);
}
void Audio::play()
{
alSourcePlay(source);
}
void Audio::stop()
{
alSourcePause(source);
}