muse 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,11 +15,16 @@
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
  module Muse
18
- module Config
18
+ module Envelope
19
19
  class << self
20
- def default(input)
21
- Math.cos(2* Math::PI*input).to_f
20
+ def default(input, duration)
21
+ Math.cos((Math::PI*input)/(2*duration.to_f))
22
22
  end
23
+
24
+ def sine(input, duration)
25
+ Math.sin((Math::PI*input)/duration.to_f)
26
+ end
27
+
23
28
  end
24
29
  end
25
30
  end
@@ -0,0 +1,46 @@
1
+ # Muse
2
+ # Copyright (C) 2012 Chang Sau Sheong
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ module Muse
18
+ module Harmonic
19
+ class << self
20
+ def default(input)
21
+ Math.sin(2 * Math::PI * input)
22
+ end
23
+
24
+ def second(input)
25
+ Math.sin(2 * Math::PI * input) +
26
+ Math.sin(3* 2 * Math::PI * input)
27
+ end
28
+
29
+ def third(input)
30
+ Math.sin(2 * Math::PI * input) +
31
+ Math.sin(3* 2 * Math::PI * input) +
32
+ Math.sin(5 * 2 * Math::PI * input)
33
+ end
34
+
35
+ def organ(input)
36
+ Math.sin(2 * 2 * Math::PI * input) +
37
+ Math.sin(2 * Math::PI * input) +
38
+ Math.sin(Math::PI * input)
39
+
40
+ end
41
+
42
+
43
+
44
+ end
45
+ end
46
+ end
data/lib/muse/version.rb CHANGED
@@ -15,5 +15,5 @@
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
  module Muse
18
- VERSION = "0.0.6"
18
+ VERSION = "0.0.7"
19
19
  end
data/lib/muse.rb CHANGED
@@ -15,17 +15,19 @@
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
  require "parallel"
18
- require "muse/wav"
19
- require "muse/config"
18
+ require "#{File.dirname(__FILE__)}/muse/wav"
19
+ require "#{File.dirname(__FILE__)}/muse/config"
20
20
 
21
21
  module Muse
22
22
  class Song
23
- attr :name, :bars
24
23
 
25
- def self.record(name, &block)
24
+ def self.record(name, options ={}, &block)
26
25
  start_time = Time.now
27
26
  puts "Start recording song named #{name}.wav"
28
27
  @name = name
28
+ @bpm = options[:bpm] || 120
29
+ @envelope = options[:envelope] || 'default'
30
+ @harmonic = options[:harmonic] || 'default'
29
31
  @bars = {}
30
32
  puts "Processing ..."
31
33
  instance_eval &block
@@ -36,7 +38,7 @@ module Muse
36
38
  end
37
39
 
38
40
  class Bar
39
- attr :bpm, :beats, :adsr
41
+ attr :bpm, :beats, :envelope, :harmonic
40
42
  attr_accessor :stream
41
43
 
42
44
  NOTES = %w(_ a ais b c cis d dis e f fis g gis)
@@ -56,7 +58,8 @@ module Muse
56
58
  def initialize(id, options={})
57
59
  @bpm = options[:bpm] || 120
58
60
  @beats = (options[:b] || 1).to_f
59
- @adsr = options[:adsr] || 'default'
61
+ @envelope = options[:envelope] || 'default'
62
+ @harmonic = options[:harmonic] || 'default'
60
63
  @stream = []
61
64
  end
62
65
 
@@ -86,12 +89,13 @@ module Muse
86
89
  stream = []
87
90
  if options
88
91
  beats = options[:b].nil? ? (@beats || 1) : options[:b].to_f
89
- volume = (options[:v].nil? ? 10 : options[:v].to_i) * 1000
90
- adsr = options[:a].nil? ? @adsr : 'default'
92
+ volume = (options[:v].nil? ? 5 : options[:v].to_i) * 1000
93
+ envelope = options[:a].nil? ? @envelope : 'default'
94
+ harmonic = options[:h].nil? ? @harmonic : 'default'
91
95
  else
92
- beats, volume, adsr = (@beats || 1), 10000, 'default'
96
+ beats, volume, envelope, harmonic = (@beats || 1), 5000, @envelope || 'default', @harmonic || 'default'
93
97
  end
