muse 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,19 @@
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
+
1
17
  module Muse
2
18
  module Config
3
19
  class << self
@@ -1,3 +1,19 @@
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
+
1
17
  module Muse
2
18
  module Config
3
19
  class << self
data/lib/muse/config.rb CHANGED
@@ -1 +1,17 @@
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
+
1
17
  Dir["#{File.dirname(__FILE__)}/config/*.rb"].each{|f| require f }
data/lib/muse/version.rb CHANGED
@@ -1,3 +1,19 @@
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
+
1
17
  module Muse
2
- VERSION = "0.0.4"
18
+ VERSION = "0.0.6"
3
19
  end
data/lib/muse/wav.rb CHANGED
@@ -1,6 +1,23 @@
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
+
1
17
  require 'bindata'
2
18
 
3
19
  module Muse
20
+
4
21
  class RiffChunk < BinData::Record
5
22
  int32be :chunk_id
6
23
  int32le :chunk_size
@@ -21,23 +38,26 @@ module Muse
21
38
  class DataChunk < BinData::Record
22
39
  int32be :chunk_id
23
40
  int32le :chunk_size
41
+ end
42
+
43
+ class TempData < BinData::Record
24
44
  array :stream do
25
45
  int16le :left
26
46
  int16le :right
27
47
  end
28
48
  end
29
-
49
+
30
50
  class WavFormat < BinData::Record
31
51
  riff_chunk :riff_chunk
32
52
  format_chunk :format_chunk
33
53
  data_chunk :data_chunk
34
54
  end
35
-
36
- class Wav
55
+
56
+ class WavHeader
37
57
  SAMPLE_RATE = 44100
38
58
  attr :wav, :file, :sample_rate, :format_chunk, :riff_chunk, :data_chunk
39
59
 
40
- def initialize(filename)
60
+ def initialize(filename, stream_size)
41
61
  @sample_rate = SAMPLE_RATE
42
62
  @file = File.open(filename, "wb")
43
63
 
@@ -54,26 +74,18 @@ module Muse
54
74
  @format_chunk.sample_rate = @sample_rate
55
75
  @format_chunk.byte_rate = @format_chunk.sample_rate * @format_chunk.num_channels * @format_chunk.bits_per_sample/2
56
76
  @format_chunk.block_align = @format_chunk.num_channels * @format_chunk.bits_per_sample/2
77
+
57
78
  @data_chunk = DataChunk.new
58
79
  @data_chunk.chunk_id = "data".unpack("N").first
59
- end
60
-
61
- def write(stream_data)
62
- stream_data.each_with_index do |s,i|
63
- @data_chunk.stream[i].left = s[0]
64
- @data_chunk.stream[i].right = s[1]
65
- end
66
- @data_chunk.chunk_size = stream_data.length * @format_chunk.num_channels * @format_chunk.bits_per_sample/8
80
+ @data_chunk.chunk_size = stream_size * @format_chunk.num_channels * @format_chunk.bits_per_sample/8
67
81
  @riff_chunk.chunk_size = 36 + @data_chunk.chunk_size
82
+
68
83
  @wav = WavFormat.new
69
84
  @wav.riff_chunk = @riff_chunk
70
85
  @wav.format_chunk = @format_chunk
71
86
  @wav.data_chunk = @data_chunk
72
87
  @wav.write(@file)
73
- end
74
88
 
75
- def close
76
- @file.close
77
89
  end
78
90
  end
79
91
  end
data/lib/muse.rb CHANGED
@@ -1,17 +1,37 @@
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
+ require "parallel"
1
18
  require "muse/wav"
2
19
  require "muse/config"
3
20
 
4
21
  module Muse
5
22
  class Song
6
- attr :wav_file, :bars
23
+ attr :name, :bars
7
24
 
8
25
  def self.record(name, &block)
26
+ start_time = Time.now
9
27
  puts "Start recording song named #{name}.wav"
