-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
140 lines (120 loc) · 3.95 KB
/
sound.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
@file sound.c
@author Alejandro Ambroa
@date 1 Oct 2017
@brief Generation of game sounds.
*/
#include <SDL.h>
#include "sound.h"
#include "msys.h"
#include "synth.h"
sample_t* player_pong_sound;
int player_pong_sound_samples;
sample_t* opponent_pong_sound;
int opponent_pong_sound_samples;
sample_t* player_score_sound;
int player_score_sound_samples;
sample_t* opp_score_sound;
int opp_score_sound_samples;
sample_t* wall_hit_sound;
int wall_hit_sound_samples;
sample_t* start_sound;
int start_sound_samples;
int init_sound(int sample_freq)
{
if (sys_init_sound(sample_freq) < 0) {
return -1;
}
SYNTH synthParams;
synthParams.totalTime = 0.1f;
synthParams.volume = 1.0f;
synthParams.attackTime = 0.0f;
synthParams.decayTime = 0.05f;
synthParams.releaseTime = 0.05f;
synthParams.decayValue = 0.3f;
synthParams.filterBeta1 = 0.2f;
synthParams.filterBeta2 = 0.3f;
synthParams.oscillator1_type = TRIANGLE;
synthParams.oscillator2_type = NONE;
synthParams.oscillator1_freq = 700.0f;
synthParams.delayTime = 0.5f;
synthParams.reverbSize = 0.1f;
player_pong_sound_samples = synthetize(&synthParams, &player_pong_sound, sample_freq);
synthParams.oscillator1_freq = 350.0f;
synthParams.oscillator2_freq = 350.0f;
opponent_pong_sound_samples = synthetize(&synthParams, &opponent_pong_sound, sample_freq);
synthParams.oscillator1_freq = 500.0f;
synthParams.oscillator2_freq = 500.0f;
start_sound_samples = synthetize(&synthParams, &start_sound, sample_freq);
synthParams.totalTime = 0.05f;
synthParams.volume = 0.7f;
synthParams.attackTime = 0.01f;
synthParams.decayTime = 0.03f;
synthParams.releaseTime = 0.01f;
synthParams.decayValue = 0.7f;
synthParams.filterBeta1 = 0.6f;
synthParams.filterBeta2 = 0.4f;
synthParams.oscillator1_type = SIN;
synthParams.oscillator2_type = SIN;
synthParams.oscillator1_freq = 200.0f;
synthParams.oscillator2_freq = 400.0f;
synthParams.delayTime = 0.0f;
synthParams.reverbSize = 0.0f;
wall_hit_sound_samples = synthetize(&synthParams, &wall_hit_sound, sample_freq);
synthParams.totalTime = 1.0f;
synthParams.volume = 1.0f;
synthParams.attackTime = 0.2f;
synthParams.decayTime = 0.8f;
synthParams.releaseTime = 0.4f;
synthParams.decayValue = 0.1f;
synthParams.filterBeta1 = 0.4f;
synthParams.filterBeta2 = 0.4f;
synthParams.oscillator1_type = SIN;
synthParams.oscillator2_type = COS;
synthParams.oscillator1_freq = 1046.50f;
synthParams.oscillator2_freq = 2.0f;
synthParams.delayTime = 0.0f;
synthParams.reverbSize = 0.0f;
player_score_sound_samples = synthetize(&synthParams, &player_score_sound, sample_freq);
synthParams.totalTime = 0.6f;
synthParams.volume = 1.0f;
synthParams.attackTime = 0.01f;
synthParams.decayTime = 0.3f;
synthParams.releaseTime = 0.3f;
synthParams.oscillator1_freq = 323.5f;
synthParams.oscillator2_freq = 30.0f;
opp_score_sound_samples = synthetize(&synthParams, &opp_score_sound, sample_freq);
return 0;
}
void play_start_sound()
{
sys_play_sound(start_sound, start_sound_samples * sizeof(sample_t));
}
void play_player_pong_sound()
{
sys_play_sound(player_pong_sound, player_pong_sound_samples * sizeof(sample_t));
}
void play_opponent_pong_sound()
{
sys_play_sound(opponent_pong_sound, opponent_pong_sound_samples * sizeof(sample_t));
}
void play_player_wins_sound()
{
sys_play_sound(player_score_sound, player_score_sound_samples * sizeof(sample_t));
}
void play_opponent_wins_sound()
{
sys_play_sound(opp_score_sound, opp_score_sound_samples * sizeof(sample_t));
}
void play_wall_hit_sound()
{
sys_play_sound(wall_hit_sound, wall_hit_sound_samples * sizeof(sample_t));
}
void dispose_sound()
{
free_samples(player_pong_sound);
free_samples(opponent_pong_sound);
free_samples(player_score_sound);
free_samples(opp_score_sound);
free_samples(wall_hit_sound);
}