94
- puts "[#{note}] -> beats : #{beats}, :octave : #{octave}"
98
+ puts "[#{note}] -> beats : #{beats}, octave : #{octave} bpm: #{bpm} envelope: #{envelope} harmonic : #{harmonic}"
95
99
  duration = ((60 * WavHeader::SAMPLE_RATE * beats)/@bpm)/WavHeader::SAMPLE_RATE.to_f
96
100
  note_frequency = note + octave.to_s
97
101
  unless note == '_'
@@ -100,7 +104,9 @@ module Muse
100
104
  freq = 0
101
105
  end
102
106
  (0.0..duration.to_f).step(1.0/WavHeader::SAMPLE_RATE) do |i|
103
- x = (Config.send(adsr.to_sym,i) * volume * Math.sin(2 * Math::PI * freq * i)).to_i
107
+ env = Envelope.send(envelope.to_sym,i, duration)
108
+ har = Harmonic.send(harmonic.to_sym, freq * i)
109
+ x = (env * volume * har).to_i
104
110
  stream << [x,x]
105
111
  end
106
112
  return stream
@@ -137,6 +143,9 @@ module Muse
137
143
  unless @bars[id]
138
144
  @bars[id] = []
139
145
  end
146
+ options[:bpm] = @bpm || options[:bpm] || 120
147
+ options[:envelope] = @envelope || options[:envelope] || 'default'
148
+ options[:harmonic] = @harmonic || options[:harmonic] || 'default'
140
149
  @bars[id] << Bar.new(id, options)
141
150
  @bars[id].last
142
151
  end
@@ -153,7 +162,7 @@ module Muse
153
162
  def save
154
163
  puts "Creating temporary files in parallel ..."
155
164
 
156
- results = Parallel.each_with_index(@bars.values, :in_processes => 4) do |item, id|
165
+ results = Parallel.each_with_index(@bars.values, :in_processes => Parallel.processor_count) do |item, id|
157
166
  puts "Writing file - #{id}"
158
167
  stream = []
159
168
  container = []
@@ -167,7 +176,7 @@ module Muse
167
176
  temp.stream[i].left = s[0]
168
177
  temp.stream[i].right = s[1]
169
178
  end
170
- File.open("#{@name}-#{id}.tmp", "w") {|file| temp.write(file) }
179
+ File.open("#{@name}-#{id.to_s.rjust(3,'0')}.tmp", "w") {|file| temp.write(file) }
171
180
  puts "Completed file - #{id}"
172
181
  end
173
182
 
@@ -177,7 +186,7 @@ module Muse
177
186
 
178
187
  puts "Combining temporary files ..."
179
188
  WavHeader.new("#{@name}.wav", stream_size)
180
- tmpfiles = Dir.glob("#{@name}-*.tmp")
189
+ tmpfiles = Dir.glob("#{@name}-*.tmp").sort
181
190
  File.open("#{@name}.wav", "ab+") do |wav|
182
191
  tmpfiles.each do |file|
183
192
  File.open(file, "rb") { |tmp| File.copy_stream(tmp, wav) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: muse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-03-15 00:00:00.000000000Z
13
+ date: 2012-03-18 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bindata
17
- requirement: &2156346880 !ruby/object:Gem::Requirement
17
+ requirement: &2160980980 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2156346880
25
+ version_requirements: *2160980980
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: parallel
28
- requirement: &2156345400 !ruby/object:Gem::Requirement
28
+ requirement: &2160980520 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2156345400
36
+ version_requirements: *2160980520
37
37
  description: Muse is a complete music creation package including writing and up to
38
38
  creating WAV files
39
39
  email:
@@ -43,8 +43,9 @@ executables: []
43
43
  extensions: []
44
44
  extra_rdoc_files: []
45
45
  files:
46
- - lib/muse/config/adsr.rb
47
46
  - lib/muse/config/chords.rb
47
+ - lib/muse/config/envelope.rb
48
+ - lib/muse/config/harmonic.rb
48
49
  - lib/muse/config.rb
49
50
  - lib/muse/version.rb
50
51
  - lib/muse/wav.rb