[JAVA] Guitar fret calculator

Overview

It was published on GitHub in a project named fret-calulator, but since there are no comments in the program, I wrote this sentence with the intention of explaining it. I did. Previously, the source code was distributed as an application for iOS, but since many people did not have a build environment, I made it a web application.

When you access the application, you will see this screen. There are 4 tabs, but "various fretting" is not implemented yet. I'm planning to implement a fret calculation function for scales other than the standard rule here, but I'm skipping the implementation because I haven't used it yet.

Use your smartphone or tablet to access the application and calculate when cutting the fret groove on the fingerboard.

It looks like the fret groove was cut.

Environment used for development

Server side

Spring Boot and Java 1.8

Client side

AngularJS 1.6.2、Angular Translate 2.13.1、Angular Translate Loader Static Files 2.13.1、HTML、CSS、JavaScript

IDE Eclipse with Sprint Tool Suite plug-in installed and Java 1.8

If you have an application deployed on heroku and want to see it in action, you can see it at https://guitar-calc.herokuapp.com/.

Computational component

GuitarCalculator.java is the main part of the program. Made as a service component.

package net.artesware.service;

import java.util.Map;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class GuitarCalculator {

  // [1]
  public List<Map<String, Object>> calcFretPositions(
    float scale,
    int numFrets)
  {
    float f = scale;
    float lastPos = 0.0f;
    List <Map<String, Object>>result = new ArrayList<Map<String, Object>>();
    Map<String, Object> pos;
    for(int i = 0; i < numFrets + 1; i++) {
      float fretPos = f / 17.817f;
      f -= fretPos;
      pos = new HashMap<String, Object>();
      pos.put("fret", i+1);
      pos.put("positionFromNut", lastPos + fretPos);
      pos.put("positionFromFret", fretPos);
      result.add(pos);
      lastPos += fretPos;
    }
    return result;
  }

  // [2]
  public Map<String, Object> calcBridgePosition(
    float scale,
    float jointPos,
    float neckAngle,
    float stringHeight,
    float fretHeight,
    float thickness)
  {
    float base = (float) ((scale - jointPos) * Math.cos((double)neckAngle * Math.PI / 180.0f));
    float height = (float) ((scale - jointPos) * Math.sin((double)neckAngle * Math.PI / 180.0f));
    float strHeight = (stringHeight - 0.1f) * 2.0f + fretHeight;
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("saddlePosition", base);
    result.put("saddleHeight", (height + strHeight + thickness));
    return result;
  }

  // [3]
  public Map<String, Object> calcFingerboardSize(
    float scale,
    int numStrings,
    int numFrets,
    float nutPitch,
    float saddlePitch,
    float nutSpacing)
  {
    float fretboardLength = (float) (scale - scale / Math.pow(2.0f, ((numFrets+1.0f)/12.0f)));
    float nutWidth = nutPitch * (numStrings - 1) + nutSpacing * 2;
    float bw = saddlePitch * (numStrings - 1) + nutSpacing * 2;
    float endWidth = nutWidth + (bw-nutWidth) * (fretboardLength/scale);
    Map<String, Object> result = new HashMap<String, Object>();
    result.put("length", fretboardLength);
    result.put("nutWidth", nutWidth);
    result.put("endWidth", endWidth);
    return result;
  }


}

Fret position calculation

[1] is a method to find the fret position of the standard scale. When called with the string length and the number of frets as arguments, the position of each fret is calculated. The result is a list of distances from the nut (0th fret) and the distance from the previous fret. For example, in the case of Gibson Les Paul, the string length is 628 mm and it is 22 frets, so call it as scale = 628, numFrets = 22.

Calculation of bridge position and height

[2] is a method to calculate the bridge position and height. When called with the string length, neck body joint position, neck preparation angle, string height at 12 frets, fret height, and fingerboard thickness as arguments, the position and height of the bridge from the neck and body joint will be calculated. ..

Calculation of fingerboard dimensions

[3] is a method to calculate the dimensions of the fingerboard. The dimensions of the fingerboard can be calculated by calling with the string length, number of strings, number of frets, string spacing at the nut position, string spacing at the saddle position, and the distance from the side of the fingerboard to the highest and lowest strings as arguments. Calculate.

REST controller

GuitarCalcController.java is a REST controller class. It's as simple as calling a method in the GuitarCalculator component when it receives an HTTP request and returning the result.

package net.artesware.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import net.artesware.service.GuitarCalculator;

@RestController
public class GuitarCalcController {

  @Autowired
  private GuitarCalculator calculator;

  // [1]
  @GetMapping("/calcFretPositions")
  @ResponseBody
  public List<Map<String, Object>> calcFretPositions(
    @RequestParam float scale,
    int numFrets) {
    return calculator.calcFretPositions(scale, numFrets);
  }

  // [2]
  @GetMapping("/calcBridgePosition")
  @ResponseBody
  public Map<String, Object> calcBridgePosition(
    @RequestParam float scale,
    float jointPos,
    float neckAngle,
    float stringHeight,
    float fretHeight,
    float thickness) {
    return calculator.calcBridgePosition(scale, jointPos, neckAngle, stringHeight, fretHeight, thickness);
  }

  // [3]
  @GetMapping("/calcFingerboardSize")
  @ResponseBody
  public Map<String, Object> calcFingerboardSize(
    @RequestParam float scale,
    int numStrings,
    int numFrets,
    float nutPitch,
    float saddlePitch,
    float nutSpacing) {
    return calculator.calcFingerboardSize(scale, numStrings, numFrets, nutPitch, saddlePitch, nutSpacing);
  }
}

in conclusion

When I made this, I just wanted to use Spring Boot. It may have been easier and better to implement this kind of program only on the client side using AngularJS without using Spring. You are free to modify or deploy the source code. No credit required.

Recommended Posts

Guitar fret calculator
Calculator program
ruby calculator