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.

127 lines
3.2KB

  1. /*
  2. Copyright (C) 2006-2009 Nasca Octavian Paul and the Vorbis authors
  3. Author: Nasca Octavian Paul and Vorbis authors (XIPHOPHORUS Company)
  4. (some lines of code took from encoder_example.c from vorbis library)
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License (version 2) for more details.
  12. You should have received a copy of the GNU General Public License (version 2)
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <stdlib.h>
  17. #include "VorbisOutputS.h"
  18. using namespace std;
  19. VorbisOutputS::VorbisOutputS(){
  20. outfile=NULL;
  21. opened=false;
  22. };
  23. VorbisOutputS::~VorbisOutputS(){
  24. close();
  25. };
  26. bool VorbisOutputS::newfile(string filename,int samplerate,REALTYPE quality){
  27. close();//inchide un posibil fisier existent
  28. outfile=fopen(filename.c_str(),"wb");
  29. if (!outfile) return false;
  30. vorbis_info_init(&vi);
  31. int ret=vorbis_encode_init_vbr(&vi,2,samplerate,quality/10.0);
  32. if (ret) return false;
  33. //adaug comentariu
  34. vorbis_comment_init(&vc);
  35. vorbis_comment_add_tag(&vc,"program","PaulStretch by Nasca Octavian PAUL");
  36. //setari analiza
  37. vorbis_analysis_init(&vd,&vi);
  38. vorbis_block_init(&vd,&vb);
  39. ogg_stream_init(&os,0x3FB771E2);
  40. ogg_packet header;
  41. ogg_packet header_comm;
  42. ogg_packet header_code;
  43. vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
  44. ogg_stream_packetin(&os,&header);
  45. ogg_stream_packetin(&os,&header_comm);
  46. ogg_stream_packetin(&os,&header_code);
  47. int eos=0;
  48. while(!eos){
  49. int result=ogg_stream_flush(&os,&og);
  50. if(result==0)break;
  51. int tmp=0;
  52. tmp=fwrite(og.header,1,og.header_len,outfile);
  53. tmp=fwrite(og.body,1,og.body_len,outfile);
  54. };
  55. opened=true;
  56. return(true);
  57. };
  58. void VorbisOutputS::close(){
  59. if (!opened) return;
  60. write(0,NULL,NULL);//scriu un pachet de EOS
  61. fclose(outfile);
  62. ogg_stream_clear(&os);
  63. vorbis_block_clear(&vb);
  64. vorbis_dsp_clear(&vd);
  65. vorbis_comment_clear(&vc);
  66. vorbis_info_clear(&vi);
  67. opened=false;
  68. };
  69. void VorbisOutputS::write(int nsmps,REALTYPE *smpsl,REALTYPE *smpsr){
  70. if (!opened) return;
  71. if (nsmps!=0){
  72. float **buffer=vorbis_analysis_buffer(&vd,nsmps);
  73. int i=0;
  74. for (i=0;i<nsmps;i++){
  75. buffer[0][i]=smpsl[i];
  76. buffer[1][i]=smpsr[i];
  77. };
  78. };
  79. vorbis_analysis_wrote(&vd,nsmps);
  80. while(vorbis_analysis_blockout(&vd,&vb)==1){
  81. vorbis_analysis(&vb,NULL);
  82. vorbis_bitrate_addblock(&vb);
  83. while(vorbis_bitrate_flushpacket(&vd,&op)){
  84. ogg_stream_packetin(&os,&op);
  85. int eos=0;
  86. while (!eos){
  87. int result=ogg_stream_pageout(&os,&og);
  88. if(result==0)break;
  89. int tmp;
  90. tmp=fwrite(og.header,1,og.header_len,outfile);
  91. tmp=fwrite(og.body,1,og.body_len,outfile);
  92. if(ogg_page_eos(&og))eos=1;
  93. };
  94. };
  95. };
  96. };