Browse files

a couple more examples

  • Loading branch information...
1 parent 56fcb83 commit 87f8639bb1e73c19f8fd816252e5f0ad800096a3 @adamjmurray committed Jul 8, 2011
Showing with 61 additions and 0 deletions.
  1. +2 −0 .yardopts
  2. +20 −0 examples/crescendo.rb
  3. +39 −0 examples/dynamic_pattern.rb
View
2 .yardopts
@@ -3,5 +3,7 @@
--charset utf-8
--protected
lib/**/*.rb
+- *.md
+- examples/*
- lib/**/*.citrus
View
20 examples/crescendo.rb
@@ -0,0 +1,20 @@
+# Generate a MIDI file of the C-major scale with a crescendo (it increases in intensity)
+#
+# NOTE: this blindly overwrites any existing MTK-crescendo.mid file, unless an argument is provided
+
+require 'mtk'
+require 'mtk/midi/file'
+include MTK
+include Pitches
+include Intensities
+
+file = ARGV[0] || 'MTK-crescendo.mid'
+
+scale = Pattern::PitchSequence C4,D4,E4,F4,G4,A4,B4,C5
+crescendo = Pattern::IntensityLines pp, [fff, scale.length-1] # step from pp to fff over the length of the scale
+
+sequencer = Sequencer::StepSequencer.new [scale, crescendo]
+timeline = sequencer.to_timeline
+
+MIDI_File(file).write timeline
+
View
39 examples/dynamic_pattern.rb
@@ -0,0 +1,39 @@
+# Generate a MIDI file using a lambda to dynamically generate the pitches
+#
+# NOTE: this blindly overwrites any existing MTK-dynamic_pattern.mid file, unless an argument is provided
+
+require 'mtk'
+require 'mtk/midi/file'
+include MTK
+include Pitches
+include Intensities
+include Intervals
+
+file = ARGV[0] || "MTK-#{File.basename(__FILE__,'.rb')}.mid"
+
+interval_generator = lambda do
+ # Randomly return intervals
+ r = rand
+ case
+ when r < 0.1 then m2
+ when r < 0.4 then M2
+ when r < 0.5 then m3
+ when r < 0.6 then M3
+ when r < 0.7 then P4
+ when r < 0.8 then -M3
+ when r < 0.95 then -P5
+ else -P8
+ end
+end
+
+pitches = Pattern::Function.new interval_generator, :type => :pitch, :max_elements => 24
+
+# we'll also use a weighted choice to generate the intensities
+intensities = Pattern::Choice.new [mp, mf, f, ff, fff], :type => :intensity, :weights => [1,2,3,2,1]
+
+
+sequencer = Sequencer::StepSequencer.new [pitches, intensities], :step_size => 0.5
+timeline = sequencer.to_timeline
+
+MIDI_File(file).write timeline
+

0 comments on commit 87f8639

Please sign in to comment.