-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynth.c
56 lines (47 loc) · 1.25 KB
/
synth.c
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
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
#include <al.h>
#include <alc.h>
#include <GL/glut.h>
ALCdevice *device;
ALCcontext *context;
ALshort waveData[48000];
ALuint buffer;
ALuint source;
int SamplingFrequency;
double baseFrequency = 440.0;
double frequency;
void GetKey(unsigned char key, int x, int y)
{
frequency = baseFrequency * pow(2.0, ((key - 49.0) / 12.0));
alSourceStop(source);
alGenBuffers(1, &buffer);
alGenSources(1, &source);
for (int i = 0; i < 48000; i++)
{
waveData[i] = 32767.0 * sin(2 * M_PI * frequency * i / SamplingFrequency);
}
alBufferData(buffer, AL_FORMAT_MONO16, waveData, sizeof(waveData), SamplingFrequency);
alSourcei(source, AL_BUFFER, buffer);
alSourcei(source, AL_LOOPING, AL_TRUE);
alSourcePlay(source);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char *argv[])
{
device = alcOpenDevice(NULL);
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
alcGetIntegerv(device, ALC_FREQUENCY, 1, &SamplingFrequency);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow("synth");
glutDisplayFunc(Display);
glutKeyboardUpFunc(GetKey);
glutMainLoop();
return 0;
}