- further work on theme colors:
[blender.git] / source / blender / src / drawsound.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifdef WIN32
41 #include "BLI_winstuff.h"
42 #endif
43
44 #include "BLI_blenlib.h"
45 #include "BLI_arithb.h"
46 #include "BLI_editVert.h"
47
48 #include "DNA_scene_types.h"
49 #include "DNA_sound_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_screen_types.h"
52
53 #include "BKE_utildefines.h"
54 #include "BKE_global.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_mywindow.h"
58 #include "BIF_screen.h"
59 #include "BIF_editsound.h"
60
61 #include "BSE_drawipo.h"
62 #include "BMF_Api.h"
63
64 /* local */
65 void drawsoundspace(ScrArea *sa, void *spacedata);
66
67 /*implementation */
68 static void draw_wave(int startsamp, int endsamp, short sampdx, short offset, short *sp, float sampfac, float y)
69 {
70         float min, max, v1[2], v2[3];
71         int i, j;
72         short value, deltasp;
73         
74         sp+= offset*startsamp;
75
76         deltasp= offset*sampdx;
77         
78         glBegin(GL_LINES);
79         for(i=startsamp; i<endsamp; i+=sampdx, sp+=deltasp) {
80         
81                 /* filter */
82                 min= max= 0.0;
83                 for(j=0; j<sampdx; j++) {
84                         value= sp[offset*j];
85                         if(value < min) min= value;
86                         else if(value > max) max= value;
87                 }
88                 v1[1]= y + 0.002*min;
89                 v2[1]= y + 0.002*max;
90                 
91                 v1[0]=v2[0]= sampfac*i;
92
93                 glVertex2fv(v1);
94                 glVertex2fv(v2);
95         }
96         glEnd();
97 }
98
99 static void draw_sample(bSample *sample)
100 {
101         float sampxlen, sampfac;
102         int samples, startsamp, endsamp;
103         short *sp, sampdx;
104         
105         /* one sample is where in v2d space? (v2d space in frames!) */
106         sampfac= 25.0/(sample->rate);
107
108         /* how many samples? */
109         samples= sample->len/(sample->channels*(sample->bits/8));
110         /* total len in v2d space */
111         sampxlen= sampfac*samples;
112
113         /* one pixel is how many samples? */
114         sampdx= (samples*((G.v2d->cur.xmax-G.v2d->cur.xmin)/sampxlen))/curarea->winx;
115
116         if(sampdx==0) sampdx= 1;
117         
118         /* start and and */
119         startsamp = G.v2d->cur.xmin/sampfac;
120         CLAMP(startsamp, 0, samples-1);
121         endsamp= G.v2d->cur.xmax/sampfac;
122         CLAMP(endsamp, 0, samples-1);
123         endsamp-= sampdx;
124         
125         /* set 'tot' for sliders */
126         G.v2d->tot.xmax= sampfac*samples;
127
128         /* channels? */
129         if(sample->channels==2) {
130                 
131                 cpack(0x905050);
132                 sp= (short *)(sample->data);
133                 draw_wave(startsamp, endsamp, sampdx, 2, sp, sampfac, 85.0);
134
135                 cpack(0x506890);
136                 sp++;
137                 draw_wave(startsamp, endsamp, sampdx, 2, sp, sampfac, 190.0);
138         }
139         else {
140                 cpack(0x905050);
141                 sp= (short *)(sample->data);
142
143                 draw_wave(startsamp, endsamp, sampdx, 1, sp, sampfac, 128.0);           
144         }
145 }
146
147 static void draw_cfra_sound(SpaceSound *ssound)
148 {
149         float vec[2];
150         
151         if(ssound->flag & SND_CFRA_NUM) {
152                 short mval[2];
153                 float x,  y;
154                 char str[32];
155                 /* little box with frame */
156                 
157                 getmouseco_areawin(mval);
158                 if(mval[1]<17) mval[1]= 17;
159                 areamouseco_to_ipoco(G.v2d, mval, &x, &y);
160                 
161                 if(ssound->flag & SND_DRAWFRAMES) 
162                         sprintf(str, "   %d\n", (G.scene->r.cfra));
163                 else sprintf(str, "   %.2f\n", (G.scene->r.cfra/(float)G.scene->r.frs_sec));
164                 
165                 glRasterPos2f(x, y);
166                 glColor3ub(0, 0, 0);
167                 BMF_DrawString(G.font, str);
168
169         }
170
171         vec[0]=  (G.scene->r.cfra);
172         vec[0]*= G.scene->r.framelen;
173
174         vec[1]= G.v2d->cur.ymin;
175         glColor3ub(0x20, 0x90, 0x20);
176         glLineWidth(4.0);
177
178         glBegin(GL_LINE_STRIP);
179                 glVertex2fv(vec);
180                 vec[1]= G.v2d->cur.ymax;
181                 glVertex2fv(vec);
182         glEnd();
183         
184         glLineWidth(1.0);
185         
186 }
187
188
189 void drawsoundspace(ScrArea *sa, void *spacedata)
190 {
191         short ofsx, ofsy;
192         
193         glClearColor(.6275, .6275, .6275, 0.0); 
194         glClear(GL_COLOR_BUFFER_BIT);
195
196         calc_scrollrcts(G.v2d, curarea->winx, curarea->winy);
197
198         if(curarea->winx>SCROLLB+10 && curarea->winy>SCROLLH+10) {
199                 if(G.v2d->scroll) {     
200                         ofsx= curarea->winrct.xmin;     /* because mywin */
201                         ofsy= curarea->winrct.ymin;
202                         glViewport(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1); 
203                         glScissor(ofsx+G.v2d->mask.xmin,  ofsy+G.v2d->mask.ymin, ( ofsx+G.v2d->mask.xmax-1)-(ofsx+G.v2d->mask.xmin)+1, ( ofsy+G.v2d->mask.ymax-1)-( ofsy+G.v2d->mask.ymin)+1);
204                 }
205         }
206
207         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
208
209         /* boundbox_seq(); */
210         calc_ipogrid(); 
211         draw_ipogrid();
212
213         if (G.ssound->sound) {
214                 sound_initialize_sample(G.ssound->sound);
215                 draw_sample(G.ssound->sound->sample);
216         }
217         
218         draw_cfra_sound(spacedata);
219
220         /* restore viewport */
221         mywinset(curarea->win);
222
223         /* ortho at pixel level curarea */
224         myortho2(-0.5, curarea->winx-0.5, -0.5, curarea->winy-0.5);
225
226         if(curarea->winx>SCROLLB+10 && curarea->winy>SCROLLH+10) {
227                 if(G.v2d->scroll) {
228                         drawscroll(0);
229                 }
230         }
231         
232         myortho2(-0.5, curarea->winx-0.5, -0.5, curarea->winy-0.5);
233         draw_area_emboss(sa);
234         curarea->win_swap= WIN_BACK_OK;
235 }