paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

AInputS.cpp (2617B)


      1 /*
      2   Copyright (C) 2006-2011 Nasca Octavian Paul
      3   Author: Nasca Octavian Paul
      4 
      5   This program is free software; you can redistribute it and/or modify
      6   it under the terms of version 2 of the GNU General Public License 
      7   as published by the Free Software Foundation.
      8 
      9   This program is distributed in the hope that it will be useful,
     10   but WITHOUT ANY WARRANTY; without even the implied warranty of
     11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12   GNU General Public License (version 2) for more details.
     13 
     14   You should have received a copy of the GNU General Public License (version 2)
     15   along with this program; if not, write to the Free Software Foundation,
     16   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     17 */
     18 
     19 
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include "AInputS.h"
     23 using namespace std;
     24 
     25 AInputS::AInputS(){
     26     handle=AF_NULL_FILEHANDLE;
     27     info.nsamples=0;
     28     info.nchannels=0;
     29     info.samplerate=1;
     30     info.currentsample=0;
     31     eof=false;
     32 };
     33 
     34 AInputS::~AInputS(){
     35     close();
     36 };
     37 
     38 bool AInputS::open(string filename){
     39     close();//inchide un posibil fisier existent
     40     handle=afOpenFile(filename.c_str(),"r",0);
     41 	if (handle==AF_NULL_FILEHANDLE){//eroare
     42 		eof=true;
     43 		return false;
     44 	};
     45     
     46   
     47     afSetVirtualChannels(handle,AF_DEFAULT_TRACK,2);
     48 
     49     eof=false;
     50     info.nsamples=afGetFrameCount(handle,AF_DEFAULT_TRACK);
     51     info.nchannels=afGetVirtualChannels(handle,AF_DEFAULT_TRACK);
     52     info.samplerate=(int) afGetRate(handle,AF_DEFAULT_TRACK);
     53     info.currentsample=0;
     54 	if (info.samplerate==0) return false;
     55         	
     56     //fac ca intrarea sa fie pe 16 biti
     57     afSetVirtualSampleFormat(handle,AF_DEFAULT_TRACK,AF_SAMPFMT_TWOSCOMP,16);
     58     
     59     return true;
     60 };
     61 
     62 void AInputS::close(){
     63     if (handle!=AF_NULL_FILEHANDLE){
     64 	afCloseFile(handle);
     65 	handle=AF_NULL_FILEHANDLE;
     66     };
     67 };
     68 
     69 int AInputS::read(int nsmps,short int *smps){
     70     if (handle==AF_NULL_FILEHANDLE) return 0;
     71 
     72     int readed=afReadFrames(handle,AF_DEFAULT_TRACK,smps,nsmps);
     73     info.currentsample+=readed;
     74     if (readed!=nsmps) {
     75 		if (readed>=0) for (int i=readed;i<nsmps;i++) smps[i]=0;
     76 		info.currentsample=info.nsamples;
     77 		eof=true;
     78     };
     79 	return nsmps;
     80 };
     81 
     82 void AInputS::seek(double pos){
     83     if (handle==AF_NULL_FILEHANDLE) return;
     84     AFframecount p=(AFframecount)(pos*info.nsamples);
     85 	if (p==info.currentsample) return;
     86     AFframecount seek=afSeekFrame(handle,AF_DEFAULT_TRACK,p);
     87 	//
     88 	printf("AInputS::seek %d %d %d - possible audiofile bug (it always seeks to the end of the file) \n",(int)p,(int)seek,(int)afGetFrameCount(handle,AF_DEFAULT_TRACK));
     89     info.currentsample=p;
     90 };
     91