Class: Ray::Sound

Inherits:
Object
  • Object
show all
Extended by:
ResourceSet
Defined in:
ext/audio.c,
lib/ray/audio.rb,
ext/audio.c

Overview

Class used to represent short sounds. You can play one sound per channel.

Instance Method Summary collapse

Methods included from ResourceSet

[], add_set, missing_pattern, need_argument_count, reject!, required_argument_count, select!, set_hash

Constructor Details

#initialize(arg) ⇒ Object

Creates a new sound from an IO object or a filename.



214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/audio.c', line 214

VALUE ray_init_sound(VALUE self, VALUE arg) {
   if (rb_respond_to(arg, RAY_METH("to_str")))
      ray_init_sound_with_filename(self, rb_String(arg));
   else if (rb_respond_to(arg, RAY_METH("read")))
      ray_init_sound_with_io(self, arg);
   else {
      rb_raise(rb_eTypeError, "Can't convert %s into String",
               RAY_OBJ_CLASSNAME(arg));
   }

   return Qnil;
}

Instance Method Details

#fade(duration, channel = 0, times = 1) ⇒ Object

Same as play, but fades for a given number of a seconds.

Parameters:

  • duration (Float)

    Duration of the fade, in seconds.

See Also:



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/audio.c', line 258

VALUE ray_sound_fade(int argc, VALUE *argv, VALUE self) {
   VALUE duration, channel, times;
   rb_scan_args(argc, argv, "12", &duration, &channel, &times);

   if (NIL_P(channel)) channel = INT2FIX(0);
   if (NIL_P(times))   times   = INT2FIX(1);

   int c_channel = NUM2INT(channel), c_times = 0;
   if (times == RAY_SYM("forever"))
      c_times = 0;
   else
      c_times = NUM2INT(times);

   Mix_Chunk *chunk = ray_rb2chunk(self);
   Mix_FadeInChannel(c_channel, chunk, c_times - 1,
                     (int)(NUM2DBL(duration) * 1000));

   return self;
}

#play(channel = 0, times = 1) ⇒ Object

Plays a sound on the given channel a given number of times.

Parameters:

  • times (Integer, :forever) (defaults to: 1)

    How many times the sound should be played. Can also be :forever or 0 to play it forever.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/audio.c', line 233

VALUE ray_sound_play(int argc, VALUE *argv, VALUE self) {
   VALUE channel, times;
   rb_scan_args(argc, argv, "02", &channel, &times);

   if (NIL_P(channel)) channel = INT2FIX(0);
   if (NIL_P(times))   times   = INT2FIX(1);

   int c_channel = NUM2INT(channel), c_times = 0;
   if (times == RAY_SYM("forever"))
      c_times = 0;
   else
      c_times = NUM2INT(times);

   Mix_Chunk *chunk = ray_rb2chunk(self);
   Mix_PlayChannel(c_channel, chunk, c_times - 1);

   return self;
}

#volumeFloat

Returns Volume of the sound, between 0 and 100.

Returns:

  • (Float)

    Volume of the sound, between 0 and 100.



279
280
281
282
283
284
# File 'ext/audio.c', line 279

VALUE ray_sound_volume(VALUE self) {
   Mix_Chunk *chunk = ray_rb2chunk(self);
   int volume = Mix_VolumeChunk(chunk, -1);

   return rb_float_new((volume / 128.0f) * 100.0f);
}

#volume=(value) ⇒ Object

Sets the volume of the sound.



287
288
289
290
291
292
293
# File 'ext/audio.c', line 287

VALUE ray_sound_set_volume(VALUE self, VALUE value) {
   Mix_Chunk *chunk = ray_rb2chunk(self);
   float volume = (float)NUM2DBL(value);
   Mix_VolumeChunk(chunk, (int)((volume / 100.0f) * 128.0f));

   return value;
}