10
- @wav_file = Wav.new(name + ".wav")
28
+ @name = name
11
29
  @bars = {}
12
30
  puts "Processing ..."
13
31
  instance_eval &block
14
32
  save
33
+ end_time = Time.now
34
+ puts "Total time taken : #{((end_time - start_time)/60.0).round(3)} minutes"
15
35
  puts "done."
16
36
  end
17
37
 
@@ -72,14 +92,14 @@ module Muse
72
92
  beats, volume, adsr = (@beats || 1), 10000, 'default'
73
93
  end
74
94
  puts "[#{note}] -> beats : #{beats}, :octave : #{octave}"
75
- duration = ((60 * Wav::SAMPLE_RATE * beats)/@bpm)/Wav::SAMPLE_RATE.to_f
95
+ duration = ((60 * WavHeader::SAMPLE_RATE * beats)/@bpm)/WavHeader::SAMPLE_RATE.to_f
76
96
  note_frequency = note + octave.to_s
77
97
  unless note == '_'
78
98
  freq = frequency_of(FREQUENCIES[note_frequency.to_sym])
79
99
  else
80
100
  freq = 0
81
101
  end
82
- (0.0..duration.to_f).step(1.0/Wav::SAMPLE_RATE) do |i|
102
+ (0.0..duration.to_f).step(1.0/WavHeader::SAMPLE_RATE) do |i|
83
103
  x = (Config.send(adsr.to_sym,i) * volume * Math.sin(2 * Math::PI * freq * i)).to_i
84
104
  stream << [x,x]
85
105
  end
@@ -131,18 +151,40 @@ module Muse
131
151
  end
132
152
 
133
153
  def save
134
- puts "Saving music file"
135
- stream = []
136
- @bars.each do |id, item|
154
+ puts "Creating temporary files in parallel ..."
155
+
156
+ results = Parallel.each_with_index(@bars.values, :in_processes => 4) do |item, id|
157
+ puts "Writing file - #{id}"
158
+ stream = []
137
159
  container = []
138
160
  item = right_size item
139
161
  item.each do |i|
140
162
  container << i.stream
141
163
  end
142
- stream += container.transpose.map {|x| x.transpose.map {|y| y.reduce(:+)}}
164
+ stream += container.transpose.map {|x| x.transpose.map {|y| y.reduce(:+)}}
165
+ temp = TempData.new
166
+ stream.each_with_index do |s,i|
167
+ temp.stream[i].left = s[0]
168
+ temp.stream[i].right = s[1]
169
+ end
170
+ File.open("#{@name}-#{id}.tmp", "w") {|file| temp.write(file) }
171
+ puts "Completed file - #{id}"
172
+ end
173
+
174
+ stream_size = results.inject(0) do |memo, bars|
175
+ memo + bars.first.stream.size
176
+ end
177
+
178
+ puts "Combining temporary files ..."
179
+ WavHeader.new("#{@name}.wav", stream_size)
180
+ tmpfiles = Dir.glob("#{@name}-*.tmp")
181
+ File.open("#{@name}.wav", "ab+") do |wav|
182
+ tmpfiles.each do |file|
183
+ File.open(file, "rb") { |tmp| File.copy_stream(tmp, wav) }
184
+ File.delete file
185
+ end
143
186
  end
144
- @wav_file.write(stream)
145
- @wav_file.close
187
+
146
188
  end
147
189
  end
148
190
  end
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.4
4
+ version: 0.0.6
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-11 00:00:00.000000000Z
13
+ date: 2012-03-15 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bindata
17
- requirement: &2168647260 !ruby/object:Gem::Requirement
17
+ requirement: &2156346880 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,18 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2168647260
25
+ version_requirements: *2156346880
26
+ - !ruby/object:Gem::Dependency
27
+ name: parallel
28
+ requirement: &2156345400 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2156345400
26
37
  description: Muse is a complete music creation package including writing and up to
27
38
  creating WAV files
28
39
  email: