| @@ -0,0 +1,50 @@ | |||
| /*******************************************************************************/ | |||
| /* Copyright (C) 2008 Jonathan Moore Liles */ | |||
| /* */ | |||
| /* This program is free software; you can redistribute it and/or modify it */ | |||
| /* under the terms of the GNU General Public License as published by the */ | |||
| /* Free Software Foundation; either version 2 of the License, or (at your */ | |||
| /* option) any later version. */ | |||
| /* */ | |||
| /* 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 for */ | |||
| /* more details. */ | |||
| /* */ | |||
| /* You should have received a copy of the GNU General Public License along */ | |||
| /* with This program; see the file COPYING. If not,write to the Free Software */ | |||
| /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |||
| /*******************************************************************************/ | |||
| #pragma once | |||
| typedef unsigned long nframes_t; | |||
| #include "Peaks.H" | |||
| class Clip | |||
| { | |||
| char *_filename; | |||
| Peaks _peaks; | |||
| nframes_t _length; /* length of clip in samples */ | |||
| public: | |||
| Clip ( const char *filename ) | |||
| { | |||
| _filename = NULL; | |||
| // FIXME: open file | |||
| _peaks.open( filename ); | |||
| } | |||
| Peaks const * peaks ( void ) { return &_peaks; } | |||
| const char *name ( void ) { return _filename; } | |||
| nframes_t length ( void ) { return _length; } | |||
| }; | |||
| @@ -4,7 +4,7 @@ CXXFLAGS=-ggdb | |||
| LIBS=`fltk-config --ldflags` | |||
| # CXXFLAGS=`fltk-config -cxxflags` | |||
| OBJS=Waveform.o Region.o main.o | |||
| OBJS=Waveform.o Region.o main.o Peaks.o | |||
| .C.o: | |||
| $(CXX) $(CXXFLAGS) -c $< -o $@ | |||
| @@ -14,3 +14,7 @@ test: $(OBJS) | |||
| clean: | |||
| rm -f $(OBJS) test | |||
| valgrind: | |||
| valgrind ./test | |||
| @@ -0,0 +1,106 @@ | |||
| /*******************************************************************************/ | |||
| /* Copyright (C) 2008 Jonathan Moore Liles */ | |||
| /* */ | |||
| /* This program is free software; you can redistribute it and/or modify it */ | |||
| /* under the terms of the GNU General Public License as published by the */ | |||
| /* Free Software Foundation; either version 2 of the License, or (at your */ | |||
| /* option) any later version. */ | |||
| /* */ | |||
| /* 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 for */ | |||
| /* more details. */ | |||
| /* */ | |||
| /* You should have received a copy of the GNU General Public License along */ | |||
| /* with This program; see the file COPYING. If not,write to the Free Software */ | |||
| /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |||
| /*******************************************************************************/ | |||
| #include "Peaks.H" | |||
| #include "Timeline.H" | |||
| #include <sys/mman.h> | |||
| #include <sys/types.h> | |||
| #include <sys/stat.h> | |||
| #include <fcntl.h> | |||
| #include <stdlib.h> | |||
| #include <stdio.h> | |||
| #include <string.h> | |||
| void | |||
| Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const | |||
| { | |||
| *mhi = -1.0; | |||
| *mlo = 1.0; | |||
| if ( e > _len ) | |||
| e = _len; | |||
| for ( int j = s; j < e; j++ ) | |||
| { | |||
| const float lo = _peaks->data[ j ].min; | |||
| const float hi = _peaks->data[ j ].max; | |||
| if ( hi > *mhi ) | |||
| *mhi = hi; | |||
| if ( lo < *mlo ) | |||
| *mlo = lo; | |||
| } | |||
| } | |||
| void | |||
| Peaks::read ( int X, float *hi, float *lo ) const | |||
| { | |||
| int start = X * timeline.fpp; | |||
| int end = (X + 1) * timeline.fpp; | |||
| downsample( start, end, hi, lo ); | |||
| } | |||
| /* virtual array. Index is a Pixel value, and it returns the | |||
| * (resampled) peaks for that pixel based on the current timeline | |||
| * zoom. */ | |||
| Peak & | |||
| Peaks::operator[] ( int X ) const | |||
| { | |||
| /* Is there a better way to return this? */ | |||
| static Peak p; | |||
| int start = X * timeline.fpp; | |||
| int end = (X + 1) * timeline.fpp; | |||
| downsample( start, end, &p.max, &p.min ); | |||
| return p; | |||
| } | |||
| bool | |||
| Peaks::open ( const char *filename ) | |||
| { | |||
| char file[512]; | |||
| snprintf( file, 512, "%s.peak", filename ); | |||
| int fd; | |||
| if ( ( fd = ::open( file, O_RDONLY ) ) < 0 ) | |||
| { | |||
| /* generate peaks here */ | |||
| } | |||
| { | |||
| struct stat st; | |||
| fstat( fd, &st ); | |||
| _len = st.st_size; | |||
| } | |||
| _peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 ); | |||
| if ( _peaks == MAP_FAILED ) | |||
| printf( "failed to create mapping! " ); | |||
| _len = (_len - sizeof( int )) / sizeof( Peak ); | |||
| } | |||
| @@ -0,0 +1,57 @@ | |||
| /*******************************************************************************/ | |||
| /* Copyright (C) 2008 Jonathan Moore Liles */ | |||
| /* */ | |||
| /* This program is free software; you can redistribute it and/or modify it */ | |||
| /* under the terms of the GNU General Public License as published by the */ | |||
| /* Free Software Foundation; either version 2 of the License, or (at your */ | |||
| /* option) any later version. */ | |||
| /* */ | |||
| /* 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 for */ | |||
| /* more details. */ | |||
| /* */ | |||
| /* You should have received a copy of the GNU General Public License along */ | |||
| /* with This program; see the file COPYING. If not,write to the Free Software */ | |||
| /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |||
| /*******************************************************************************/ | |||
| #include <stdlib.h> | |||
| struct Peak { | |||
| float min; | |||
| float max; | |||
| }; | |||
| class Peaks | |||
| { | |||
| struct peaks { | |||
| int chunksize; /* should always be a power of 2 */ | |||
| Peak data[]; | |||
| }; | |||
| peaks *_peaks; | |||
| size_t _len; | |||
| public: | |||
| Peaks ( void ) | |||
| { | |||
| _peaks = new peaks; | |||
| _peaks->chunksize = 0; | |||
| // _peaks->data = NULL; | |||
| _len = 0; | |||
| } | |||
| void downsample ( int s, int e, float *mhi, float *mlo ) const; | |||
| void read ( int X, float *hi, float *lo ) const; | |||
| bool open ( const char *filename ); | |||
| Peak & operator[] ( int X ) const; | |||
| }; | |||
| @@ -31,6 +31,12 @@ | |||
| extern Timeline timeline; | |||
| Region::Region ( int X, int Y, int W, int H, const char *L ) : Waveform( X, Y, W, H, L ) | |||
| { | |||
| init(); | |||
| } | |||
| void | |||
| Region::init ( void ) | |||
| { | |||
| align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP ); | |||
| labeltype( FL_SHADOW_LABEL ); | |||
| @@ -52,6 +58,11 @@ Region::Region ( const Region & rhs ) : Waveform( rhs ) | |||
| _track = rhs._track; | |||
| } | |||
| Region::Region ( Clip *c ) : Waveform( c ) | |||
| { | |||
| init(); | |||
| } | |||
| void | |||
| Region::trim ( enum trim_e t, int X ) | |||
| { | |||
| @@ -219,10 +230,10 @@ Region::draw ( void ) | |||
| draw_label(); | |||
| static char pat[200]; | |||
| /* static char pat[200]; */ | |||
| sprintf( pat, "start %lu, end %lu", _start, _end ); | |||
| /* sprintf( pat, "start %lu, end %lu", _start, _end ); */ | |||
| fl_draw( pat, x(), y() + h() / 2 ); | |||
| /* fl_draw( pat, x(), y() + h() / 2 ); */ | |||
| } | |||
| @@ -32,11 +32,14 @@ class Region : public Waveform | |||
| void trim ( enum trim_e t, int X ); | |||
| void init ( void ); | |||
| public: | |||
| Region ( int X, int Y, int W, int H, const char *L=0 ); | |||
| Region ( const Region & rhs ); | |||
| Region ( Clip *c ); | |||
| int handle ( int m ); | |||
| void draw ( void ); | |||
| @@ -26,3 +26,5 @@ struct Timeline { | |||
| float fpp; /* frames per pixel */ | |||
| }; | |||
| extern Timeline timeline; | |||
| @@ -23,16 +23,21 @@ | |||
| #include <FL/Fl.H> | |||
| #include "Waveform.H" | |||
| #include "Clip.H" | |||
| // extern Timeline timeline; | |||
| // #include "Timeline.H" | |||
| #include <math.h> | |||
| extern Fl_Color velocity_colors[]; | |||
| Waveform::Waveform ( int X, int Y, int W, int H, const char *L ) : Fl_Widget( X, Y, W, H, L ) | |||
| { | |||
| _scale = 1; | |||
| _clip = NULL; | |||
| _start = _end = 0; | |||
| } | |||
| int measure = 50; | |||
| @@ -55,6 +60,12 @@ Waveform::draw ( void ) | |||
| draw( X, y(), W, h() ); | |||
| } | |||
| void | |||
| Waveform::read_peaks ( tick_t X, float *hi, float *lo ) | |||
| { | |||
| _clip->peaks()->read( X, hi, lo ); | |||
| } | |||
| void | |||
| Waveform::draw ( int X, int Y, int W, int H ) | |||
| { | |||
| @@ -69,50 +80,53 @@ Waveform::draw ( int X, int Y, int W, int H ) | |||
| j = 0; | |||
| for ( int x = X; x < X + W; ++x ) | |||
| { | |||
| float lo = _peaks[ start + j++ ]; | |||
| float hi = _peaks[ start + j++ ]; | |||
| // read_peaks( x, &hi, &lo ); | |||
| Peak p = (*_clip->peaks())[ x ]; | |||
| int mid = Y + (H / 2); | |||
| // FIXME: cache this stuff. | |||
| // fl_color( fl_color_average( selection_color(), fl_contrast( fl_darker( FL_BLUE ), selection_color() ), fabs( hi - lo ) ) ); | |||
| fl_color( fl_color_average( FL_RED, selection_color(), fabs( hi - lo ) ) ); | |||
| fl_color( fl_color_average( FL_RED, selection_color(), fabs( p.max - p.min ) ) ); | |||
| hi *= _scale; | |||
| lo *= _scale; | |||
| p.max *= _scale; | |||
| p.min *= _scale; | |||
| if ( lo < -1.0 || hi > 1.0 ) | |||
| if ( p.min < -1.0 || p.max > 1.0 ) | |||
| fl_color( FL_RED ); | |||
| fl_line( x, mid + (H / 2 * lo), x, mid + (H / 2 * hi) ); | |||
| fl_line( x, mid + (H / 2 * p.min), x, mid + (H / 2 * p.max) ); | |||
| } | |||
| fl_color( fl_darker( fl_darker( selection_color() ) ) ); | |||
| fl_line_style( FL_SOLID, 2 ); | |||
| fl_begin_line(); | |||
| j = 0; | |||
| for ( int x = X; x < X + W; ++x ) | |||
| { | |||
| float v = _peaks[ start + j ] * _scale; | |||
| j += 2; | |||
| fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v )); | |||
| Peak p = (*_clip->peaks())[ x ]; | |||
| p.min *= _scale; | |||
| fl_vertex( x, Y + (H / 2) + ((float)H / 2 * p.min )); | |||
| } | |||
| fl_end_line(); | |||
| fl_begin_line(); | |||
| j = 1; | |||
| for ( int x = X; x < X + W; ++x ) | |||
| { | |||
| float v = _peaks[ start + j ] * _scale; | |||
| j += 2; | |||
| fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v )); | |||
| Peak p = (*_clip->peaks())[ x ]; | |||
| p.max *= _scale; | |||
| fl_vertex( x, Y + (H / 2) + ((float)H / 2 * p.max )); | |||
| } | |||
| fl_end_line(); | |||
| @@ -125,53 +139,12 @@ Waveform::draw ( int X, int Y, int W, int H ) | |||
| } | |||
| void | |||
| Waveform::downsample ( int s, int e, float *mhi, float *mlo ) | |||
| { | |||
| *mhi = -1.0; | |||
| *mlo = 1.0; | |||
| int start = s * 2; | |||
| int end = e * 2; | |||
| for ( int j = start; j < end; ) | |||
| { | |||
| float lo = _peaks[ j++ ]; | |||
| float hi = _peaks[ j++ ]; | |||
| if ( hi > *mhi ) | |||
| *mhi = hi; | |||
| if ( lo < *mlo ) | |||
| *mlo = lo; | |||
| } | |||
| } | |||
| void | |||
| Waveform::normalize ( void ) | |||
| { | |||
| /* float mhi = -1.0; */ | |||
| /* float mlo = 1.0; */ | |||
| float mhi, mlo; | |||
| downsample( _start, _end, &mhi, &mlo ); | |||
| /* int start = _start * 2; */ | |||
| /* int end = _end * 2; */ | |||
| /* for ( int j = start; j < end; ) */ | |||
| /* { */ | |||
| /* float lo = _peaks[ j++ ]; */ | |||
| /* float hi = _peaks[ j++ ]; */ | |||
| /* if ( hi > mhi ) */ | |||
| /* mhi = hi; */ | |||
| /* if ( lo < mlo ) */ | |||
| /* mlo = lo; */ | |||
| /* } */ | |||
| _clip->peaks()->downsample( _start, _end, &mhi, &mlo ); | |||
| _scale = 1.0f / (float)mhi; | |||
| @@ -180,6 +153,5 @@ Waveform::normalize ( void ) | |||
| _scale = fabs( _scale ); | |||
| // printf( "scale = %f, hi=%f, lo=%f\n", _scale, mhi, mlo ); | |||
| redraw(); | |||
| } | |||
| @@ -26,12 +26,16 @@ | |||
| typedef unsigned long tick_t; | |||
| #include "Clip.H" | |||
| class Waveform : public Fl_Widget | |||
| { | |||
| protected: | |||
| float *_peaks; | |||
| Clip *_clip; /* clip this waveform represents */ | |||
| // float *_peaks; | |||
| tick_t _start; | |||
| tick_t _end; | |||
| @@ -43,9 +47,18 @@ public: | |||
| Waveform ( int X, int Y, int W, int H, const char *L=0 ); | |||
| Waveform ( Clip *c ) : Fl_Widget( 0, 0, 500, 100, "" ) | |||
| { | |||
| _clip = c; | |||
| label( _clip->name() ); | |||
| _end = _clip->length(); | |||
| } | |||
| Waveform ( const Waveform & rhs ) : Fl_Widget( rhs.x(), rhs.y(), rhs.w(), rhs.h(), rhs.label() ) | |||
| { | |||
| _peaks = rhs._peaks; | |||
| _clip = rhs._clip; | |||
| _start = rhs._start; | |||
| _end = rhs._end; | |||
| @@ -58,9 +71,9 @@ public: | |||
| void start ( tick_t s ) { _start = s; } | |||
| void end ( tick_t e ) { _end = e; } | |||
| void peaks ( float *p ) { _peaks = p; } | |||
| // void peaks ( float *p ) { _peaks = p; } | |||
| void normalize ( void ); | |||
| void downsample ( int s, int e, float *mhi, float *mlo ); | |||
| void read_peaks ( tick_t X, float *hi, float *lo ); | |||
| }; | |||
| @@ -24,6 +24,7 @@ | |||
| #include <FL/Fl_Scroll.H> | |||
| #include <FL/Fl_Pack.H> | |||
| #include <FL/Fl_Group.H> | |||
| #include <FL/Fl_Slider.H> | |||
| #include "Waveform.H" | |||
| #include "Region.H" | |||
| @@ -48,6 +49,13 @@ init_colors ( void ) | |||
| Timeline timeline; | |||
| void | |||
| cb_zoom ( Fl_Widget *w, void *v ) | |||
| { | |||
| timeline.fpp = ((Fl_Slider*)w)->value(); | |||
| timeline.scroll->redraw(); | |||
| } | |||
| int | |||
| main ( int argc, char **argv ) | |||
| { | |||
| @@ -56,8 +64,8 @@ main ( int argc, char **argv ) | |||
| Fl_Double_Window *main_window = new Fl_Double_Window( 0, 0, 800, 600 ); | |||
| timeline.scroll = new Fl_Scroll( 0, 0, 800, 600 ); | |||
| timeline.fpp = 100; | |||
| timeline.scroll = new Fl_Scroll( 0, 24, 800, 600 - 24 ); | |||
| timeline.fpp = 1; | |||
| Fl_Pack *tracks = new Fl_Pack( 0, 0, 5000, 5000 ); | |||
| tracks->type( Fl_Pack::VERTICAL ); | |||
| @@ -72,30 +80,16 @@ main ( int argc, char **argv ) | |||
| // pack->type( Fl_Pack::VERTICAL ); | |||
| // pack->box( FL_DOWN_BOX ); | |||
| Region *wave = new Region( 0, 0, 5000, 100, "foo" ); | |||
| FILE *fp; | |||
| fp = fopen( "peaks", "r" ); | |||
| // Region *wave = new Region( 0, 0, 5000, 100, "foo" ); | |||
| struct stat st; | |||
| Region *wave = new Region( new Clip( "foo.wav" ) ); | |||
| fstat( fileno( fp ), &st ); | |||
| wave->resize( 0, 0, 500, 100 ); | |||
| size_t len = st.st_size; | |||
| /* float chunk_size; */ | |||
| /* fread( &chunk_size, sizeof( chunk_size ), 1, fp ); */ | |||
| /* printf( "%f\n", chunk_size ); */ | |||
| float *peaks = new float[ len / sizeof( float ) ]; | |||
| fread( peaks, len, 1, fp ); | |||
| wave->peaks( peaks ); | |||
| // wave->peaks( peaks ); | |||
| wave->start( 0 ); | |||
| wave->end( (len / sizeof( float )) / 2 ); | |||
| // wave->end( (len / sizeof( float )) / 2 ); | |||
| wave->end( 50 ); | |||
| wave->color( FL_CYAN ); | |||
| wave->selection_color( fl_darker( FL_GRAY ) ); | |||
| @@ -125,6 +119,12 @@ main ( int argc, char **argv ) | |||
| tracks->end(); | |||
| timeline.scroll->end(); | |||
| Fl_Slider *zoom_slider = new Fl_Slider( 0, 0, 800, 24 ); | |||
| zoom_slider->type( 1 ); | |||
| zoom_slider->callback( cb_zoom, 0 ); | |||
| zoom_slider->range( 1, 256 ); | |||
| zoom_slider->value( 1 ); | |||
| main_window->end(); | |||
| main_window->show(); | |||