Info vi = new Info(); // struct that stores all the static vorbis bitstream settings #endregion Fields #region Constructors public OggMediaStreamSource(Stream input) { convbuffer=new byte[convsize]; vb = new Block (vd); if (input == null) throw new ArgumentNullException ("input"); this.input = input; }
public override int inverse(Block vb, Object vl, float[][] fin, int[] nonzero, int ch) { //System.err.println("Residue0.inverse"); int i=0; for(i=0;i<ch;i++)if(nonzero[i]!=0)break; if(i==ch)return(0); /* no nonzero vectors */ return(_2inverse(vb, vl, fin, ch)); }
public override Object inverse1(Block vb, Object i, Object memo) { //System.err.println("Floor0.inverse "+i.getClass()+"]"); LookFloor0 look=(LookFloor0)i; InfoFloor0 info=look.vi; float[] lsp=null; if(memo is float[]) { lsp=(float[])memo; } int ampraw=vb.opb.read(info.ampbits); if(ampraw>0) { // also handles the -1 out of data case int maxval=(1<<info.ampbits)-1; float amp=(float)ampraw/maxval*info.ampdB; int booknum=vb.opb.read(ilog(info.numbooks)); if(booknum!=-1 && booknum<info.numbooks) { CodeBook b=vb.vd.fullbooks[info.books[booknum]]; float last=0.0f; if(lsp==null||lsp.Length<look.m+1) { lsp=new float[look.m+1]; } else { for(int j=0; j<lsp.Length; j++)lsp[j]=0.0f; } for(int j=0;j<look.m;j+=b.dim) { if(b.decodev_set(lsp, j, vb.opb, b.dim)==-1) { //goto eop; return(null); } } for(int j=0;j<look.m;) { for(int k=0;k<b.dim;k++,j++)lsp[j]+=last; last=lsp[j-1]; } lsp[look.m]=amp; return(lsp); } } // eop: return(null); }
public override int inverse(Block vb, Object vl, float[][] fin, int[] nonzero, int ch) { //System.err.println("Residue0.inverse"); int used=0; for(int i=0;i<ch;i++) { if(nonzero[i]!=0) { fin[used++]=fin[i]; } } if(used!=0) return(_01inverse(vb, vl, fin, used, 0)); else return(0); }
public VorbisFileInstance(VorbisFile sourceFile) { vorbisFile = sourceFile; oy = new SyncState(); bittrack = 0; samptrack = 0; os = new StreamState(); // take physical pages, weld into a logical // stream of packets vd = new DspState(); // central working state for // the packet->PCM decoder vb = new Block(vd); // local working space for packet->PCM decode raw_seek(0); lastStreamPosition = sourceFile.datasource.Position; }
/// <summary> /// Decodes a stream of Ogg Vorbis data into wav file format. /// </summary> /// <returns> /// A MemoryStream with the entire decoded stream. /// </returns> /// <param name='input'> /// Ogg Vorbis data to be decoded /// </param> /// <param name='output'> /// <para>Stream to write wav data.</para> /// <para>If writeWavHeader is true then this stream must be seekable.</para> /// </param> /// <param name='writeWavHeader'> /// Write wav header in the beginning of the returned stream. /// </param> public static void DecodeStream(Stream input, Stream output, bool writeWavHeader) { int convsize = 4096 * 2; byte[] convbuffer = new byte[convsize]; // take 8k out of the data segment, not the stack long start = output.Position; DebugWriter s_err = new DebugWriter(); if (writeWavHeader && !output.CanSeek) throw new ArgumentException("To write wav header, the output stream must be seekable.", "output"); if (writeWavHeader) output.Seek(HEADER_SIZE, SeekOrigin.Current); // reserve place for WAV header SyncState oy = new SyncState(); // sync and verify incoming physical bitstream StreamState os = new StreamState(); // take physical pages, weld into a logical stream of packets Page og = new Page(); // one Ogg bitstream page. Vorbis packets are inside Packet op = new Packet(); // one raw packet of data for decode Info vi = new Info(); // struct that stores all the static vorbis bitstream settings Comment vc = new Comment(); // struct that stores all the bitstream user comments DspState vd = new DspState(); // central working state for the packet->PCM decoder Block vb = new Block(vd); // local working space for packet->PCM decode int bytes = 0; // Decode setup oy.init(); // Now we can read pages // we repeat if the bitstream is chained while (true) { int eos = 0; // grab some data at the head of the stream. We want the first page // (which is guaranteed to be small and only contain the Vorbis // stream initial header) We need the first page to get the stream // serialno. // submit a 4k block to libvorbis' Ogg layer int index = oy.buffer(4096); bytes = input.Read(oy.data, index, 4096); oy.wrote(bytes); // Get the first page. if (oy.pageout(og) != 1) { // have we simply run out of data? If so, we're done. if (bytes < 4096) break; // error case. Must not be Vorbis data s_err.WriteLine("Input does not appear to be an Ogg bitstream."); } // Get the serial number and set up the rest of decode. // serialno first; use it to set up a logical stream os.init(og.serialno()); // extract the initial header from the first page and verify that the // Ogg bitstream is in fact Vorbis data // I handle the initial header first instead of just having the code // read all three Vorbis headers at once because reading the initial // header is an easy way to identify a Vorbis bitstream and it's // useful to see that functionality seperated out. vi.init(); vc.init(); if (os.pagein(og) < 0) { // error; stream version mismatch perhaps s_err.WriteLine("Error reading first page of Ogg bitstream data."); } if (os.packetout(op) != 1) { // no page? must not be vorbis s_err.WriteLine("Error reading initial header packet."); } if (vi.synthesis_headerin(vc, op) < 0) { // error case; not a vorbis header s_err.WriteLine("This Ogg bitstream does not contain Vorbis audio data."); } // At this point, we're sure we're Vorbis. We've set up the logical // (Ogg) bitstream decoder. Get the comment and codebook headers and // set up the Vorbis decoder // The next two packets in order are the comment and codebook headers. // They're likely large and may span multiple pages. Thus we reead // and submit data until we get our two pacakets, watching that no // pages are missing. If a page is missing, error out; losing a // header page is the only place where missing data is fatal. */ int i = 0; while (i < 2) { while (i < 2) { int result = oy.pageout(og); if (result == 0) break; // Need more data // Don't complain about missing or corrupt data yet. We'll // catch it at the packet output phase if (result == 1) { os.pagein(og); // we can ignore any errors here // as they'll also become apparent // at packetout while (i < 2) { result = os.packetout(op); if (result == 0) break; if (result == -1) { // Uh oh; data at some point was corrupted or missing! // We can't tolerate that in a header. Die. s_err.WriteLine("Corrupt secondary header. Exiting."); } vi.synthesis_headerin(vc, op); i++; } } } // no harm in not checking before adding more index = oy.buffer(4096); bytes = input.Read(oy.data, index, 4096); if (bytes == 0 && i < 2) { s_err.WriteLine("End of file before finding all Vorbis headers!"); } oy.wrote(bytes); } // Throw the comments plus a few lines about the bitstream we're // decoding { byte[][] ptr = vc.user_comments; for (int j = 0; j < vc.user_comments.Length; j++) { if (ptr [j] == null) break; s_err.WriteLine(vc.getComment(j)); } s_err.WriteLine("\nBitstream is " + vi.channels + " channel, " + vi.rate + "Hz"); s_err.WriteLine("Encoded by: " + vc.getVendor() + "\n"); } convsize = 4096 / vi.channels; // OK, got and parsed all three headers. Initialize the Vorbis // packet->PCM decoder. vd.synthesis_init(vi); // central decode state vb.init(vd); // local state for most of the decode // so multiple block decodes can // proceed in parallel. We could init // multiple vorbis_block structures // for vd here float[][][] _pcm = new float[1][][]; int[] _index = new int[vi.channels]; // The rest is just a straight decode loop until end of stream while (eos == 0) { while (eos == 0) { int result = oy.pageout(og); if (result == 0) break; // need more data if (result == -1) { // missing or corrupt data at this page position s_err.WriteLine("Corrupt or missing data in bitstream; continuing..."); } else { os.pagein(og); // can safely ignore errors at // this point while (true) { result = os.packetout(op); if (result == 0) break; // need more data if (result == -1) { // missing or corrupt data at this page position // no reason to complain; already complained above } else { // we have a packet. Decode it int samples; if (vb.synthesis(op) == 0) { // test for success! vd.synthesis_blockin(vb); } // **pcm is a multichannel float vector. In stereo, for // example, pcm[0] is left, and pcm[1] is right. samples is // the size of each channel. Convert the float values // (-1.<=range<=1.) to whatever PCM format and write it out while ((samples = vd.synthesis_pcmout(_pcm, _index)) > 0) { float[][] pcm = _pcm [0]; bool clipflag = false; int bout = (samples < convsize ? samples : convsize); // convert floats to 16 bit signed ints (host order) and // interleave for (i = 0; i < vi.channels; i++) { int ptr = i * 2; //int ptr=i; int mono = _index [i]; for (int j = 0; j < bout; j++) { int val = (int)(pcm [i] [mono + j] * 32767.0); // short val=(short)(pcm[i][mono+j]*32767.); // int val=(int)Math.round(pcm[i][mono+j]*32767.); // might as well guard against clipping if (val > 32767) { val = 32767; clipflag = true; } if (val < -32768) { val = -32768; clipflag = true; } if (val < 0) val = val | 0x8000; convbuffer [ptr] = (byte)(val); convbuffer [ptr + 1] = (byte)((uint)val >> 8); ptr += 2 * (vi.channels); } } if (clipflag) { //s_err.WriteLine("Clipping in frame "+vd.sequence); } output.Write(convbuffer, 0, 2 * vi.channels * bout); vd.synthesis_read(bout); // tell libvorbis how // many samples we // actually consumed } } } if (og.eos() != 0) eos = 1; } } if (eos == 0) { index = oy.buffer(4096); bytes = input.Read(oy.data, index, 4096); oy.wrote(bytes); if (bytes == 0) eos = 1; } } // clean up this logical bitstream; before exit we see if we're // followed by another [chained] os.clear(); // ogg_page and ogg_packet structs always point to storage in // libvorbis. They're never freed or manipulated directly vb.clear(); vd.clear(); vi.clear(); // must be called last } // OK, clean up the framer oy.clear(); s_err.WriteLine("Done."); if (writeWavHeader) { long end = output.Position; int length = (int)(end - (start + HEADER_SIZE)); output.Seek(start, SeekOrigin.Begin); WriteHeader(output, length, vi.rate, (ushort)16, (ushort)vi.channels); output.Seek(end, SeekOrigin.Begin); } }
override public int forward(Block vb, Object i, float[] fin, float[] fout, Object vs){return 0;}
public abstract int inverse(Block vb, Object i, float[] fin, float[] fout);
public abstract int forward(Block vb, Object i, float[] fin, float[] fout, Object vs);
// Unike in analysis, the window is only partially applied for each // block. The time domain envelope is not yet handled at the point of // calling (as it relies on the previous block). public int synthesis_blockin(Block vb) { // Shift out any PCM/multipliers that we returned previously // centerW is currently the center of the last block added if(centerW>vi.blocksizes[1]/2 && pcm_returned>8192) { // don't shift too much; we need to have a minimum PCM buffer of // 1/2 long block int shiftPCM=centerW-vi.blocksizes[1]/2; shiftPCM=(pcm_returned<shiftPCM?pcm_returned:shiftPCM); pcm_current-=shiftPCM; centerW-=shiftPCM; pcm_returned-=shiftPCM; if(shiftPCM!=0) { for(int i=0;i<vi.channels;i++) { Array.Copy(pcm[i], shiftPCM, pcm[i], 0, pcm_current); } } } lW=W; W=vb.W; nW=-1; glue_bits+=vb.glue_bits; time_bits+=vb.time_bits; floor_bits+=vb.floor_bits; res_bits+=vb.res_bits; if(sequence+1 != vb.sequence)granulepos=-1; // out of sequence; lose count sequence=vb.sequence; { int sizeW=vi.blocksizes[W]; int _centerW=centerW+vi.blocksizes[lW]/4+sizeW/4; int beginW=_centerW-sizeW/2; int endW=beginW+sizeW; int beginSl=0; int endSl=0; // Do we have enough PCM/mult storage for the block? if(endW>pcm_storage) { // expand the storage pcm_storage=endW+vi.blocksizes[1]; for(int i=0;i<vi.channels;i++) { float[] foo=new float[pcm_storage]; Array.Copy(pcm[i], 0, foo, 0, pcm[i].Length); pcm[i]=foo; } } // overlap/add PCM switch(W) { case 0: beginSl=0; endSl=vi.blocksizes[0]/2; break; case 1: beginSl=vi.blocksizes[1]/4-vi.blocksizes[lW]/4; endSl=beginSl+vi.blocksizes[lW]/2; break; } for(int j=0;j<vi.channels;j++) { int _pcm=beginW; // the overlap/add section int i=0; for(i=beginSl;i<endSl;i++) { pcm[j][_pcm+i]+=vb.pcm[j][i]; } // the remaining section for(;i<sizeW;i++) { pcm[j][_pcm+i]=vb.pcm[j][i]; } } // track the frame number... This is for convenience, but also // making sure our last packet doesn't end with added padding. If // the last packet is partial, the number of samples we'll have to // return will be past the vb->granulepos. // // This is not foolproof! It will be confused if we begin // decoding at the last page after a seek or hole. In that case, // we don't have a starting point to judge where the last frame // is. For this reason, vorbisfile will always try to make sure // it reads the last two marked pages in proper sequence if(granulepos==-1) { granulepos=vb.granulepos; } else { granulepos+=(_centerW-centerW); if(vb.granulepos!=-1 && granulepos!=vb.granulepos) { if(granulepos>vb.granulepos && vb.eofflag!=0) { // partial last frame. Strip the padding off _centerW = _centerW - (int)(granulepos-vb.granulepos); }// else{ Shouldn't happen *unless* the bitstream is out of // spec. Either way, believe the bitstream } granulepos=vb.granulepos; } } // Update, cleanup centerW=_centerW; pcm_current=endW; if(vb.eofflag!=0)eofflag=1; } return(0); }
public override int inverse(Block vb, Object i, float[] fin, float[] fout) { return 0; }
internal static int _2inverse(Block vb, Object vl, float[][] fin, int ch) { int i,k,l,s; LookResidue0 look=(LookResidue0 )vl; InfoResidue0 info=look.info; // move all this setup out later int samples_per_partition=info.grouping; int partitions_per_word=look.phrasebook.dim; int n=info.end-info.begin; int partvals=n/samples_per_partition; int partwords=(partvals+partitions_per_word-1)/partitions_per_word; int[][] partword=new int[partwords][]; for(s=0;s<look.stages;s++) { for(i=0,l=0;i<partvals;l++) { if(s==0) { // fetch the partition word for each channel int temp=look.phrasebook.decode(vb.opb); if(temp==-1) { // goto eopbreak; return(0); } partword[l]=look.decodemap[temp]; if(partword[l]==null) { // goto errout; return(0); } } // now we decode residual values for the partitions for(k=0;k<partitions_per_word && i<partvals;k++,i++) { int offset=info.begin+i*samples_per_partition; if((info.secondstages[partword[l][k]]&(1<<s))!=0) { CodeBook stagebook=look.fullbooks[look.partbooks[partword[l][k]][s]]; if(stagebook!=null) { if(stagebook.decodevv_add(fin, offset, ch, vb.opb,samples_per_partition)==-1) { // goto errout; return(0); } } } } } } // errout: // eopbreak: return(0); }
public abstract int inverse(Block vd, Object lm);
public abstract int inverse(Block vb, Object vl, float[][] fin, int[] nonzero,int ch);
internal static int _01inverse(Block vb, Object vl, float[][] fin, int ch, int decodepart) { { int i,j,k,l,s; LookResidue0 look=(LookResidue0 )vl; InfoResidue0 info=look.info; // move all this setup out later int samples_per_partition=info.grouping; int partitions_per_word=look.phrasebook.dim; int n=info.end-info.begin; int partvals=n/samples_per_partition; int partwords=(partvals+partitions_per_word-1)/partitions_per_word; if(partword.Length<ch) { partword=new int[ch][][]; for(j=0;j<ch;j++) { partword[j]=new int[partwords][]; } } else { for(j=0;j<ch;j++) { if(partword[j]==null || partword[j].Length<partwords) partword[j]=new int[partwords][]; } } for(s=0;s<look.stages;s++) { // each loop decodes on partition codeword containing // partitions_pre_word partitions for(i=0,l=0;i<partvals;l++) { if(s==0) { // fetch the partition word for each channel for(j=0;j<ch;j++) { int temp=look.phrasebook.decode(vb.opb); if(temp==-1) { //goto eopbreak; return(0); } partword[j][l]=look.decodemap[temp]; if(partword[j][l]==null) { // goto errout; return(0); } } } // now we decode residual values for the partitions for(k=0;k<partitions_per_word && i<partvals;k++,i++) for(j=0;j<ch;j++) { int offset=info.begin+i*samples_per_partition; if((info.secondstages[partword[j][l][k]]&(1<<s))!=0) { CodeBook stagebook=look.fullbooks[look.partbooks[partword[j][l][k]][s]]; // CodeBook stagebook=look.partbooks[partword[j][l][k]][s]; if(stagebook!=null) { if(decodepart==0) { if(stagebook.decodevs_add(fin[j],offset,vb.opb,samples_per_partition)==-1) { // goto errout; return(0); } } else if(decodepart==1) { if(stagebook.decodev_add(fin[j], offset, vb.opb,samples_per_partition)==-1) { // goto errout; return(0); } } } } } } } return(0); } }
public abstract int forward(Block vb,Object vl, float[][] fin, int ch);
public abstract int inverse2(Block vb, Object i, Object memo, float[] fout);
public abstract Object inverse1(Block vb, Object i, Object memo);
override public Object inverse1(Block vb, Object ii, Object memo) { LookFloor1 look=(LookFloor1)ii; InfoFloor1 info=look.vi; CodeBook[] books=vb.vd.fullbooks; /* unpack wrapped/predicted values from stream */ if(vb.opb.read(1)==1) { int[] fit_value=null; if(memo is int[]) { fit_value=(int[])memo; } if(fit_value==null || fit_value.Length<look.posts) { fit_value=new int[look.posts]; } else { for(int i=0; i<fit_value.Length; i++) fit_value[i]=0; } fit_value[0]=vb.opb.read(ilog(look.quant_q-1)); fit_value[1]=vb.opb.read(ilog(look.quant_q-1)); /* partition by partition */ for(int i=0,j=2;i<info.partitions;i++) { int clss=info.partitionclass[i]; int cdim=info.class_dim[clss]; int csubbits=info.class_subs[clss]; int csub=1<<csubbits; int cval=0; /* decode the partition's first stage cascade value */ if(csubbits!=0) { cval=books[info.class_book[clss]].decode(vb.opb); if(cval==-1) { //goto eop; return(null); } } for(int k=0;k<cdim;k++) { int book=info.class_subbook[clss][cval&(csub-1)]; cval = (int)((uint)cval >> csubbits); if(book>=0) { if((fit_value[j+k]=books[book].decode(vb.opb))==-1) { return(null); } } else { fit_value[j+k]=0; } } j+=cdim; } /* unwrap positive values and reconsitute via linear interpolation */ for(int i=2;i<look.posts;i++) { int predicted=render_point(info.postlist[look.loneighbor[i-2]], info.postlist[look.hineighbor[i-2]], fit_value[look.loneighbor[i-2]], fit_value[look.hineighbor[i-2]], info.postlist[i]); int hiroom=look.quant_q-predicted; int loroom=predicted; int room=(hiroom<loroom?hiroom:loroom)<<1; int val=fit_value[i]; if(val!=0) { if(val>=room) { if(hiroom>loroom) { val = val-loroom; } else { val = -1-(val-hiroom); } } else { if((val&1)!=0) { val= (int)(-((uint)(val+1) >> 1)); } else { val>>=1; } } fit_value[i]=val+predicted; fit_value[look.loneighbor[i-2]]&=0x7fff; fit_value[look.hineighbor[i-2]]&=0x7fff; } else { fit_value[i]=predicted|0x8000; } } return(fit_value); } // eop: // return(NULL); return(null); }
override public int inverse2(Block vb, Object i, Object memo, float[] fout) { //System.err.println("Floor0.inverse "+i.getClass()+"]"); LookFloor0 look=(LookFloor0)i; InfoFloor0 info=look.vi; if(memo!=null) { float[] lsp=(float[])memo; float amp=lsp[look.m]; Lsp.lsp_to_curve(fout,look.linearmap,look.n,look.ln, lsp,look.m,amp,info.ampdB); return(1); } // eop: // memset(out,0,sizeof(float)*look->n); for(int j=0; j<look.n; j++) { fout[j]=0.0f; } return(0); }
override public int inverse2(Block vb, Object i, Object memo, float[] fout) { LookFloor1 look=(LookFloor1)i; InfoFloor1 info=look.vi; int n=vb.vd.vi.blocksizes[vb.mode]/2; if(memo!=null) { /* render the lines */ int[] fit_value=(int[] )memo; int hx=0; int lx=0; int ly=fit_value[0]*info.mult; for(int j=1;j<look.posts;j++) { int current=look.forward_index[j]; int hy=fit_value[current]&0x7fff; if(hy==fit_value[current]) { hy*=info.mult; hx=info.postlist[current]; render_line(lx,hx,ly,hy,fout); lx=hx; ly=hy; } } for(int j=hx;j<n;j++) { fout[j]*=fout[j-1]; /* be certain */ } return(1); } for(int j=0; j<n; j++) { fout[j]=0.0f; } return(0); }
public AudioSample OggToWav(Stream ogg, float volume, float pitch) { AudioSample sample = new AudioSample(); TextWriter s_err = new StringWriter(); Stream input = null; MemoryStream output = null; input = ogg; output = new MemoryStream(); SyncState oy = new SyncState(); StreamState os = new StreamState(); Page og = new Page(); Packet op = new Packet(); Info vi = new Info(); Comment vc = new Comment(); DspState vd = new DspState(); Block vb = new Block(vd); byte[] buffer; int bytes = 0; oy.init(); while (true) { int eos = 0; int index = oy.buffer(4096); buffer = oy.data; try { bytes = input.Read(buffer, index, 4096); } catch (Exception e) { s_err.WriteLine(e); } oy.wrote(bytes); if (oy.pageout(og) != 1) { if (bytes < 4096) break; s_err.WriteLine("Input does not appear to be an Ogg bitstream."); } os.init(og.serialno()); vi.init(); vc.init(); if (os.pagein(og) < 0) { s_err.WriteLine("Error reading first page of Ogg bitstream data."); } if (os.packetout(op) != 1) { s_err.WriteLine("Error reading initial header packet."); } if (vi.synthesis_headerin(vc, op) < 0) { s_err.WriteLine("This Ogg bitstream does not contain Vorbis audio data."); } int i = 0; while (i < 2) { while (i < 2) { int result = oy.pageout(og); if (result == 0) break; if (result == 1) { os.pagein(og); while (i < 2) { result = os.packetout(op); if (result == 0) break; vi.synthesis_headerin(vc, op); i++; } } } index = oy.buffer(4096); buffer = oy.data; try { bytes = input.Read(buffer, index, 4096); } catch (Exception e) { s_err.WriteLine(e); } oy.wrote(bytes); } sample.Channels = vi.channels; sample.Rate = (int)((float)vi.rate * pitch); convsize = 4096 / vi.channels; vd.synthesis_init(vi); vb.init(vd); float[][][] _pcm = new float[1][][]; int[] _index = new int[vi.channels]; while (eos == 0) { while (eos == 0) { int result = oy.pageout(og); if (result == 0) break; if (result != -1) { os.pagein(og); while (true) { result = os.packetout(op); if (result == 0) break; if (result != -1) { int samples; if (vb.synthesis(op) == 0) { vd.synthesis_blockin(vb); } while ((samples = vd.synthesis_pcmout(_pcm, _index)) > 0) { float[][] pcm = _pcm[0]; int bout = (samples < convsize ? samples : convsize); for (i = 0; i < vi.channels; i++) { int ptr = i * 2; int mono = _index[i]; for (int j = 0; j < bout; j++) { int val = (int)(pcm[i][mono + j] * 32767.0); if (val > 32767) { val = 32767; } if (val < -32768) { val = -32768; } val = (int)((float)val * volume); if (val < 0) val = val | 0x8000; convbuffer[ptr] = (byte)(val); convbuffer[ptr + 1] = (byte)((uint)val >> 8); ptr += 2 * (vi.channels); } } output.Write(convbuffer, 0, 2 * vi.channels * bout); vd.synthesis_read(bout); } } } if (og.eos() != 0) eos = 1; } } if (eos == 0) { index = oy.buffer(4096); buffer = oy.data; try { bytes = input.Read(buffer, index, 4096); } catch (Exception e) { s_err.WriteLine(e); } oy.wrote(bytes); if (bytes == 0) eos = 1; } } os.clear(); vb.clear(); vd.clear(); vi.clear(); break; } oy.clear(); input.Close(); sample.Pcm = output.ToArray(); return sample; }
private VorbisFile() { os=new StreamState(); // take physical pages, weld into a logical // stream of packets vd=new DspState(); // central working state for // the packet->PCM decoder vb=new Block(vd); // local working space for packet->PCM decode }
int inverse(Block vb, Object i, float[] fout) { //System.err.println("Floor0.inverse "+i.getClass()+"]"); LookFloor0 look=(LookFloor0)i; InfoFloor0 info=look.vi; int ampraw=vb.opb.read(info.ampbits); if(ampraw>0) { // also handles the -1 out of data case int maxval=(1<<info.ampbits)-1; float amp=(float)ampraw/maxval*info.ampdB; int booknum=vb.opb.read(ilog(info.numbooks)); if(booknum!=-1 && booknum<info.numbooks) { lock(this) { if(lsp==null||lsp.Length<look.m) { lsp=new float[look.m]; } else { for(int j=0; j<look.m; j++)lsp[j]=0.0f; } CodeBook b=vb.vd.fullbooks[info.books[booknum]]; float last=0.0f; //memset(out,0,sizeof(float)*look->m); for(int j=0; j<look.m; j++)fout[j]=0.0f; for(int j=0;j<look.m;j+=b.dim) { if(b.decodevs(lsp, j, vb.opb, 1, -1)==-1) { //goto eop; // memset(out,0,sizeof(float)*look->n); for(int k=0; k<look.n; k++)fout[k]=0.0f; return(0); } } for(int j=0;j<look.m;) { for(int k=0;k<b.dim;k++,j++)lsp[j]+=last; last=lsp[j-1]; } // take the coefficients back to a spectral envelope curve /* lsp_to_lpc(out,out,look.m); lpc_to_curve(out,out,amp,look,"",0); for(int j=0;j<look.n;j++){ out[j]=fromdB(out[j]-info.ampdB); } */ Lsp.lsp_to_curve(fout,look.linearmap,look.n,look.ln, lsp,look.m,amp,info.ampdB); return(1); } } } // eop: // memset(out,0,sizeof(float)*look->n); return(0); }
public override int forward(Block vb, Object i) { return 0; }
public override int forward(Block vb,Object vl, float[][] fin, int ch) { return 0; }
public abstract int forward(Block vb, Object i);
Stream DecodeStream( Stream input, bool skipWavHeader ) { int convsize = 4096 * 2; byte [] convbuffer = new byte [ convsize ]; Stream output = new MemoryStream (); if ( !skipWavHeader ) output.Seek ( HEADER_SIZE, SeekOrigin.Begin ); SyncState oy = new SyncState (); StreamState os = new StreamState (); Page og = new Page (); Packet op = new Packet (); Info vi = new Info (); Comment vc = new Comment (); DspState vd = new DspState (); Block vb = new Block ( vd ); byte [] buffer; int bytes = 0; oy.init (); while ( true ) { int eos = 0; int index = oy.buffer ( 4096 ); buffer = oy.data; bytes = input.Read ( buffer, index, 4096 ); oy.wrote ( bytes ); if ( oy.pageout ( og ) != 1 ) { if ( bytes < 4096 ) break; throw new Exception ( "Input does not appear to be an Ogg bitstream." ); } os.init ( og.serialno () ); vi.init (); vc.init (); if ( os.pagein ( og ) < 0 ) { throw new Exception ( "Error reading first page of Ogg bitstream data." ); } if ( os.packetout ( op ) != 1 ) { throw new Exception ( "Error reading initial header packet." ); } if ( vi.synthesis_headerin ( vc, op ) < 0 ) { throw new Exception ( "This Ogg bitstream does not contain Vorbis audio data." ); } int i = 0; while ( i < 2 ) { while ( i < 2 ) { int result = oy.pageout ( og ); if ( result == 0 ) break; if ( result == 1 ) { os.pagein ( og ); while ( i < 2 ) { result = os.packetout ( op ); if ( result == 0 ) break; if ( result == -1 ) { throw new Exception ( "Corrupt secondary header. Exiting." ); } vi.synthesis_headerin ( vc, op ); i++; } } } index = oy.buffer ( 4096 ); buffer = oy.data; bytes = input.Read ( buffer, index, 4096 ); if ( bytes == 0 && i < 2 ) { throw new Exception ( "End of file before finding all Vorbis headers!" ); } oy.wrote ( bytes ); } { byte [] [] ptr = vc.user_comments; for ( int j = 0; j < vc.user_comments.Length; j++ ) { if ( ptr [ j ] == null ) break; } } convsize = 4096 / vi.channels; vd.synthesis_init ( vi ); vb.init ( vd ); float [] [] [] _pcm = new float [ 1 ] [][]; int [] _index = new int [ vi.channels ]; while ( eos == 0 ) { while ( eos == 0 ) { int result = oy.pageout ( og ); if ( result == 0 ) break; if ( result == -1 ) throw new Exception ( "Corrupt or missing data in bitstream; continuing..." ); else { os.pagein ( og ); while ( true ) { result = os.packetout ( op ); if ( result == 0 ) break; if ( result == -1 ) { } else { int samples; if ( vb.synthesis ( op ) == 0 ) { vd.synthesis_blockin ( vb ); } while ( ( samples = vd.synthesis_pcmout ( _pcm, _index ) ) > 0 ) { float [] [] pcm = _pcm [ 0 ]; bool clipflag = false; int bout = ( samples < convsize ? samples : convsize ); for ( i = 0; i < vi.channels; i++ ) { int ptr = i * 2; int mono = _index [ i ]; for ( int j = 0; j < bout; j++ ) { int val = ( int ) ( pcm [ i ] [ mono + j ] * 32767.0 ); if ( val > 32767 ) { val = 32767; clipflag = true; } if ( val < -32768 ) { val = -32768; clipflag = true; } if ( val < 0 ) val = val | 0x8000; convbuffer [ ptr ] = ( byte ) ( val ); convbuffer [ ptr + 1 ] = ( byte ) ( ( uint ) val >> 8 ); ptr += 2 * ( vi.channels ); } } if ( clipflag ) { } output.Write ( convbuffer, 0, 2 * vi.channels * bout ); vd.synthesis_read ( bout ); } } } if ( og.eos () != 0 ) eos = 1; } } if ( eos == 0 ) { index = oy.buffer ( 4096 ); buffer = oy.data; bytes = input.Read ( buffer, index, 4096 ); oy.wrote ( bytes ); if ( bytes == 0 ) eos = 1; } } os.clear (); vb.clear (); vd.clear (); vi.clear (); } oy.clear (); output.Seek ( 0, SeekOrigin.Begin ); if ( !skipWavHeader ) { WriteHeader ( output, ( int ) ( output.Length - HEADER_SIZE ), vi.rate, ( ushort ) 16, ( ushort ) vi.channels ); output.Seek ( 0, SeekOrigin.Begin ); } return output; }
public override int inverse(Block vb, Object l) { #if DEBUG Debug.Assert(l != null); #endif lock(this) { //System.err.println("Mapping0.inverse"); DspState vd=vb.vd; Info vi=vd.vi; LookMapping0 look=(LookMapping0)l; InfoMapping0 info=look.map; InfoMode mode=look.mode; int n=vb.pcmend=vi.blocksizes[vb.W]; float[] window=vd.wnd[vb.W][vb.lW][vb.nW][mode.windowtype]; // float[][] pcmbundle=new float[vi.channels][]; // int[] nonzero=new int[vi.channels]; if(pcmbundle==null || pcmbundle.Length<vi.channels) { pcmbundle=new float[vi.channels][]; nonzero=new int[vi.channels]; zerobundle=new int[vi.channels]; floormemo=new Object[vi.channels]; } // time domain information decode (note that applying the // information would have to happen later; we'll probably add a // function entry to the harness for that later // NOT IMPLEMENTED // recover the spectral envelope; store it in the PCM vector for now for(int i=0;i<vi.channels;i++) { float[] pcm=vb.pcm[i]; int submap=info.chmuxlist[i]; floormemo[i]=look.floor_func[submap].inverse1(vb,look. floor_look[submap], floormemo[i] ); if(floormemo[i]!=null){ nonzero[i]=1; } else{ nonzero[i]=0; } for(int j=0; j<n/2; j++) { pcm[j]=0; } //_analysis_output("ifloor",seq+i,pcm,n/2,0,1); } for(int i=0; i<info.coupling_steps; i++) { if(nonzero[info.coupling_mag[i]]!=0 || nonzero[info.coupling_ang[i]]!=0) { nonzero[info.coupling_mag[i]]=1; nonzero[info.coupling_ang[i]]=1; } } // recover the residue, apply directly to the spectral envelope for(int i=0;i<info.submaps;i++) { int ch_in_bundle=0; for(int j=0;j<vi.channels;j++) { if(info.chmuxlist[j]==i) { if(nonzero[j]!=0) { zerobundle[ch_in_bundle]=1; } else { zerobundle[ch_in_bundle]=0; } pcmbundle[ch_in_bundle++]=vb.pcm[j]; } } look.residue_func[i].inverse(vb,look.residue_look[i], pcmbundle,zerobundle,ch_in_bundle); } for(int i=info.coupling_steps-1;i>=0;i--) { float[] pcmM=vb.pcm[info.coupling_mag[i]]; float[] pcmA=vb.pcm[info.coupling_ang[i]]; for(int j=0;j<n/2;j++) { float mag=pcmM[j]; float ang=pcmA[j]; if(mag>0) { if(ang>0) { pcmM[j]=mag; pcmA[j]=mag-ang; } else { pcmA[j]=mag; pcmM[j]=mag+ang; } } else { if(ang>0) { pcmM[j]=mag; pcmA[j]=mag+ang; } else { pcmA[j]=mag; pcmM[j]=mag-ang; } } } } // /* compute and apply spectral envelope */ for(int i=0;i<vi.channels;i++) { float[] pcm=vb.pcm[i]; int submap=info.chmuxlist[i]; look.floor_func[submap].inverse2(vb,look.floor_look[submap],floormemo[i],pcm); } // transform the PCM data; takes PCM vector, vb; modifies PCM vector // only MDCT right now.... for(int i=0;i<vi.channels;i++) { float[] pcm=vb.pcm[i]; //_analysis_output("out",seq+i,pcm,n/2,0,0); ((Mdct)vd.transform[vb.W][0]).backward(pcm,pcm); } // now apply the decoded pre-window time information // NOT IMPLEMENTED // window the data for(int i=0;i<vi.channels;i++) { float[] pcm=vb.pcm[i]; if(nonzero[i]!=0) { for(int j=0;j<n;j++) { pcm[j]*=window[j]; } } else { for(int j=0;j<n;j++) { pcm[j]=0.0f; } } //_analysis_output("final",seq++,pcm,n,0,0); } // now apply the decoded post-window time information // NOT IMPLEMENTED // all done! return(0); } }
new int forward(Block vb,Object vl, float[][] fin, int ch) { return 0; }