dec_snd.cpp Source File

Main Page Modules Class Hierarchy Compound List File List Compound Members File Members

# dec_snd.cpp

    00001 /* MuSE - Multiple Streaming Engine
    00002  * Copyright (C) 2004 Angelo Michele, Failla aka pallotron <pallotron@freaknet.org>
    00003  *
    00004  * This source code is free software; you can redistribute it and/or
    00005  * modify it under the terms of the GNU Public License as published
    00006  * by the Free Software Foundation; either version 2 of the License,
    00007  * or (at your option) any later version.
    00008  *
    00009  * This source code is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00012  * Please refer to the GNU Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU Public License along with
    00015  * this source code; if not, write to:
    00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017  *
    00018  *
    00019  */
    00020
    00021
    00022 #include <dec_snd.h>
    00023 #include <jutils.h>
    00024 #include <config.h>
    00025
    00026 #ifdef HAVE_SNDFILE
    00027
    00028 /* ----- LibSndFile input channel ----- */
    00029
    00030 MuseDecSndFile::MuseDecSndFile ():MuseDec (){
    00031     func ("MuseDecSndFile::MuseDecSndFile()");
    00032     strncpy (name, "Snd", 4);
    00033     memset(&sf_info_struct, 0, sizeof(sf_info_struct));
    00034 }
    00035
    00036 MuseDecSndFile::~MuseDecSndFile (){
    00037     func ("MuseDecSndFile::~MuseSndFile()");
    00038     sf_close (sf);
    00039 }
    00040
    00041 int MuseDecSndFile::load (char *file) {
    00042
    00043         int res;
    00044         /*  0 => error
    00045          *  1 => success && seekable
    00046          *  2 => success && !seekable
    00047          */
    00048
    00049         /* all the info about the audio file into the sf_info_struct struct */
    00050         if(!(sf = sf_open(file, SFM_READ, &sf_info_struct))) {
    00051                 warning("MuseDecSndFile:_load(): cannot open input file");
    00052                 return (0);
    00053         }
    00054         /*
    00055          * this is sndfile file info structure
    00056          *
    00057          * typedef struct
    00058          *     {    sf_count_t  frames ;     // used to be called samples
    00059          *          int         samplerate ;
    00060          *          int         channels ;
    00061          *          int         format ;
    00062          *          int         sections ;
    00063          *          int         seekable ;
    00064          *     } SF_INFO ;
    00065          */
    00066         samplerate = sf_info_struct.samplerate;
    00067         channels = sf_info_struct.channels;
    00068         seekable = sf_info_struct.seekable ? true : false;
    00069
    00070         func("Opened audio file: samplerate => %d, channels => %d, seekable => %s",
    00071                 samplerate, channels, seekable ? "true" : "false");
    00072
    00073         framepos = 0;
    00074
    00075         if(seekable) {
    00076                 frametot = sf_info_struct.frames;
    00077                 func("Audio file is seekable: total frames: %d", frametot);
    00078                 res = 1;
    00079         }
    00080         else res = 2;
    00081
    00082         return (res);
    00083
    00084 }
    00085
    00086 IN_DATATYPE *MuseDecSndFile::get_audio () {
    00087
    00088         frames = sf_read_short(sf, snd_buffer, IN_CHUNK);
    00089
    00090         if(frames!=0) {
    00091
    00092                 framepos += frames;
    00093                 fps = samplerate;
    00094                 func("MuseDecSndFile::get_audio => Frames readed: %d/%d", framepos, frametot);
    00095                 return ((IN_DATATYPE *) snd_buffer);
    00096
    00097         } else { framepos=0; eos = true; return (NULL); }
    00098 }
    00099
    00100 bool MuseDecSndFile::seek (float pos) {
    00101
    00102         if(pos==0.0) {
    00103
    00104                 framepos = 0;
    00105                 sf_seek(sf, 0, SEEK_SET);
    00106                 func("MuseDecSndFile::seek => Stop. Return to the begin of the track");
    00107
    00108         } else  {
    00109
    00110                 if((framepos = sf_seek(sf, (sf_count_t)(frametot * pos), SEEK_SET))==-1) {
    00111                         func("MuseDecSndFile::seek error"); //,sf_strerror(sf));
    00112                         return false;
    00113                 }
    00114                 func("MuseDecSndFile::seek at position %d/%d", framepos, frametot);
    00115
    00116         }
    00117
    00118         return true;
    00119 }
    00120
    00121 #endif /* HAVE SNDFILE */