Last active
March 26, 2018 01:05
-
-
Save ta1kt0me/cfaf111bca65298201a51d580d470aa2 to your computer and use it in GitHub Desktop.
猫踏んじゃった
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "coreaudio" | |
| DEV = CoreAudio.default_output_device | |
| buf = DEV.output_buffer(1024) | |
| BIT_DEPTH = 0x05A0 | |
| WAVE_AMPLITUDE = 2.0 | |
| SAMPLING_RATE = DEV.nominal_rate | |
| def pitch(scale) | |
| 440 * (2**(scale / 12.0)) | |
| end | |
| # default is A | |
| def phase(scale = 0) | |
| Math::PI * WAVE_AMPLITUDE * pitch(scale) / SAMPLING_RATE | |
| end | |
| (3..30).zip( | |
| %i[ | |
| do | |
| do_sharp | |
| re | |
| re_sharp | |
| mi | |
| fa | |
| fa_sharp | |
| so | |
| so_sharp | |
| ra | |
| ra_sharp | |
| si | |
| octave_do | |
| octave_do_sharp | |
| octave_re | |
| octave_re_sharp | |
| octave_mi | |
| octave_fa | |
| octave_fa_sharp | |
| octave_so | |
| octave_so_sharp | |
| octave_ra | |
| octave_ra_sharp | |
| octave_si | |
| double_octave_do | |
| double_octave_do_sharp | |
| double_octave_re | |
| double_octave_re_sharp | |
| ] | |
| ).to_h.invert.each do |k, v| | |
| define_method "#{k}_phase".to_sym do | |
| phase(v) | |
| end | |
| end | |
| th = Thread.start do | |
| score = [ | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[octave_ra_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[octave_ra_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[octave_ra_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[double_octave_do_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[double_octave_re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[double_octave_re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[double_octave_re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[double_octave_re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[double_octave_do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_ra_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[do_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[do_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[fa_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[so_phase], 1], | |
| [[so_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[so_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[so_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[so_sharp_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[so_sharp_phase], 1], | |
| [[ra_phase], 1], | |
| [[ra_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[octave_re_sharp_phase], 0.5], | |
| [[octave_do_sharp_phase], 0.5], | |
| [[do_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[re_sharp_phase], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 2], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 1], | |
| [[octave_do_sharp_phase], 0.3], | |
| [[octave_do_phase], 0.3], | |
| [[octave_do_sharp_phase], 0.4], | |
| [[octave_re_phase], 1], | |
| [[octave_do_sharp_phase], 1], | |
| [[], 1], | |
| [[si_phase, octave_fa_phase], 1], | |
| [[ra_sharp_phase, octave_fa_sharp_phase], 2], | |
| [[fa_sharp_phase], 1] | |
| ] | |
| pos = 0 | |
| score.each do |tone, long| | |
| (pos..long * SAMPLING_RATE / 4 + pos) | |
| .step(1) | |
| .map { |i| tone.map { |t| Math.sin(t * i) }.sum } | |
| .map { |v| v * BIT_DEPTH } | |
| .yield_self { |wave| buf << wave } | |
| pos = long * SAMPLING_RATE + pos | |
| end | |
| end | |
| buf.start | |
| sleep 10 * 5 # Sloppy | |
| buf.stop | |
| puts "#{buf.dropped_frame} frame dropped." | |
| th.kill.join |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source "https://rubygems.org" | |
| # gem "rails" | |
| gem 'coreaudio' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment