jamming 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "rmagick"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.1"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.1)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ rmagick (2.13.1)
12
+ shoulda (2.11.3)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.1)
20
+ rcov
21
+ rmagick
22
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Andrés Mejía
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ jamming
2
+ ===
3
+
4
+ Jamming is a Ruby library to generate images of guitar chords.
5
+
6
+ Installation
7
+ ---
8
+
9
+ gem install jamming
10
+
11
+ or if you are using Bundler, add this line to your Gemfile:
12
+
13
+ gem "jamming"
14
+
15
+ Usage
16
+ ---
17
+
18
+ File.open("/tmp/old_good_jimi.png", 'w') do |f|
19
+ chord = Jamming::Chord.new("0-7-6-7-8-x")
20
+ f.write chord.to_png(:label => "Em7#9")
21
+ end
22
+
23
+ This will write Em7#9, <em>The Jimi Hendrix Chord</em>, to <tt>tmp/old_good_jimi.png</tt>:
24
+
25
+ ![Em7#9 - The Jimi Hendrix Chord](https://github.com/andmej/jamming/raw/master/examples/Em7%239.png)
26
+
27
+ Thanks
28
+ ---
29
+
30
+ Some code based on [ander/chords](https://github.com/ander/chords), thanks!
31
+
32
+ Contributing to jamming
33
+ ---
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ Copyright
44
+ ---
45
+
46
+ Copyright (c) 2010 Andrés Mejía. See LICENSE.txt for
47
+ further details.
48
+
49
+ Other important notes
50
+ ---
51
+
52
+ You suck if you don't play guitar, kthxbai.
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "jamming"
17
+ gem.homepage = "http://github.com/andmej@gmail.com/jamming"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{A Ruby library to generate images of guitar chords}
20
+ gem.description = %Q{A Ruby library to generate images of guitar chords}
21
+ gem.email = "andmej@gmail.com"
22
+ gem.authors = ["Andrés Mejía"]
23
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
24
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
25
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
26
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "jamming #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
Binary file
@@ -0,0 +1,2 @@
1
+ require 'jamming/chord'
2
+ require 'jamming/png_formatter'
@@ -0,0 +1,23 @@
1
+ require 'jamming/png_formatter'
2
+
3
+ module Jamming
4
+ class Chord
5
+ attr_reader :frets
6
+
7
+ def initialize(chord_as_string)
8
+ @frets = parse(chord_as_string)
9
+ end
10
+
11
+ def to_png(options = {})
12
+ Jamming::PNGFormatter.new(frets).print(options)
13
+ end
14
+
15
+ protected
16
+
17
+ def parse(chord)
18
+ frets = chord.split(chord =~ /-/ ? "-" : "")
19
+ raise ArgumentError.new("'#{chord}' is not a valid chord expression.") unless frets.size == 6
20
+ frets.collect { |f| f =~ /[0-9]+/ ? f.to_i : nil }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,108 @@
1
+ require 'rvg/rvg' # rmagick's RVG (Ruby Vector Graphics)
2
+
3
+ module Jamming
4
+ Magick::RVG::dpi = 72
5
+ # Formats a single fingering as png data
6
+ class PNGFormatter
7
+
8
+ def initialize(frets)
9
+ @frets = frets
10
+ @strings = ["E", "A", "D", "G", "B", "e"]
11
+ end
12
+
13
+ def print(options={})
14
+ @label = options[:label]
15
+
16
+ @max_fret = @frets.compact.max
17
+ @min_fret = @frets.compact.delete_if { |f| f == 0 }.min
18
+ @min_fret = 1 if @max_fret <= 4
19
+
20
+ @number_of_frets = [@max_fret - @min_fret + 1, 4].max
21
+
22
+ get_png_data
23
+ end
24
+
25
+ private
26
+
27
+ def get_png_data
28
+ width = 400
29
+ height = 300
30
+
31
+ rvg = Magick::RVG.new(280, 210).viewbox(0,0,width,height) do |canvas|
32
+ canvas.background_fill = 'white'
33
+ x_div = @strings.size - 1
34
+
35
+ width_of_chord = 260
36
+ margin_side_of_chord = (width - width_of_chord) / 2
37
+
38
+ height_of_chord = 200
39
+ margin_top_of_chord = ((height - height_of_chord) * 2 / 3.0).floor
40
+ margin_bottom_of_chord = ((height - height_of_chord) / 3.0).ceil
41
+
42
+ height_of_fret = height_of_chord / @number_of_frets
43
+ radius_of_finger = (height_of_fret * 0.6) / 2
44
+
45
+ width_of_fret = width_of_chord / x_div
46
+
47
+ # Draw all horizontal lines
48
+ (@number_of_frets+1).times do |n|
49
+ canvas.line(margin_side_of_chord, n*height_of_fret+margin_top_of_chord, width - margin_side_of_chord, n*height_of_fret+margin_top_of_chord)
50
+ end
51
+
52
+ (@number_of_frets).times do |i|
53
+ canvas.text(margin_side_of_chord - radius_of_finger - 4, i*height_of_fret+margin_top_of_chord + height_of_fret / 2 + 10) do |txt|
54
+ txt.tspan(@min_fret + i).styles(
55
+ :text_anchor => 'end',
56
+ :font_size => 24,
57
+ :font_family => 'helvetica',
58
+ :fill => 'black')
59
+ end
60
+ end
61
+
62
+ @strings.each_with_index do |note, i|
63
+ # Draw vertical lines
64
+ canvas.line(i*width_of_fret+margin_side_of_chord, margin_top_of_chord, i*width_of_fret+margin_side_of_chord, height - margin_bottom_of_chord)
65
+
66
+
67
+ if [0,nil].include?(@frets[i])
68
+ # Add a text at the top. Either an X or O
69
+ canvas.text(i*width_of_fret+margin_side_of_chord, margin_top_of_chord - 6) do |txt|
70
+ txt.tspan((@frets[i] == 0 ? "O" : 'X').to_s).styles(
71
+ :text_anchor => 'middle',
72
+ :font_size => 24,
73
+ :font_family => 'helvetica',
74
+ :fill => 'black')
75
+ end
76
+ else
77
+ # Add a finger
78
+ canvas.circle(radius_of_finger, i*width_of_fret+margin_side_of_chord,
79
+ (@frets[i] - @min_fret + 1)*height_of_fret - (height_of_fret / 2) + margin_top_of_chord)
80
+ end
81
+
82
+ canvas.text(i*width_of_fret+margin_side_of_chord, height - margin_bottom_of_chord + 20) do |txt|
83
+ txt.tspan(note).styles(:text_anchor => 'middle',
84
+ :font_size => 18,
85
+ :font_family => 'helvetica',
86
+ :fill => 'black')
87
+ end
88
+ end
89
+
90
+ if @label
91
+ canvas.text(width / 2, margin_top_of_chord / 2) do |txt|
92
+ txt.tspan(@label).styles(:text_anchor => 'middle',
93
+ :font_size => 36,
94
+ :font_family => 'helvetica',
95
+ :fill => 'black',
96
+ :font_weight => 'bold',
97
+ )
98
+ end
99
+ end
100
+
101
+ end
102
+ img = rvg.draw
103
+ img.format = 'PNG'
104
+ img.to_blob
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'jamming'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestJamming < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jamming
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Andr\xC3\xA9s Mej\xC3\xADa"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-20 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rmagick
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ prerelease: false
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: shoulda
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: &id003 !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 0
56
+ - 0
57
+ version: 1.0.0
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: jeweler
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 5
71
+ - 1
72
+ version: 1.5.1
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: rcov
78
+ requirement: &id005 !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ description: A Ruby library to generate images of guitar chords
90
+ email: andmej@gmail.com
91
+ executables: []
92
+
93
+ extensions: []
94
+
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.markdown
98
+ files:
99
+ - .document
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.markdown
104
+ - Rakefile
105
+ - VERSION
106
+ - examples/Em7#9.png
107
+ - lib/jamming.rb
108
+ - lib/jamming/chord.rb
109
+ - lib/jamming/png_formatter.rb
110
+ - test/helper.rb
111
+ - test/test_jamming.rb
112
+ has_rdoc: true
113
+ homepage: http://github.com/andmej@gmail.com/jamming
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: -2157425641403757243
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.3.7
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: A Ruby library to generate images of guitar chords
145
+ test_files:
146
+ - test/helper.rb
147
+ - test/test_jamming.rb