non/Engine/Audio_File_SF.C

126 lines
3.2 KiB
C++
Raw Normal View History

2008-02-17 23:03:31 +01:00
/*******************************************************************************/
/* 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_SF.H"
2008-03-20 09:43:45 +01:00
// #include "Timeline.H"
2008-02-17 23:03:31 +01:00
#include <sndfile.h>
#include <stdlib.h>
#include <string.h>
Audio_File_SF *
Audio_File_SF::from_file ( const char *filename )
{
2008-02-17 23:03:31 +01:00
SNDFILE *in;
SF_INFO si;
Audio_File_SF *c = NULL;
2008-02-17 23:03:31 +01:00
memset( &si, 0, sizeof( si ) );
if ( ! ( in = sf_open( filename, SFM_READ, &si ) ) )
{
printf( "couldn't open file\n" );
return NULL;
}
2008-02-17 23:03:31 +01:00
2008-03-20 09:43:45 +01:00
/* if ( si.samplerate != timeline->sample_rate() ) */
/* { */
/* printf( "error: samplerate mismatch!\n" ); */
/* goto invalid; */
/* } */
c = new Audio_File_SF;
2008-02-17 23:03:31 +01:00
2008-02-28 13:35:19 +01:00
c->_filename = strdup( filename );
c->_length = si.frames;
2008-02-22 15:03:00 +01:00
c->_channels = si.channels;
2008-02-17 23:03:31 +01:00
2008-02-22 19:40:51 +01:00
c->_in = in;
// sf_close( in );
2008-02-17 23:03:31 +01:00
return c;
invalid:
sf_close( in );
return NULL;
}
2008-02-17 23:03:31 +01:00
bool
Audio_File_SF::open ( void )
2008-02-17 23:03:31 +01:00
{
2008-02-22 19:40:51 +01:00
/* SF_INFO si; */
/* memset( &si, 0, sizeof( si ) ); */
2008-02-17 23:03:31 +01:00
2008-02-22 19:40:51 +01:00
/* if ( ! ( _in = sf_open( _filename, SFM_READ, &si ) ) ) */
/* return false; */
2008-02-17 23:03:31 +01:00
2008-02-22 19:57:45 +01:00
seek( 0 );
2008-02-17 23:03:31 +01:00
return true;
}
void
Audio_File_SF::close ( void )
2008-02-17 23:03:31 +01:00
{
2008-02-22 19:40:51 +01:00
// sf_close( _in );
2008-02-17 23:03:31 +01:00
}
void
Audio_File_SF::seek ( nframes_t offset )
2008-02-17 23:03:31 +01:00
{
sf_seek( _in, offset, SEEK_SET );
}
nframes_t
2008-02-22 15:03:00 +01:00
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
2008-02-17 23:03:31 +01:00
{
2008-02-22 15:03:00 +01:00
if ( _channels == 1 )
return sf_read_float ( _in, buf, len );
sample_t *tmp = new sample_t[ len * _channels ];
nframes_t rlen = sf_readf_float( _in, tmp, len );
for ( int i = channel; i < rlen; i += _channels )
*(buf++) = tmp[ i ];
delete tmp;
return rlen;
2008-02-17 23:03:31 +01:00
}
/** read samples from /start/ to /end/ into /buf/ */
nframes_t
2008-02-22 15:03:00 +01:00
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end )
2008-02-17 23:03:31 +01:00
{
open();
seek( start );
2008-02-22 15:03:00 +01:00
nframes_t len = read( buf, channel, end - start );
2008-02-17 23:03:31 +01:00
close();
return len;
}