miniaudio 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,237 @@
1
+ #include <ruby.h>
2
+
3
+ #define STB_VORBIS_HEADER_ONLY
4
+ #include "miniaudio/extras/stb_vorbis.c" // Enables Vorbis decoding.
5
+
6
+ #define MINIAUDIO_IMPLEMENTATION
7
+ #include "miniaudio/miniaudio.h"
8
+
9
+ // The stb_vorbis implementation must come after the implementation of miniaudio.
10
+ #undef STB_VORBIS_HEADER_ONLY
11
+ #include "miniaudio/extras/stb_vorbis.c"
12
+
13
+ #define OPEN_FILE(FORMAT, FRAME_TYPE, CODE) \
14
+ static VALUE rb_dr ## FORMAT ## _open_file_and_read_pcm_frames_ ## CODE (VALUE mod, VALUE filename) { \
15
+ if (TYPE(filename) != T_STRING) { \
16
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Filename must be a string"); \
17
+ } \
18
+ \
19
+ unsigned int num_channels; \
20
+ unsigned int sample_rate; \
21
+ uint64_t total_frames; \
22
+ \
23
+ FRAME_TYPE * frames = dr ## FORMAT ## _open_file_and_read_pcm_frames_ ## CODE (\
24
+ StringValuePtr(filename),\
25
+ &num_channels,\
26
+ &sample_rate,\
27
+ &total_frames,\
28
+ NULL\
29
+ );\
30
+ \
31
+ if (frames == NULL) {\
32
+ return Qnil;\
33
+ }\
34
+ \
35
+ VALUE ret_str = rb_str_new((char*)frames, sizeof(FRAME_TYPE)*total_frames);\
36
+ dr ## FORMAT ## _free(frames, NULL);\
37
+ VALUE ret = rb_ary_new();\
38
+ rb_ary_push(ret, ret_str);\
39
+ rb_ary_push(ret, INT2NUM(sample_rate));\
40
+ rb_ary_push(ret, INT2NUM(num_channels));\
41
+ return ret;\
42
+ }
43
+
44
+ #define OPEN_MP3_FILE(FRAME_TYPE, CODE) \
45
+ static VALUE rb_drmp3_open_file_and_read_pcm_frames_ ## CODE (VALUE mod, VALUE filename) { \
46
+ if (TYPE(filename) != T_STRING) { \
47
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Filename must be a string"); \
48
+ } \
49
+ \
50
+ uint64_t total_frames; \
51
+ \
52
+ drmp3_config config;\
53
+ FRAME_TYPE * frames = drmp3_open_file_and_read_pcm_frames_ ## CODE (\
54
+ StringValueCStr(filename),\
55
+ &config,\
56
+ &total_frames,\
57
+ NULL\
58
+ );\
59
+ \
60
+ if (frames == NULL) {\
61
+ return Qnil;\
62
+ }\
63
+ \
64
+ VALUE ret_str = rb_str_new((char*)frames, sizeof(FRAME_TYPE)*total_frames*config.channels);\
65
+ drmp3_free(frames, NULL);\
66
+ VALUE ary = rb_ary_new_from_args(3, ret_str, INT2NUM(config.sampleRate), INT2NUM(config.channels));\
67
+ return ary;\
68
+ }
69
+
70
+ #define OPEN_MEMORY(FORMAT, FRAME_TYPE, CODE) \
71
+ static VALUE rb_dr ## FORMAT ## _open_memory_and_read_pcm_frames_ ## CODE (VALUE mod, VALUE str) { \
72
+ if (TYPE(str) != T_STRING) { \
73
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Memory storage must be a string"); \
74
+ } \
75
+ \
76
+ unsigned int num_channels; \
77
+ unsigned int sample_rate; \
78
+ uint64_t total_frames; \
79
+ \
80
+ FRAME_TYPE * frames = dr ## FORMAT ## _open_memory_and_read_pcm_frames_ ## CODE (\
81
+ StringValuePtr(str),\
82
+ RSTRING_LEN(str),\
83
+ &num_channels,\
84
+ &sample_rate,\
85
+ &total_frames,\
86
+ NULL\
87
+ );\
88
+ \
89
+ if (frames == NULL) {\
90
+ return Qnil;\
91
+ }\
92
+ \
93
+ VALUE ret_str = rb_str_new((char*)frames, sizeof(FRAME_TYPE)*total_frames);\
94
+ dr ## FORMAT ## _free(frames, NULL);\
95
+ VALUE ret = rb_ary_new();\
96
+ rb_ary_push(ret, ret_str);\
97
+ rb_ary_push(ret, INT2NUM(sample_rate));\
98
+ rb_ary_push(ret, INT2NUM(num_channels));\
99
+ return ret;\
100
+ }
101
+
102
+ #define OPEN_MP3_MEMORY(FRAME_TYPE, CODE) \
103
+ static VALUE rb_drmp3_open_memory_and_read_pcm_frames_ ## CODE (VALUE mod, VALUE str) { \
104
+ if (TYPE(str) != T_STRING) { \
105
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Memory must be a string"); \
106
+ } \
107
+ \
108
+ uint64_t total_frames; \
109
+ \
110
+ drmp3_config config;\
111
+ FRAME_TYPE * frames = drmp3_open_memory_and_read_pcm_frames_ ## CODE (\
112
+ StringValuePtr(str),\
113
+ RSTRING_LEN(str),\
114
+ &config,\
115
+ &total_frames,\
116
+ NULL\
117
+ );\
118
+ \
119
+ if (frames == NULL) {\
120
+ return Qnil;\
121
+ }\
122
+ \
123
+ VALUE ret_str = rb_str_new((char*)frames, sizeof(FRAME_TYPE)*total_frames);\
124
+ drmp3_free(frames, NULL);\
125
+ VALUE ret = rb_ary_new();\
126
+ rb_ary_push(ret, ret_str);\
127
+ rb_ary_push(ret, INT2NUM(config.sampleRate));\
128
+ rb_ary_push(ret, INT2NUM(config.channels));\
129
+ return ret;\
130
+ }
131
+
132
+ static VALUE rb_stb_vorbis_decode_filename_s16 (VALUE mod, VALUE filename) {
133
+ if (TYPE(filename) != T_STRING) {
134
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Filename must be a string");
135
+ }
136
+
137
+ int num_channels;
138
+ int sample_rate;
139
+ int64_t total_frames;
140
+
141
+ int16_t *output;
142
+
143
+ total_frames = stb_vorbis_decode_filename (
144
+ StringValueCStr(filename),
145
+ &num_channels,
146
+ &sample_rate,
147
+ &output
148
+ );
149
+
150
+ if (total_frames < 0) {
151
+ return Qnil;
152
+ }
153
+
154
+ VALUE ret_str = rb_str_new((char*)output, sizeof(int16_t)*total_frames);
155
+ free(output);
156
+ VALUE ret = rb_ary_new();
157
+ rb_ary_push(ret, ret_str);
158
+ rb_ary_push(ret, INT2NUM(sample_rate));
159
+ rb_ary_push(ret, INT2NUM(num_channels));
160
+ return ret;
161
+ }
162
+
163
+ static VALUE rb_stb_vorbis_decode_memory_s16(VALUE mod, VALUE str) {
164
+ if (TYPE(str) != T_STRING) {
165
+ rb_raise(rb_const_get(rb_cObject, rb_intern("ArgumentError")), "Memory storage must be a string");
166
+ }
167
+
168
+ int num_channels;
169
+ int sample_rate;
170
+ int64_t total_frames;
171
+
172
+ int16_t *output;
173
+
174
+ total_frames = stb_vorbis_decode_memory(
175
+ (uint8*)StringValuePtr(str),
176
+ RSTRING_LEN(str),
177
+ &num_channels,
178
+ &sample_rate,
179
+ &output
180
+ );
181
+
182
+ if (total_frames < 0) {
183
+ return Qnil;
184
+ }
185
+
186
+ VALUE ret_str = rb_str_new((char*)output, sizeof(int16_t)*total_frames);
187
+ free(output);
188
+ VALUE ret = rb_ary_new();
189
+ rb_ary_push(ret, ret_str);
190
+ rb_ary_push(ret, INT2NUM(sample_rate));
191
+ rb_ary_push(ret, INT2NUM(num_channels));
192
+ return ret;
193
+ }
194
+
195
+ OPEN_FILE(wav, int16_t, s16)
196
+ OPEN_FILE(wav, int32_t, s32)
197
+ OPEN_FILE(wav, float, f32)
198
+
199
+ OPEN_MP3_FILE(int16_t, s16)
200
+ OPEN_MP3_FILE(float, f32)
201
+
202
+ OPEN_FILE(flac, int16_t, s16)
203
+ OPEN_FILE(flac, int32_t, s32)
204
+ OPEN_FILE(flac, float, f32)
205
+
206
+ OPEN_MEMORY(wav, int16_t, s16)
207
+ OPEN_MEMORY(wav, int32_t, s32)
208
+ OPEN_MEMORY(wav, float, f32)
209
+
210
+ OPEN_MP3_MEMORY(int16_t, s16)
211
+ OPEN_MP3_MEMORY(float, f32)
212
+
213
+ OPEN_MEMORY(flac, int16_t, s16)
214
+ OPEN_MEMORY(flac, int32_t, s32)
215
+ OPEN_MEMORY(flac, float, f32)
216
+
217
+ void Init_miniaudio(void) {
218
+ VALUE mod = rb_const_get(rb_cObject, rb_intern("Miniaudio"));
219
+ rb_define_module_function(mod, "wav_read_file_s16", &rb_drwav_open_file_and_read_pcm_frames_s16, 1);
220
+ rb_define_module_function(mod, "wav_read_file_s32", &rb_drwav_open_file_and_read_pcm_frames_s32, 1);
221
+ rb_define_module_function(mod, "wav_read_file_f32", &rb_drwav_open_file_and_read_pcm_frames_f32, 1);
222
+ rb_define_module_function(mod, "mp3_read_file_s16", rb_drmp3_open_file_and_read_pcm_frames_s16, 1);
223
+ rb_define_module_function(mod, "mp3_read_file_f32", rb_drmp3_open_file_and_read_pcm_frames_f32, 1);
224
+ rb_define_module_function(mod, "flac_read_file_s16", &rb_drflac_open_file_and_read_pcm_frames_s16, 1);
225
+ rb_define_module_function(mod, "flac_read_file_s32", &rb_drflac_open_file_and_read_pcm_frames_s32, 1);
226
+ rb_define_module_function(mod, "flac_read_file_f32", &rb_drflac_open_file_and_read_pcm_frames_f32, 1);
227
+ rb_define_module_function(mod, "vorbis_read_file_s16", &rb_stb_vorbis_decode_filename_s16, 1);
228
+ rb_define_module_function(mod, "wav_read_s16", &rb_drwav_open_memory_and_read_pcm_frames_s16, 1);
229
+ rb_define_module_function(mod, "wav_read_s32", &rb_drwav_open_memory_and_read_pcm_frames_s32, 1);
230
+ rb_define_module_function(mod, "wav_read_f32", &rb_drwav_open_memory_and_read_pcm_frames_f32, 1);
231
+ rb_define_module_function(mod, "mp3_read_s16", rb_drmp3_open_memory_and_read_pcm_frames_s16, 1);
232
+ rb_define_module_function(mod, "mp3_read_f32", rb_drmp3_open_memory_and_read_pcm_frames_f32, 1);
233
+ rb_define_module_function(mod, "flac_read_s16", &rb_drflac_open_memory_and_read_pcm_frames_s16, 1);
234
+ rb_define_module_function(mod, "flac_read_s32", &rb_drflac_open_memory_and_read_pcm_frames_s32, 1);
235
+ rb_define_module_function(mod, "flac_read_f32", &rb_drflac_open_memory_and_read_pcm_frames_f32, 1);
236
+ rb_define_module_function(mod, "vorbis_file_s16", &rb_stb_vorbis_decode_memory_s16, 1);
237
+ }
@@ -0,0 +1,6 @@
1
+ module Miniaudio
2
+ VERSION = '0.1.0'
3
+ end
4
+
5
+ require 'miniaudio/miniaudio'
6
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: miniaudio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Razuvaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Miniaudio library (https://github.com/mackron/miniaudio) Ruby wrapper
14
+ email: team@orlando-labs.com
15
+ executables: []
16
+ extensions:
17
+ - ext/miniaudio/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ext/miniaudio/extconf.rb
21
+ - ext/miniaudio/miniaudio/extras/stb_vorbis.c
22
+ - ext/miniaudio/miniaudio/miniaudio.h
23
+ - ext/miniaudio/rb-miniaudio.c
24
+ - lib/miniaudio.rb
25
+ homepage: https://github.com/orlando-labs/rb-miniaudio
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.5.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.1.4
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Miniaudio library Ruby wrapper
48
+ test_files: []