Skip to content
Browse files

new sharp/flat syntax and nice syntax reference

  • Loading branch information...
1 parent a420000 commit 271e0d9f0ec2a2eefa1f7410b37f4deb70705795 @txus committed Aug 19, 2011
Showing with 192 additions and 11 deletions.
  1. +90 −5 Readme.md
  2. +2 −2 examples/my_song.rb
  3. +2 −2 lib/core_ext/array.rb
  4. +2 −2 lib/core_ext/fixnum.rb
  5. +96 −0 lib/niki.rb
View
95 Readme.md
@@ -47,8 +47,93 @@ Now you should install the gem and run the example!
$ gem install niki
$ ruby my_song.rb
-# TODO
-
-* Tests + refactor
-* Documentation
-* Expand this README
+## Syntax reference
+
+A Niki Song is represented this way:
+
+ song = Niki::Song.new do
+
+ # +instrument+ lets you configure different instruments in different
+ # MIDI channels.
+ instrument :piano do
+ channel 3
+ end
+
+ instrument :drums do
+ channel 10
+
+ # You can define macros for notes:
+ note[:kick] = c1 # Define a macro for the c1 note, name it :kick
+ note[:snare] = d1 # And the snare will be d1
+ note[:hh] = e1 # And the hh will be e1
+ end
+
+ # Define your song parts like this:
+ part :intro do
+ # Notes are defined like this:
+ # +instrument+ +note or [array of notes]+, +duration+, +options+
+ #
+ # +instrument+
+ # Instrument must be one of those you defined above.
+ #
+ # +note or [array of notes]+
+ # Has to be a musical note like c3 or f2 or a chord like a3MAJ. It
+ # can also be an array of notes. Examples of valid musical notes:
+ #
+ # c2 - a C in the 2nd octave
+ # f4 - an F in the 4th octave
+ # +f4 - an F sharp in the 4th octave
+ # -d3 - a D flat in the 3rd octave
+ # f3MAJ - a F major chord in the 3rd octave
+ # -d3MIN - a D flat minor chord in the 3rd octave
+ # [c3, -e3, g3, -b3] - a C min 7th chord in the 3rd octave
+ # :kick - whatever note you defined in your instrument
+ #
+ # +duration+
+ # Duration can be expressed as a number representing a fraction.
+ # For example, a quarter note (4 of them fit in a single
+ # measure) is expressed with the number 4. An eighth note (8 of them
+ # fit in a single measure) is expressed with 8, and so on.
+ #
+ # +options+
+ # Options is an optional hash where you can specify the following
+ # per-note settings:
+ #
+ # :velocity => (a number between 0 and 127)
+ # :base => (a base note to add to some chord for example)
+ #
+ drums [:kick, :hh], 8
+ drums :hh, 8
+ drums :hh, 8
+
+ piano f2MAJ, 4
+ piano f2MAJ, 4
+ piano f2MAJ, 4
+ piano f2MAJ, 4
+ piano a2MIN, 4, :base => f2
+ piano silence, 4
+ piano a2MIN, 4 :base => f2
+ piano a2MIN, 8
+ piano a2MIN, 8
+ end
+
+ # You can repeat any part!
+ repeat :intro
+
+ part :chorus do
+ drums :from => :intro # Use the drums from the intro
+
+ piano c2MAJ, 4
+ piano c2MAJ, 4
+ piano c2MAJ, 4
+ piano c2MAJ, 4
+ piano g2MAJ, 4
+ piano silence, 4
+ piano g2MAJ, 4
+ piano g2MAJ, 8
+ piano g2MAJ, 8
+ end
+ end
+
+ # Play the song!!!
+ song.play
View
4 examples/my_song.rb
@@ -19,9 +19,9 @@
channel 10
note[:kick] = c1
- note[:snare] = c1.sharp
+ note[:snare] = +c1
note[:hh] = g1
- note[:ohh] = g1.sharp
+ note[:ohh] = +g1
end
part :intro do
View
4 lib/core_ext/array.rb
@@ -1,11 +1,11 @@
class Array
- def sharp
+ def +@
map do |element|
element + 1
end
end
- def flat
+ def -@
map do |element|
element - 1
end
View
4 lib/core_ext/fixnum.rb
@@ -1,8 +1,8 @@
class Fixnum
- def sharp
+ def +@
self + 1
end
- def flat
+ def -@
self - 1
end
end
View
96 lib/niki.rb
@@ -5,5 +5,101 @@
require 'niki/song'
require 'niki/instrument'
+# = Niki
+#
+# Niki is a Ruby DSL to describe and play musical pieces.
+#
+# == Syntax
+#
+# A Niki Song is represented this way:
+#
+# song = Niki::Song.new do
+#
+# # +instrument+ lets you configure different instruments in different
+# # MIDI channels.
+# instrument :piano do
+# channel 3
+# end
+#
+# instrument :drums do
+# channel 10
+#
+# # You can define macros for notes:
+# note[:kick] = c1 # Define a macro for the c1 note, name it :kick
+# note[:snare] = d1 # And the snare will be d1
+# note[:hh] = e1 # And the hh will be e1
+# end
+#
+# # Define your song parts like this:
+# part :intro do
+# # Notes are defined like this:
+# # +instrument+ +note or [array of notes]+, +duration+, +options+
+# #
+# # +instrument+
+# # Instrument must be one of those you defined above.
+# #
+# # +note or [array of notes]+
+# # Has to be a musical note like c3 or f2 or a chord like a3MAJ. It
+# # can also be an array of notes. Examples of valid musical notes:
+# #
+# # c2 - a C in the 2nd octave
+# # f4 - an F in the 4th octave
+# # +f4 - an F sharp in the 4th octave
+# # -d3 - a D flat in the 3rd octave
+# # f3MAJ - a F major chord in the 3rd octave
+# # -d3MIN - a D flat minor chord in the 3rd octave
+# # [c3, -e3, g3, -b3] - a C min 7th chord in the 3rd octave
+# # :kick - whatever note you defined in your instrument
+# # silence - a silent note
+# #
+# # +duration+
+# # Duration can be expressed as a number representing a fraction.
+# # For example, a quarter note (4 of them fit in a single
+# # measure) is expressed with the number 4. An eighth note (8 of them
+# # fit in a single measure) is expressed with 8, and so on.
+# #
+# # +options+
+# # Options is an optional hash where you can specify the following
+# # per-note settings:
+# #
+# # :velocity => (a number between 0 and 127)
+# # :base => (a base note to add to some chord for example)
+# #
+# drums [:kick, :hh], 8
+# drums :hh, 8
+# drums :hh, 8
+#
+# piano f2MAJ, 4
+# piano f2MAJ, 4
+# piano f2MAJ, 4
+# piano f2MAJ, 4
+# piano a2MIN, 4, :base => f2
+# piano silence, 4
+# piano a2MIN, 4 :base => f2
+# piano a2MIN, 8
+# piano a2MIN, 8
+# end
+#
+# # You can repeat any part!
+# repeat :intro
+#
+# part :chorus do
+# drums :from => :intro # Use the drums from the intro
+#
+# piano c2MAJ, 4
+# piano c2MAJ, 4
+# piano c2MAJ, 4
+# piano c2MAJ, 4
+# piano g2MAJ, 4
+# piano silence, 4
+# piano g2MAJ, 4
+# piano g2MAJ, 8
+# piano g2MAJ, 8
+# end
+# end
+#
+# # Play the song!!!
+# song.play
+#
module Niki
end

0 comments on commit 271e0d9

Please sign in to comment.
Something went wrong with that request. Please try again.