Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

[![Gem Version](https://badge.fury.io/rb/foxtracker.svg)](https://badge.fury.io/rb/foxtracker)

Foxtracker is a parser for tracker music formats. Right now it only supports XM
(FastTracker II) modules. Support for more formats is to be done.
Foxtracker is a parser for tracker music formats. It currently supports the
following formats:

- FastTracker II `.xm`
- Symphonie Pro `.SymMOD`

Support for more formats is to be done.

## Installation

Expand Down
7 changes: 4 additions & 3 deletions lib/foxtracker/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def initialize(argv)
end

def run
xm = Foxtracker::Parser.read(@filename, debug: true)
require "pp"
pp xm
mod = Foxtracker::Parser.read(@filename, debug: true)
puts "Parsing successful. Now play with it!"
rescue StandardError => e
raise
ensure
binding.irb
end
Expand Down
15 changes: 3 additions & 12 deletions lib/foxtracker/format/extended_module/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
require "dry-struct"

require "foxtracker/types"
require "foxtracker/format/support/dry_types_unspect"

module Foxtracker
module Format
class ExtendedModule < Dry::Struct
class Sample < Dry::Struct
include Foxtracker::Format::Support::DryTypesUnspect.new(:data)

attribute :sample_length, Types::Strict::Integer
attribute :sample_loop_start, Types::Strict::Integer
attribute :sample_loop_length, Types::Strict::Integer
Expand Down Expand Up @@ -37,18 +40,6 @@ def sample_type
def looping?
!sample_loop_length.zero?
end

# HACK: to remove :data from inspect output as sample data is really too spammy
# https://github.com/dry-rb/dry-struct/blob/cb41a5a03/lib/dry/struct.rb#L178
def inspect
klass = self.class
attrs = klass
.attribute_names
.reject { |key| key == :data }
.map { |key| " #{key}=#{@attributes[key].inspect}" }
.join
"#<#{klass.name || klass.inspect}#{attrs}>"
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/foxtracker/format/support/dry_types_unspect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Foxtracker
module Format
module Support
# HACK: this module builder removes certain attributes from the inspect
# output as e.g. sample data is really too spammy
class DryTypesUnspect < Module
def initialize(*field_names)
# https://github.com/dry-rb/dry-struct/blob/cb41a5a03/lib/dry/struct.rb#L178
define_method :inspect do
klass = self.class
attrs = klass
.attribute_names
.reject { |key| field_names.include?(key) }
.map { |key| " #{key}=#{@attributes[key].inspect}" }
.join
"#<#{klass.name || klass.inspect}#{attrs}>"
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/foxtracker/format/symphonie_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "dry-struct"

require "foxtracker/types"
require "foxtracker/format/symphonie_module/pattern"
require "foxtracker/format/symphonie_module/instrument"

module Foxtracker
module Format
class SymphonieModule < Dry::Struct
# Header
attribute :version_number, Types::Strict::Integer
attribute :number_of_channels, Types::Strict::Integer
attribute :number_of_patterns, Types::Strict::Integer
attribute :number_of_instruments, Types::Strict::Integer

attribute :info_text, Types::Strict::String

attribute :patterns, Types::Strict::Array.of(Pattern)
attribute :instruments, Types::Strict::Array.of(Instrument)
end
end
end
56 changes: 56 additions & 0 deletions lib/foxtracker/format/symphonie_module/instrument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "dry-struct"

require "foxtracker/types"
require "foxtracker/format/support/dry_types_unspect"

module Foxtracker
module Format
class SymphonieModule < Dry::Struct
class Instrument < Dry::Struct
include Foxtracker::Format::Support::DryTypesUnspect.new(:data)

attribute :name, Types::Strict::String.optional

attribute :instrument_type, Types::Strict::Integer.optional
attribute :loop_start, Types::Strict::Integer.optional
attribute :loop_length, Types::Strict::Integer.optional
attribute :loop_number, Types::Strict::Integer.optional
attribute :multi, Types::Strict::Integer.optional
attribute :auto_maximize, Types::Strict::Integer.optional
attribute :volume, Types::Strict::Integer.optional
attribute :relation, Types::Strict::Integer.optional
attribute :child_number, Types::Strict::Integer.optional
attribute :sample_type, Types::Strict::Integer.optional
attribute :finetune, Types::Strict::Integer.optional
attribute :tune, Types::Strict::Integer.optional
attribute :linesample_flags, Types::Strict::Integer.optional
attribute :filter, Types::Strict::Integer.optional
attribute :playflag, Types::Strict::Integer.optional
attribute :downsample, Types::Strict::Integer.optional
attribute :reso, Types::Strict::Integer.optional
attribute :loadflags, Types::Strict::Integer.optional
attribute :info, Types::Strict::Integer.optional
attribute :range_start, Types::Strict::Integer.optional
attribute :range_length, Types::Strict::Integer.optional
attribute :loop_start_low, Types::Strict::Integer.optional
attribute :loop_length_low, Types::Strict::Integer.optional

attribute :reso_filter_flags, Types::Strict::Integer.optional
attribute :reso_filter_numb, Types::Strict::Integer.optional
attribute :reso_filter, Types::Strict::Integer.optional

attribute :vfade_status, Types::Strict::Integer.optional
attribute :vfade_start, Types::Strict::Integer.optional
attribute :vfade_end, Types::Strict::Integer.optional

attribute :data, Types::Strict::Array.of(Types::Strict::Integer).optional.meta(omittable: true)

def empty?
name.nil? && data.nil?
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/foxtracker/format/symphonie_module/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "dry-struct"

require "foxtracker/types"

module Foxtracker
module Format
class SymphonieModule < Dry::Struct
class Note < Dry::Struct
attribute :type, Types::Strict::Symbol | Types::Strict::Integer
attribute :empty?, Types::Strict::Bool
attribute :note, Types::Strict::Integer
attribute :instrument, Types::Strict::Integer
attribute :volume, Types::Strict::Integer
attribute :volume_fx, Types::Strict::Symbol.optional
attribute :note_fx, Types::Strict::Symbol.optional
end
end
end
end
16 changes: 16 additions & 0 deletions lib/foxtracker/format/symphonie_module/pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "dry-struct"

require "foxtracker/types"
require "foxtracker/format/symphonie_module/note"

module Foxtracker
module Format
class SymphonieModule < Dry::Struct
class Pattern < Dry::Struct
attribute :channels, Types::Strict::Array.of(Types::Strict::Array.of(Note))
end
end
end
end
12 changes: 11 additions & 1 deletion lib/foxtracker/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require "foxtracker/errors"
require "foxtracker/parser/base"
require "foxtracker/parser/extended_module"
require "foxtracker/parser/symphonie_module"

module Foxtracker
module Parser
Expand All @@ -11,7 +14,14 @@ def read(filename, debug: false)
end

def parse(bin, debug: false)
ExtendedModule.parse(bin, debug: debug)
Foxtracker::Parser::Base.parsers.each do |klass|
puts "trying to parse with #{klass}... " if debug
return klass.parse(bin, debug: debug)
rescue Errors::WrongFormat
next
end

raise Errors::WrongFormat.new("there is no parser for this module format yet")
end
end
end
4 changes: 4 additions & 0 deletions lib/foxtracker/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def self.parse(*args)
new.parse(*args)
end

def self.parsers
ObjectSpace.each_object(Class).select { |klass| klass < self }
end

def parse(*_args)
raise NotImplementedError
end
Expand Down
Loading