Update NTK.
[nondaw.git] / timeline / src / Audio_Region.H
blob6920fc1072c0142e270d83ebca64a64807bd8277
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
5 /* This program is free software; you can redistribute it and/or modify it     */
6 /* under the terms of the GNU General Public License as published by the       */
7 /* Free Software Foundation; either version 2 of the License, or (at your      */
8 /* option) any later version.                                                  */
9 /*                                                                             */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       */
12 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   */
13 /* more details.                                                               */
14 /*                                                                             */
15 /* You should have received a copy of the GNU General Public License along     */
16 /* with This program; see the file COPYING.  If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 /*******************************************************************************/
20 #pragma once
22 #include "Timeline.H"
23 #include "Sequence_Region.H"
25 class Audio_File;
27 class Fl_Menu_;
28 class Fl_Menu_Button;
30 class Audio_Region : public Sequence_Region
33     /* not permitted */
34     Audio_Region & operator = ( const Audio_Region &rhs );
36     static void peaks_ready_callback ( void *v );
38 public:
40     static bool inherit_track_color;
41     static bool show_box;
43     struct Fade
44     {
45         enum fade_type_e { Linear = 0, Sigmoid, Logarithmic, Parabolic };
46         enum fade_dir_e { In, Out };
48         fade_type_e type;
49         nframes_t length;
51         Fade ( )
52             {
53                 type   = Linear;
54                 length = 0;
55             }
57         bool
58         operator< ( const Fade &rhs ) const
59             {
60                 return length < rhs.length;
61             }
63         double increment ( void ) const
64             {
65                 return 1.0f / length;
66             }
68         /** Return gain for frame /index/ of /nframes/ on a gain curve
69          * of type /type/.*/
70         /* FIXME: calling a function per sample is bad, switching on
71          * type mid fade is bad. */
72         inline double
73         gain ( const double fi ) const
74             {
75                 switch ( type )
76                 {
77                     case Linear:
78                         return fi;
79                     case Sigmoid:
80                         return (1.0f - cos( fi * M_PI )) / 2.0f;
81                     case Logarithmic:
82                         return pow( 0.1f, (1.0f - fi) * 3.0f );
83                     case Parabolic:
84                         return 1.0f - (1.0f - fi) * (1.0f - fi);
85                     default:
86                         return 1.0f;
87                 }
88             }
90         void apply ( sample_t *buf, fade_dir_e dir, nframes_t start, nframes_t nframes ) const;
91     };
93 /*     struct Fade_In : public Fade; */
94 /*     struct Fade_Out : public Fade; */
96 private:
98     Audio_File *_clip;                                          /* clip this region represents */
100     bool _adjusting_gain;
101     float _scale;                                               /* amplitude adjustment */
103     Fade _fade_in;
104     Fade _fade_out;
106     nframes_t _loop;                                            /* loop point */
108     friend class Track;                                  /* for _clip */
110     Fl_Menu_Button & menu ( void );
112     static void menu_cb ( Fl_Widget *w, void *v );
113     void menu_cb ( const Fl_Menu_ *m );
115     void draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W );
117 protected:
119     virtual void get ( Log_Entry &e ) const;
120     virtual void set ( Log_Entry &e );
122     void draw_label ( const char *label, Fl_Align align )
123         {
124             Sequence_Widget::draw_label( label, align );
125         }
127     int handle ( int m );
128     void draw_label ( void );
129     void draw_box ( void );
130     void draw ( void );
131     void resize ( void );
133 public:
135     LOG_CREATE_FUNC( Audio_Region );
137     SEQUENCE_WIDGET_CLONE_FUNC( Audio_Region );
139     static Fl_Boxtype _box;
140     static Fl_Color _selection_color;
141     Fl_Color selection_color ( void ) const { return _selection_color; }
142     void selection_color ( Fl_Color v ) { _selection_color = v; }
144     void init ( void );
146     Audio_Region ( )
147         {
148             init();
149         }
151     bool current ( void ) const { return this == belowmouse(); }
153     const char * source_name ( void ) const;
155     Audio_Region ( const Audio_Region & rhs );
156     Audio_Region ( Audio_File *c );
157     Audio_Region ( Audio_File *c, Sequence *t, nframes_t o );
158     ~Audio_Region ( );
160     Fl_Boxtype box ( void ) const { return Audio_Region::_box; }
161     Fl_Align align ( void ) const { return  (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ); }
163     void normalize ( void );
164     void split ( nframes_t where );
165     bool recording ( void ) const;
167     /* Engine */
168     nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
169     nframes_t write ( nframes_t nframes );
170     void prepare ( void );
171     bool finalize ( nframes_t frame );