Upgrade to Pro — share decks privately, control downloads, hide ads and more …

omotesandorb_8.pdf

ta1kt0me
February 04, 2016

 omotesandorb_8.pdf

MacでRubyで音を作る。サンプルコードは、https://github.com/nagachika/ruby-coreaudio より拝借させていただきました。
その他参考サイト http://www.joelstrait.com

ta1kt0me

February 04, 2016
Tweet

More Decks by ta1kt0me

Other Decks in Programming

Transcript

  1. αϯϓϧ ˌruby-coreaudio/examples/outbuffer_sine.rb require "coreaudio" dev = CoreAudio.default_output_device buf = dev.output_buffer(1024)

    phase = Math::PI * 2.0 * 440.0 / dev.nominal_rate th = Thread.start do i = 0 wav = NArray.sint(1024) loop do 1024.times {|j| wav[j] = (0.4 * Math.sin(phase*(i+j)) * 0x7FFF).round } i += 1024 buf << wav end end buf.start sleep 2 buf.stop puts "#{buf.dropped_frame} frame dropped." th.kill.join
  2. Θ͔Βͳ͍ • 440.0 ❓ • Math::PI * 2 ❓ •

    0x7FFF ❓ • 0.4 ❓ • Math.sin ❓
  3. ͱΓ͋͑ͣ • प೾਺ • Իͷߴ͕ܾ͞·Δ • ج४͸440Hz ϥ • Ϗοτਂ౓

    • Իͷେ͖͕ܾ͞·Δ • αϯϓϦϯάϨʔτͱ࠶ੜ࣌ؒ • ੜ੒͢Δ਺஋ͷ਺͕ܾ·Δ • αΠϯ೾ • ౳଎ԁӡಈͰදݱ
  4. αΠϯ೾ • ౳଎ԁӡಈ STANDARD_PITCH = 440.0 BIT_DEPTH = 0x05A0 PLAY_TIME

    = 2 tone = 3 waves = [] dev = CoreAudio.default_output_device phase = STANDARD_PITCH * 2 ** (tone/12.0) / dev.nominal_rate (0...PLAY_TIME * dev.nominal_rate).inject(0.0) do |position, i| waves << Math.sin(position * 2 * Math::PI) * BIT_DEPTH phase * i - (phase * i).floor end
  5. ϊίΪϦ೾ • ΪβΪβͳ೾ STANDARD_PITCH = 440.0 BIT_DEPTH = 0x05A0 PLAY_TIME

    = 2 tone = 3 waves = [] dev = CoreAudio.default_output_device phase = STANDARD_PITCH * 2 ** (tone/12.0) / dev.nominal_rate (0...PLAY_TIME * dev.nominal_rate).inject(0.0) do |position, i| waves << ((position * 2.0) - 1.0) * BIT_DEPTH phase * i - (phase * i).floor end
  6. ࡾ֯೾ STANDARD_PITCH = 440.0 BIT_DEPTH = 0x05A0 PLAY_TIME = 2

    tone = 3 waves = [] dev = CoreAudio.default_output_device phase = STANDARD_PITCH * 2 ** (tone/12.0) / dev.nominal_rate (0...PLAY_TIME * dev.nominal_rate).inject(0.0) do |position, i| waves << BIT_DEPTH - (((position * 2.0) - 1.0) * BIT_DEPTH * 2.0).abs phase * i - (phase * i).floor end
  7. ۣܗ೾ • ࠷େ͔࠷খ͔ STANDARD_PITCH = 440.0 BIT_DEPTH = 0x05A0 PLAY_TIME

    = 2 tone = 3 waves = [] dev = CoreAudio.default_output_device phase = STANDARD_PITCH * 2 ** (tone/12.0) / dev.nominal_rate (0...PLAY_TIME * dev.nominal_rate).inject(0.0) do |position, i| waves << ((position >= 0.5) ? BIT_DEPTH : -BIT_DEPTH) phase * i - (phase * i).floor end