zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

Microtonal.h (4777B)


      1 /*
      2   ZynAddSubFX - a software synthesizer
      3 
      4   Microtonal.h - Tuning settings and microtonal capabilities
      5   Copyright (C) 2002-2005 Nasca Octavian Paul
      6   Author: Nasca Octavian Paul
      7 
      8   This program is free software; you can redistribute it and/or
      9   modify it under the terms of the GNU General Public License
     10   as published by the Free Software Foundation; either version 2
     11   of the License, or (at your option) any later version.
     12 */
     13 
     14 #ifndef MICROTONAL_H
     15 #define MICROTONAL_H
     16 
     17 #include <cstdio>
     18 #include <stdint.h>
     19 #include "../globals.h"
     20 #include "../Containers/NotePool.h"
     21 
     22 #define MAX_OCTAVE_SIZE 128
     23 #define MICROTONAL_MAX_NAME_LEN 120
     24 
     25 namespace zyn {
     26 
     27 class XMLwrapper;
     28 
     29 struct KbmInfo
     30 {
     31     uint8_t   Pmapsize;
     32     uint8_t   Pfirstkey;
     33     uint8_t   Plastkey;
     34     uint8_t   Pmiddlenote;
     35     uint8_t   PAnote;
     36     float     PAfreq;
     37     uint8_t   Pmappingenabled;
     38     short int Pmapping[128];
     39 };
     40 
     41 struct OctaveTuning {
     42     unsigned char type; //1 for cents or 2 for division
     43 
     44     /*
     45      * The real tuning in logarithmic power of two.
     46      * For example 1/12 for one halftone and
     47      * 1 for one octave.
     48      */
     49     float tuning_log2;
     50 
     51     //the real tuning is x1/x2
     52     unsigned int x1, x2;
     53 };
     54 
     55 struct SclInfo
     56 {
     57     char Pname[MICROTONAL_MAX_NAME_LEN];
     58     char Pcomment[MICROTONAL_MAX_NAME_LEN];
     59     unsigned char octavesize;
     60     OctaveTuning octave[MAX_OCTAVE_SIZE];
     61 };
     62 
     63 /**Tuning settings and microtonal capabilities*/
     64 class Microtonal
     65 {
     66     public:
     67         /**Constructor*/
     68         Microtonal(const int& gzip_compression);
     69         /**Destructor*/
     70         ~Microtonal();
     71         void defaults();
     72         /**Updates the logarithmic power of two frequency for a given note
     73          */
     74         bool updatenotefreq_log2(float &note_log2_freq, int keyshift) const;
     75         /**Calculates the frequency for a given note
     76          */
     77         float getnotefreq(float note_log2_freq, int keyshift) const;
     78 
     79         //Parameters
     80         /**if the keys are inversed (the pitch is lower to keys from the right direction)*/
     81         unsigned char Pinvertupdown;
     82 
     83         /**the central key of the inversion*/
     84         unsigned char Pinvertupdowncenter;
     85 
     86         /**0 for 12 key temperate scale, 1 for microtonal*/
     87         unsigned char Penabled;
     88 
     89         /**the note of "A" key*/
     90         unsigned char PAnote;
     91 
     92         /**the frequency of the "A" note*/
     93         float PAfreq;
     94 
     95         /**if the scale is "tuned" to a note, you can tune to other note*/
     96         unsigned char Pscaleshift;
     97 
     98         //first and last key (to retune)
     99         unsigned char Pfirstkey;
    100         unsigned char Plastkey;
    101 
    102         /**The middle note where scale degree 0 is mapped to*/
    103         unsigned char Pmiddlenote;
    104 
    105         /**Map size*/
    106         unsigned char Pmapsize;
    107 
    108         /**Mapping ON/OFF*/
    109         unsigned char Pmappingenabled;
    110         /**Mapping (keys)*/
    111         short int Pmapping[128];
    112 
    113         /**Fine detune to be applied to all notes*/
    114         unsigned char Pglobalfinedetune;
    115 
    116         // Functions
    117         /** Return the current octave size*/
    118         unsigned char getoctavesize() const;
    119         /**Convert tuning to string*/
    120         void tuningtoline(int n, char *line, int maxn);
    121         /**load the tunings from a .scl file*/
    122         static int loadscl(SclInfo &scl, const char *filename);
    123         /**load the mapping from .kbm file*/
    124         static int loadkbm(KbmInfo &kbm, const char *filename);
    125         /**Load text into the internal tunings
    126          *
    127          *\todo better description*/
    128         int texttotunings(const char *text);
    129         /**Load text into the internal mappings
    130          *
    131          *\todo better description*/
    132         void texttomapping(const char *text);
    133 
    134         /**Name of Microtonal tuning*/
    135         char Pname[MICROTONAL_MAX_NAME_LEN];
    136         /**Comment about the tuning*/
    137         char Pcomment[MICROTONAL_MAX_NAME_LEN];
    138 
    139         void add2XML(XMLwrapper& xml) const;
    140         void getfromXML(XMLwrapper& xml);
    141         int saveXML(const char *filename) const;
    142         int loadXML(const char *filename);
    143 
    144         //simple operators primarily for debug
    145         bool operator==(const Microtonal &micro) const;
    146         bool operator!=(const Microtonal &micro) const;
    147 
    148         void clone(Microtonal &m);
    149 
    150         static const rtosc::Ports ports;
    151 
    152         //only paste handler should access there (quasi-private)
    153         unsigned char octavesize;
    154         OctaveTuning octave[MAX_OCTAVE_SIZE];
    155     private:
    156         //loads a line from the text file, while ignoring the lines beginning with "!"
    157         static int loadline(FILE *file, char *line);
    158         //Grab a 0..127 integer from the provided descriptor
    159 
    160         static int linetotunings(struct OctaveTuning &tune, const char *line);
    161         void apply(void);
    162 
    163         const int& gzip_compression;
    164 };
    165 
    166 }
    167 
    168 #endif