Changed the backend api somewhat. Added bpm==0 check.
[epichord.git] / src / util.cpp
blobe18241eadc143ec695a71941ee469a1113104e37
1 /*
2 Epichord - a midi sequencer
3 Copyright (C) 2008 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <vector>
28 #include <fltk/TextDisplay.h>
29 #include <fltk/TextBuffer.h>
31 #include "seq.h"
32 #include "backend.h"
36 #include "util.h"
41 extern std::vector<track*> tracks;
43 void load_text(fltk::TextDisplay* o, const char* filename){
44 fltk::TextBuffer* T = new fltk::TextBuffer();
45 //printf("loading file %s\n",filename);
46 T->loadfile(filename);
47 o->buffer(T);
51 void hsv_to_rgb(float h, float s, float v, unsigned char* r, unsigned char* g, unsigned char* b){
53 float lh = h - 360*floor(h/360);
55 float R,G,B;
57 float V = v;
58 float S = s;
60 if(lh < 60){
61 R = V;
62 G = lh*V*S/60 + V*(1-S);
63 B = V*(1-S);
65 else if(lh < 120){
66 R = -(lh-60)*V*S/60 + V;
67 G = V;
68 B = V*(1-S);
70 else if(lh < 180){
71 R = V*(1-S);
72 G = V;
73 B = (lh-120)*V*S/60 + V*(1-S);
75 else if(lh < 240){
76 R = V*(1-S);
77 G = -(lh-180)*V*S/60 + V;
78 B = V;
80 else if(lh < 300){
81 R = (lh-240)*V*S/60 + V*(1-S);
82 G = V*(1-S);
83 B = V;
85 else{
86 R = V;
87 G = V*(1-S);
88 B = -(lh-300)*V*S/60 + V;
91 *r = (unsigned char)(255*R);
92 *g = (unsigned char)(255*G);
93 *b = (unsigned char)(255*B);
98 int ypix2note(int ypix, int black){
99 int udy = 900 - ypix + 1;
100 int white = udy/12;
101 int note = 2*white - white/7 - (white+4)/7;
103 if(black){
104 if(udy%12<4 && white%7!=0 && (white+4)%7!=0){note--;}
105 else if(udy%12>8 && (white+1)%7!=0 && (white+5)%7!=0){note++;}
108 return note;
111 int note2ypix(int note, int* black){
112 int key = (note + (note+7)/12 + note/12)/2;
114 if((note + (note+7)/12 + note/12)%2 == 0){//white key
115 *black=0;
116 return 900 - key*12 - 12;
118 else{//black key
119 *black=1;
120 return 900 - key*12 - 12 - 3;
125 void unmodify_blocks(){
126 seqpat* s;
127 for(int i=0; i<tracks.size(); i++){
128 s = tracks[i]->head->next;
129 while(s){
130 s->modified = 0;
131 s = s->next;
136 int unmodify_and_unstick_tracks(){
137 for(int i=0; i<tracks.size(); i++){
138 if(tracks[i]->modified){
139 midi_track_off(i);
140 tracks[i]->restate();
141 tracks[i]->modified = 0;
147 float randf(float l, float r){
148 float L,R;
149 if(l > r){
150 L=r;
151 R=l;
153 else{
154 L=l;
155 R=r;
157 float M = (rand()*1.0)/RAND_MAX;
158 float ans = M*(R-L) + L;
159 return ans;