famitracker_parser 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
+ SHA256:
3
+ metadata.gz: 3600d6dcc1556a9756f1b6280ab55988f6b324d79e56d516b29a43a4633bb98d
4
+ data.tar.gz: 2e01a37abbf997f99b9a27309a443b309fdc8b4ae33c9a39defc4839b63d5d1a
5
+ SHA512:
6
+ metadata.gz: 5e19ce7271653340355c9a17af6dd3e0760a40866bf2dd75033dcfbc19afebb6f730c5adf4331ba2dc2cb9afef89f192bca68da666118eff414d7ce998085c96
7
+ data.tar.gz: d4004452ca7b9bba6a7e80b2d9670763acfdd357d8f3ddc3d5d6b0327497de8cc9c2996b7d8ce44481ca884c93eee1a290438152e9733c83e6139f6710700a70
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-26
4
+
5
+ - Initial release
6
+ - Parses FamiTracker and Dn-FamiTracker exports, with 2A03 and VRC7 instruments
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wendelscardua@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in famitracker_parser.gemspec
6
+ gemspec
7
+
8
+ gem 'pry'
9
+
10
+ gem "rake", "~> 13.0"
11
+
12
+ gem "rspec", "~> 3.0"
13
+
14
+ gem "rspec-file_fixtures", "~> 0.1.6"
15
+
16
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ famitracker_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.2)
13
+ method_source (1.0.0)
14
+ parallel (1.22.1)
15
+ parser (3.1.2.1)
16
+ ast (~> 2.4.1)
17
+ pry (0.14.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.5.0)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-file_fixtures (0.1.6)
34
+ rspec (~> 3.0)
35
+ rspec-mocks (3.11.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-support (3.11.0)
39
+ rubocop (1.33.0)
40
+ json (~> 2.3)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.1.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.19.1, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 3.0)
49
+ rubocop-ast (1.21.0)
50
+ parser (>= 3.1.1.0)
51
+ ruby-progressbar (1.11.0)
52
+ unicode-display_width (2.2.0)
53
+
54
+ PLATFORMS
55
+ x86_64-linux
56
+
57
+ DEPENDENCIES
58
+ famitracker_parser!
59
+ pry
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rspec-file_fixtures (~> 0.1.6)
63
+ rubocop (~> 1.21)
64
+
65
+ BUNDLED WITH
66
+ 2.3.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Wendel Scardua
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # FamitrackerParser
2
+
3
+ A ruby parser for text files exported from FamiTracker (and its forks)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'famitracker_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install famitracker_parser
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'famitracker_parser'
25
+ song = FamiTrackerParser::Parser.parse_file! 'my-exported-song.txt'
26
+ puts "This is #{song.song_information.title} by #{song.song_information.author}"
27
+ puts "These are the DPCM samples: #{song.dpcm_samples.map(&:name).join(', ')}"
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wendelscardua/famitracker_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/wendelscardua/famitracker_parser/blob/master/CODE_OF_CONDUCT.md).
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
43
+
44
+ ## Code of Conduct
45
+
46
+ Everyone interacting in the FamitrackerParser project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/wendelscardua/famitracker_parser/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "famitracker_parser"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/famitracker_parser/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "famitracker_parser"
7
+ spec.version = FamitrackerParser::VERSION
8
+ spec.authors = ["Wendel Scardua"]
9
+ spec.email = ["wendelscardua@gmail.com"]
10
+
11
+ spec.summary = "Ruby FamiTracker text parser."
12
+ spec.description = "Ruby gem for parsing text files exported from FamiTracker"
13
+ spec.homepage = "http://github.com/wendelscardua/famitracker_parser"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FamitrackerParser
4
+ class ArgsTokenizer
5
+ def initialize(string)
6
+ @tokens = string.chars
7
+ end
8
+
9
+ def read(type)
10
+ drop_spaces
11
+
12
+ return nil if @tokens.empty?
13
+
14
+ case type
15
+ when :string then read_string
16
+ when :decimal then read_decimal
17
+ when :hexadecimal then read_hexadecimal
18
+ when :rest then read_rest
19
+ when String then read_literal(type)
20
+ else
21
+ if (type.is_a?(Hash) && (subtype = type[:array]))
22
+ read_array(subtype)
23
+ else
24
+ raise FamitrackerParser::ParserError, "Invalid type: #{type.inspect}"
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def drop_spaces
32
+ @tokens.shift while @tokens.first == " "
33
+ end
34
+
35
+ def read_literal(text)
36
+ text_tokens = text.chars
37
+ while !text_tokens.empty? && !@tokens.empty? &&
38
+ text_tokens.first == @tokens.first
39
+ text_tokens.shift
40
+ @tokens.shift
41
+ end
42
+
43
+ raise FamitrackerParser::ParserError, "Literal doesn't match: #{text}" unless text_tokens.empty?
44
+ end
45
+
46
+ def read_string
47
+ raise FamitrackerParser::ParserError, "Invalid string" if @tokens.shift != '"'
48
+
49
+ string = ""
50
+ string += @tokens.shift while @tokens.first && @tokens.first != '"'
51
+
52
+ raise FamitrackerParser::ParserError, "Invalid string" if @tokens.shift != '"'
53
+
54
+ string
55
+ end
56
+
57
+ def read_decimal
58
+ decimal = ""
59
+
60
+ while !@tokens.empty? && @tokens.first != " "
61
+ digit = @tokens.shift
62
+ raise ParserError, "Invalid decimal digit '#{digit}'" unless digit =~ /[-+\d]/
63
+
64
+ decimal += digit
65
+ end
66
+
67
+ decimal.to_i
68
+ end
69
+
70
+ def read_hexadecimal
71
+ hexadecimal = ""
72
+
73
+ while !@tokens.empty? && @tokens.first != " "
74
+ digit = @tokens.shift
75
+ raise ParserError, "Invalid hexadecimal digit '#{digit}'" unless digit =~ /[-+\dA-F]/
76
+
77
+ hexadecimal += digit
78
+ end
79
+
80
+ hexadecimal.to_i(16)
81
+ end
82
+
83
+ def read_rest
84
+ rest = @tokens.join
85
+
86
+ @tokens = []
87
+
88
+ rest
89
+ end
90
+
91
+ def read_array(type)
92
+ arr = []
93
+ until (value = read(type)).nil?
94
+ arr << value
95
+ end
96
+ arr
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FamitrackerParser
4
+ class Groove
5
+ attr_accessor :id, :groove
6
+ end
7
+ end
@@ -0,0 +1,292 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "song"
4
+ require_relative "row_channel"
5
+
6
+ module FamitrackerParser
7
+ # The Parser itself
8
+ class Parser
9
+ def initialize(source)
10
+ @source = source
11
+ @current_line = 0
12
+ end
13
+
14
+ def parse!
15
+ song = Song.new
16
+ song.song_information = SongInformation.new
17
+ song.song_comments = []
18
+ song.global_settings = GlobalSettings.new
19
+ song.macros = []
20
+ song.dpcm_samples = []
21
+ song.instruments = []
22
+ song.tracks = []
23
+ song.extras = {}
24
+
25
+ @source.lines.map(&:chomp).each.with_index do |line, line_number|
26
+ @current_line = line_number + 1
27
+ if (description = export_description(line))
28
+ song.export_description = description
29
+ elsif comment(line) || empty(line)
30
+ next
31
+ elsif (command, args = famitracker_command(line))
32
+ case command
33
+ # Song information
34
+ when "AUTHOR"
35
+ song.song_information
36
+ .author = parse_arg(args, :string)
37
+ when "COPYRIGHT"
38
+ song.song_information
39
+ .copyright = parse_arg(args, :string)
40
+ when "TITLE"
41
+ song.song_information
42
+ .title = parse_arg(args, :string)
43
+ # Song comment
44
+ when "COMMENT"
45
+ song.song_comments << parse_arg(args, :string)
46
+ # Global settings
47
+ when "MACHINE"
48
+ song.global_settings
49
+ .machine = parse_arg(args, :decimal)
50
+ when "FRAMERATE"
51
+ song.global_settings
52
+ .framerate = parse_arg(args, :decimal)
53
+ when "EXPANSION"
54
+ song.global_settings
55
+ .expansion = parse_arg(args, :decimal)
56
+ when "VIBRATO"
57
+ song.global_settings
58
+ .vibrato = parse_arg(args, :decimal)
59
+ when "SPLIT"
60
+ song.global_settings
61
+ .split = parse_arg(args, :decimal)
62
+ when "N163CHANNELS"
63
+ song.global_settings
64
+ .n163_channels = parse_arg(args, :decimal)
65
+ # Macros
66
+ when "MACRO"
67
+ song.macros << Macro.new.tap do |macro|
68
+ parsed = parse_args(args,
69
+ {
70
+ type: :decimal,
71
+ id: :decimal,
72
+ loop_index: :decimal,
73
+ release_index: :decimal,
74
+ arpeggio_type: :decimal,
75
+ separator1: ":",
76
+ values: { array: :decimal }
77
+ })
78
+ macro.type = parsed[:type]
79
+ macro.id = parsed[:id]
80
+ macro.loop_index = parsed[:loop_index]
81
+ macro.release_index = parsed[:release_index]
82
+ macro.arpeggio_type = parsed[:arpeggio_type]
83
+ macro.values = parsed[:values]
84
+ end
85
+ # DPCM samples
86
+ when "DPCMDEF"
87
+ song.dpcm_samples << DPCMSample.new.tap do |dpcm_sample|
88
+ parsed = parse_args(args,
89
+ {
90
+ id: :decimal,
91
+ size: :decimal,
92
+ name: :string
93
+ })
94
+ dpcm_sample.id = parsed[:id]
95
+ dpcm_sample.size = parsed[:size]
96
+ dpcm_sample.name = parsed[:name]
97
+ dpcm_sample.bytes = []
98
+ end
99
+ when "DPCM"
100
+ parsed = parse_args(args,
101
+ {
102
+ separator: ":",
103
+ bytes: { array: :hexadecimal }
104
+ })
105
+ song.dpcm_samples.last.bytes += parsed[:bytes]
106
+ # Instruments
107
+ when "INST2A03"
108
+ parsed = parse_args(args,
109
+ {
110
+ id: :decimal,
111
+ volume_macro: :decimal,
112
+ arpeggio_macro: :decimal,
113
+ pitch_macro: :decimal,
114
+ hi_pitch_macro: :decimal,
115
+ duty_noise_macro: :decimal,
116
+ name: :string
117
+ })
118
+ song.instruments << Instrument2A03.new.tap do |instrument|
119
+ instrument.id = parsed[:id]
120
+ instrument.volume_macro = parsed[:volume_macro]
121
+ instrument.arpeggio_macro = parsed[:arpeggio_macro]
122
+ instrument.pitch_macro = parsed[:pitch_macro]
123
+ instrument.hi_pitch_macro = parsed[:hi_pitch_macro]
124
+ instrument.duty_noise_macro = parsed[:duty_noise_macro]
125
+ instrument.name = parsed[:name]
126
+ instrument.dpcm_keys = []
127
+ end
128
+ when "KEYDPCM"
129
+ parsed = parse_args(args,
130
+ {
131
+ instrument_id: :decimal,
132
+ octave: :decimal,
133
+ note: :decimal,
134
+ dpcm_sample_id: :decimal,
135
+ pitch: :decimal,
136
+ loop: :decimal,
137
+ loop_point: :decimal,
138
+ delta_counter: :decimal
139
+ })
140
+ song.instruments.last.dpcm_keys << DPCMKey.new.tap do |dpcm_key|
141
+ dpcm_key.octave = parsed[:octave]
142
+ dpcm_key.note = parsed[:note]
143
+ dpcm_key.dpcm_sample_id = parsed[:dpcm_sample_id]
144
+ dpcm_key.pitch = parsed[:pitch]
145
+ dpcm_key.loop = parsed[:loop]
146
+ dpcm_key.loop_point = parsed[:loop_point]
147
+ dpcm_key.delta_counter = parsed[:delta_counter]
148
+ end
149
+ when "INSTVRC7"
150
+ parsed = parse_args(args, {
151
+ id: :decimal,
152
+ patch: :decimal,
153
+ r0: :hexadecimal,
154
+ r1: :hexadecimal,
155
+ r2: :hexadecimal,
156
+ r3: :hexadecimal,
157
+ r4: :hexadecimal,
158
+ r5: :hexadecimal,
159
+ r6: :hexadecimal,
160
+ r7: :hexadecimal,
161
+ name: :string
162
+ })
163
+ song.instruments << InstrumentVRC7.new.tap do |instrument|
164
+ instrument.id = parsed[:id]
165
+ instrument.patch = parsed[:patch]
166
+ instrument.patch_registers = %i[r0 r1 r2 r3 r4 r5 r6 r7].map { |r| parsed[r] }
167
+ instrument.name = parsed[:name]
168
+ end
169
+ # Tracks
170
+ when "TRACK"
171
+ parsed = parse_args(args,
172
+ {
173
+ rows: :decimal,
174
+ speed: :decimal,
175
+ tempo: :decimal,
176
+ name: :string
177
+ })
178
+ song.tracks << Track.new.tap do |track|
179
+ track.rows = parsed[:rows]
180
+ track.speed = parsed[:speed]
181
+ track.tempo = parsed[:tempo]
182
+ track.name = parsed[:name]
183
+ track.pattern_order = []
184
+ track.patterns = []
185
+ end
186
+ when "COLUMNS"
187
+ # it's just a list of how many effects per column, no need to parse it
188
+ next
189
+ when "ORDER"
190
+ parsed = parse_args(args, {
191
+ frame: :hexadecimal,
192
+ separator: ":",
193
+ patterns: { array: :hexadecimal }
194
+ })
195
+ song.tracks.last.pattern_order << parsed[:patterns]
196
+ when "PATTERN"
197
+ song.tracks.last.patterns << Pattern.new.tap do |pattern|
198
+ pattern.id = parse_arg(args, :hexadecimal)
199
+ pattern.rows = []
200
+ end
201
+ when "ROW"
202
+ parsed = parse_args(args, {
203
+ id: :hexadecimal,
204
+ rest: :rest
205
+ })
206
+ song.tracks.last.patterns.last.rows << Row.new.tap do |row|
207
+ row.id = parsed[:id]
208
+ row.channels = RowChannel.parse_all(parsed[:rest])
209
+ end
210
+ ### Dn-Famitracker
211
+ # Grooves
212
+ when "GROOVE"
213
+ dn_famitracker_extras = (song.extras[:dn_famitracker] ||= {})
214
+ (dn_famitracker_extras[:grooves] ||= []) << Groove.new.tap do |groove|
215
+ parsed = parse_args(args, {
216
+ id: :decimal,
217
+ size: :decimal,
218
+ separator: ":",
219
+ groove: { array: :decimal }
220
+ })
221
+ groove.id = parsed[:id]
222
+ groove.groove = parsed[:groove]
223
+ end
224
+ # Tracks using default groove
225
+ when "USEGROOVE"
226
+ dn_famitracker_extras = (song.extras[:dn_famitracker] ||= {})
227
+ parsed = parse_args(args, {
228
+ separator: ":",
229
+ tracks: { array: :decimal }
230
+ })
231
+ dn_famitracker_extras[:default_groove_tracks] = parsed[:tracks]
232
+ else
233
+ raise ParserError, "Unknown command '#{command}' at line #{@current_line}"
234
+ end
235
+ else
236
+ raise ParserError, "Invalid text at line #{@current_line}"
237
+ end
238
+ end
239
+
240
+ song
241
+ end
242
+
243
+ def self.parse_file!(path)
244
+ content = File.open(path, "r:iso-8859-1:utf-8", &:read)
245
+ Parser.new(content).parse!
246
+ end
247
+
248
+ private
249
+
250
+ def empty(line)
251
+ line == ""
252
+ end
253
+
254
+ def comment(line)
255
+ line.start_with?("#")
256
+ end
257
+
258
+ def parse_arg(args, type)
259
+ tokenizer = ArgsTokenizer.new(args.strip)
260
+ tokenizer.read(type)
261
+ end
262
+
263
+ def parse_args(args, types)
264
+ tokenizer = ArgsTokenizer.new(args.strip)
265
+
266
+ output = {}
267
+
268
+ types.each do |name, type|
269
+ output[name] = tokenizer.read(type)
270
+ end
271
+
272
+ output
273
+ end
274
+
275
+ def famitracker_command(line)
276
+ return unless (match = line.match(/\A(?<command>[A-Z0-9]+)\s+(?<args>.*)\z/))
277
+
278
+ [match["command"], match["args"]]
279
+ end
280
+
281
+ def export_description(line)
282
+ return unless (match = line.match(/\A# (?<program>.*) text export (?<version>.*)\z/))
283
+
284
+ ExportDescription.new.tap do |export_description|
285
+ export_description.program = match["program"]
286
+ export_description.version = match["version"]
287
+ end
288
+ end
289
+ end
290
+
291
+ ParserError = Class.new(StandardError)
292
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FamitrackerParser
4
+ class RowChannel
5
+ NOTE_OCTAVE_REGEX = /(?:\.\.\.|---|===|[0-9ABCDEFG][-#][0-9#])/.freeze
6
+ EFFECT_REGEX = /(\.\.\.|[0-9A-Z][0-9A-F][0-9A-F])/.freeze
7
+ ROW_CHANNEL_REGEX = /:
8
+ \s(?<note_octave>#{NOTE_OCTAVE_REGEX})
9
+ \s(?<instrument>[.0-9A-F]{2})
10
+ \s(?<volume>[.0-9A-F])
11
+ \s(?<effects>#{EFFECT_REGEX}(?:\s#{EFFECT_REGEX})*)/x.freeze
12
+
13
+ attr_reader :note_octave,
14
+ :instrument,
15
+ :volume,
16
+ :effects
17
+
18
+ def initialize(note_octave:, instrument:, volume:, effects:)
19
+ @note_octave = note_octave
20
+ @instrument = instrument
21
+ @volume = volume
22
+ @effects = effects
23
+ end
24
+
25
+ def self.from_hash(hash)
26
+ (@memoized_row_channels ||= {})[hash] ||=
27
+ RowChannel.new(
28
+ note_octave: hash["note_octave"].then do |note_octave|
29
+ case note_octave
30
+ when "..." then nil
31
+ when "---" then :halt
32
+ when "===" then :release
33
+ else note_octave
34
+ end
35
+ end,
36
+ instrument: hash["instrument"].then do |instrument|
37
+ if instrument == ".."
38
+ nil
39
+ else
40
+ instrument.to_i(16)
41
+ end
42
+ end,
43
+ volume: hash["volume"].then do |volume|
44
+ if volume == "."
45
+ nil
46
+ else
47
+ volume.to_i(16)
48
+ end
49
+ end,
50
+ effects: hash["effects"].then do |effects|
51
+ effects.scan(EFFECT_REGEX).map do |(effect_match)|
52
+ if effect_match == "..."
53
+ nil
54
+ else
55
+ Effect.new.tap do |effect|
56
+ effect.command = effect_match[0]
57
+ effect.argument = effect_match[1..]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ )
63
+ end
64
+
65
+ def self.parse_all(row_channel_string)
66
+ row_channel_string.scan(ROW_CHANNEL_REGEX)
67
+ .map { |match| ROW_CHANNEL_REGEX.names.zip(match).to_h }
68
+ .map do |row_channel_data|
69
+ RowChannel.from_hash(row_channel_data)
70
+ end
71
+ end
72
+ end
73
+
74
+ class Effect
75
+ attr_accessor :command, :argument
76
+ end
77
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FamitrackerParser
4
+ class Song
5
+ attr_accessor :export_description,
6
+ :song_information,
7
+ :song_comments,
8
+ :global_settings,
9
+ :macros,
10
+ :dpcm_samples,
11
+ :instruments,
12
+ :tracks,
13
+ :extras
14
+ end
15
+
16
+ class ExportDescription
17
+ attr_accessor :program, :version
18
+ end
19
+
20
+ class SongInformation
21
+ attr_accessor :title, :author, :copyright
22
+ end
23
+
24
+ class GlobalSettings
25
+ EXPANSIONS = [
26
+ [1, "Konami VRC6"],
27
+ [2, "Konami VRC7"],
28
+ [4, "Nintendo FDS sound"],
29
+ [8, "Nintendo MMC5"],
30
+ [16, "Namco 163"]
31
+ ].freeze
32
+
33
+ attr_accessor :machine,
34
+ :framerate,
35
+ :expansion,
36
+ :vibrato,
37
+ :split,
38
+ :n163_channels
39
+
40
+ def expansion_description
41
+ if @expansion == 0
42
+ ["NES channels only"]
43
+ else
44
+ EXPANSIONS.reject { |(key, _value)| (@expansion & key) == 0 }
45
+ .map { |_key, value| value }
46
+ end
47
+ end
48
+
49
+ def vibrato_description
50
+ case @vibrato
51
+ when 0 then "Old style (bend up)"
52
+ when 1 then "New style (bend up & down)"
53
+ end
54
+ end
55
+ end
56
+
57
+ class Macro
58
+ attr_accessor :type,
59
+ :id,
60
+ :loop_index,
61
+ :release_index,
62
+ :arpeggio_type,
63
+ :values
64
+
65
+ def type_description
66
+ case @type
67
+ when 0 then "Volume"
68
+ when 1 then "Arpeggio"
69
+ when 2 then "Pitch"
70
+ when 3 then "Hi-Pitch"
71
+ when 4 then "Duty / Noise"
72
+ end
73
+ end
74
+
75
+ def arpeggio_type_description
76
+ return unless @type == 1
77
+
78
+ case @arpeggio_type
79
+ when 0 then "Absolute"
80
+ when 1 then "Fixed"
81
+ when 2 then "Relative"
82
+ end
83
+ end
84
+ end
85
+
86
+ class DPCMSample
87
+ attr_accessor :id,
88
+ :size,
89
+ :name,
90
+ :bytes
91
+ end
92
+
93
+ class Instrument2A03
94
+ attr_accessor :id,
95
+ :volume_macro,
96
+ :arpeggio_macro,
97
+ :pitch_macro,
98
+ :hi_pitch_macro,
99
+ :duty_noise_macro,
100
+ :name,
101
+ :dpcm_keys
102
+ end
103
+
104
+ class InstrumentVRC7
105
+ attr_accessor :id,
106
+ :patch,
107
+ :patch_registers,
108
+ :name
109
+ end
110
+
111
+ class DPCMKey
112
+ attr_accessor :octave,
113
+ :note,
114
+ :dpcm_sample_id,
115
+ :pitch,
116
+ :loop,
117
+ :loop_point,
118
+ :delta_counter
119
+ end
120
+
121
+ class Track
122
+ attr_accessor :rows,
123
+ :speed,
124
+ :tempo,
125
+ :name,
126
+ :pattern_order,
127
+ :patterns
128
+ end
129
+
130
+ class Pattern
131
+ attr_accessor :id,
132
+ :rows
133
+ end
134
+
135
+ class Row
136
+ attr_accessor :id,
137
+ :channels
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FamitrackerParser
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "famitracker_parser/args_tokenizer"
4
+ require_relative "famitracker_parser/dn_famitracker_extras"
5
+ require_relative "famitracker_parser/parser"
6
+ require_relative "famitracker_parser/version"
7
+
8
+ # Main Famitracker parser module
9
+ module FamitrackerParser
10
+ end
@@ -0,0 +1,4 @@
1
+ module FamitrackerParser
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: famitracker_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Wendel Scardua
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby gem for parsing text files exported from FamiTracker
14
+ email:
15
+ - wendelscardua@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - famitracker_parser.gemspec
32
+ - lib/famitracker_parser.rb
33
+ - lib/famitracker_parser/args_tokenizer.rb
34
+ - lib/famitracker_parser/dn_famitracker_extras.rb
35
+ - lib/famitracker_parser/parser.rb
36
+ - lib/famitracker_parser/row_channel.rb
37
+ - lib/famitracker_parser/song.rb
38
+ - lib/famitracker_parser/version.rb
39
+ - sig/famitracker_parser.rbs
40
+ homepage: http://github.com/wendelscardua/famitracker_parser
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: http://github.com/wendelscardua/famitracker_parser
45
+ source_code_uri: http://github.com/wendelscardua/famitracker_parser
46
+ changelog_uri: http://github.com/wendelscardua/famitracker_parser/blob/master/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.6.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.2.15
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Ruby FamiTracker text parser.
66
+ test_files: []