native_audio 0.5.0 → 0.5.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/ext/audio/audio.c +40 -1
  3. data/lib/native_audio.rb +33 -0
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9a953a3fe6e4bd23e99278c28dbd273bc08268105382a4dae34854e3917aa64
4
- data.tar.gz: af2a5c84a91fa2957984d4c36637bbe914f38fab7d26903406051eb2456dbc96
3
+ metadata.gz: e0dd09de66ecf033b152768c52c807b025e05f1a7a3cbbc5b0b15bab85746a42
4
+ data.tar.gz: 4af9bdb8da0bcb06d75335a69d56f5a513206ce06f5a849c7837f5bf713aa2cf
5
5
  SHA512:
6
- metadata.gz: '094beb2919f1a5e0b9e18ea7ac5e5776f5f5ed5eb213f9aac5c976ec3e74cb740ba73632f3bab2acb589afc4d2f12a1fe8485b3ddd59b5bc71d8d21fa758f357'
7
- data.tar.gz: b0c335b765410df6cfbae2a730f921388e5761639f96b329113ef398527dd46f9067680a55e0fb7541cf801784158f07b744f07e9570251ea0485503b573c81f
6
+ metadata.gz: 84abd6f60b5f33320866d5ad74aac58c604236322fe9d62c1894502ca531b203148631588dcd2d41afc63e4da08aa75805145eb951ce839dcc1e5eeaa8f18577
7
+ data.tar.gz: f37a3d01e2fba67d3b34c9b830ff53907b02a9a64d47df968c97365866d8b58a1870baa672cf0c553739bb58120646b16b68ecadec5224056c1d3384a90c4415
data/ext/audio/audio.c CHANGED
@@ -180,6 +180,29 @@ VALUE audio_duration(VALUE self, VALUE clip)
180
180
  // Playback Controls
181
181
  // ============================================================================
182
182
 
183
+ static void cleanup_finished_channels(void)
184
+ {
185
+ for (int i = 0; i < MAX_CHANNELS; i++) {
186
+ if (channels[i] != NULL && ma_sound_at_end(channels[i])) {
187
+ ma_sound_uninit(channels[i]);
188
+ free(channels[i]);
189
+ channels[i] = NULL;
190
+
191
+ if (delay_nodes[i] != NULL) {
192
+ multi_tap_delay_uninit(delay_nodes[i]);
193
+ free(delay_nodes[i]);
194
+ delay_nodes[i] = NULL;
195
+ }
196
+
197
+ if (reverb_nodes[i] != NULL) {
198
+ reverb_uninit(reverb_nodes[i]);
199
+ free(reverb_nodes[i]);
200
+ reverb_nodes[i] = NULL;
201
+ }
202
+ }
203
+ }
204
+ }
205
+
183
206
  VALUE audio_play(VALUE self, VALUE channel_id, VALUE clip)
184
207
  {
185
208
  int channel = NUM2INT(channel_id);
@@ -195,6 +218,8 @@ VALUE audio_play(VALUE self, VALUE channel_id, VALUE clip)
195
218
  return Qnil;
196
219
  }
197
220
 
221
+ cleanup_finished_channels();
222
+
198
223
  // Clean up existing resources on this channel
199
224
  if (channels[channel] != NULL) {
200
225
  ma_sound_stop(channels[channel]);
@@ -295,7 +320,21 @@ VALUE audio_stop(VALUE self, VALUE channel_id)
295
320
  }
296
321
 
297
322
  ma_sound_stop(channels[channel]);
298
- ma_sound_seek_to_pcm_frame(channels[channel], 0);
323
+ ma_sound_uninit(channels[channel]);
324
+ free(channels[channel]);
325
+ channels[channel] = NULL;
326
+
327
+ if (delay_nodes[channel] != NULL) {
328
+ multi_tap_delay_uninit(delay_nodes[channel]);
329
+ free(delay_nodes[channel]);
330
+ delay_nodes[channel] = NULL;
331
+ }
332
+
333
+ if (reverb_nodes[channel] != NULL) {
334
+ reverb_uninit(reverb_nodes[channel]);
335
+ free(reverb_nodes[channel]);
336
+ reverb_nodes[channel] = NULL;
337
+ }
299
338
 
