Node for Max 上で tonal ライブラリを利用して Key Scaler を試作 #max8 #n4m

Midi NoteのPitchを特定のスケールに矯正

cycling’74のN4Mサンプルにも入っていた tonal をnode objectで使ってみたのでメモです。

鍵盤の音を特定のスケール(とりあえずC Major)に矯正するスクリプトとなっております。Node for Maxのご参考にどうぞ。

tonal.js

引用 “tonal is a music theory library. Contains functions to manipulate tonal elements of music (note, intervals, chords, scales, modes, keys). It deals with abstractions (not actual music or sound).

tonal is implemented in Typescript and published as a collection of Javascript npm packages.

It uses a functional programing style: all functions are pure, there is no data mutation, and entities are represented by data structures instead of objects.”

https://github.com/tonaljs/tonal ※導入方法はこちら参照

keyscaler.js


const MAX_API = require("max-api");
const { Scale } = require("@tonaljs/tonal");
const { Key } = require("@tonaljs/tonal");
const { Mode } = require("@tonaljs/tonal");
const { Midi } = require("@tonaljs/tonal");
const { Note } = require("@tonaljs/tonal");
const { Chord } = require("@tonaljs/tonal");

const notes = Scale.get("C major").notes;
console.log("notes:"+notes);

let midiNotes = notes.map((value) => {
    return Note.midi(value+"0")
});
console.log("midiNotes:"+midiNotes);

const handlers = {
    "getScaledPitch" : (pitch) => {

        let returnPitch = pitch;

        while(
            midiNotes.every(value => {
                return ((value % 12) != (returnPitch % 12))
            })   
        ){
            returnPitch++;            
        };

        //MAX_API.post(pitch + "->" + returnPitch);

        MAX_API.outlet(returnPitch);  
    },
    /*
    [maxAPI.MESSAGE_TYPES.BANG] : () =>{
        console.log("bang");
    },
    */
};

MAX_API.addHandlers(handlers);

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です