-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.c
More file actions
80 lines (72 loc) · 1.46 KB
/
Copy pathsound.c
File metadata and controls
80 lines (72 loc) · 1.46 KB
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
#include "common.h"
#include <math.h>
#include <stdlib.h>
#ifndef PI
#define PI 3.14159265358979323846f
#endif
#define SAMPLE_RATE 44100
#define TONE_DURATION 0.10f
#define TONE_BASE_HZ 440.0f
#define TONE_AMPLITUDE 18000
#define TONE_VOLUME 0.25f
static Sound g_tone = {0};
static int g_last_val = -1;
void InitSounds(void)
{
short *data;
int frames;
int i;
float t;
float env;
Wave wave;
if (!IsAudioDeviceReady())
return ;
frames = (int)(SAMPLE_RATE * TONE_DURATION);
data = (short *)malloc((size_t)frames * sizeof(short));
if (!data)
return ;
i = 0;
while (i < frames)
{
t = (float)i / SAMPLE_RATE;
env = 1.0f - (float)i / frames;
env = env * env;
data[i] = (short)(TONE_AMPLITUDE
* sinf(2.0f * PI * TONE_BASE_HZ * t) * env);
i++;
}
wave.frameCount = (unsigned int)frames;
wave.sampleRate = (unsigned int)SAMPLE_RATE;
wave.sampleSize = 16;
wave.channels = 1;
wave.data = data;
g_tone = LoadSoundFromWave(wave);
SetSoundVolume(g_tone, TONE_VOLUME);
free(data);
}
void UnloadSounds(void)
{
if (IsSoundValid(g_tone))
UnloadSound(g_tone);
}
void PlayNoteForValue(int val, int max_val)
{
float t;
float pitch;
if (!IsAudioDeviceReady())
return ;
if (!IsSoundValid(g_tone))
return ;
if (max_val <= 0)
return ;
if (val == g_last_val)
return ;
g_last_val = val;
t = (float)val / (float)max_val;
if (t > 1.0f)
t = 1.0f;
pitch = 0.4f + t * 1.8f;
SetSoundPitch(g_tone, pitch);
StopSound(g_tone);
PlaySound(g_tone);
}