300
339
  return Qnil;
301
340
  }
data/lib/native_audio.rb CHANGED
@@ -25,6 +25,7 @@ module NativeAudio
25
25
 
26
26
  class DelayTap
27
27
  attr_reader :id, :audio_source, :time_ms, :volume
28
+ attr_writer :id
28
29
 
29
30
  def initialize(audio_source, id, time_ms, volume)
30
31
  @audio_source = audio_source
@@ -56,11 +57,13 @@ module NativeAudio
56
57
  @clip = clip
57
58
  @channel = AudioSource.channels.count
58
59
  @delay_taps = []
60
+ @params = {}
59
61
  AudioSource.channels << self
60
62
  end
61
63
 
62
64
  def play
63
65
  NativeAudio.audio_driver.play(@channel, @clip.clip)
66
+ apply_params
64
67
  end
65
68
 
66
69
  def stop
@@ -76,18 +79,22 @@ module NativeAudio
76
79
  end
77
80
 
78
81
  def set_pos(angle, distance)
82
+ @params[:pos] = [angle, distance]
79
83
  NativeAudio.audio_driver.set_pos(@channel, angle, distance)
80
84
  end
81
85
 
82
86
  def set_volume(volume)
87
+ @params[:volume] = volume
83
88
  NativeAudio.audio_driver.set_volume(@channel, volume)
84
89
  end
85
90
 
86
91
  def set_pitch(pitch)
92
+ @params[:pitch] = pitch
87
93
  NativeAudio.audio_driver.set_pitch(@channel, pitch)
88
94
  end
89
95
 
90
96
  def set_looping(looping)
97
+ @params[:looping] = looping
91
98
  NativeAudio.audio_driver.set_looping(@channel, looping)
92
99
  end
93
100
 
@@ -99,10 +106,12 @@ module NativeAudio
99
106
  end
100
107
 
101
108
  def enable_reverb(enabled = true)
109
+ @params[:reverb_enabled] = enabled
102
110
  NativeAudio.audio_driver.enable_reverb(@channel, enabled)
103
111
  end
104
112
 
105
113
  def set_reverb(room_size: 0.5, damping: 0.3, wet: 0.3, dry: 1.0)
114
+ @params[:reverb] = { room_size: room_size, damping: damping, wet: wet, dry: dry }
106
115
  NativeAudio.audio_driver.enable_reverb(@channel, true)
107
116
  NativeAudio.audio_driver.set_reverb_room_size(@channel, room_size)
108
117
  NativeAudio.audio_driver.set_reverb_damping(@channel, damping)
@@ -117,5 +126,29 @@ module NativeAudio
117
126
  def self.channels
118
127
  @channels ||= []
119
128
  end
129
+
130
+ private
131
+
132
+ def apply_params
133
+ NativeAudio.audio_driver.set_volume(@channel, @params[:volume]) if @params.key?(:volume)
134
+ NativeAudio.audio_driver.set_pitch(@channel, @params[:pitch]) if @params.key?(:pitch)
135
+ NativeAudio.audio_driver.set_looping(@channel, @params[:looping]) if @params.key?(:looping)
136
+ NativeAudio.audio_driver.set_pos(@channel, *@params[:pos]) if @params.key?(:pos)
137
+
138
+ if @params.key?(:reverb)
139
+ r = @params[:reverb]
140
+ NativeAudio.audio_driver.enable_reverb(@channel, true)
141
+ NativeAudio.audio_driver.set_reverb_room_size(@channel, r[:room_size])
142
+ NativeAudio.audio_driver.set_reverb_damping(@channel, r[:damping])
143
+ NativeAudio.audio_driver.set_reverb_wet(@channel, r[:wet])
144
+ NativeAudio.audio_driver.set_reverb_dry(@channel, r[:dry])
145
+ elsif @params.key?(:reverb_enabled)
146
+ NativeAudio.audio_driver.enable_reverb(@channel, @params[:reverb_enabled])
147
+ end
148
+
149
+ @delay_taps.each do |tap|
150
+ tap.id = NativeAudio.audio_driver.add_delay_tap(@channel, tap.time_ms, tap.volume)
151
+ end
152
+ end
120
153
  end
121
154
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: native_audio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Hatfull