著作一覧 |
いたずらラッコのバンデッタ
ううむ、確かに疑う余地が入る余地もなく、スクリプティング言語だけど、おれもト止めをさした覚えがあるなあ。
多分、JavaScriptという言語名かscript要素につられたのかも知れない。
require 'rjb' module Java File = Rjb::import('java.io.File') end module Javax #interfaces Clip = Rjb::import('javax.sound.sampled.Clip') DataLine = Rjb::import('javax.sound.sampled.DataLine') Line = Rjb::import('javax.sound.sampled.Line') LineListner = Rjb::import('javax.sound.sampled.LineListener') Mixer = Rjb::import('javax.sound.sampled.Mixer') Port = Rjb::import('javax.sound.sampled.Port') SourceDataLine = Rjb::import('javax.sound.sampled.SourceDataLine') TargetDataLine = Rjb::import('javax.sound.sampled.TargetDataLine') #classes AudioFormat = Rjb::import('javax.sound.sampled.AudioFormat') AudioInputStream = Rjb::import('javax.sound.sampled.AudioInputStream') AudioSystem = Rjb::import('javax.sound.sampled.AudioSystem') DataLineInfo = Rjb::import('javax.sound.sampled.DataLine$Info') end module SoundSampled class SoundFile include Javax def initialize(file = nil) file = Java::File.new('/windows/media/ding.wav') unless file @ais = AudioSystem.getAudioInputStream(file) @clip = AudioSystem.line(DataLineInfo.new(Clip, @ais.format)) end def play(count = 0) @clip.open(@ais) @clip.loop(count) while @clip.running? sleep 0.1 end sleep 3 @clip.close end end class Sin include Javax FREQ = 1000 TERM = 5 RATE = 8000 DR = 2 * Math::PI * FREQ / RATE def initialize @format = AudioFormat.new(RATE, 8, 1, true, true) @line = AudioSystem.line(DataLineInfo.new(SourceDataLine, @format)) end def generate @wave = Array.new(RATE) do |i| (Math.sin(DR * i) * 100).to_i end p @wave if $DEBUG end def play @line.open(@format) @line.start off = 0 len = @wave.size while len > 0 written = @line.write(@wave, off, len) off += written len -= written end @line.drain @line.close end end end if __FILE__ == $0 # SoundSampled::SoundFile.new.play s = SoundSampled::Sin.new s.generate s.play end
ジェズイットを見習え |