sfml3-rb 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +9 -0
- data/CHANGELOG.md +43 -17
- data/README.md +97 -29
- data/ext/audio/audio_enums.c +58 -20
- data/ext/audio/listener.c +89 -6
- data/ext/audio/music.c +206 -5
- data/ext/audio/sound.c +175 -5
- data/ext/audio/sound_buffer.c +189 -47
- data/ext/audio/sound_buffer_recorder.c +83 -16
- data/ext/audio/sound_recorder.c +172 -52
- data/ext/audio/sound_source_cone.c +108 -29
- data/ext/audio/sound_stream.c +178 -5
- data/ext/core/exceptions.c +10 -8
- data/ext/core/unicode.c +12 -2
- data/ext/ext.c +4 -0
- data/ext/graphics/blend_mode.c +203 -53
- data/ext/graphics/circle.c +271 -4
- data/ext/graphics/color.c +240 -90
- data/ext/graphics/drawable.c +15 -4
- data/ext/graphics/font.c +138 -25
- data/ext/graphics/glyph.c +31 -8
- data/ext/graphics/image.c +180 -37
- data/ext/graphics/polygon.c +293 -26
- data/ext/graphics/rect.c +211 -54
- data/ext/graphics/rectangle.c +279 -26
- data/ext/graphics/render_state.c +116 -2
- data/ext/graphics/render_texture.c +163 -2
- data/ext/graphics/shader.c +283 -17
- data/ext/graphics/shape.c +252 -26
- data/ext/graphics/sprite.c +216 -25
- data/ext/graphics/stencil_mode.c +148 -39
- data/ext/graphics/target.c +67 -22
- data/ext/graphics/target.h +15 -15
- data/ext/graphics/text.c +306 -6
- data/ext/graphics/texture.c +257 -3
- data/ext/graphics/transform.c +178 -5
- data/ext/graphics/transformable.c +126 -2
- data/ext/graphics/vertex.c +79 -2
- data/ext/graphics/vertex_array.c +128 -16
- data/ext/graphics/vertex_buffer.c +124 -2
- data/ext/graphics/view.c +130 -2
- data/ext/network/ftp.c +249 -10
- data/ext/network/http.c +156 -38
- data/ext/network/ip_address.c +107 -26
- data/ext/network/network_enums.c +248 -141
- data/ext/network/packet.c +273 -39
- data/ext/network/socket_selector.c +91 -19
- data/ext/network/tcp_listener.c +75 -16
- data/ext/network/tcp_socket.c +128 -25
- data/ext/network/udp_socket.c +122 -26
- data/ext/ports.rb +7 -0
- data/ext/system/buffer.c +41 -12
- data/ext/system/clock.c +68 -9
- data/ext/system/input_stream.c +94 -42
- data/ext/system/sleep.c +10 -2
- data/ext/system/time.c +172 -34
- data/ext/system/vec2.c +156 -35
- data/ext/system/vec3.c +166 -35
- data/ext/window/clipboard.c +47 -17
- data/ext/window/context.c +59 -9
- data/ext/window/context_settings.c +116 -38
- data/ext/window/cursor.c +39 -9
- data/ext/window/event.c +163 -42
- data/ext/window/joystick.c +69 -3
- data/ext/window/keyboard.c +168 -116
- data/ext/window/mouse.c +36 -2
- data/ext/window/sensor.c +28 -2
- data/ext/window/touch.c +20 -2
- data/ext/window/video_mode.c +90 -19
- data/ext/window/vulkan.c +34 -6
- data/ext/window/window.c +340 -16
- data/lib/sfml/version.rb +1 -1
- data/sig/audio/audio_enums.rbs +30 -0
- data/sig/audio/listener.rbs +16 -0
- data/sig/audio/music.rbs +56 -0
- data/sig/audio/sound.rbs +51 -0
- data/sig/audio/sound_buffer.rbs +17 -0
- data/sig/audio/sound_buffer_recorder.rbs +14 -0
- data/sig/audio/sound_recorder.rbs +17 -0
- data/sig/audio/sound_source_cone.rbs +14 -0
- data/sig/audio/sound_stream.rbs +51 -0
- data/sig/graphics/blend_mode.rbs +29 -0
- data/sig/graphics/circle.rbs +44 -0
- data/sig/graphics/color.rbs +39 -0
- data/sig/graphics/drawable.rbs +5 -0
- data/sig/graphics/font.rbs +20 -0
- data/sig/graphics/glyph.rbs +7 -0
- data/sig/graphics/image.rbs +22 -0
- data/sig/graphics/polygon.rbs +42 -0
- data/sig/graphics/rect.rbs +33 -0
- data/sig/graphics/rectangle.rbs +42 -0
- data/sig/graphics/render_state.rbs +21 -0
- data/sig/graphics/render_texture.rbs +37 -0
- data/sig/graphics/shader.rbs +39 -0
- data/sig/graphics/shape.rbs +38 -0
- data/sig/graphics/sprite.rbs +34 -0
- data/sig/graphics/stencil_mode.rbs +18 -0
- data/sig/graphics/target.rbs +10 -0
- data/sig/graphics/text.rbs +48 -0
- data/sig/graphics/texture.rbs +34 -0
- data/sig/graphics/transform.rbs +37 -0
- data/sig/graphics/transformable.rbs +25 -0
- data/sig/graphics/vertex.rbs +17 -0
- data/sig/graphics/vertex_array.rbs +17 -0
- data/sig/graphics/vertex_buffer.rbs +22 -0
- data/sig/graphics/view.rbs +24 -0
- data/sig/network/ftp.rbs +46 -0
- data/sig/network/http.rbs +27 -0
- data/sig/network/ip_address.rbs +22 -0
- data/sig/network/network_enums.rbs +95 -0
- data/sig/network/packet.rbs +40 -0
- data/sig/network/socket_selector.rbs +14 -0
- data/sig/network/tcp_listener.rbs +13 -0
- data/sig/network/tcp_socket.rbs +18 -0
- data/sig/network/udp_socket.rbs +17 -0
- data/sig/system/buffer.rbs +11 -0
- data/sig/system/clock.rbs +13 -0
- data/sig/system/input_stream.rbs +7 -0
- data/sig/system/sleep.rbs +3 -0
- data/sig/system/time.rbs +28 -0
- data/sig/system/vec2.rbs +26 -0
- data/sig/system/vec3.rbs +27 -0
- data/sig/window/clipboard.rbs +8 -0
- data/sig/window/context.rbs +11 -0
- data/sig/window/context_settings.rbs +20 -0
- data/sig/window/cursor.rbs +6 -0
- data/sig/window/event.rbs +20 -0
- data/sig/window/joystick.rbs +15 -0
- data/sig/window/keyboard.rbs +10 -0
- data/sig/window/mouse.rbs +9 -0
- data/sig/window/sensor.rbs +8 -0
- data/sig/window/touch.rbs +6 -0
- data/sig/window/video_mode.rbs +16 -0
- data/sig/window/vulkan.rbs +7 -0
- data/sig/window/window.rbs +57 -0
- metadata +66 -2
data/ext/audio/sound_buffer.c
CHANGED
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
#include "system/time.h"
|
|
13
13
|
|
|
14
14
|
typedef struct {
|
|
15
|
-
sfSoundBuffer
|
|
15
|
+
sfSoundBuffer* buffer;
|
|
16
16
|
bool owns;
|
|
17
17
|
} SoundBuffer;
|
|
18
18
|
|
|
19
19
|
static VALUE rb_cSoundBuffer;
|
|
20
20
|
|
|
21
|
-
static void SoundBuffer_free(void
|
|
22
|
-
SoundBuffer
|
|
21
|
+
static void SoundBuffer_free(void* ptr) {
|
|
22
|
+
SoundBuffer* sound_buffer = ptr;
|
|
23
23
|
|
|
24
24
|
if (sound_buffer->owns) {
|
|
25
25
|
sfSoundBuffer_destroy(sound_buffer->buffer);
|
|
@@ -31,11 +31,10 @@ static void SoundBuffer_free(void *ptr) {
|
|
|
31
31
|
static const rb_data_type_t SoundBuffer_data_type = {
|
|
32
32
|
.wrap_struct_name = "SFML::SoundBuffer",
|
|
33
33
|
.function = {.dmark = NULL, .dfree = SoundBuffer_free, .dsize = NULL},
|
|
34
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
35
|
-
};
|
|
34
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
36
35
|
|
|
37
|
-
static VALUE SoundBuffer_wrap(VALUE klass, sfSoundBuffer
|
|
38
|
-
SoundBuffer
|
|
36
|
+
static VALUE SoundBuffer_wrap(VALUE klass, sfSoundBuffer* buffer) {
|
|
37
|
+
SoundBuffer* ptr;
|
|
39
38
|
|
|
40
39
|
if (buffer == NULL) {
|
|
41
40
|
rb_raise(rb_eRuntimeError, "failed to create sound buffer");
|
|
@@ -48,36 +47,60 @@ static VALUE SoundBuffer_wrap(VALUE klass, sfSoundBuffer *buffer) {
|
|
|
48
47
|
return TypedData_Wrap_Struct(klass, &SoundBuffer_data_type, ptr);
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
VALUE sound_buffer_from_borrowed(const sfSoundBuffer
|
|
52
|
-
SoundBuffer
|
|
50
|
+
VALUE sound_buffer_from_borrowed(const sfSoundBuffer* buffer) {
|
|
51
|
+
SoundBuffer* ptr;
|
|
53
52
|
|
|
54
53
|
if (buffer == NULL) {
|
|
55
54
|
return Qnil;
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
ptr = malloc(sizeof(SoundBuffer));
|
|
59
|
-
ptr->buffer = (sfSoundBuffer
|
|
58
|
+
ptr->buffer = (sfSoundBuffer*)buffer;
|
|
60
59
|
ptr->owns = false;
|
|
61
60
|
|
|
62
61
|
return TypedData_Wrap_Struct(rb_cSoundBuffer, &SoundBuffer_data_type, ptr);
|
|
63
62
|
}
|
|
64
63
|
|
|
64
|
+
/* call-seq:
|
|
65
|
+
* SoundBuffer.from_file(path) -> SoundBuffer
|
|
66
|
+
*
|
|
67
|
+
* Loads and decodes an audio file into a buffer.
|
|
68
|
+
*
|
|
69
|
+
* @return [SoundBuffer]
|
|
70
|
+
* @raise [RuntimeError] if the file cannot be opened or decoded
|
|
71
|
+
*/
|
|
65
72
|
static VALUE SoundBuffer_from_file(VALUE klass, VALUE rb_path) {
|
|
66
73
|
return SoundBuffer_wrap(klass, sfSoundBuffer_createFromFile(StringValueCStr(rb_path)));
|
|
67
74
|
}
|
|
68
75
|
|
|
76
|
+
/* call-seq:
|
|
77
|
+
* SoundBuffer.from_memory(data) -> SoundBuffer
|
|
78
|
+
*
|
|
79
|
+
* Decodes audio held in a String into a buffer.
|
|
80
|
+
*
|
|
81
|
+
* @return [SoundBuffer]
|
|
82
|
+
* @raise [RuntimeError] if +data+ cannot be decoded
|
|
83
|
+
*/
|
|
69
84
|
static VALUE SoundBuffer_from_memory(VALUE klass, VALUE rb_data) {
|
|
70
85
|
StringValue(rb_data);
|
|
71
86
|
|
|
72
|
-
return SoundBuffer_wrap(
|
|
73
|
-
|
|
87
|
+
return SoundBuffer_wrap(
|
|
88
|
+
klass, sfSoundBuffer_createFromMemory(RSTRING_PTR(rb_data), (size_t)RSTRING_LEN(rb_data)));
|
|
74
89
|
}
|
|
75
90
|
|
|
91
|
+
/* call-seq:
|
|
92
|
+
* SoundBuffer.from_stream(stream) -> SoundBuffer
|
|
93
|
+
*
|
|
94
|
+
* Decodes audio read from an SFML InputStream into a buffer.
|
|
95
|
+
*
|
|
96
|
+
* @return [SoundBuffer]
|
|
97
|
+
* @raise [RuntimeError] if the stream cannot be decoded
|
|
98
|
+
*/
|
|
76
99
|
static VALUE SoundBuffer_from_stream(VALUE klass, VALUE rb_stream) {
|
|
77
100
|
VALUE holder = Qnil;
|
|
78
|
-
sfInputStream
|
|
101
|
+
sfInputStream* stream = input_stream_from_rb(rb_stream, &holder);
|
|
79
102
|
|
|
80
|
-
(void)
|
|
103
|
+
(void)holder;
|
|
81
104
|
|
|
82
105
|
return SoundBuffer_wrap(klass, sfSoundBuffer_createFromStream(stream));
|
|
83
106
|
}
|
|
@@ -85,12 +108,13 @@ static VALUE SoundBuffer_from_stream(VALUE klass, VALUE rb_stream) {
|
|
|
85
108
|
/* SFML rejects a buffer whose channel map does not have exactly one entry per
|
|
86
109
|
channel, so an omitted map is filled with the conventional layout for 1 and
|
|
87
110
|
2 channels and left Unspecified beyond that. */
|
|
88
|
-
static void SoundBuffer_default_channel_map(unsigned int channel_count, sfSoundChannel
|
|
111
|
+
static void SoundBuffer_default_channel_map(unsigned int channel_count, sfSoundChannel* map) {
|
|
89
112
|
static const sfSoundChannel surround[] = {
|
|
90
|
-
sfSoundChannelFrontLeft,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
113
|
+
sfSoundChannelFrontLeft, sfSoundChannelFrontRight,
|
|
114
|
+
sfSoundChannelFrontCenter, sfSoundChannelLowFrequencyEffects,
|
|
115
|
+
sfSoundChannelBackLeft, sfSoundChannelBackRight,
|
|
116
|
+
sfSoundChannelSideLeft, sfSoundChannelSideRight,
|
|
117
|
+
sfSoundChannelTopCenter};
|
|
94
118
|
unsigned int i;
|
|
95
119
|
|
|
96
120
|
for (i = 0; i < channel_count; i++) {
|
|
@@ -106,28 +130,76 @@ static void SoundBuffer_default_channel_map(unsigned int channel_count, sfSoundC
|
|
|
106
130
|
}
|
|
107
131
|
}
|
|
108
132
|
|
|
133
|
+
typedef struct {
|
|
134
|
+
sfSoundChannel* channel_map;
|
|
135
|
+
VALUE rb_channel_map;
|
|
136
|
+
unsigned int channel_count;
|
|
137
|
+
} ChannelMapFillContext;
|
|
138
|
+
|
|
139
|
+
/* sound_channel_from_rb raises ArgumentError on an unrecognized entry; run
|
|
140
|
+
through rb_protect so the caller can free the samples/channel_map buffers
|
|
141
|
+
(already allocated by this point) before that exception propagates,
|
|
142
|
+
instead of leaking them. */
|
|
143
|
+
static VALUE SoundBuffer_fill_channel_map(VALUE raw) {
|
|
144
|
+
ChannelMapFillContext* ctx = (ChannelMapFillContext*)raw;
|
|
145
|
+
long i;
|
|
146
|
+
|
|
147
|
+
for (i = 0; i < (long)ctx->channel_count; i++) {
|
|
148
|
+
ctx->channel_map[i] = sound_channel_from_rb(rb_ary_entry(ctx->rb_channel_map, i));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return Qnil;
|
|
152
|
+
}
|
|
153
|
+
|
|
109
154
|
/* Copies the samples into a contiguous int16 buffer: an Array is converted
|
|
110
|
-
element by element, a String
|
|
111
|
-
storage. The channel map, when given, is
|
|
112
|
-
|
|
155
|
+
element by element, and a String's packed int16 samples are copied out of
|
|
156
|
+
the (possibly unaligned) Ruby storage. The channel map, when given, is
|
|
157
|
+
translated symbol by symbol. */
|
|
158
|
+
/* call-seq:
|
|
159
|
+
* SoundBuffer.from_samples(samples, channel_count, sample_rate) -> SoundBuffer
|
|
160
|
+
* SoundBuffer.from_samples(samples, channel_count, sample_rate, channel_map) -> SoundBuffer
|
|
161
|
+
*
|
|
162
|
+
* +samples+ is an Array of Integer samples or a packed String of int16
|
|
163
|
+
* samples, interleaved by channel. +channel_map+, if given, is an Array of
|
|
164
|
+
* channel Symbols (see SoundChannel), one per channel; if omitted, a
|
|
165
|
+
* conventional layout is assumed for 1 and 2 channels.
|
|
166
|
+
*
|
|
167
|
+
* @return [SoundBuffer]
|
|
168
|
+
* @raise [ArgumentError] if +channel_count+ is 0, +samples+ is neither an
|
|
169
|
+
* Array nor a String, or +channel_map+ doesn't have exactly one entry per
|
|
170
|
+
* channel
|
|
171
|
+
*/
|
|
172
|
+
static VALUE SoundBuffer_from_samples(int argc, VALUE* argv, VALUE klass) {
|
|
113
173
|
VALUE rb_samples, rb_channel_count, rb_sample_rate, rb_channel_map;
|
|
114
|
-
int16_t
|
|
115
|
-
sfSoundChannel
|
|
174
|
+
int16_t* samples;
|
|
175
|
+
sfSoundChannel* channel_map;
|
|
116
176
|
unsigned int channel_count;
|
|
117
177
|
uint64_t sample_count;
|
|
118
|
-
sfSoundBuffer
|
|
178
|
+
sfSoundBuffer* buffer;
|
|
119
179
|
|
|
120
180
|
rb_scan_args(argc, argv, "31", &rb_samples, &rb_channel_count, &rb_sample_rate,
|
|
121
181
|
&rb_channel_map);
|
|
122
182
|
|
|
123
|
-
channel_count = (unsigned int)
|
|
183
|
+
channel_count = (unsigned int)NUM2INT(rb_channel_count);
|
|
124
184
|
|
|
125
185
|
if (channel_count == 0) {
|
|
126
186
|
rb_raise(rb_eArgError, "channel count must be positive");
|
|
127
187
|
}
|
|
128
188
|
|
|
189
|
+
/* channel_map's malloc below (sizeof(sfSoundChannel) * channel_count) can
|
|
190
|
+
wrap on the 32-bit targets this gem ships if channel_count is large
|
|
191
|
+
enough, allocating far less than the subsequent write loop assumes.
|
|
192
|
+
Check before any allocation happens, so there's nothing to clean up on
|
|
193
|
+
the raise path. (Written with channel_count as the divisor, not
|
|
194
|
+
compared directly against a constant bound, so this isn't optimized
|
|
195
|
+
away as tautological on 64-bit hosts where it can't actually trigger
|
|
196
|
+
-- it still does real work on the 32-bit targets.) */
|
|
197
|
+
if (SIZE_MAX / channel_count < sizeof(sfSoundChannel)) {
|
|
198
|
+
rb_raise(rb_eArgError, "channel count too large");
|
|
199
|
+
}
|
|
200
|
+
|
|
129
201
|
if (RB_TYPE_P(rb_samples, T_STRING)) {
|
|
130
|
-
size_t bytes = (size_t)
|
|
202
|
+
size_t bytes = (size_t)RSTRING_LEN(rb_samples);
|
|
131
203
|
|
|
132
204
|
if (bytes % sizeof(int16_t) != 0) {
|
|
133
205
|
rb_raise(rb_eArgError, "sample string length must be a multiple of 2 bytes");
|
|
@@ -137,16 +209,21 @@ static VALUE SoundBuffer_from_samples(int argc, VALUE *argv, VALUE klass) {
|
|
|
137
209
|
samples = malloc(bytes > 0 ? bytes : 1);
|
|
138
210
|
|
|
139
211
|
if (bytes > 0) {
|
|
140
|
-
|
|
212
|
+
const int16_t* src = (const int16_t*)RSTRING_PTR(rb_samples);
|
|
213
|
+
size_t i;
|
|
214
|
+
|
|
215
|
+
for (i = 0; i < sample_count; i++) {
|
|
216
|
+
samples[i] = src[i];
|
|
217
|
+
}
|
|
141
218
|
}
|
|
142
219
|
} else if (RB_TYPE_P(rb_samples, T_ARRAY)) {
|
|
143
220
|
long i;
|
|
144
221
|
|
|
145
|
-
sample_count = (uint64_t)
|
|
146
|
-
samples = malloc(sizeof(int16_t) * ((size_t)
|
|
222
|
+
sample_count = (uint64_t)RARRAY_LEN(rb_samples);
|
|
223
|
+
samples = malloc(sizeof(int16_t) * ((size_t)sample_count + 1));
|
|
147
224
|
|
|
148
|
-
for (i = 0; i < (long)
|
|
149
|
-
samples[i] = (int16_t)
|
|
225
|
+
for (i = 0; i < (long)sample_count; i++) {
|
|
226
|
+
samples[i] = (int16_t)NUM2INT(rb_ary_entry(rb_samples, i));
|
|
150
227
|
}
|
|
151
228
|
} else {
|
|
152
229
|
rb_raise(rb_eArgError, "samples must be an Array of integers or a String");
|
|
@@ -158,23 +235,30 @@ static VALUE SoundBuffer_from_samples(int argc, VALUE *argv, VALUE klass) {
|
|
|
158
235
|
if (NIL_P(rb_channel_map)) {
|
|
159
236
|
SoundBuffer_default_channel_map(channel_count, channel_map);
|
|
160
237
|
} else {
|
|
161
|
-
|
|
238
|
+
ChannelMapFillContext ctx = {.channel_map = channel_map,
|
|
239
|
+
.rb_channel_map = rb_channel_map,
|
|
240
|
+
.channel_count = channel_count};
|
|
241
|
+
int state = 0;
|
|
162
242
|
|
|
163
243
|
if (!RB_TYPE_P(rb_channel_map, T_ARRAY) ||
|
|
164
|
-
(unsigned long)
|
|
244
|
+
(unsigned long)RARRAY_LEN(rb_channel_map) != channel_count) {
|
|
165
245
|
free(samples);
|
|
166
246
|
free(channel_map);
|
|
167
247
|
rb_raise(rb_eArgError, "channel map must have exactly one entry per channel");
|
|
168
248
|
}
|
|
169
249
|
|
|
170
|
-
|
|
171
|
-
|
|
250
|
+
rb_protect(SoundBuffer_fill_channel_map, (VALUE)&ctx, &state);
|
|
251
|
+
|
|
252
|
+
if (state) {
|
|
253
|
+
free(samples);
|
|
254
|
+
free(channel_map);
|
|
255
|
+
rb_jump_tag(state);
|
|
172
256
|
}
|
|
173
257
|
}
|
|
174
258
|
|
|
175
259
|
buffer = sfSoundBuffer_createFromSamples(samples, sample_count, channel_count,
|
|
176
|
-
|
|
177
|
-
|
|
260
|
+
(unsigned int)NUM2INT(rb_sample_rate), channel_map,
|
|
261
|
+
channel_count);
|
|
178
262
|
|
|
179
263
|
free(samples);
|
|
180
264
|
free(channel_map);
|
|
@@ -182,17 +266,38 @@ static VALUE SoundBuffer_from_samples(int argc, VALUE *argv, VALUE klass) {
|
|
|
182
266
|
return SoundBuffer_wrap(klass, buffer);
|
|
183
267
|
}
|
|
184
268
|
|
|
269
|
+
/* call-seq: copy -> SoundBuffer
|
|
270
|
+
*
|
|
271
|
+
* Creates an independent copy of the buffer.
|
|
272
|
+
*
|
|
273
|
+
* @return [SoundBuffer] an independent copy
|
|
274
|
+
*/
|
|
185
275
|
static VALUE SoundBuffer_copy(VALUE self) {
|
|
186
276
|
return SoundBuffer_wrap(Get_Klass_SoundBuffer(),
|
|
187
277
|
sfSoundBuffer_copy(Get_SoundBuffer_Struct(self)));
|
|
188
278
|
}
|
|
189
279
|
|
|
280
|
+
/* call-seq:
|
|
281
|
+
* save_to_file(path) -> true or false
|
|
282
|
+
*
|
|
283
|
+
* Writes the buffer to an audio file.
|
|
284
|
+
*
|
|
285
|
+
* @return [Boolean] whether the file was written successfully
|
|
286
|
+
*/
|
|
190
287
|
static VALUE SoundBuffer_save_to_file(VALUE self, VALUE rb_path) {
|
|
191
|
-
return BOOL2RB(
|
|
288
|
+
return BOOL2RB(
|
|
289
|
+
sfSoundBuffer_saveToFile(Get_SoundBuffer_Struct(self), StringValueCStr(rb_path)));
|
|
192
290
|
}
|
|
193
291
|
|
|
292
|
+
/* call-seq: samples -> Array<Integer>
|
|
293
|
+
*
|
|
294
|
+
* Returns the buffer's raw audio samples as Integers.
|
|
295
|
+
*
|
|
296
|
+
* @return [Array<Integer>] the raw int16 audio samples, interleaved by
|
|
297
|
+
* channel
|
|
298
|
+
*/
|
|
194
299
|
static VALUE SoundBuffer_samples(VALUE self) {
|
|
195
|
-
const int16_t
|
|
300
|
+
const int16_t* samples = sfSoundBuffer_getSamples(Get_SoundBuffer_Struct(self));
|
|
196
301
|
uint64_t count = sfSoundBuffer_getSampleCount(Get_SoundBuffer_Struct(self));
|
|
197
302
|
VALUE rb_array;
|
|
198
303
|
uint64_t i;
|
|
@@ -201,7 +306,7 @@ static VALUE SoundBuffer_samples(VALUE self) {
|
|
|
201
306
|
return rb_ary_new();
|
|
202
307
|
}
|
|
203
308
|
|
|
204
|
-
rb_array = rb_ary_new_capa((long)
|
|
309
|
+
rb_array = rb_ary_new_capa((long)count);
|
|
205
310
|
|
|
206
311
|
for (i = 0; i < count; i++) {
|
|
207
312
|
rb_ary_push(rb_array, INT2NUM(samples[i]));
|
|
@@ -210,21 +315,46 @@ static VALUE SoundBuffer_samples(VALUE self) {
|
|
|
210
315
|
return rb_array;
|
|
211
316
|
}
|
|
212
317
|
|
|
318
|
+
/* call-seq: sample_count -> Integer
|
|
319
|
+
*
|
|
320
|
+
* Returns the total number of samples stored in the buffer.
|
|
321
|
+
*
|
|
322
|
+
* @return [Integer] total number of int16 samples, across all channels
|
|
323
|
+
*/
|
|
213
324
|
static VALUE SoundBuffer_sample_count(VALUE self) {
|
|
214
325
|
return ULL2NUM(sfSoundBuffer_getSampleCount(Get_SoundBuffer_Struct(self)));
|
|
215
326
|
}
|
|
216
327
|
|
|
328
|
+
/* call-seq: sample_rate -> Integer
|
|
329
|
+
*
|
|
330
|
+
* Returns the buffer's sample rate in samples per second.
|
|
331
|
+
*
|
|
332
|
+
* @return [Integer]
|
|
333
|
+
*/
|
|
217
334
|
static VALUE SoundBuffer_sample_rate(VALUE self) {
|
|
218
335
|
return UINT2NUM(sfSoundBuffer_getSampleRate(Get_SoundBuffer_Struct(self)));
|
|
219
336
|
}
|
|
220
337
|
|
|
338
|
+
/* call-seq: channel_count -> Integer
|
|
339
|
+
*
|
|
340
|
+
* Returns the number of audio channels in the buffer.
|
|
341
|
+
*
|
|
342
|
+
* @return [Integer]
|
|
343
|
+
*/
|
|
221
344
|
static VALUE SoundBuffer_channel_count(VALUE self) {
|
|
222
345
|
return UINT2NUM(sfSoundBuffer_getChannelCount(Get_SoundBuffer_Struct(self)));
|
|
223
346
|
}
|
|
224
347
|
|
|
348
|
+
/* call-seq: channel_map -> Array<Symbol>
|
|
349
|
+
*
|
|
350
|
+
* Returns the channel layout of the buffer.
|
|
351
|
+
*
|
|
352
|
+
* @return [Array<Symbol>] one entry per channel, e.g.
|
|
353
|
+
* +[:front_left, :front_right]+
|
|
354
|
+
*/
|
|
225
355
|
static VALUE SoundBuffer_channel_map(VALUE self) {
|
|
226
356
|
size_t count = 0;
|
|
227
|
-
sfSoundChannel
|
|
357
|
+
sfSoundChannel* map = sfSoundBuffer_getChannelMap(Get_SoundBuffer_Struct(self), &count);
|
|
228
358
|
VALUE rb_array;
|
|
229
359
|
size_t i;
|
|
230
360
|
|
|
@@ -232,7 +362,7 @@ static VALUE SoundBuffer_channel_map(VALUE self) {
|
|
|
232
362
|
return rb_ary_new();
|
|
233
363
|
}
|
|
234
364
|
|
|
235
|
-
rb_array = rb_ary_new_capa((long)
|
|
365
|
+
rb_array = rb_ary_new_capa((long)count);
|
|
236
366
|
|
|
237
367
|
for (i = 0; i < count; i++) {
|
|
238
368
|
rb_ary_push(rb_array, ID2SYM(rb_intern(sound_channel_name(map[i]))));
|
|
@@ -241,12 +371,24 @@ static VALUE SoundBuffer_channel_map(VALUE self) {
|
|
|
241
371
|
return rb_array;
|
|
242
372
|
}
|
|
243
373
|
|
|
374
|
+
/* call-seq: duration -> Time
|
|
375
|
+
*
|
|
376
|
+
* Returns the buffer's total playing duration.
|
|
377
|
+
*
|
|
378
|
+
* @return [Time]
|
|
379
|
+
*/
|
|
244
380
|
static VALUE SoundBuffer_duration(VALUE self) {
|
|
245
381
|
return time_to_rb(sfSoundBuffer_getDuration(Get_SoundBuffer_Struct(self)));
|
|
246
382
|
}
|
|
247
383
|
|
|
248
|
-
|
|
249
|
-
|
|
384
|
+
/* Document-class: SFML::SoundBuffer
|
|
385
|
+
* Audio samples held fully decoded in memory, ready to be played through one
|
|
386
|
+
* or more Sound instances (a single buffer may back several simultaneous
|
|
387
|
+
* Sounds). For long audio, prefer Music, which streams instead of loading
|
|
388
|
+
* everything up front.
|
|
389
|
+
*/
|
|
390
|
+
void Init_SoundBuffer(VALUE rb_mSFML) {
|
|
391
|
+
rb_cSoundBuffer = rb_define_class_under(rb_mSFML, "SoundBuffer", rb_cObject);
|
|
250
392
|
|
|
251
393
|
rb_define_singleton_method(rb_cSoundBuffer, "from_file", SoundBuffer_from_file, 1);
|
|
252
394
|
rb_define_singleton_method(rb_cSoundBuffer, "from_memory", SoundBuffer_from_memory, 1);
|
|
@@ -267,8 +409,8 @@ VALUE Get_Klass_SoundBuffer(void) {
|
|
|
267
409
|
return rb_cSoundBuffer;
|
|
268
410
|
}
|
|
269
411
|
|
|
270
|
-
void
|
|
271
|
-
SoundBuffer
|
|
412
|
+
void* Get_SoundBuffer_Struct(VALUE self) {
|
|
413
|
+
SoundBuffer* ptr;
|
|
272
414
|
TypedData_Get_Struct(self, SoundBuffer, &SoundBuffer_data_type, ptr);
|
|
273
415
|
return ptr->buffer;
|
|
274
416
|
}
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
#include "core/macros.h"
|
|
9
9
|
|
|
10
10
|
typedef struct {
|
|
11
|
-
sfSoundBufferRecorder
|
|
11
|
+
sfSoundBufferRecorder* handle;
|
|
12
12
|
} SoundBufferRecorder;
|
|
13
13
|
|
|
14
14
|
static VALUE rb_cSoundBufferRecorder;
|
|
15
15
|
|
|
16
|
-
static void SoundBufferRecorder_free(void
|
|
17
|
-
SoundBufferRecorder
|
|
16
|
+
static void SoundBufferRecorder_free(void* ptr) {
|
|
17
|
+
SoundBufferRecorder* recorder = ptr;
|
|
18
18
|
|
|
19
19
|
if (recorder->handle != NULL) {
|
|
20
20
|
sfSoundBufferRecorder_destroy(recorder->handle);
|
|
@@ -26,11 +26,10 @@ static void SoundBufferRecorder_free(void *ptr) {
|
|
|
26
26
|
static const rb_data_type_t SoundBufferRecorder_data_type = {
|
|
27
27
|
.wrap_struct_name = "SFML::SoundBufferRecorder",
|
|
28
28
|
.function = {.dmark = NULL, .dfree = SoundBufferRecorder_free, .dsize = NULL},
|
|
29
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
30
|
-
};
|
|
29
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY};
|
|
31
30
|
|
|
32
|
-
static VALUE SoundBufferRecorder_wrap(VALUE klass, sfSoundBufferRecorder
|
|
33
|
-
SoundBufferRecorder
|
|
31
|
+
static VALUE SoundBufferRecorder_wrap(VALUE klass, sfSoundBufferRecorder* handle) {
|
|
32
|
+
SoundBufferRecorder* ptr;
|
|
34
33
|
|
|
35
34
|
if (handle == NULL) {
|
|
36
35
|
rb_raise(rb_eRuntimeError, "failed to create sound buffer recorder (no capture device?)");
|
|
@@ -42,49 +41,115 @@ static VALUE SoundBufferRecorder_wrap(VALUE klass, sfSoundBufferRecorder *handle
|
|
|
42
41
|
return TypedData_Wrap_Struct(klass, &SoundBufferRecorder_data_type, ptr);
|
|
43
42
|
}
|
|
44
43
|
|
|
44
|
+
/* call-seq:
|
|
45
|
+
* SoundBufferRecorder.new -> SoundBufferRecorder
|
|
46
|
+
*
|
|
47
|
+
* Creates a recorder that captures into a SoundBuffer.
|
|
48
|
+
*
|
|
49
|
+
* @return [SoundBufferRecorder]
|
|
50
|
+
* @raise [RuntimeError] if no capture device is available
|
|
51
|
+
*/
|
|
45
52
|
static VALUE SoundBufferRecorder_new(VALUE klass) {
|
|
46
53
|
return SoundBufferRecorder_wrap(klass, sfSoundBufferRecorder_create());
|
|
47
54
|
}
|
|
48
55
|
|
|
56
|
+
/* call-seq:
|
|
57
|
+
* start(sample_rate) -> true or false
|
|
58
|
+
*
|
|
59
|
+
* Starts capturing audio at the given sample rate.
|
|
60
|
+
*
|
|
61
|
+
* @return [Boolean] whether recording started successfully
|
|
62
|
+
*/
|
|
49
63
|
static VALUE SoundBufferRecorder_start(VALUE self, VALUE rb_sample_rate) {
|
|
50
64
|
return BOOL2RB(sfSoundBufferRecorder_start(Get_SoundBufferRecorder_Struct(self),
|
|
51
|
-
(unsigned int)
|
|
65
|
+
(unsigned int)NUM2INT(rb_sample_rate)));
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
/* call-seq: stop -> self
|
|
69
|
+
*
|
|
70
|
+
* Stops capturing audio.
|
|
71
|
+
*
|
|
72
|
+
* @return [self]
|
|
73
|
+
*/
|
|
54
74
|
static VALUE SoundBufferRecorder_stop(VALUE self) {
|
|
55
75
|
sfSoundBufferRecorder_stop(Get_SoundBufferRecorder_Struct(self));
|
|
56
76
|
return self;
|
|
57
77
|
}
|
|
58
78
|
|
|
79
|
+
/* call-seq: sample_rate -> Integer
|
|
80
|
+
*
|
|
81
|
+
* Returns the sample rate used for capture.
|
|
82
|
+
*
|
|
83
|
+
* @return [Integer]
|
|
84
|
+
*/
|
|
59
85
|
static VALUE SoundBufferRecorder_sample_rate(VALUE self) {
|
|
60
86
|
return UINT2NUM(sfSoundBufferRecorder_getSampleRate(Get_SoundBufferRecorder_Struct(self)));
|
|
61
87
|
}
|
|
62
88
|
|
|
89
|
+
/* call-seq: buffer -> SoundBuffer
|
|
90
|
+
*
|
|
91
|
+
* Returns the buffer holding the audio recorded so far.
|
|
92
|
+
*
|
|
93
|
+
* @return [SoundBuffer] the buffer containing the audio recorded so far
|
|
94
|
+
*/
|
|
63
95
|
static VALUE SoundBufferRecorder_buffer(VALUE self) {
|
|
64
|
-
return sound_buffer_from_borrowed(
|
|
96
|
+
return sound_buffer_from_borrowed(
|
|
97
|
+
sfSoundBufferRecorder_getBuffer(Get_SoundBufferRecorder_Struct(self)));
|
|
65
98
|
}
|
|
66
99
|
|
|
100
|
+
/* call-seq: device -> String
|
|
101
|
+
*
|
|
102
|
+
* Returns the name of the capture device in use.
|
|
103
|
+
*
|
|
104
|
+
* @return [String] the name of the capture device in use
|
|
105
|
+
*/
|
|
67
106
|
static VALUE SoundBufferRecorder_device(VALUE self) {
|
|
68
107
|
return rb_str_new_cstr(sfSoundBufferRecorder_getDevice(Get_SoundBufferRecorder_Struct(self)));
|
|
69
108
|
}
|
|
70
109
|
|
|
110
|
+
/* call-seq:
|
|
111
|
+
* device=(value) -> true or false
|
|
112
|
+
*
|
|
113
|
+
* Must be called while not recording. Get available names from
|
|
114
|
+
* SoundRecorder.available_devices.
|
|
115
|
+
*
|
|
116
|
+
* @return [Boolean] whether the device was set successfully
|
|
117
|
+
*/
|
|
71
118
|
static VALUE SoundBufferRecorder_set_device(VALUE self, VALUE rb_name) {
|
|
72
119
|
return BOOL2RB(sfSoundBufferRecorder_setDevice(Get_SoundBufferRecorder_Struct(self),
|
|
73
120
|
StringValueCStr(rb_name)));
|
|
74
121
|
}
|
|
75
122
|
|
|
123
|
+
/* call-seq: channel_count -> Integer
|
|
124
|
+
*
|
|
125
|
+
* Returns the number of capture channels.
|
|
126
|
+
*
|
|
127
|
+
* @return [Integer]
|
|
128
|
+
*/
|
|
76
129
|
static VALUE SoundBufferRecorder_channel_count(VALUE self) {
|
|
77
130
|
return UINT2NUM(sfSoundBufferRecorder_getChannelCount(Get_SoundBufferRecorder_Struct(self)));
|
|
78
131
|
}
|
|
79
132
|
|
|
133
|
+
/* call-seq:
|
|
134
|
+
* channel_count=(value) -> Integer
|
|
135
|
+
*
|
|
136
|
+
* Must be called while not recording.
|
|
137
|
+
*
|
|
138
|
+
* @return [Integer] +value+
|
|
139
|
+
*/
|
|
80
140
|
static VALUE SoundBufferRecorder_set_channel_count(VALUE self, VALUE rb_count) {
|
|
81
141
|
sfSoundBufferRecorder_setChannelCount(Get_SoundBufferRecorder_Struct(self),
|
|
82
|
-
(unsigned int)
|
|
142
|
+
(unsigned int)NUM2INT(rb_count));
|
|
83
143
|
return rb_count;
|
|
84
144
|
}
|
|
85
145
|
|
|
86
|
-
|
|
87
|
-
|
|
146
|
+
/* Document-class: SFML::SoundBufferRecorder
|
|
147
|
+
* Records audio from a capture device directly into a SoundBuffer. For
|
|
148
|
+
* custom processing of captured samples as they arrive, subclass
|
|
149
|
+
* SoundRecorder instead.
|
|
150
|
+
*/
|
|
151
|
+
void Init_SoundBufferRecorder(VALUE rb_mSFML) {
|
|
152
|
+
rb_cSoundBufferRecorder = rb_define_class_under(rb_mSFML, "SoundBufferRecorder", rb_cObject);
|
|
88
153
|
|
|
89
154
|
rb_define_singleton_method(rb_cSoundBufferRecorder, "new", SoundBufferRecorder_new, 0);
|
|
90
155
|
|
|
@@ -94,16 +159,18 @@ void Init_SoundBufferRecorder(VALUE rb_module) {
|
|
|
94
159
|
rb_define_method(rb_cSoundBufferRecorder, "buffer", SoundBufferRecorder_buffer, 0);
|
|
95
160
|
rb_define_method(rb_cSoundBufferRecorder, "device", SoundBufferRecorder_device, 0);
|
|
96
161
|
rb_define_method(rb_cSoundBufferRecorder, "device=", SoundBufferRecorder_set_device, 1);
|
|
97
|
-
rb_define_method(rb_cSoundBufferRecorder, "channel_count", SoundBufferRecorder_channel_count,
|
|
98
|
-
|
|
162
|
+
rb_define_method(rb_cSoundBufferRecorder, "channel_count", SoundBufferRecorder_channel_count,
|
|
163
|
+
0);
|
|
164
|
+
rb_define_method(rb_cSoundBufferRecorder,
|
|
165
|
+
"channel_count=", SoundBufferRecorder_set_channel_count, 1);
|
|
99
166
|
}
|
|
100
167
|
|
|
101
168
|
VALUE Get_Klass_SoundBufferRecorder(void) {
|
|
102
169
|
return rb_cSoundBufferRecorder;
|
|
103
170
|
}
|
|
104
171
|
|
|
105
|
-
void
|
|
106
|
-
SoundBufferRecorder
|
|
172
|
+
void* Get_SoundBufferRecorder_Struct(VALUE self) {
|
|
173
|
+
SoundBufferRecorder* ptr;
|
|
107
174
|
TypedData_Get_Struct(self, SoundBufferRecorder, &SoundBufferRecorder_data_type, ptr);
|
|
108
175
|
return ptr->handle;
|
|
109
176
|
}
|