Git Product home page Git Product logo

scalemap's Introduction

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>

scalemap's People

Contributors

ellapollack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

scalemap's Issues

C/C++ API problem

Delete line 778 in the header file. The line
#include "tinyexpr.h"
prevents compilation, since tinyexpr.h does not exist and is embedded in the header file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.