sonic-midi 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3dee7fe63f4fce57922002d7691fb72ab6f06608
4
+ data.tar.gz: d94ff3f2d95ecdc20f15d84a1896d2ef86c302ec
5
+ SHA512:
6
+ metadata.gz: 4213548e921eb67791b6ab29e687e46c0164390515372294af9e165756b356d30e6b76c2f0bfb3d85819471f9e6968e2d3b697a6861e4f201eac3966da23a0f7
7
+ data.tar.gz: cf354a568eb3710633fb80a007fdaf4551d8431aadcf74972d668c6946f2b4166c23104c38d93eddd319e7d28f48be845c8581ab23d9b3531722fb449805fe2a
data/bin/sonic_midi ADDED
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright 2017 Google Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'commander/import'
18
+ require 'melody_to_mid'
19
+ require 'rubygems'
20
+ require 'sonic_pi'
21
+
22
+ def local_path(filename)
23
+ return File.join(File.dirname(File.expand_path(__FILE__)), '../lib', filename)
24
+ end
25
+
26
+ def check_input(args)
27
+ if args.length < 1
28
+ raise 'Missing input file'
29
+ end
30
+
31
+ input = args[0]
32
+ if not File.file?(input)
33
+ raise 'File %s doesn''t exist' % input
34
+ end
35
+
36
+ return input
37
+ end
38
+
39
+ def play_music(input)
40
+ melody = File.read(input)
41
+ shared_functions = File.read(local_path('shared_functions.rb'))
42
+ sonic_play = File.read(local_path('sonic_play.rb'))
43
+ sonic_pi_command = melody + "\n" + shared_functions + "\n" + sonic_play + "\nsonic_play()"
44
+ app = SonicPi.new
45
+ app.test_connection!
46
+ app.run(sonic_pi_command)
47
+ end
48
+
49
+ def convert_music(input)
50
+ output = input + '.mid'
51
+ melody_converter = MelodyToMidi.new
52
+ melody_converter.make_midi(input, output)
53
+ puts 'Saved midi as %s' % output
54
+ end
55
+
56
+ program :name, 'sonic_midi'
57
+ program :version, '0.1.0'
58
+ program :description, 'Sends music to Sonic Pi and convert it to Midi'
59
+
60
+ command :sonic do |c|
61
+ c.syntax = 'sonic_midi sonic <filename>'
62
+ c.summary = 'Send music to SonicPi'
63
+ c.description = 'Send music specified in <filename> to SonicPi'
64
+ c.example 'description', 'sonic_midi sonic melody.rb'
65
+ c.action do |args, options|
66
+ input = check_input(args)
67
+ play_music(input)
68
+ end
69
+ end
70
+
71
+ command :midi do |c|
72
+ c.syntax = 'sonic_midi midi <filename>'
73
+ c.summary = 'Converts music to Midi'
74
+ c.description = 'Converts music specified in <filename> to Midi and saves as <filename>.mid'
75
+ c.example 'description', 'sonic_midi midi melody.rb'
76
+ c.action do |args, options|
77
+ input = check_input(args)
78
+ convert_music(input)
79
+ end
80
+ end
81
+
82
+ command :all do |c|
83
+ c.syntax = 'sonic_midi all <filename>'
84
+ c.summary = 'Sends music to SonicPi and converts it to Midi'
85
+ c.description = 'Sends music specified in <filename> to SonicPi and converts it to Midi, saving as <filename>.mid'
86
+ c.example 'description', 'sonic_midi all melody.rb'
87
+ c.action do |args, options|
88
+ input = check_input(args)
89
+ play_music(input)
90
+ convert_music(input)
91
+ end
92
+ end
@@ -0,0 +1,65 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ # Copyright 2017 Google Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ $LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')
18
+
19
+ require 'midilib/sequence'
20
+ require 'midilib/consts'
21
+ include MIDI
22
+
23
+ class MelodyToMidi
24
+ def make_midi(melody_file, output_file)
25
+ seq = Sequence.new()
26
+
27
+ require_relative 'shared_functions'
28
+ load melody_file
29
+
30
+ track = Track.new(seq)
31
+ seq.tracks << track
32
+ bpm = 60
33
+ if defined? mybpm
34
+ bpm = mybpm()
35
+ end
36
+ track.events << Tempo.new(Tempo.bpm_to_mpq(bpm))
37
+ track.events << MetaEvent.new(META_SEQ_NAME, 'Sequence Name')
38
+
39
+ track = Track.new(seq)
40
+ seq.tracks << track
41
+
42
+ track.name = 'Melody Track'
43
+ track.instrument = GM_PATCH_NAMES[0]
44
+
45
+ track.events << Controller.new(0, CC_VOLUME, 127)
46
+ track.events << ProgramChange.new(0, 1, 0)
47
+
48
+ music = melody()
49
+
50
+ delta_time = 0
51
+ (0..music[0].length-1).each do |i|
52
+ note = music[0][i].to_i
53
+ duration = (480 * music[1][i]).to_i
54
+ if note == 0
55
+ delta_time = duration
56
+ else
57
+ track.events << NoteOn.new(0, note, 127, delta_time)
58
+ track.events << NoteOff.new(0, note, 127, duration)
59
+ delta_time = 0
60
+ end
61
+ end
62
+
63
+ File.open(output_file, 'wb') { |file| seq.write(file) }
64
+ end
65
+ end
@@ -0,0 +1,33 @@
1
+ # Copyright 2017 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ def repa(array, x)
16
+ result = []
17
+ (0..x).each do
18
+ result.concat(array)
19
+ end
20
+ return result
21
+ end
22
+
23
+ def rep(value, times)
24
+ return Array.new(times, value)
25
+ end
26
+
27
+ def clone(array)
28
+ return [].replace(array)
29
+ end
30
+
31
+ Q = 0.25
32
+ H = 0.5
33
+ F = 1
data/lib/sonic_play.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Copyright 2017 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ def sonic_play
16
+ set_volume! 5
17
+ use_synth mysynth()
18
+ use_bpm mybpm()
19
+ m = melody()
20
+ (0..m[0].length-1).each do |i|
21
+ if m[0][i] != 0
22
+ play m[0][i]
23
+ end
24
+ sleep m[1][i]
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sonic-midi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Airapetyan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: commander
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: midilib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sonic-pi-cli
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Command-line tool to generate Midi music also playable in Sonic Pi
56
+ email: davidair@google.com
57
+ executables:
58
+ - sonic_midi
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/sonic_midi
63
+ - lib/melody_to_mid.rb
64
+ - lib/shared_functions.rb
65
+ - lib/sonic_play.rb
66
+ homepage: http://rubygems.org/gems/sonic-midi
67
+ licenses:
68
+ - Apache-2.0
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.6.11
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Dual Sonic Pi Midi music generator
90
+ test_files: []