From 928780a820e2c78d56fa9e50b0fed502d4266a7f Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Tue, 15 Apr 2008 19:47:51 -0500 Subject: [PATCH] Move some DSPish code into functions in dsp.c --- Timeline/Audio_File.C | 2 +- Timeline/Audio_Track.C | 15 ++----- Timeline/Makefile | 1 + Timeline/Region.C | 6 +-- Timeline/dsp.c | 94 ++++++++++++++++++++++++++++++++++++++++++ Timeline/dsp.h | 30 ++++++++++++++ 6 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 Timeline/dsp.c create mode 100644 Timeline/dsp.h diff --git a/Timeline/Audio_File.C b/Timeline/Audio_File.C index f0227b2..36e29bc 100644 --- a/Timeline/Audio_File.C +++ b/Timeline/Audio_File.C @@ -68,7 +68,7 @@ Audio_File::read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, P *pbuf = new Peak[ *peaks * *channels ]; - /* deinterlace */ + /* deintereave peaks */ int k = 0; for ( int i = 0; i < *channels; i++ ) for ( int j = i; j < *peaks * *channels; j += *channels ) diff --git a/Timeline/Audio_Track.C b/Timeline/Audio_Track.C index d8154e6..5221e49 100644 --- a/Timeline/Audio_Track.C +++ b/Timeline/Audio_Track.C @@ -19,6 +19,8 @@ #include "Audio_Track.H" +#include "dsp.h" + #include static @@ -142,18 +144,9 @@ Audio_Track::play ( sample_t *buf, nframes_t frame, nframes_t nframes, int chann continue; if ( channels == 1 ) - { -// memcpy( buf, cbuf, nframes * sizeof( sample_t ) ); - for ( unsigned int j = 0; j < nfr; ++j ) - buf[ j ] += cbuf[ j ]; - } + buffer_mix( buf, cbuf, nframes ); else - { - /* mix and interleave */ - int k = 0; - for ( unsigned int j = i; k < nfr; j += channels ) - buf[ j ] += cbuf[ k++ ]; - } + buffer_interleave_one_channel_and_mix( buf, cbuf, i, channels, nframes ); } } diff --git a/Timeline/Makefile b/Timeline/Makefile index e2cff85..ef96d42 100644 --- a/Timeline/Makefile +++ b/Timeline/Makefile @@ -13,6 +13,7 @@ SRCS= \ Audio_File_SF.C \ Port.C \ Disk_Stream.C \ + dsp.c \ Engine.C \ Transport.C \ Loggable.C \ diff --git a/Timeline/Region.C b/Timeline/Region.C index 0398a17..47ce61e 100644 --- a/Timeline/Region.C +++ b/Timeline/Region.C @@ -22,6 +22,8 @@ #include "Timeline.H" #include "Waveform.H" +#include "dsp.h" + #include #include #include @@ -850,9 +852,7 @@ Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) co /* apply gain */ - if ( _scale != 1.0f ) - for ( int i = cnt; i--; ) - buf[i] *= _scale; + buffer_apply_gain( buf, cnt, _scale ); /* perform declicking if necessary */ diff --git a/Timeline/dsp.c b/Timeline/dsp.c new file mode 100644 index 0000000..0d48be2 --- /dev/null +++ b/Timeline/dsp.c @@ -0,0 +1,94 @@ + +/*******************************************************************************/ +/* 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. */ +/*******************************************************************************/ + +/* General DSP related functions. */ + +#include "dsp.h" + +/* TODO: these functions are all targets for optimization (SSE?) */ + +void +buffer_apply_gain ( sample_t *buf, nframes_t nframes, float g ) +{ + if ( g != 1.0f ) + while ( nframes-- ) + *(buf++) *= g; +} + +void +buffer_apply_gain_buffer ( sample_t *buf, sample_t *gainbuf, nframes_t nframes ) +{ + while ( nframes-- ) + *(buf++) *= *(gainbuf++); +} + +void +buffer_mix ( sample_t *dst, sample_t *src, nframes_t nframes ) +{ + while ( nframes-- ) + *(dst++) += *(src++); +} + +void +buffer_mix_with_gain ( sample_t *dst, sample_t *src, nframes_t nframes, float g ) +{ + while ( nframes-- ) + *(dst++) += *(src++) * g; +} + +void +buffer_interleave_one_channel ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes ) +{ + nframes *= channels; + + dst += channel; + + while ( nframes-- ) + { + *dst = *(src++); + dst += channels; + } +} + +void +buffer_interleave_one_channel_and_mix ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes ) +{ + nframes *= channels; + + dst += channel; + + while ( nframes-- ) + { + *dst += *(src++); + dst += channels; + } +} + +void +buffer_deinterleave_one_channel ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes ) +{ + nframes *= channels; + src += channel; + + while ( nframes-- ) + { + *(dst++) = *src; + src += channels; + } +} diff --git a/Timeline/dsp.h b/Timeline/dsp.h new file mode 100644 index 0000000..65cf34f --- /dev/null +++ b/Timeline/dsp.h @@ -0,0 +1,30 @@ + +/*******************************************************************************/ +/* 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 + +#include "types.h" + +void buffer_apply_gain ( sample_t *buf, nframes_t nframes, float g ); +void buffer_apply_gain_buffer ( sample_t *buf, sample_t *gainbuf, nframes_t nframes ); +void buffer_mix ( sample_t *dst, sample_t *src, nframes_t nframes ); +void buffer_mix_with_gain ( sample_t *dst, sample_t *src, nframes_t nframes, float g ); +void buffer_interleave_one_channel ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes ); +void buffer_interleave_one_channel_and_mix ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes ); +void buffer_deinterleave_one_channel ( sample_t *dst, sample_t *src, int channel, int channels, nframes_t nframes );