Skip to content

Instantly share code, notes, and snippets.

@FiXato
Last active May 12, 2018
Embed
What would you like to do?
MoonBlaster (MSX Audio File Format) metadata extractor & renamer
#!/usr/bin/env ruby
# encoding: utf-8
#
# MoonBlaster (MSX Audio File Format) metadata extractor & renamer
#
# Extracts the song titles from MoonBlaster files, and copies them renamed to a new directory.
# If a sample kit is detected in the song file, a separate sub-directory will be created for it.
# If the matching wave, drum and/or sample kit is found, it will be copied to this sub-directory as well.
#
# Copyright © 2018, Filip H.F. "FiXato" Slagter
# See LICENSE file for license details
require 'fileutils'
require 'zaru'
require 'set'
# create a new target output directory 'RENAMED' in the current directory
BASE_TARGET_DIR = File.expand_path(File.join(__FILE__, '..', 'RENAMED'))
FileUtils.mkdir(BASE_TARGET_DIR) unless File.exist?(BASE_TARGET_DIR)
sample_kits = {
:missing => Set.new([]),
:known => Set.new([]),
:found => Set.new([]),
}
files = Dir.glob("*.MBM")
puts "Found #{files.size} MBM files:"
files.each do |fn|
open fn do |f|
base_filename, _, ext = File.basename(fn).rpartition('.')
f.seek 207 # Song title seems to start here
name = f.read(40) # Song title seems to be 40 bytes long, though according to https://www.msx.org/wiki/Moonblaster_file_format it's supposed to be 41?
f.rewind
f.seek 320 # Sample-kit name starts here
sample_kit_name = f.read(8)
sample_kit_name.gsub!(/^[^A-Za-z0-9]/, '')
sample_kit_name = sample_kit_name.to_s.strip
sample_kit_name = nil if sample_kit_name == 'NONE'
sample_kit_name = Zaru.sanitize!(sample_kit_name) if sample_kit_name
puts "#{fn}: #{name} | #{sample_kit_name}"
# Replace some common characters to Unicode alternatives so they can be displayed in the filename without interfering with filename conventions
sanitised_name = name.dup
# sanitised_name.gsub!("/", '⁄')
# sanitised_name.gsub!("/", '∕')
sanitised_name.gsub!("/", '/')
sanitised_name.gsub!(/\([cC]\)/, '©')
sanitised_name.gsub!(/\([rR]\)/, '®')
sanitised_name.gsub!(/\([pP]\)/, '℗')
sanitised_name.gsub!(".", '.')
sanitised_name.gsub!(":", '﹕')
sanitised_name = Zaru.sanitize!(sanitised_name)
# Create a sub-directory for the sample kit if it has one
target_dir = BASE_TARGET_DIR
if sample_kit_name
sample_kits[:known] << sample_kit_name
target_dir = File.join(target_dir, sample_kit_name)
FileUtils.mkdir_p(target_dir) unless File.exist?(target_dir)
found = false
%w[MBD MBK MWK].each do |sample_ext|
sample_kit_fn = "#{sample_kit_name}.#{sample_ext}"
sample_kit_target_path = File.join(target_dir, sample_kit_fn)
if File.exist?(sample_kit_fn)
found = true
sample_kits[:found] << sample_kit_name
FileUtils.cp(sample_kit_fn, sample_kit_target_path) unless File.exist?(sample_kit_target_path)
end
end
sample_kits[:missing] << sample_kit_name unless found
end
# Construct a new filename from the old filename, the sanitised name, and the file extension
new_filename = "#{base_filename} - #{sanitised_name}.#{ext}"
target_path = File.join(target_dir, new_filename)
# Copy the original file to the target output directory with its new sanitised name
unless File.exist?(target_path)
puts "Copying #{fn} to #{target_path}"
FileUtils.cp(fn, target_path)
end
end
end
puts "Identified #{sample_kits[:known].size} sample kits:"
puts sample_kits[:known].sort
puts "Found #{sample_kits[:known].size} of them:"
puts sample_kits[:found].sort
puts "Missing #{sample_kits[:missing].size} sample files:"
puts sample_kits[:missing].sort
MIT License
Copyright © 2018, Filip H.F. "FiXato" Slagter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment