Frank Faruk Logo

Decent Sampler Preset Coordinate Adjuster

A simple Python script for adjusting the x and y coordinates in Decent Sampler .dspreset files. This script allows users to easily update the graphical layout of the Decent Sampler interface by shifting the positions of various elements based on user-specified adjustments.

Features

Requirements

Usage

  1. Clone or download the script and save it as adjust_dspreset.py.
  2. Make sure Python 3.x is installed on your system. You can download it from python.org.
  3. Run the script using a terminal or command prompt:
  4. python adjust_dspreset.py

Code

To begin, copy the Python code below. Then, open your chosen code editor (e.g., Sublime Text, Visual Studio Code) and paste the code into a new file. Save the file with a .py extension.

import xml.etree.ElementTree as ET
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox
import logging
from pathlib import Path

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def adjust_values(file_path, x_adjustment, y_adjustment):
    # Parse the .dspreset file
    tree = ET.parse(file_path)
    root = tree.getroot()

    # Iterate over all elements and adjust x and y values
    for elem in root.iter():
        if 'x' in elem.attrib and x_adjustment != 0:
            try:
                original_x = int(elem.attrib['x'])
                new_x = original_x + x_adjustment
                elem.set('x', str(new_x))
                logging.info(f'Adjusted x from {original_x} to {new_x}')
            except ValueError:
                logging.warning(f'Skipping non-integer x value: {elem.attrib["x"]}')

        if 'y' in elem.attrib and y_adjustment != 0:
            try:
                original_y = int(elem.attrib['y'])
                new_y = original_y + y_adjustment
                elem.set('y', str(new_y))
                logging.info(f'Adjusted y from {original_y} to {new_y}')
            except ValueError:
                logging.warning(f'Skipping non-integer y value: {elem.attrib["y"]}')

    # Save the adjusted .dspreset to the same directory with "_adjusted" suffix
    new_file_path = Path(file_path).parent / (Path(file_path).stem + '_adjusted.dspreset')
    tree.write(new_file_path)
    logging.info(f'Adjusted .dspreset file saved to {new_file_path}')

def select_file_and_adjust():
    # Open file dialog to select a .dspreset file
    file_path = filedialog.askopenfilename(
        title='Select a Decent Sampler Preset (.dspreset) file',
        filetypes=[('Decent Sampler Preset Files', '*.dspreset')]
    )

    if not file_path:
        messagebox.showwarning('No File Selected', 'Please select a .dspreset file to proceed.')
        return

    # Ask user for x and y adjustments
    try:
        x_adjustment = simpledialog.askinteger('X Adjustment', 'Enter the amount to adjust X (0 means no change):', initialvalue=0)
        y_adjustment = simpledialog.askinteger('Y Adjustment', 'Enter the amount to adjust Y (0 means no change):', initialvalue=0)

        if x_adjustment is None or y_adjustment is None:
            messagebox.showwarning('Input Canceled', 'Adjustments were not specified.')
            return

        adjust_values(file_path, x_adjustment, y_adjustment)
        messagebox.showinfo('Success', f'File adjusted successfully and saved as: {Path(file_path).stem}_adjusted.dspreset')
    except Exception as e:
        logging.error(f'Error adjusting file: {e}')
        messagebox.showerror('Error', f'An error occurred: {e}')

def main():
    # Create a simple Tkinter GUI
    root = tk.Tk()
    root.withdraw() # Hide the root window

    # Prompt user to select a file and adjust it
    select_file_and_adjust()

    # Close the Tkinter root window
    root.quit()

if __name__ == '__main__':
    main()