midi_lyrics 0.0.1

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: d996919ed3cfcda1dcfda3adc2280ceb1681a5b1
4
+ data.tar.gz: 3fe6246949062d40b66c03df9c68d279da5ded7e
5
+ SHA512:
6
+ metadata.gz: 5aec8699ca8f49b4ed1637e5a329b79f4c7941028fa4058af5572425a3b8e85d62f358b20e1c8a0e14cb850575dd54eb0a53095ba1ec2671af3445c640beb715
7
+ data.tar.gz: 415d7b89ecfe943be95089f60c3f28c8a5bb2f840aa911f81bc322cfc3844a29748e04599a8e426634d7b03fa039ee07b36f9e246bf815dea2e056d9dbd85dd8
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --profile
4
+ --tty
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ midi_lyrics
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in midi_lyrics.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'simplecov', :require => false
8
+ gem 'coveralls', :require => false
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mateus Del Bianco
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # MidiLyrics
2
+
3
+ [![Build Status](https://travis-ci.org/mateusdelbianco/midi_lyrics.png)](https://travis-ci.org/mateusdelbianco/midi_lyrics)
4
+ [![Code Climate](https://codeclimate.com/github/mateusdelbianco/midi_lyrics.png)](https://codeclimate.com/github/mateusdelbianco/midi_lyrics)
5
+ [![Coverage Status](https://coveralls.io/repos/mateusdelbianco/midi_lyrics/badge.png?branch=master)](https://coveralls.io/r/mateusdelbianco/midi_lyrics?branch=master)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'midi_lyrics'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install midi_lyrics
20
+
21
+ ## Usage
22
+
23
+ MidiLyrics::Parser.new("test.mid").extract
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ task :default => :spec
4
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,113 @@
1
+ require "midi_lyrics/version"
2
+ require "midilib"
3
+
4
+ module MidiLyrics
5
+ class FileNotFound < StandardError; end
6
+
7
+ class LyricSyllable
8
+ attr_accessor :text, :start_in_pulses, :duration_in_pulses
9
+ attr_writer :sequence
10
+
11
+ def initialize(fields = {})
12
+ self.start_in_pulses = fields[:start_in_pulses]
13
+ self.duration_in_pulses = fields[:duration_in_pulses]
14
+ self.text = fields[:text]
15
+ self.sequence = fields[:sequence]
16
+ end
17
+
18
+ def start
19
+ format_time(start_in_pulses)
20
+ end
21
+
22
+ def duration
23
+ format_time(duration_in_pulses)
24
+ end
25
+
26
+ private
27
+ def format_time(time)
28
+ @sequence.pulses_to_seconds(time.to_f).round(3)
29
+ end
30
+ end
31
+
32
+ class Parser
33
+ def initialize(file)
34
+ @file = file
35
+
36
+ unless File.exists?(@file)
37
+ raise MidiLyrics::FileNotFound
38
+ end
39
+ end
40
+
41
+ def read_sequence_from_file
42
+ @sequence = ::MIDI::Sequence.new()
43
+ File.open(@file, "rb") do | file |
44
+ @sequence.read(file)
45
+ end
46
+ @sequence
47
+ end
48
+
49
+ def load_tracks
50
+ @lyrics_track = ::MIDI::Track.new(@sequence)
51
+ @noteon_track = ::MIDI::Track.new(@sequence)
52
+ @sequence.tracks[1].each do | event |
53
+ if event.kind_of?(::MIDI::MetaEvent) && event.meta_type == ::MIDI::META_LYRIC
54
+ text = event.data.collect{|x| x.chr(Encoding::UTF_8)}.join
55
+ if text.gsub(" ", "") != ""
56
+ @lyrics_track.events << event
57
+ end
58
+ end
59
+ if event.kind_of?(::MIDI::NoteOn)
60
+ @noteon_track.events << event
61
+ end
62
+ end
63
+ end
64
+
65
+ def calculate_durations
66
+ @durations = {}
67
+ @noteon_track.each do |event|
68
+ @durations[event.time_from_start] = event.off.time_from_start - event.time_from_start
69
+ end
70
+ end
71
+
72
+ def load_lyrics
73
+ @lyrics = @lyrics_track.collect do |event|
74
+ LyricSyllable.new(
75
+ :sequence => @sequence,
76
+ :start_in_pulses => event.time_from_start,
77
+ :duration_in_pulses => @durations[event.time_from_start],
78
+ :text => event.data.collect{|x| x.chr(Encoding::UTF_8)}.join,
79
+ )
80
+ end
81
+ end
82
+
83
+ def remove_heading_blank_lines
84
+ while @lyrics.first.text.strip == ""
85
+ @lyrics.shift
86
+ end
87
+ end
88
+
89
+ def consolidate_carriage_returns
90
+ new_lyrics = []
91
+ @lyrics.each do |l|
92
+ if ["\r", "\n"].include?(l.text)
93
+ l.start_in_pulses = new_lyrics.last.start_in_pulses + new_lyrics.last.duration_in_pulses
94
+ l.duration_in_pulses = 0
95
+ new_lyrics << l
96
+ else
97
+ new_lyrics << l
98
+ end
99
+ end
100
+ @lyrics = new_lyrics
101
+ end
102
+
103
+ def extract
104
+ read_sequence_from_file
105
+ load_tracks
106
+ calculate_durations
107
+ load_lyrics
108
+ remove_heading_blank_lines
109
+ consolidate_carriage_returns
110
+ @lyrics
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,3 @@
1
+ module MidiLyrics
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'midi_lyrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "midi_lyrics"
8
+ spec.version = MidiLyrics::VERSION
9
+ spec.authors = ["Mateus Del Bianco"]
10
+ spec.email = ["mateus@delbianco.net.br"]
11
+ spec.description = %q{MIDI Lyrics extractor}
12
+ spec.summary = %q{Extracts lyrics with timing from MIDI files}
13
+ spec.homepage = "https://github.com/mateusdelbianco/midi_lyrics"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency("midilib", ["~> 2.0.0"])
26
+ end
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+
3
+ describe MidiLyrics do
4
+ QUARTER_NOTE_DURATION = 0.417
5
+ HALF_NOTE_DURATION = 0.875
6
+
7
+ it "returns an array" do
8
+ expect(MidiLyrics::Parser.new("spec/fixtures/one_note_one_syllable.mid").extract).to be_kind_of(Array)
9
+ end
10
+
11
+ context "parsing" do
12
+ it "parses one_note_one_syllable.mid correctly" do
13
+ lyrics = MidiLyrics::Parser.new("spec/fixtures/one_note_one_syllable.mid").extract
14
+ lyrics = lyrics.collect{|x| { text: x.text, start: x.start, duration: x.duration } }
15
+ expect(lyrics).to eq([
16
+ { :text => "Test ", :start => 0.0, :duration => QUARTER_NOTE_DURATION },
17
+ { :text => "\r", :start => QUARTER_NOTE_DURATION, :duration => 0.0 },
18
+ { :text => "\n", :start => QUARTER_NOTE_DURATION, :duration => 0.0 }
19
+ ])
20
+ end
21
+
22
+ it "parses two_notes_one_syllable.mid correctly" do
23
+ lyrics = MidiLyrics::Parser.new("spec/fixtures/two_notes_one_syllable.mid").extract
24
+ lyrics = lyrics.collect{|x| { text: x.text, start: x.start, duration: x.duration } }
25
+ expect(lyrics).to eq([
26
+ { text: "Test ", start: 0, duration: 0.5 + QUARTER_NOTE_DURATION },
27
+ { text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, duration: 0 },
28
+ { text: "\n", start: 0.5 + QUARTER_NOTE_DURATION, duration: 0 }
29
+ ])
30
+ end
31
+
32
+ it "parses two_notes_two_syllables.mid correctly" do
33
+ lyrics = MidiLyrics::Parser.new("spec/fixtures/two_notes_two_syllables.mid").extract
34
+ lyrics = lyrics.collect{|x| { text: x.text, start: x.start, duration: x.duration } }
35
+ expect(lyrics).to eq([
36
+ { text: "Test", start: 0, duration: QUARTER_NOTE_DURATION },
37
+ { text: "ing ", start: 0.5, duration: QUARTER_NOTE_DURATION },
38
+ { text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, duration: 0 },
39
+ { text: "\n", start: 0.5 + QUARTER_NOTE_DURATION, duration: 0 }
40
+ ])
41
+ end
42
+
43
+ it "parses spaces_and_returns.mid correctly" do
44
+ lyrics = MidiLyrics::Parser.new("spec/fixtures/spaces_and_returns.mid").extract
45
+ lyrics = lyrics.collect{|x| { text: x.text, start: x.start, duration: x.duration } }
46
+ expect(lyrics).to eq([
47
+ { text: "Test", start: 0.5, duration: QUARTER_NOTE_DURATION },
48
+ { text: "ing ", start: 1, duration: QUARTER_NOTE_DURATION },
49
+ { text: "\r", start: 1 + QUARTER_NOTE_DURATION, duration: 0 },
50
+ { text: "One", start: 1.5, duration: HALF_NOTE_DURATION },
51
+ { text: "Two", start: 2.5, duration: HALF_NOTE_DURATION },
52
+ { text: "Three ", start: 3.5, duration: HALF_NOTE_DURATION },
53
+ { text: "\r", start: 3.5 + HALF_NOTE_DURATION, duration: 0 },
54
+ { text: "\n", start: 3.5 + HALF_NOTE_DURATION, duration: 0 }
55
+ ])
56
+ end
57
+ end
58
+
59
+ context "error handling" do
60
+ it "raises MidiLyrics::FileNotFound if file does not exist" do
61
+ expect { MidiLyrics::Parser.new("test.mid").extract }.to raise_error(MidiLyrics::FileNotFound)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require "simplecov"
8
+ require "coveralls"
9
+
10
+ class SimpleCov::Formatter::MergedFormatter
11
+ def format(result)
12
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
13
+ Coveralls::SimpleCov::Formatter.new.format(result)
14
+ end
15
+ end
16
+ SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
17
+ SimpleCov.start do
18
+ add_filter "spec"
19
+ end
20
+
21
+ require "midi_lyrics"
22
+
23
+ RSpec.configure do |config|
24
+ config.treat_symbols_as_metadata_keys_with_true_values = true
25
+ config.run_all_when_everything_filtered = true
26
+ config.filter_run :focus
27
+
28
+ # Run specs in random order to surface order dependencies. If you find an
29
+ # order dependency and want to debug it, you can fix the order by providing
30
+ # the seed, which is printed after each run.
31
+ # --seed 1234
32
+ config.order = 'random'
33
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: midi_lyrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mateus Del Bianco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: midilib
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ description: MIDI Lyrics extractor
70
+ email:
71
+ - mateus@delbianco.net.br
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .ruby-gemset
79
+ - .ruby-version
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/midi_lyrics.rb
86
+ - lib/midi_lyrics/version.rb
87
+ - midi_lyrics.gemspec
88
+ - spec/fixtures/complete_example.mid
89
+ - spec/fixtures/complete_example.nwc
90
+ - spec/fixtures/one_note_one_syllable.mid
91
+ - spec/fixtures/one_note_one_syllable.nwc
92
+ - spec/fixtures/repeating_lyrics.mid
93
+ - spec/fixtures/repeating_lyrics.nwc
94
+ - spec/fixtures/spaces_and_returns.mid
95
+ - spec/fixtures/spaces_and_returns.nwc
96
+ - spec/fixtures/two_notes_one_syllable.mid
97
+ - spec/fixtures/two_notes_one_syllable.nwc
98
+ - spec/fixtures/two_notes_two_syllables.mid
99
+ - spec/fixtures/two_notes_two_syllables.nwc
100
+ - spec/midi_lyrics_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/mateusdelbianco/midi_lyrics
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Extracts lyrics with timing from MIDI files
126
+ test_files:
127
+ - spec/fixtures/complete_example.mid
128
+ - spec/fixtures/complete_example.nwc
129
+ - spec/fixtures/one_note_one_syllable.mid
130
+ - spec/fixtures/one_note_one_syllable.nwc
131
+ - spec/fixtures/repeating_lyrics.mid
132
+ - spec/fixtures/repeating_lyrics.nwc
133
+ - spec/fixtures/spaces_and_returns.mid
134
+ - spec/fixtures/spaces_and_returns.nwc
135
+ - spec/fixtures/two_notes_one_syllable.mid
136
+ - spec/fixtures/two_notes_one_syllable.nwc
137
+ - spec/fixtures/two_notes_two_syllables.mid
138
+ - spec/fixtures/two_notes_two_syllables.nwc
139
+ - spec/midi_lyrics_spec.rb
140
+ - spec/spec_helper.rb