Skip to content

Instantly share code, notes, and snippets.

@KC-Liu
Last active August 29, 2015 14:00
Show Gist options
  • Save KC-Liu/11139685 to your computer and use it in GitHub Desktop.
Save KC-Liu/11139685 to your computer and use it in GitHub Desktop.
###
# electric guitar remix
# version 1.1 ( with organ harmony , heart beat)
#
# electric_guitar_remix.coffee
# Source: https://gist.github.com/KC-Liu/11139685
#
# Inspired by "Mom, I got full marks in the exam" by SelfKill
# (https://www.youtube.com/watch?v=jq6bvDQ2FyA)
#
# Coded by Kunjie
# (https://github.com/kc-liu)
###
###
# constants
###
VOLUME = {solo:0.3,bass:0.4,drum:0.6}
BPS = 5
TAU = 2 * Math.PI
na = null
###
# notes
###
SOLO = [0, 2, 4, 0, 7, 4, 0, 9, na, 11, 9, 7, 4, 5, 7, na,
0, 2, 4, 0, 7, 4, 0, 11, na, 11, na, na, 12, na, 11, na,
5, 7, 9, 5,12, 9, 5, 16, na, 16, 14, 9, 12, na, 11, na,
5, 7, 9, 5,12, 9, 5, 16, na, 16, na, na, 17, na, 16, na ];
BASS = [12,11,11,12,
11, 9, 9, 7,
12,11,11,12,
14,16,14,12 ];
###
# sound effect
###
harmony = (tt,freq) ->
Math.sin( TAU * tt * freq ) * Math.sin( tt % 6 * TAU)
pluck = (tt,freq,duration,steps) ->
_sum = 0
_scalar = Math.max( 0 , 0.95 - (tt * duration) / ((tt * duration) + 1) )
for i in [0...steps] by 1
do(i) ->
_sum += Math.sin( TAU * tt * (freq + i * freq ) )
return _scalar * _sum / 16
drums = (tt) ->
_s = tt * 0.75 % 5 +1
_f = Math.sin( 2 * TAU / (16 * _s % 2 + 0.5) * 1.2 )
Math.sin( TAU * _s * _f)
###
# music
###
return (t) ->
note = (tt,song) ->
_f = song[ Math.floor( tt % song.length ) ]
if _f?
(2 ** (_f / 12) ) * 240
else
0
_m = note( t * BPS , SOLO )
_b = note( 0.25 *t * BPS , BASS )
pluck( t % (1 / BPS) , _m , 10 , 50 )* VOLUME.solo + drums( 0.25*t % (1 / BPS )) * VOLUME.drum + harmony( t , _b ) * VOLUME.bass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment