miniaudio 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 306f78eb852bb174b8633fc9ad08aa6326781cd093dbd73273d08c443abc6dc7
4
- data.tar.gz: cc91095d14079ae4a07ab781aadc9b3d1ba1a56fef79d6a9004dc30b9287ce77
3
+ metadata.gz: 46b073fb1490d4f4e235a5ae135b6206aeab32660025b92d6e888f4e07cd95b1
4
+ data.tar.gz: 8810ae96dd14fc4d8ebcd0ae7db2f62e76a213976ff4392e560ba64b90e94e67
5
5
  SHA512:
6
- metadata.gz: 10af5458bab8aaafe256dacbf00d7c1c91933c6d05592e721a81307562e9f888b44e619edd53f83f8cbe66b91866628a14a937a95ca4447ad84cf4037f71085a
7
- data.tar.gz: c44b2f23f95db3c3d9d7e1014b7cbb1d82d4a8d6c376f470aeedefd776a39587cf706cd1c1337df4d7d1b4e518d01dab18cfef812761f9bc462c26abdde288a2
6
+ metadata.gz: a91fb070b3fc260997f6ef33bb5dab393a87025bc385f69176e8ba269a13e9c99de9d192c1177e18c158ea230c08f9375e00c21592c3dade212c229f8538eb8c
7
+ data.tar.gz: 630e68a04d05808e9d0e13b80fd525893da8c14ffe066aed8e2f226579b4568c008f2582cc0845a2296532e8ea0c708119a0b2adb1a36cc6b85a7997bfb25042
@@ -39,6 +39,23 @@ if (ID2SYM(rb_intern(MA_SAMPLE_FORMAT_U8)) == rb) { \
39
39
  rb_raise(rb_eArgError, "Unknown %s sample format: %+"PRIsVALUE". Allowed: :u8, :s16, :s24, :s32, :f32", err, rb); \
40
40
  }
41
41
 
