paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

VorbisOutputS.cpp (3298B)


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