mml2wav 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05336b0d10a736402a502dfc2c1949e129e84377
4
- data.tar.gz: 9341b84d926a6c3a120e775aac4ae1789358e9d4
3
+ metadata.gz: bd7a4796c9f409d00b80f36bab6500965ebfcccf
4
+ data.tar.gz: c07c86df8f9a0fe8121a2774a885d311548fab66
5
5
  SHA512:
6
- metadata.gz: 3ab94e18b6ca8c6b8ba78d0a87f1397b4ed187b86073083cd57f4a0928fa87a3ba5545e02e1dabe9a0ffcb30772aba61e9eb9e5695ba07a6cb52f4cff89ee54a
7
- data.tar.gz: 918b643d4a3809ab117beb2f3980d3c6e45ffeb9d1e11c26d5080d01d5942ca7545060068af2fa959a0615d1157a13aada3e69699289034a5d5ce5abe67e0f81
6
+ metadata.gz: f21edb3881d011862b646a69f32bfad62325c924e7be9daa30e5587be76f1050fcbd174cf2ae179e746ae4b0663d61838a014ba4e8e03d31a5ee92c2cbbe03dc
7
+ data.tar.gz: 7123c9626a12a1302ded2e1b26ce5eadc751860154567f15379c4de424b07e7ad127cfa687086c01849240e6e36328402216b27eecc533bc405b2fb3a2a11097
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # mml2wav
1
+ # mml2wav [![Gem Version](https://badge.fury.io/rb/mml2wav.svg)](http://badge.fury.io/rb/mml2wav)
2
2
 
3
3
  MML (Music Macro Language) to WAV audio converter by Ruby.
4
4
 
@@ -32,11 +32,25 @@ g | so
32
32
  a | la
33
33
  b | si
34
34
 
35
+ ### length
36
+
37
+ MML | length
38
+ --- | -----------------------------
39
+ c4 | 1/4 (default)
40
+ c8 | 1/8
41
+ c16 | 1/16
42
+ c. | 1.5 times longer than default
43
+
35
44
  ### signs
36
45
 
37
46
  MML | mean
38
- --- | ------
47
+ --- | --------------
39
48
  r | rest
49
+ t90 | BPM
50
+ l4 | default length
51
+ o4 | octave
52
+ < | up octave
53
+ > | down octave
40
54
 
41
55
  ## License
42
56
 
@@ -1,4 +1,4 @@
1
1
  #!/bin/sh
2
2
 
3
- echo 'cdefedcr efgagfer crcrcrcr ccddeefferdrc' | mml2wav --output=kaerunouta.wav
3
+ echo 'T100V6cdefedcr efgagfer crcrcrcr c8c8d8d8e8e8f8f8 edc' | mml2wav --output=kaerunouta.wav
4
4
  aplay kaerunouta.wav
@@ -31,6 +31,10 @@ module Mml2wav
31
31
  "Specify sampling rate", Integer) do |rate|
32
32
  options[:sampling_rate] = rate
33
33
  end
34
+ parser.on("--bpm=BPM",
35
+ "Specify BPM (beats per minute)", Integer) do |bpm|
36
+ options[:bpm] = bpm
37
+ end
34
38
  parser.parse!(arguments)
35
39
 
36
40
  unless File.pipe?('/dev/stdin') || IO.select([ARGF], nil, nil, 0)
@@ -1,14 +1,24 @@
1
1
  module Mml2wav
2
2
  module Scale
3
3
  FREQUENCIES = {
4
- c: 261.626,
5
- d: 293.665,
6
- e: 329.628,
7
- f: 349.228,
8
- g: 391.995,
9
- a: 440.000,
10
- b: 493.883,
11
- r: 0,
4
+ "c" => 32.70325 ,
5
+ "c#" => 34.647875,
6
+ "c+" => 34.647875,
7
+ "d" => 36.708125,
8
+ "d#" => 38.890875,
9
+ "d+" => 38.890875,
10
+ "e" => 41.2035 ,
11
+ "f" => 43.6535 ,
12
+ "f#" => 46.24925 ,
13
+ "f+" => 46.24925 ,
14
+ "g" => 48.999375,
15
+ "g#" => 51.913125,
16
+ "g+" => 51.913125,
17
+ "a" => 55.0 ,
18
+ "a#" => 58.2705 ,
19
+ "a+" => 58.2705 ,
20
+ "b" => 61.735375,
21
+ "r" => 0,
12
22
  }
13
23
  end
14
24
  end
@@ -1,3 +1,3 @@
1
1
  module Mml2wav
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,17 +8,44 @@ module Mml2wav
8
8
 
9
9
  def write(sounds, options={})
10
10
  output_path = options[:output] || "doremi.wav"
11
- sampling_rate = options[:sampling_rate] || 8000
11
+ sampling_rate = options[:sampling_rate] || 22050
12
+ bpm = options[:bpm] || 60
13
+ velocity = 5
14
+ octave = 4
15
+ default_length = 4.0
12
16
 
13
17
  format = Format.new(:mono, :pcm_8, sampling_rate)
14
- @sine_waves = {}
15
18
  Writer.new(output_path, format) do |writer|
16
19
  buffer_format = Format.new(:mono, :float, sampling_rate)
17
- sounds.split(//).each do |sound|
18
- frequency = Scale::FREQUENCIES[sound.downcase.to_sym]
20
+ sounds.scan(/T\d+|V\d+|L\d+|[A-G][#+]?(?:\d+|\.)?|O\d+|[><]|./i).each do |sound|
21
+ base_sec = 60.0
22
+ length = default_length
23
+ case sound
24
+ when /\AT(\d+)/i
25
+ bpm = $1.to_i
26
+ when /\AV(\d+)/i
27
+ velocity = $1.to_i
28
+ when /\AL(\d+)/i
29
+ default_length = $1.to_f
30
+ when /\A([A-G][#+]?)(\d+)/i
31
+ length = $2.to_f
32
+ sound = $1
33
+ when /\A([A-G][#+]?)(\.)/i
34
+ length = default_length / 1.5
35
+ sound = $1
36
+ when /\AO(\d+)/i
37
+ octave = $1.to_i
38
+ when "<"
39
+ octave += 1
40
+ when ">"
41
+ octave -= 1
42
+ end
43
+ sec = base_sec / length / bpm
44
+ amplitude = velocity.to_f / 10
45
+ frequency = Scale::FREQUENCIES[sound.downcase]
19
46
  next unless frequency
20
- @sine_waves[sound] ||= sine_wave(frequency, sampling_rate)
21
- samples = @sine_waves[sound]
47
+ frequency *= (2 ** octave)
48
+ samples = sine_wave(frequency, sampling_rate, sec, amplitude)
22
49
  buffer = Buffer.new(samples, buffer_format)
23
50
  writer.write(buffer)
24
51
  end
@@ -26,7 +53,7 @@ module Mml2wav
26
53
  end
27
54
 
28
55
  private
29
- def sine_wave(frequency, sampling_rate, sec=0.1, amplitude=0.5)
56
+ def sine_wave(frequency, sampling_rate, sec, amplitude=0.5)
30
57
  max = sampling_rate * sec
31
58
  if frequency == 0
32
59
  return Array.new(max) { 0.0 }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mml2wav
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masafumi Yokoyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-25 00:00:00.000000000 Z
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wavefile