42
+ #define CHECK_MA_G711_CONVERTIBLE_SAMPLE_FORMAT(rb, c, err) \
43
+ ma_format c = ma_format_unknown; \
44
+ if (ID2SYM(rb_intern(MA_SAMPLE_FORMAT_S16)) == rb) { \
45
+ c = ma_format_s16; \
46
+ } else if (ID2SYM(rb_intern(MA_SAMPLE_FORMAT_S32)) == rb) { \
47
+ c = ma_format_s32; \
48
+ } else if (ID2SYM(rb_intern(MA_SAMPLE_FORMAT_F32)) == rb) { \
49
+ c = ma_format_f32; \
50
+ } else { \
51
+ rb_raise(rb_eArgError, "Unknown %s sample format: %+"PRIsVALUE". Allowed: :s16, :s32, :f32", err, rb); \
52
+ }
53
+
54
+ #define CHECK_G711_FORMAT(rb, err) \
55
+ if (ID2SYM(rb_intern(G711_FORMAT_ALAW)) != rb && ID2SYM(rb_intern(G711_FORMAT_MULAW)) != rb) { \
56
+ rb_raise(rb_eArgError, "Unknown %s format: %+"PRIsVALUE". Allowed: :alaw and :mulaw", err, rb); \
57
+ }
58
+
42
59
  static int sample_width(ma_format ma_fmt) {
43
60
  switch (ma_fmt) {
44
61
  case ma_format_u8:
@@ -56,6 +73,26 @@ static int sample_width(ma_format ma_fmt) {
56
73
  return 0;
57
74
  }
58
75
 
76
+ #define SAMPLE_WIDTH_s16 2
77
+ #define SAMPLE_WIDTH_s32 4
78
+ #define SAMPLE_WIDTH_f32 4
79
+
80
+ #define FROM_G711_CONVERSION(FROM, TO) \
81
+ static VALUE rb_g711_conversion_ ## FROM ## _to_ ## TO (VALUE mod, VALUE rb_source) { \
82
+ int mem_len = RSTRING_LEN(rb_source) * SAMPLE_WIDTH_ ## TO; \
83
+ void *converted = malloc(mem_len); \
84
+ \
85
+ if (converted == NULL) { \
86
+ rb_raise(rb_eNoMemError, "Unable to allocate memory for converted frames"); \
87
+ } \
88
+ \
89
+ drwav_ ## FROM ## _to_ ## TO (converted, (void*)StringValuePtr(rb_source), RSTRING_LEN(rb_source)); \
90
+ \
91
+ VALUE rb_output = rb_str_new(converted, mem_len); \
92
+ free(converted); \
93
+ return rb_output; \
94
+ }
95
+
59
96
  static VALUE rb_ma_convert_frames(
60
97
  VALUE mod, VALUE rb_source,
61
98
  VALUE rb_source_format, VALUE rb_source_channels, VALUE rb_source_sample_rate,
@@ -113,6 +150,22 @@ static VALUE rb_ma_convert_frames(
113
150
  return ret;
114
151
  }
115
152
 
153
+ FROM_G711_CONVERSION(alaw, s16)
154
+ FROM_G711_CONVERSION(alaw, s32)
155
+ FROM_G711_CONVERSION(alaw, f32)
156
+
157
+ FROM_G711_CONVERSION(mulaw, s16)
158
+ FROM_G711_CONVERSION(mulaw, s32)
159
+ FROM_G711_CONVERSION(mulaw, f32)
160
+
116
161
  void define_convert_functions_under(VALUE mod) {
117
- rb_define_module_function(mod, "convert_frames", &rb_ma_convert_frames, 7);
162
+ rb_define_module_function(mod, "_convert_frames", &rb_ma_convert_frames, 7);
163
+
164
+ rb_define_module_function(mod, "alaw_to_s16", &rb_g711_conversion_alaw_to_s16, 1);
165
+ rb_define_module_function(mod, "alaw_to_s32", &rb_g711_conversion_alaw_to_s32, 1);
166
+ rb_define_module_function(mod, "alaw_to_f32", &rb_g711_conversion_alaw_to_f32, 1);
167
+
168
+ rb_define_module_function(mod, "mulaw_to_s16", &rb_g711_conversion_mulaw_to_s16, 1);
169
+ rb_define_module_function(mod, "mulaw_to_s32", &rb_g711_conversion_mulaw_to_s32, 1);
170
+ rb_define_module_function(mod, "mulaw_to_f32", &rb_g711_conversion_mulaw_to_f32, 1);
118
171
  }
data/lib/miniaudio.rb CHANGED
@@ -1,8 +1,48 @@
1
1
  module Miniaudio
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.3'
3
3
 
4
- FRAME_FORMATS = %i[u8 s16 s24 s32 f32]
4
+ FRAME_FORMATS = %i[u8 s16 s24 s32 f32].freeze
5
+
6
+ G711_ALGORYTHMS = %i[alaw mulaw].freeze
7
+ G711_ALGOS = G711_ALGORYTHMS
8
+ G711_CONVERTIBLE_FORMATS = %i[s16 s32 f32].freeze
9
+
10
+ G711_SAMPLE_RATE = 8000
11
+
12
+ class << self
13
+ def convert_frames(
14
+ source_string,
15
+ src_frame_format, src_channels_num, src_sample_rate,
16
+ dst_frame_format, dst_channels_num, dst_sample_rate)
17
+
18
+ raise ArgumentError, "Unable to convert to G.711 codec" if G711_ALGOS.include?(dst_frame_format)
19
+
20
+ if G711_ALGOS.include?(src_frame_format)
21
+ target_format = FRAME_FORMATS.include?(dst_frame_format) ? dst_frame_format : :s16
22
+
23
+ pcm = method("#{src_frame_format}_to_#{target_format}").(source_string)
24
+
25
+ if src_sample_rate != dst_sample_rate || src_channels_num != dst_channels_num || dst_frame_format != target_format
26
+ converted, bytes_used = _convert_frames(
27
+ pcm,
28
+ target_format, src_channels_num, G711_SAMPLE_RATE,
29
+ dst_frame_format, dst_channels_num, dst_sample_rate
30
+ )
31
+
32
+ [converted, bytes_used * source_string.length / pcm.length]
33
+ else
34
+ [pcm, source_string.length]
35
+ end
36
+ else
37
+ _convert_frames(
38
+ source_string,
39
+ src_frame_format, src_channels_num, src_sample_rate,
40
+ dst_frame_format, dst_channels_num, dst_sample_rate
41
+ )
42
+ end
43
+ end
44
+ end
5
45
  end
6
46
 
7
- require 'miniaudio/miniaudio'
47
+ require_relative 'miniaudio/miniaudio'
8
48
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miniaudio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Razuvaev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-29 00:00:00.000000000 Z
11
+ date: 2021-11-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Miniaudio library (https://github.com/mackron/miniaudio) Ruby wrapper
14
14
  email: team@orlando-labs.com
@@ -28,7 +28,7 @@ homepage: https://github.com/orlando-labs/rb-miniaudio
28
28
  licenses:
29
29
  - MIT
30
30
  metadata: {}
31
- post_install_message:
31
+ post_install_message:
32
32
  rdoc_options: []
33
33
  require_paths:
34
34
  - lib
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  requirements: []
46
46
  rubygems_version: 3.2.3
47
- signing_key:
47
+ signing_key:
48
48
  specification_version: 4
49
49
  summary: Miniaudio library Ruby wrapper
50
50
  test_files: []