Class: Win32::Sound
- Inherits:
-
Object
- Object
- Win32::Sound
- Defined in:
- lib/win32/wave_out_play_freq.rb
Class Method Summary collapse
-
.play_freq(frequency = 440, duration = 1000, volume = 1, pause_execution = false) ⇒ Object
Plays a frequency for a specified duration at a given volume.
Class Method Details
.play_freq(frequency = 440, duration = 1000, volume = 1, pause_execution = false) ⇒ Object
Plays a frequency for a specified duration at a given volume. Defaults are 440Hz, 1 second, full volume. Result is a single channel, 44100Hz sampled, 16 bit sine wave. If multiple instances are plays in simultaneous threads, they will be started and played at the same time
ex.: threads = []
[440, 660].each do |freq|
threads << Thread.new { Win32::Sound.play_freq(freq) }
end
threads.each { |th| th.join }
the first frequency in this array (440) will wait until the thread for 660 finished calculating its PCM array and they will both start streaming at the same time.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/win32/wave_out_play_freq.rb', line 24 def self.play_freq(frequency = 440, duration = 1000, volume = 1, pause_execution = false) if frequency > HIGH_FREQUENCY || frequency < LOW_FREQUENCY raise ArgumentError, 'invalid frequency' end if duration < 0 || duration > 5000 raise ArgumentError, 'invalid duration' end stream(pause_execution) { |wfx| data = generate_pcm_integer_array_for_freq(frequency, duration, volume) data_buffer = FFI::MemoryPointer.new(:int, data.size) data_buffer.write_array_of_int data buffer_length = wfx[:nAvgBytesPerSec]*duration/1000 WAVEHDR.new(data_buffer, buffer_length) } end |