summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/synti/zynaddsubfx/Misc/Util.C
blob: 746197310818133baad3fbda49058e6400d715c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
  ZynAddSubFX - a software synthesizer
 
  Util.C - Miscellaneous functions
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include "Util.h"
#include <math.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int SAMPLE_RATE=44100;
int SOUND_BUFFER_SIZE=256;
int OSCIL_SIZE=512;

Config config;
REALTYPE *denormalkillbuf;


/*
 * Transform the velocity according the scaling parameter (velocity sensing)
 */
REALTYPE VelF(REALTYPE velocity,unsigned char scaling){
    REALTYPE x;
    x=pow(VELOCITY_MAX_SCALE,(64.0-scaling)/64.0);
    if ((scaling==127)||(velocity>0.99)) return(1.0);
       else return(pow(velocity,x));
};

/*
 * Get the detune in cents 
 */
REALTYPE getdetune(unsigned char type,unsigned short int coarsedetune,unsigned short int finedetune){
    REALTYPE det=0.0,octdet=0.0,cdet=0.0,findet=0.0;
    //Get Octave
    int octave=coarsedetune/1024;
    if (octave>=8) octave-=16;
    octdet=octave*1200.0;

    //Coarse and fine detune
    int cdetune=coarsedetune%1024;
    if (cdetune>512) cdetune-=1024;
    
    int fdetune=finedetune-8192;

    switch (type){
//	case 1: is used for the default (see below)
	case 2:	cdet=fabs(cdetune*10.0);
		findet=fabs(fdetune/8192.0)*10.0;
		break;
	case 3:	cdet=fabs(cdetune*100);
		findet=pow(10,fabs(fdetune/8192.0)*3.0)/10.0-0.1;
		break;
	case 4:	cdet=fabs(cdetune*701.95500087);//perfect fifth
		findet=(pow(2,fabs(fdetune/8192.0)*12.0)-1.0)/4095*1200;
		break;
      //case ...: need to update N_DETUNE_TYPES, if you'll add more
	default:cdet=fabs(cdetune*50.0);
		findet=fabs(fdetune/8192.0)*35.0;//almost like "Paul's Sound Designer 2"
		break;
    };
    if (finedetune<8192) findet=-findet;
    if (cdetune<0) cdet=-cdet;
    
    det=octdet+cdet+findet;
    return(det);
};


bool fileexists(char *filename){
    struct stat tmp;
    int result=stat(filename,&tmp);
    if (result>=0) return(true);
    
    return(false);
};

void newFFTFREQS(FFTFREQS *f,int size){
    f->c=new REALTYPE[size];
    f->s=new REALTYPE[size];
    for (int i=0;i<size;i++){
	f->c[i]=0.0;f->s[i]=0.0;
    };
};
void deleteFFTFREQS(FFTFREQS *f){
    delete[] f->c;
    delete[] f->s;
    f->c=f->s=NULL;
};