8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DiverseAdvent Calendar 2018

Day 1

始まるよ、Diverse Advent Calendar!!

Last updated at Posted at 2018-11-30

MacOSを利用しているあなたは、何も考えずに以下のコマンドを実行してほしい。1

gem install osxdsp
brew install sox

次に以下のプログラムを作って欲しい。

sound.rb
BaseRate = 4000
Freq = 440.0

C=3
D=5
E=7
F=8
G=10
A=12
B=14

def play(sounds, rate)
  data = []

  sounds.each do |scale|
    (0..1).step(1.0 / rate) do |t|
      value = Math.sin(2 * Math::PI * Freq * 2**(scale / 12.0) * t)
      data << 128 + (value * 60).round
    end
  end

  open("/dev/dsp","wb") do |io|
    io.puts(data.pack("C*"))
  end
end

play [B,G,A], BaseRate
play [D+12], BaseRate*2
sleep 1
play [D,A,B], BaseRate
play [G+12], BaseRate*2

オーディオはONにして、以下のコマンドを実行してみよう。2

ruby -rosxdsp sound.rb

さぁ、どうだろう?
始まりを感じることが出来たかな???

  1. Linuxの人は何もしなくても大丈夫なはず、Windowsの人はよくわかりません!

  2. Linuxの人は-rosxdspの引数が不要です。

8
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?