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.

138 lines
3.2KB

  1. /*
  2. Copyright (C) 2006-2009 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. #define BUFSIZE 4096
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "VorbisInputS.h"
  19. using namespace std;
  20. VorbisInputS::VorbisInputS(){
  21. info.nsamples=0;
  22. info.nchannels=0;
  23. info.samplerate=1;
  24. info.currentsample=0;
  25. eof=false;
  26. vorbis_opened=false;
  27. real_channels=1;
  28. buf=NULL;
  29. bufsize=0;
  30. };
  31. VorbisInputS::~VorbisInputS(){
  32. close();
  33. };
  34. bool VorbisInputS::open(string filename){
  35. close();//inchide un posibil fisier existent
  36. eof=true;
  37. FILE *f=fopen(filename.c_str(),"rb");
  38. if (!f) return false;
  39. int result=ov_open(f,&vorbisfile,NULL,0);
  40. if (result<0){
  41. fclose(f);
  42. return false;
  43. };
  44. vorbis_opened=true;
  45. vorbis_info *vinfo=ov_info(&vorbisfile,0);
  46. if (!vinfo) return false;
  47. real_channels=vinfo->channels;
  48. if ((real_channels!=1)&&(real_channels!=2))return false;//only mono&stereo oggs are allowed
  49. info.nsamples=ov_pcm_total(&vorbisfile,0);
  50. info.nchannels=2;
  51. bufsize=real_channels*BUFSIZE;
  52. buf=new short int[bufsize];
  53. info.samplerate=vinfo->rate;
  54. info.currentsample=0;
  55. eof=false;
  56. return(true);
  57. };
  58. void VorbisInputS::close(){
  59. if (vorbis_opened){
  60. delete [] buf;
  61. ov_clear(&vorbisfile);
  62. vorbis_opened=false;
  63. };
  64. };
  65. int VorbisInputS::read(int nsmps,short int *smps){
  66. if (!vorbis_opened) {
  67. eof=true;
  68. return 0;
  69. };
  70. if (eof) {
  71. for (int i=0;i<nsmps;i++) smps[i]=0;
  72. return nsmps;
  73. };
  74. int current_section=0;
  75. int nsmps_todo=nsmps;
  76. int nsmps_done=0;
  77. short int *smps_orig=smps;
  78. while (nsmps_todo>=0){
  79. int chunk_size=(nsmps_todo<BUFSIZE)?nsmps_todo:BUFSIZE;
  80. if (chunk_size==0) break;
  81. int readed=ov_read(&vorbisfile,(char *)buf,chunk_size*sizeof(short int)*real_channels,0,2,1,&current_section);
  82. if ((readed<=0)||(current_section!=0)){
  83. eof=true;
  84. for (int i=0;i<nsmps_todo;i++) smps[i]=0;
  85. return nsmps;
  86. };
  87. readed/=sizeof(short int);//from bytes to short int samples
  88. switch(real_channels){
  89. case 1:
  90. for (int i=0;i<readed;i++){
  91. smps[i*2]=smps[i*2+1]=buf[i];
  92. };
  93. smps+=readed*2;
  94. nsmps_done+=readed;
  95. nsmps_todo-=readed;
  96. break;
  97. case 2:
  98. for (int i=0;i<readed;i++){
  99. smps[i]=buf[i];
  100. };
  101. nsmps_done+=readed/2;
  102. nsmps_todo-=readed/2;
  103. smps+=readed;
  104. break;
  105. };
  106. info.currentsample+=readed/real_channels;
  107. };
  108. return nsmps;
  109. };
  110. void VorbisInputS::seek(double pos){
  111. if (!vorbis_opened) return;
  112. long int p=(long int)(pos*info.nsamples);
  113. ov_pcm_seek(&vorbisfile,p);
  114. info.currentsample=p;
  115. };