diff --git a/Audio_File.C b/Audio_File.C new file mode 100644 index 0000000..6fecbf1 --- /dev/null +++ b/Audio_File.C @@ -0,0 +1,43 @@ + +/*******************************************************************************/ +/* 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 "Audio_File.H" +#include "Audio_File_SF.H" + +/** attmpet to open any supported filetype */ +Audio_File * +Audio_File::from_file ( const char * filename ) +{ + + Audio_File *a; + + if ( ( a = Audio_File_SF::from_file( filename ) ) ) + goto done; + + a->_peaks.open(); + +// TODO: other formats + + return NULL; + +done: + + a->_peaks.open(); + return a; +} diff --git a/Audio_File.H b/Audio_File.H new file mode 100644 index 0000000..31616cd --- /dev/null +++ b/Audio_File.H @@ -0,0 +1,61 @@ + +/*******************************************************************************/ +/* 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 + +/* Base class for all audio file library interfaces */ +#include + +typedef unsigned long nframes_t; +typedef float sample_t; + +#include "Peaks.H" + +class Audio_File +{ + +protected: + + Peaks _peaks; + const char *_filename; + nframes_t _length; /* length of file in samples */ + +public: + + Audio_File ( ) : _peaks( this ) + { + _filename = NULL; + _length = 0; + } + + static Audio_File *from_file ( const char *filename ); + + Peaks const * peaks ( void ) { return &_peaks; } + const char *name ( void ) { return _filename; } + nframes_t length ( void ) { return _length; } + +// Peaks const * peaks ( void ) { return &_peaks; } + + virtual bool open ( void ) = 0; + virtual void close ( void ) = 0; + virtual void seek ( nframes_t offset ) = 0; + virtual nframes_t read ( sample_t *buf, nframes_t len ) = 0; + virtual nframes_t read ( sample_t *buf, nframes_t start, nframes_t end ) = 0; + +}; diff --git a/Clip.C b/Audio_File_SF.C similarity index 69% rename from Clip.C rename to Audio_File_SF.C index 8f9f2c4..c1b4c65 100644 --- a/Clip.C +++ b/Audio_File_SF.C @@ -17,7 +17,7 @@ /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*******************************************************************************/ -#include "Clip.H" +#include "Audio_File_SF.H" #include "Timeline.H" #include @@ -25,56 +25,13 @@ #include #include - -Clip::Clip ( void ) : _peaks( this ) -{ - _filename = NULL; - _length = 0; -} - - -/* Clip::Clip ( const char *filename ) : _peaks( this ) */ -/* { */ -/* _filename = filename; */ - -/* SNDFILE *in; */ -/* SF_INFO si; */ - -/* memset( &si, 0, sizeof( si ) ); */ - -/* if ( ! ( in = sf_open( filename, SFM_READ, &si ) ) ) */ -/* { */ -/* printf( "couldn't open file\n" ); */ -/* return; */ -/* } */ - -/* if ( si.channels != 1 ) */ -/* { */ -/* printf( "error: incompatible format\n" ); */ -/* return; */ -/* } */ - -/* if ( si.samplerate != timeline->sample_rate ) */ -/* { */ -/* printf( "error: samplerate mismatch!\n" ); */ -/* return; */ -/* } */ - -/* _length = si.frames; */ - -/* sf_close( in ); */ - -/* _peaks.open(); */ -/* } */ - - -Clip * -Clip::from_file ( const char *filename ) +Audio_File_SF * +Audio_File_SF::from_file ( const char *filename ) { SNDFILE *in; SF_INFO si; - Clip *c = NULL; + Audio_File_SF *c = NULL; memset( &si, 0, sizeof( si ) ); @@ -96,18 +53,15 @@ Clip::from_file ( const char *filename ) goto invalid; } - c = new Clip; + c = new Audio_File_SF; c->_filename = filename; c->_length = si.frames; sf_close( in ); - c->_peaks.open(); - return c; - invalid: sf_close( in ); @@ -115,7 +69,7 @@ invalid: } bool -Clip::open ( void ) +Audio_File_SF::open ( void ) { SF_INFO si; @@ -128,27 +82,26 @@ Clip::open ( void ) } void -Clip::close ( void ) +Audio_File_SF::close ( void ) { sf_close( _in ); } void -Clip::seek ( nframes_t offset ) +Audio_File_SF::seek ( nframes_t offset ) { sf_seek( _in, offset, SEEK_SET ); } - nframes_t -Clip::read ( sample_t *buf, nframes_t len ) +Audio_File_SF::read ( sample_t *buf, nframes_t len ) { return sf_read_float ( _in, buf, len ); } /** read samples from /start/ to /end/ into /buf/ */ nframes_t -Clip::read ( sample_t *buf, nframes_t start, nframes_t end ) +Audio_File_SF::read ( sample_t *buf, nframes_t start, nframes_t end ) { open(); diff --git a/Clip.H b/Audio_File_SF.H similarity index 76% rename from Clip.H rename to Audio_File_SF.H index 6d043ee..8b74648 100644 --- a/Clip.H +++ b/Audio_File_SF.H @@ -17,36 +17,19 @@ /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*******************************************************************************/ -#pragma once - -typedef unsigned long nframes_t; -typedef float sample_t; - -#include "Peaks.H" +#include "Audio_File.H" #include -class Clip +class Audio_File_SF : public Audio_File { - const char *_filename; - - Peaks _peaks; - - nframes_t _length; /* length of clip in samples */ +// Audio_File_SF ( const char *filename ) SNDFILE *_in; public: - Clip ( ); - -// Clip ( const char *filename ); - - static Clip *from_file ( const char *filename ); - - Peaks const * peaks ( void ) { return &_peaks; } - const char *name ( void ) { return _filename; } - nframes_t length ( void ) { return _length; } + static Audio_File_SF *from_file ( const char *filename ); bool open ( void ); void close ( void ); diff --git a/Audio_Track.H b/Audio_Track.H index efbc019..4e93f3e 100644 --- a/Audio_Track.H +++ b/Audio_Track.H @@ -66,7 +66,7 @@ public: printf( "pasted file \"%s\"\n", file ); - Clip *c = Clip::from_file( file ); + Audio_File *c = Audio_File::from_file( file ); if ( ! c ) { diff --git a/Makefile b/Makefile index 6729f33..85efb3d 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CXXFLAGS=-ggdb -Wall -O0 LIBS=-lsndfile `fltk-config --ldflags` # CXXFLAGS=`fltk-config -cxxflags` -SRCS= Clip.C Waveform.C Region.C Peaks.C main.C Track.C Timeline.C +SRCS= Waveform.C Region.C Peaks.C main.C Track.C Timeline.C Audio_File.C Audio_File_SF.C OBJS=$(SRCS:.C=.o) @@ -24,7 +24,7 @@ test: $(OBJS) $(CXX) $(CXXFLAGS) $(LIBS) $(OBJS) -o $@ clean: - rm -f $(OBJS) test + rm -f $(OBJS) test makedepend valgrind: valgrind ./test diff --git a/Peaks.C b/Peaks.C index 08e2838..211dfe0 100644 --- a/Peaks.C +++ b/Peaks.C @@ -31,7 +31,7 @@ #include -#include "Clip.H" +#include "Audio_File.H" #include "assert.h" diff --git a/Peaks.H b/Peaks.H index 2c0dd7f..dcef47c 100644 --- a/Peaks.H +++ b/Peaks.H @@ -28,7 +28,7 @@ struct Peak { float max; }; -class Clip; +class Audio_File; class Peaks { @@ -56,7 +56,7 @@ class Peaks static peakbuffer peakbuf; - Clip *_clip; + Audio_File *_clip; peakdata *_peaks; @@ -67,9 +67,11 @@ class Peaks Peak & peak ( nframes_t start, nframes_t end ) const; + Peaks ( ); + public: - Peaks ( Clip *c ) + Peaks ( Audio_File *c ) { _peaks = new peakdata; diff --git a/Region.C b/Region.C index 171360f..fd5a16c 100644 --- a/Region.C +++ b/Region.C @@ -81,7 +81,7 @@ Region::Region ( const Region & rhs ) _scale = rhs._scale; } -Region::Region ( Clip *c ) +Region::Region ( Audio_File *c ) { init(); _clip = c; diff --git a/Region.H b/Region.H index b05dc78..81646a9 100644 --- a/Region.H +++ b/Region.H @@ -23,7 +23,7 @@ // #include "Waveform.H" -#include "Clip.H" +#include "Audio_File.H" #include "Track.H" #include "Timeline.H" @@ -38,7 +38,7 @@ using namespace std; class Region : public Track_Widget { - Clip *_clip; /* clip this region represents */ + Audio_File *_clip; /* clip this region represents */ float _scale; /* amplitude adjustment */ @@ -56,7 +56,7 @@ public: Fl_Boxtype box ( void ) const { return Region::_box; } Region ( const Region & rhs ); - Region ( Clip *c ); + Region ( Audio_File *c ); int handle ( int m ); void draw_box( int X, int Y, int W, int H ); diff --git a/Timeline.H b/Timeline.H index 3457896..677267e 100644 --- a/Timeline.H +++ b/Timeline.H @@ -24,7 +24,7 @@ #include #include -#include "Clip.H" +#include "Audio_File.H" // just for nframes_t #include #include diff --git a/Waveform.C b/Waveform.C index 62a8c36..9f03e83 100644 --- a/Waveform.C +++ b/Waveform.C @@ -24,27 +24,14 @@ #include #include "Timeline.H" -// #include "Waveform.H" -#include "Clip.H" +#include "Audio_File.H" -// extern Timeline timeline; -// #include "Timeline.H" #include -/* void */ -/* Waveform::draw ( void ) */ -/* { */ -/* int X, Y, W, H; */ - -/* fl_clip_box( x(), y(), w(), h(), X, Y, W, H ); */ - -/* draw( X, y(), W, h() ); */ -/* } */ - /** draw a portion of /clip/'s waveform. coordinates are the portion to draw */ void -draw_waveform ( int X, int Y, int W, int H, Clip *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color ) +draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color ) { fl_push_clip( X, Y, W, H ); diff --git a/Waveform.H b/Waveform.H index f190ad0..45d6f06 100644 --- a/Waveform.H +++ b/Waveform.H @@ -24,6 +24,6 @@ #include "Timeline.H" -#include "Clip.H" +#include "Audio_File.H" -void draw_waveform ( int X, int Y, int W, int H, Clip *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color ); +void draw_waveform ( int X, int Y, int W, int H, Audio_File *_clip, nframes_t _start, nframes_t _end, float _scale, Fl_Color color );