Skip to content

Commit

Permalink
Little bit of cleanup all around
Browse files Browse the repository at this point in the history
  • Loading branch information
beneggett committed Mar 10, 2015
1 parent a25c405 commit 1926252
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 52 deletions.
6 changes: 4 additions & 2 deletions lib/music_theory/chord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def flatten_third
new_samples[i] += value
end
end
normalize_samples(new_samples)
end

def normalize_samples(new_samples)
max = new_samples.map {|s| s.abs }.max
multiplier = 1.0 / max
new_samples.map!{ |s| multiplier * s }
end

end

def samples
flatten_third
Expand Down
18 changes: 14 additions & 4 deletions lib/music_theory/modes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
require 'music_theory/output'

module MusicTheory
class Modes
include MusicTheory::Output
S = 1
T = 2
I = [T, T, S, T, T, T, S]
Expand All @@ -18,15 +15,28 @@ class Modes

def self.ionian; I; end
self.singleton_class.send(:alias_method, :major, :ionian)
self.singleton_class.send(:alias_method, :i, :ionian)

def self.dorian; II; end
self.singleton_class.send(:alias_method, :ii, :dorian)

def self.phrygian; III; end
self.singleton_class.send(:alias_method, :iii, :phrygian)

def self.lydian; IV; end
self.singleton_class.send(:alias_method, :iv, :lydian)

def self.mixolydian; V; end
self.singleton_class.send(:alias_method, :v, :mixolydian)

def self.aeolian; VI; end
self.singleton_class.send(:alias_method, :minor, :aeolian)
self.singleton_class.send(:alias_method, :vi, :aeolian)

def self.locrian; VII; end
def self.chromatic; CHROMATIC; end
self.singleton_class.send(:alias_method, :vii, :locrian)

def self.chromatic; CHROMATIC; end

end

Expand Down
8 changes: 3 additions & 5 deletions lib/music_theory/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Note
attr_accessor :frequency, :duration, :output_file_name, :distort

def initialize(options = {})
@frequency = options[:frequency].to_f || 220.0 # Note frequency in Hz
@frequency = options[:frequency] || 440.0 # Note frequency in Hz
@frequency = @frequency.to_f
@duration = options[:duration] || 1.0 # Number of seconds per note
@distort = options[:distort] || false
@output_file_name = options[:output_file_name] || 'note' # File name to write (without extension)
Expand Down Expand Up @@ -41,7 +42,7 @@ def samples

def distort!(samples)
samples.map do |sample|
negative = (sample) < 0
negative = sample < 0
sample *= 8.to_f
if sample.abs > 5
sample = 5
Expand All @@ -51,9 +52,6 @@ def distort!(samples)
end
end

def scale

end
end

end
16 changes: 8 additions & 8 deletions lib/music_theory/octave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
module MusicTheory
class Octave
include MusicTheory::Output
attr_accessor :starting_note, :number_of_octaves, :direction, :output_file_name, :all_notes
attr_accessor :starting_note, :amount, :direction, :output_file_name, :all_notes

def initialize(options = {})
@starting_note = options[:starting_note] || MusicTheory::Note.new # Note to start on
@number_of_octaves= options[:number_of_octaves] || 2 # Number of octaves to repeat
@amount= options[:amount] || 2 # Number of octaves to repeat
@direction = options[:direction] || 'asc' # Number of seconds per note
@output_file_name = options[:output_file_name] || 'octave' # File name to write (without extension)
@all_notes = []
all_notes << starting_note
number_of_octaves.to_i.times do
build_octave
end

def build_octave
@all_notes = [ starting_note ]
amount.to_i.times do
new_note = all_notes.last.clone
if direction == 'asc'
new_note.frequency = all_notes.last.frequency * 2
Expand All @@ -27,9 +30,6 @@ def samples
all_notes.map(&:samples).flatten
end

def sample_rate
starting_note.sample_rate
end
end

end
35 changes: 13 additions & 22 deletions lib/music_theory/scale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,21 @@
module MusicTheory
class Scale
include MusicTheory::Output
attr_accessor :starting_note, :number_of_octaves, :direction, :output_file_name, :all_notes, :scale_type, :scale_notes, :distort, :duration, :frequency, :sample_rate
attr_accessor :starting_note, :number_of_octaves, :direction, :output_file_name, :all_notes, :scale_type, :scale_notes, :distort, :duration, :frequency

def initialize(scale_type = :major, options = {})
@scale_type = scale_type
@scale_type = scale_type
@distort = options[:distort] || false
@duration = options[:duration] || 0.5
@frequency = options[:frequency] || 220.0
@starting_note = create_new_note # Note to start on

@number_of_octaves= options[:number_of_octaves] || 2 # Number of octaves to repeat
@direction = options[:direction] || 'asc' # Number of seconds per note
@starting_note = create_new_note(frequency) # Note to start on
@output_file_name = options[:output_file_name] || 'scale' # File name to write (without extension)
set_all_notes
set_scale_notes
end

def third
third ||= MusicTheory::Third.new self
end

def chord
third.chord
end

def twelth_root_of_two
(2 ** (1.0/12))
def twelfth_root_of_two
2 ** (1.0/12)
end

def mode
Expand All @@ -44,15 +33,14 @@ def set_scale_notes
end
end

def create_new_note
MusicTheory::Note.new( frequency: frequency, duration: duration, distort: distort)
def create_new_note(note_frequency)
MusicTheory::Note.new( frequency: note_frequency, duration: duration, distort: distort)
end

def set_all_notes
@all_notes = [@starting_note]
12.times do
new_note = create_new_note
new_note.frequency = (all_notes.last.frequency * twelth_root_of_two)
new_note = create_new_note(all_notes.last.frequency * twelfth_root_of_two)
all_notes << new_note
end
end
Expand All @@ -61,10 +49,13 @@ def samples
scale_notes.map(&:samples).flatten
end

def sample_rate
starting_note.sample_rate
def third
third ||= MusicTheory::Third.new self
end

def chord
third.chord
end
end

end
17 changes: 7 additions & 10 deletions lib/music_theory/third.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@
module MusicTheory
class Third
include MusicTheory::Output

attr_accessor :scale, :all_notes

def initialize(scale)
@scale = scale
@all_notes = [@scale.scale_notes.first]
@all_notes = [scale.scale_notes.first]
current = 0
double_scale_notes = @scale.scale_notes * 2
@scale.mode.in_groups_of(2, false) do |group|
double_scale_notes = scale.scale_notes * 2
scale.mode.in_groups_of(2, false) do |group|
current += group.sum
all_notes << double_scale_notes[current]
end
all_notes.uniq! {|note| note.frequency}
all_notes.sort_by! {|note| note.frequency}
end


def output_file_name
scale.output_file_name || 'thirds'
end

def samples
all_notes.map(&:samples).flatten
# chord_samples
end

def chord
chord ||= MusicTheory::Chord.new self
end

def output_file_name
scale.output_file_name || 'thirds'
end

end
end
2 changes: 1 addition & 1 deletion lib/music_theory/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MusicTheory
VERSION = "0.0.1"
VERSION = "0.1.0"
end
157 changes: 157 additions & 0 deletions note-charted.html

Large diffs are not rendered by default.

0 comments on commit 1926252

Please sign in to comment.