You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6KB

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