1
+ #include " Audio/EffectProcessor.hpp"
2
+ #include " Audio/SoundSourceCone.hpp"
3
+ #include " Audio/SoundStatus.hpp"
4
+ #include " Audio/SoundChannel.hpp"
5
+ #include " SFML/Audio/SoundChannel.hpp"
1
6
#include " System/Vector3.hpp"
2
7
#include < SFML/Audio/SoundStream.hpp>
3
- #include < cstdint>
8
+ #include < cstdio>
9
+ #include < map>
10
+ #include < mutex>
4
11
5
12
typedef bool (*sfCustomSoundStreamGetDataCb)(sf::SoundStream::Chunk *, void *);
6
13
typedef void (*sfCustomSoundStreamSeekCb)(int64_t , void *);
@@ -11,10 +18,16 @@ class sfCustomSoundStream final : public sf::SoundStream {
11
18
sfCustomSoundStreamSeekCb onSeek,
12
19
unsigned int channelCount,
13
20
unsigned int sampleRate,
21
+ const sfSoundChannel *soundChannels,
22
+ size_t soundChannelMapLen,
14
23
void *userData) : myGetDataCb(onGetData),
15
24
mySeekCallCb (onSeek),
16
25
myUserData(userData) {
17
- initialize (channelCount, sampleRate);
26
+ std::vector<sf::SoundChannel> castedSoundChannels (soundChannelMapLen);
27
+ for (size_t i = 0 ; i < soundChannelMapLen; i++) {
28
+ castedSoundChannels.push_back (static_cast <sf::SoundChannel>(soundChannels[i]));
29
+ }
30
+ initialize (channelCount, sampleRate, castedSoundChannels);
18
31
}
19
32
20
33
private:
@@ -35,12 +48,10 @@ extern "C" sfCustomSoundStream *sfCustomSoundStream_new(sfCustomSoundStreamGetDa
35
48
sfCustomSoundStreamSeekCb onSeek,
36
49
unsigned int channelCount,
37
50
unsigned int sampleRate,
51
+ const sfSoundChannel *channelMap,
52
+ size_t soundChannelMapLen,
38
53
void *userData) {
39
- return new sfCustomSoundStream (onGetData, onSeek, channelCount, sampleRate, userData);
40
- }
41
-
42
- extern " C" void sfCustomSoundStream_del (sfCustomSoundStream *soundStream) {
43
- delete soundStream;
54
+ return new sfCustomSoundStream (onGetData, onSeek, channelCount, sampleRate, channelMap, soundChannelMapLen, userData);
44
55
}
45
56
46
57
extern " C" void sfCustomSoundStream_play (sfCustomSoundStream *soundStream) {
@@ -55,9 +66,8 @@ extern "C" void sfCustomSoundStream_stop(sfCustomSoundStream *soundStream) {
55
66
soundStream->stop ();
56
67
}
57
68
58
- extern " C" sf::SoundStream::Status sfCustomSoundStream_getStatus (const sfCustomSoundStream *soundStream) {
59
-
60
- return soundStream->getStatus ();
69
+ extern " C" sfSoundStatus sfCustomSoundStream_getStatus (const sfCustomSoundStream *soundStream) {
70
+ return static_cast <sfSoundStatus>(soundStream->getStatus ());
61
71
}
62
72
63
73
extern " C" unsigned int sfCustomSoundStream_getChannelCount (const sfCustomSoundStream *soundStream) {
@@ -68,16 +78,48 @@ extern "C" unsigned int sfCustomSoundStream_getSampleRate(const sfCustomSoundStr
68
78
return soundStream->getSampleRate ();
69
79
}
70
80
81
+ extern " C" const std::vector<sf::SoundChannel> *sfCustomSoundStream_getChannelMap (const sfCustomSoundStream *soundStream) {
82
+ return new std::vector (soundStream->getChannelMap ());
83
+ }
84
+
71
85
extern " C" void sfCustomSoundStream_setPitch (sfCustomSoundStream *soundStream, float pitch) {
72
86
soundStream->setPitch (pitch);
73
87
}
74
88
89
+ extern " C" void sfCustomSoundStream_setPan (sfCustomSoundStream *soundStream, float pan) {
90
+ soundStream->setPan (pan);
91
+ }
92
+
75
93
extern " C" void sfCustomSoundStream_setVolume (sfCustomSoundStream *soundStream, float volume) {
76
94
soundStream->setVolume (volume);
77
95
}
78
96
79
- extern " C" void sfCustomSoundStream_setPosition (sfCustomSoundStream *soundStream, sfVector3f position) {
80
- soundStream->setPosition (position.x , position.y , position.z );
97
+ extern " C" void sfCustomSoundStream_setSpatializationEnabled (sfCustomSoundStream *soundStream, bool enabled) {
98
+ soundStream->setSpatializationEnabled (enabled);
99
+ }
100
+
101
+ extern " C" void sfCustomSoundStream_setPosition (sfCustomSoundStream *soundStream, sf::Vector3f position) {
102
+ soundStream->setPosition (position);
103
+ }
104
+
105
+ extern " C" void sfCustomSoundStream_setDirection (sfCustomSoundStream *soundStream, sfVector3f position) {
106
+ soundStream->setDirection (convertVector3 (position));
107
+ }
108
+
109
+ extern " C" void sfCustomSoundStream_setCone (sfCustomSoundStream *soundStream, sfSoundSourceCone cone) {
110
+ soundStream->setCone (convertCone (cone));
111
+ }
112
+
113
+ extern " C" void sfCustomSoundStream_setVelocity (sfCustomSoundStream *soundStream, sfVector3f velocity) {
114
+ soundStream->setVelocity (convertVector3 (velocity));
115
+ }
116
+
117
+ extern " C" void sfCustomSoundStream_setDopplerFactor (sfCustomSoundStream *soundStream, float factor) {
118
+ soundStream->setDopplerFactor (factor);
119
+ }
120
+
121
+ extern " C" void sfCustomSoundStream_setDirectionalAttenuationFactor (sfCustomSoundStream *soundStream, float factor) {
122
+ soundStream->setDirectionalAttenuationFactor (factor);
81
123
}
82
124
83
125
extern " C" void sfCustomSoundStream_setRelativeToListener (sfCustomSoundStream *soundStream, bool relative) {
@@ -88,6 +130,18 @@ extern "C" void sfCustomSoundStream_setMinDistance(sfCustomSoundStream *soundStr
88
130
soundStream->setMinDistance (distance);
89
131
}
90
132
133
+ extern " C" void sfCustomSoundStream_setMaxDistance (sfCustomSoundStream *soundStream, float distance) {
134
+ soundStream->setMaxDistance (distance);
135
+ }
136
+
137
+ extern " C" void sfCustomSoundStream_setMinGain (sfCustomSoundStream *soundStream, float gain) {
138
+ soundStream->setMinGain (gain);
139
+ }
140
+
141
+ extern " C" void sfCustomSoundStream_setMaxGain (sfCustomSoundStream *soundStream, float gain) {
142
+ soundStream->setMaxGain (gain);
143
+ }
144
+
91
145
extern " C" void sfCustomSoundStream_setAttenuation (sfCustomSoundStream *soundStream, float attenuation) {
92
146
soundStream->setAttenuation (attenuation);
93
147
}
@@ -96,21 +150,48 @@ extern "C" void sfCustomSoundStream_setPlayingOffset(sfCustomSoundStream *soundS
96
150
soundStream->setPlayingOffset (sf::microseconds (timeOffset));
97
151
}
98
152
99
- extern " C" void sfCustomSoundStream_setLoop (sfCustomSoundStream *soundStream, bool loop) {
100
- soundStream->setLoop (loop);
153
+ extern " C" void sfCustomSoundStream_setLooping (sfCustomSoundStream *soundStream, bool loop) {
154
+ soundStream->setLooping (loop);
101
155
}
102
156
103
157
extern " C" float sfCustomSoundStream_getPitch (const sfCustomSoundStream *soundStream) {
104
158
return soundStream->getPitch ();
105
159
}
106
160
161
+ extern " C" float sfCustomSoundStream_getPan (const sfCustomSoundStream *soundStream) {
162
+ return soundStream->getPan ();
163
+ }
164
+
107
165
extern " C" float sfCustomSoundStream_getVolume (const sfCustomSoundStream *soundStream) {
108
166
return soundStream->getVolume ();
109
167
}
110
168
169
+ extern " C" bool sfCustomSoundStream_isSpatializationEnabled (const sfCustomSoundStream *soundStream) {
170
+ return soundStream->isSpatializationEnabled ();
171
+ }
172
+
111
173
extern " C" sfVector3f sfCustomSoundStream_getPosition (const sfCustomSoundStream *soundStream) {
112
- sf::Vector3f pos = soundStream->getPosition ();
113
- return {pos.x , pos.y , pos.z };
174
+ return convertVector3 (soundStream->getPosition ());
175
+ }
176
+
177
+ extern " C" sfVector3f sfCustomSoundStream_getDirection (const sfCustomSoundStream *soundStream) {
178
+ return convertVector3 (soundStream->getDirection ());
179
+ }
180
+
181
+ extern " C" sfSoundSourceCone sfCustomSoundStream_getCone (const sfCustomSoundStream *soundStream) {
182
+ return convertCone (soundStream->getCone ());
183
+ }
184
+
185
+ extern " C" sfVector3f sfCustomSoundStream_getVelocity (const sfCustomSoundStream *soundStream) {
186
+ return convertVector3 (soundStream->getVelocity ());
187
+ }
188
+
189
+ extern " C" float sfCustomSoundStream_getDopplerFactor (const sfCustomSoundStream *soundStream) {
190
+ return soundStream->getDopplerFactor ();
191
+ }
192
+
193
+ extern " C" float sfCustomSoundStream_getDirectionalAttenuationFactor (const sfCustomSoundStream *soundStream) {
194
+ return soundStream->getDirectionalAttenuationFactor ();
114
195
}
115
196
116
197
extern " C" bool sfCustomSoundStream_isRelativeToListener (const sfCustomSoundStream *soundStream) {
@@ -121,14 +202,56 @@ extern "C" float sfCustomSoundStream_getMinDistance(const sfCustomSoundStream *s
121
202
return soundStream->getMinDistance ();
122
203
}
123
204
205
+ extern " C" float sfCustomSoundStream_getMaxDistance (const sfCustomSoundStream *soundStream) {
206
+ return soundStream->getMaxDistance ();
207
+ }
208
+
209
+ extern " C" float sfCustomSoundStream_getMinGain (const sfCustomSoundStream *soundStream) {
210
+ return soundStream->getMinGain ();
211
+ }
212
+
213
+ extern " C" float sfCustomSoundStream_getMaxGain (const sfCustomSoundStream *soundStream) {
214
+ return soundStream->getMaxGain ();
215
+ }
216
+
124
217
extern " C" float sfCustomSoundStream_getAttenuation (const sfCustomSoundStream *soundStream) {
125
218
return soundStream->getAttenuation ();
126
219
}
127
220
128
- extern " C" bool sfCustomSoundStream_getLoop (const sfCustomSoundStream *soundStream) {
129
- return soundStream->getLoop ();
221
+ extern " C" bool sfCustomSoundStream_isLooping (const sfCustomSoundStream *soundStream) {
222
+ return soundStream->isLooping ();
130
223
}
131
224
132
225
extern " C" int64_t sfCustomSoundStream_getPlayingOffset (const sfCustomSoundStream *soundStream) {
133
226
return soundStream->getPlayingOffset ().asMicroseconds ();
134
227
}
228
+
229
+ static std::map<sfCustomSoundStream *, std::pair<sfEffectProcessor, void *>> processors;
230
+ static std::mutex processorMutex;
231
+
232
+ extern " C" void sfCustomSoundStream_setEffectProcessor (sfCustomSoundStream *soundStream, sfEffectProcessor effectProcessor, void *userData) {
233
+ std::unique_lock<std::mutex> lock (processorMutex);
234
+ if (!effectProcessor) {
235
+ processors.erase (soundStream);
236
+ soundStream->setEffectProcessor (nullptr );
237
+ } else {
238
+ processors[soundStream] = {effectProcessor, userData};
239
+ soundStream->setEffectProcessor (
240
+ [soundStream](const float *inputFrames,
241
+ unsigned int &inputFrameCount,
242
+ float *outputFrames,
243
+ unsigned int &outputFrameCount,
244
+ unsigned int frameChannelCount) {
245
+ std::unique_lock<std::mutex> lock (processorMutex);
246
+ auto it = processors.find (soundStream);
247
+ if (it != processors.end ()) {
248
+ it->second .first (inputFrames, &inputFrameCount, outputFrames, &outputFrameCount, frameChannelCount, it->second .second );
249
+ }
250
+ });
251
+ }
252
+ }
253
+
254
+ extern " C" void sfCustomSoundStream_del (sfCustomSoundStream *music) {
255
+ sfCustomSoundStream_setEffectProcessor (music, nullptr , nullptr );
256
+ delete music;
257
+ }
0 commit comments