concertina

Elecronic concertina
git clone https://git.woozle.org/neale/concertina.git

concertina / pkg / layouts
Neale Pickett  ·  2026-02-16

abc.go

  1package layouts
  2
  3var baseMidiNote = map[byte]int8{
  4	'C': 60,
  5	'D': 62,
  6	'E': 64,
  7	'F': 65,
  8	'G': 67,
  9	'A': 69,
 10	'B': 71,
 11	'c': 72,
 12	'd': 74,
 13	'e': 76,
 14	'f': 77,
 15	'g': 79,
 16	'a': 81,
 17	'b': 83,
 18}
 19
 20func ABC2MIDI(note string) int8 {
 21	if note == "" {
 22		return -1
 23	}
 24
 25	val, ok := baseMidiNote[note[0]]
 26	if !ok {
 27		return -1
 28	}
 29
 30	for _, m := range note[1:] {
 31		switch m {
 32		case '♯':
 33			fallthrough
 34		case '^':
 35			val += 1
 36
 37		case '♭':
 38			fallthrough
 39		case '_':
 40			val -= 1
 41
 42		case '\'':
 43			val += 12
 44
 45		case ',':
 46			val -= 12
 47
 48		default:
 49			return -1
 50		}
 51	}
 52
 53	return val
 54}
 55
 56func MIDI2ABC(val int8) string {
 57	octave := make([]byte, 0, 8)
 58	for val < baseMidiNote['C'] {
 59		octave = append(octave, ',')
 60		val += 12
 61	}
 62	for val > baseMidiNote['b'] {
 63		octave = append(octave, '\'')
 64		val -= 12
 65	}
 66
 67	var base string
 68	switch val % 12 {
 69	case 0:
 70		base = "C"
 71	case 1:
 72		base = "C^"
 73	case 2:
 74		base = "D"
 75	case 3:
 76		base = "D^"
 77	case 4:
 78		base = "E"
 79	case 5:
 80		base = "F"
 81	case 6:
 82		base = "F^"
 83	case 7:
 84		base = "G"
 85	case 8:
 86		base = "G^"
 87	case 9:
 88		base = "A"
 89	case 10:
 90		base = "B_"
 91	case 11:
 92		base = "B"
 93	}
 94
 95	if val >  baseMidiNote['B'] {
 96		base = string(base[0] + 0x20) + base[1:]
 97	}
 98
 99	return base + string(octave)
100}