Fixed one segfault during undo.
[epichord.git] / src / util.cpp
blob196e592d323c866b0a59d0c4c4aaba842b6d4fe5
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 "backend.h"
33 #include "seq.h"
35 #include "util.h"
40 extern std::vector<track*> tracks;
42 void load_text(fltk::TextDisplay* o, const char* filename){
43 fltk::TextBuffer* T = new fltk::TextBuffer();
44 //printf("loading file %s\n",filename);
45 T->loadfile(filename);
46 o->buffer(T);
50 void hsv_to_rgb(float h, float s, float v, unsigned char* r, unsigned char* g, unsigned char* b){
52 float lh = h - floor(h/360);
54 float R,G,B;
56 float V = v;
57 float S = s;
59 if(lh < 60){
60 R = V;
61 G = lh*V*S/60 + V*(1-S);
62 B = V*(1-S);
64 else if(lh < 120){
65 R = -(lh-60)*V*S/60 + V;
66 G = V;
67 B = V*(1-S);
69 else if(lh < 180){
70 R = V*(1-S);
71 G = V;
72 B = (lh-120)*V*S/60 + V*(1-S);
74 else if(lh < 240){
75 R = V*(1-S);
76 G = -(lh-180)*V*S/60 + V;
77 B = V;
79 else if(lh < 300){
80 R = (lh-240)*V*S/60 + V*(1-S);
81 G = V*(1-S);
82 B = V;
84 else{
85 R = V;
86 G = V*(1-S);
87 B = -(lh-300)*V*S/60 + V;
90 double Z = sqrt(R*R + G*G + B*B);
92 //R = R / Z * 255;
93 //G = G / Z * 255;
94 //B = B / Z * 255;
96 *r = (unsigned char)(255*R);
97 *g = (unsigned char)(255*G);
98 *b = (unsigned char)(255*B);
103 int ypix2note(int ypix, int black){
104 int udy = 900 - ypix + 1;
105 int white = udy/12;
106 int note = 2*white - white/7 - (white+4)/7;
108 if(black){
109 if(udy%12<4 && white%7!=0 && (white+4)%7!=0){note--;}
110 else if(udy%12>8 && (white+1)%7!=0 && (white+5)%7!=0){note++;}
113 return note;
116 int note2ypix(int note, int* black){
117 int key = (note + (note+7)/12 + note/12)/2;
119 if((note + (note+7)/12 + note/12)%2 == 0){//white key
120 *black=0;
121 return 900 - key*12 - 12;
123 else{//black key
124 *black=1;
125 return 900 - key*12 - 12 - 3;
130 void unmodify_blocks(){
131 seqpat* s;
132 for(int i=0; i<tracks.size(); i++){
133 s = tracks[i]->head->next;
134 while(s){
135 s->modified = 0;
136 s = s->next;
141 int unmodify_and_unstick_tracks(){
142 for(int i=0; i<tracks.size(); i++){
143 if(tracks[i]->modified){
144 midi_track_off(i);
145 tracks[i]->restate();
146 tracks[i]->modified = 0;
152 float randf(float l, float r){
153 float L,R;
154 if(l > r){
155 L=r;
156 R=l;
158 else{
159 L=l;
160 R=r;
162 float M = (rand()*1.0)/RAND_MAX;
163 float ans = M*(R-L) + L;
164 return ans;