Mod Archive Forums

Music Production => Tracking => Topic started by: ZedFox on January 28, 2017, 11:03:39

Title: Reversing a .xm file?
Post by: ZedFox on January 28, 2017, 11:03:39
Is it possible? I'd like to know because instead of reversing my song manually I'd like a switch to reverse the whole song but keep the samples not reversed.
Title: Re: Reversing a .xm file?
Post by: Saga Musix on January 28, 2017, 12:54:11
It sure is possible. I have written a tool years ago that reads the OpenMPT clipboard data and then reverses it. I can see if I can find it if you're interested. Back then OpenMPT could only copy one pattern at a time but these days it would be possible to process all patterns in one go.
Title: Re: Reversing a .xm file?
Post by: ZedFox on January 29, 2017, 06:25:59
It sure is possible. I have written a tool years ago that reads the OpenMPT clipboard data and then reverses it. I can see if I can find it if you're interested. Back then OpenMPT could only copy one pattern at a time but these days it would be possible to process all patterns in one go.
I'm definitely interested, I'd like to see what it is capable of!
Title: Re: Reversing a .xm file?
Post by: Saga Musix on January 30, 2017, 00:30:53
Okay, I just rewrote this whole thing in Python and now it can handle more than one pattern. Here's the Python 3 script, reverse.py:

Code: [Select]
#!/usr/bin/python3
# OpenMPT clipboard reverser by Saga Musix in 2017 (Public Domain / CC0)

import sys

if(len(sys.argv) != 3):
    print("Usage: " + sys.argv[0] + " infile outfile")
    sys.exit()

lines = list(open(sys.argv[1]))
f = open(sys.argv[2], 'w')

# ModPlug signature
f.write(lines.pop(0))

# Reverse order list if present
if(lines[0][:8] == "Orders: "):
    f.write("Orders: " + ",".join(reversed(lines[0][8:-1].split(","))) + "\n")
    lines.pop(0)

# Reverse patterns
pattern = list()
for line in lines:
    if(line[0] == "|"):
        pattern.append(line)
    else:
        f.write("".join(reversed(pattern)))
        pattern = list()
        f.write(line)
f.write("".join(reversed(pattern)))
You will need Python 3 to run this, typically this comes pre-installed on Linux, on Windows you have to download the Python interpreter first.

To use it, copy some pattern data or multiple patterns in OpenMPT to the clipboard. Paste it into a text editor and save it, then run "python reverse.py infile.txt outfile.txt" on the command line (substitute the names of those text files of course). Then open the output file in a text editor, copy it and paste it back into OpenMPT.

Note: If you copy more than one pattern, current OpenMPT versions will get a bit confused. You need the latest test version (r7547 or later, soon available from https://buildbot.openmpt.org/) in that case so that the clipboard data is parsed correctly.

I hope this helps! It's not very luxurious but then again this is probably not a feature you need every day.