Skip to content

a string format and API for musical scales in C, C++, and JavaScript

Notifications You must be signed in to change notification settings

ellapollack/scalemap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 

Repository files navigation

scalemap

a string format for musical scales, consisting of a sequence of newline-separated math expressions corresponding to the frequency interval of each scale degree (ending with the octave).

Try it out in Frequency Explorer.

Examples:

12-tone equal temperament:

2^(1/12)

5-limit JI:

9/8
5/4
4/3
3/2
5/3
15/8
2

C API

typedef struct tuning

  • int baseNote, the note number of the tonic.
  • double baseFreq, the frequency of the tonic.
  • size_t scaleSize, the number of degrees in scale (at least 1).
  • double* scale, an array containing the frequency ratio of each scale degree, ending with the octave.

tuning newTuning(const char* baseNoteExpr, const char* baseFreqExpr, const char* scaleExpr)

  • Returns a tuning specified by:
    • baseNoteExpr, a math expression for baseNote (rounded to nearest integer).
    • baseFreqExpr, a math expression for baseFreq.
    • scaleExpr, math expressions for each scale degree, separated by \n.
  • Math expressions are parsed by TinyExpr.
  • Be sure to call free(tuning.scale) when you're done with it to prevent a memory leak.

double noteToFreq(int note, tuning tuning)

  • Returns the frequency of a note according to a tuning.
#include "scalemap.h"

int main() {
  tuning t = newTuning("69","440","2^(1/12)");
  noteToFreq(60, t); // returns 261.626
  free(t.scale);
}

C++ API

Defines all the same functions and types as in C, plus the Tuning convenience class:

class Tuning

  • Tuning(std::string baseNoteExpr, std::string baseFreqExpr, std::string scaleExpr)
  • int baseNote
  • double baseFreq
  • std::vector<double> scale
  • noteToFreq(int note)
#include "scalemap.h"

int main() {
  Tuning t ("69","440","2^(1/12)");
  t.noteToFreq(60); // returns 261.626
}

JavaScript API

Ported to WebAssembly with Emscripten

parseExpr(mathExpr)

  • Returns the Number result of parsing the String mathExpr using TinyExpr
  • If the math expression is invalid, returns 0

class Tuning

  • Tuning(baseNoteExpr, baseFreqExpr, scaleExpr) (Object)
  • baseNote (Number)
  • baseFreq (Number)
  • scale (Array of Numbers)
  • noteToFreq(note) (Number)
<script src="scalemap.js"></script>
<script>
  Module.onRuntimeInitialized = function() { // wait for WebAssembly to initialize
    var t  = new Tuning("69","440","2^(1/12)");
    t.noteToFreq(60); // returns 261.626
  }
</script>

About

a string format and API for musical scales in C, C++, and JavaScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages