Skip to content

Instantly share code, notes, and snippets.

@RPGP1
Created April 23, 2014 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RPGP1/11207954 to your computer and use it in GitHub Desktop.
Save RPGP1/11207954 to your computer and use it in GitHub Desktop.
安い電子ピアノの音を作った。ProcとFiberで周波数を返すことで、倍音の合成を楽にした。
require 'dxruby'
piano = [0.419414141440249,
0.166971777138902,
0.235853904101082,
0.0470590406691173,
0.0373803247025181,
0.00373803247025181,
0.0166971777138902,
0.0419414141440249,
0.0059243822153007,
0.00419414141440249,
0.0013263039698352,
0.00470590406691173,
0.00235853904101082,
0.00296922473343717,
0.000528011120724962,
0.0026463243292058,
0.000938951304256777,
0.000419414141440249,
0.00118206965736613,
0.000664726617590573,
0.000528011120724962,
0.000333152494588633,
0.000333152494588633,
0.000373803247025182,
0.000296922473343716,
0.000333152494588633,
0.000264632432920579,
0.000235853904101082,
0.000187345415297443,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067,
0.00001053520691067]
height = [261.6,293.7,329.6,349.2,392.0,440.0,493.9,523.3]
p = Proc.new do
last = 0
8.times{|t|
1000.times{Fiber.yield(last = height[t])}
}
loop{Fiber.yield(last)}
end
vol = 64
s = nil
piano.each_with_index do |v,i|
f = Fiber.new(&p)
if i == 0
s = SoundEffect.new(8000, WAVE_SIN){
[f.resume * (i + 1), vol * v]
}
else
s.add(WAVE_SIN){
[f.resume * (i + 1), vol * v]
}
end
end
s.play
Window.loop{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment