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.

210 lines
4.7KB

  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 //minimum 2 buffere de mp3
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "MP3InputS.h"
  20. using namespace std;
  21. MP3InputS::MP3InputS(){
  22. info.nsamples=0;
  23. info.nchannels=2;
  24. info.samplerate=44100;
  25. info.currentsample=0;
  26. eof=false;
  27. MP3_opened=false;
  28. f=NULL;
  29. buf=NULL;
  30. };
  31. MP3InputS::~MP3InputS(){
  32. close();
  33. };
  34. bool MP3InputS::open(string filename){
  35. pcm_remained=0;
  36. close();//inchide un posibil fisier existent
  37. eof=true;
  38. f=fopen(filename.c_str(),"rb");
  39. if (!f) return false;
  40. mad_stream_init(&stream);
  41. mad_frame_init(&frame);
  42. mad_synth_init(&synth);
  43. buf=new unsigned char[BUFSIZE];
  44. MP3_opened=true;
  45. eof=false;
  46. fseek(f,0,SEEK_END);
  47. info.nsamples=ftell(f);
  48. rewind(f);
  49. info.currentsample=0;
  50. info.samplerate=44100;
  51. {//find the samplerate
  52. const int n=8192;
  53. short int tmpsmp[n*2];
  54. read(n,tmpsmp);//read just a sample (and ignore it) to fill info.samplerate
  55. seek(0);
  56. pcm_remained=0;
  57. info.currentsample=0;
  58. //reinit the mad
  59. mad_synth_finish(&synth);
  60. mad_frame_finish(&frame);
  61. mad_stream_finish(&stream);
  62. mad_stream_init(&stream);
  63. mad_frame_init(&frame);
  64. mad_synth_init(&synth);
  65. };
  66. info.nchannels=2;
  67. return true;
  68. };
  69. void MP3InputS::close(){
  70. if (MP3_opened){
  71. MP3_opened=false;
  72. mad_synth_finish(&synth);
  73. mad_frame_finish(&frame);
  74. mad_stream_finish(&stream);
  75. delete [] buf;
  76. fclose(f);
  77. f=NULL;
  78. };
  79. };
  80. int MP3InputS::read(int nsmps,short int *smps){
  81. int orig_nsmps=nsmps;
  82. short int *orig_smps=smps;
  83. if (!MP3_opened) {
  84. eof=true;
  85. return 0;
  86. };
  87. if (eof) {
  88. for (int i=0;i<nsmps;i++) smps[i]=0;
  89. return nsmps;
  90. };
  91. if (pcm_remained>0){
  92. int nconverted=pcm_remained;
  93. if (nconverted>nsmps) nconverted=nsmps;
  94. convertsmps(nconverted,smps,synth.pcm.length-pcm_remained);
  95. nsmps-=nconverted;
  96. smps+=nconverted*2;
  97. if (nsmps<=0){
  98. pcm_remained-=orig_nsmps;
  99. return orig_nsmps;
  100. };
  101. };
  102. while (nsmps>0){
  103. int remaining=0;
  104. if (stream.next_frame!=NULL){
  105. remaining=stream.bufend-stream.next_frame;
  106. memmove(buf,stream.next_frame,remaining);
  107. };
  108. int readsize=BUFSIZE-remaining;
  109. int readed=fread(buf+remaining,1,readsize,f);
  110. if (feof(f)) eof=true;
  111. if ((eof) || (readed<1)){
  112. for (int i=0;i<orig_nsmps;i++){
  113. orig_smps[i]=0;
  114. };
  115. return orig_nsmps;
  116. };
  117. info.currentsample+=readed;
  118. if (readed!=readsize) {
  119. for (int i=readed;i<readsize;i++) buf[i+remaining]=0;
  120. };
  121. mad_stream_buffer(&stream, buf, BUFSIZE);
  122. while(1){
  123. if (mad_frame_decode(&frame, &stream)!=0){
  124. if (stream.error==MAD_ERROR_BUFLEN) break;
  125. if (stream.error==MAD_ERROR_LOSTSYNC) continue;
  126. if (!MAD_RECOVERABLE(stream.error)) {
  127. printf("Non recoverable MP3 error: 0x%x\n",stream.error);///in caz ca nu este recuperabil
  128. close();
  129. return orig_nsmps;
  130. };
  131. };
  132. mad_synth_frame(&synth, &frame);
  133. int n=synth.pcm.length;
  134. if (synth.pcm.samplerate!=0) info.samplerate=synth.pcm.samplerate;
  135. if (nsmps<synth.pcm.length){
  136. pcm_remained=synth.pcm.length-nsmps;
  137. n=nsmps;
  138. }else{
  139. pcm_remained=0;
  140. };
  141. convertsmps(n,smps,0);
  142. nsmps-=n;
  143. smps+=n*2;
  144. if (nsmps<=0) return orig_nsmps;
  145. };
  146. };
  147. return orig_nsmps;
  148. };
  149. void MP3InputS::convertsmps(int nsmps, short int *smps,int pcmstart){
  150. mad_fixed_t *l=synth.pcm.samples[0],*r=synth.pcm.samples[0];
  151. if (synth.pcm.channels==2) r=synth.pcm.samples[1];
  152. l+=pcmstart;r+=pcmstart;
  153. for (int i=0;i<nsmps;i++){//todo optimize (avoid reconverting r[] for mono samples)
  154. smps[i*2]=madpcm2short(l[i]);
  155. smps[i*2+1]=madpcm2short(r[i]);
  156. };
  157. };
  158. short int MP3InputS::madpcm2short(mad_fixed_t x){
  159. if (x>=MAD_F_ONE) x=MAD_F_ONE-1;
  160. else if (x<=-MAD_F_ONE) x=-MAD_F_ONE+1;
  161. int result= x >> (MAD_F_FRACBITS + 1 - 16);
  162. return result;
  163. };
  164. void MP3InputS::seek(double pos){
  165. if (!MP3_opened) return;
  166. pcm_remained=0;
  167. int p=(int)(pos*info.nsamples);
  168. info.currentsample=p;
  169. fseek(f,p,SEEK_SET);
  170. /* if (p==0) {
  171. fseek(f,0,SEEK_SET);
  172. }else{
  173. //todo add other thing here
  174. fseek(f,p,SEEK_SET);
  175. };
  176. */
  177. };