/// <summary>Initialise the Speex Decoder.</summary> /// <remarks>Initialise the Speex Decoder.</remarks> /// <param name="mode">the mode of the decoder (0=NB, 1=WB, 2=UWB).</param> /// <param name="sampleRate">the number of samples per second.</param> /// <param name="channels">the number of audio channels (1=mono, 2=stereo, ...).</param> /// <param name="enhanced">whether to enable perceptual enhancement or not.</param> /// <returns>true if initialisation successful.</returns> public virtual bool init(int mode, int sampleRate, int channels, bool enhanced) { switch (mode) { case 0: { decoder = new org.xiph.speex.NbDecoder(); ((org.xiph.speex.NbDecoder)decoder).nbinit(); break; } case 1: { //Wideband decoder = new org.xiph.speex.SbDecoder(); ((org.xiph.speex.SbDecoder)decoder).wbinit(); break; } case 2: { decoder = new org.xiph.speex.SbDecoder(); ((org.xiph.speex.SbDecoder)decoder).uwbinit(); break; } default: { //*/ return false; break; } } decoder.setPerceptualEnhancement(enhanced); this.frameSize = decoder.getFrameSize(); this.sampleRate = sampleRate; this.channels = channels; int secondSize = sampleRate * channels; decodedData = new float[secondSize * 2]; outputData = new short[secondSize * 2]; outputSize = 0; bits.init(); return true; }
/// <summary>Ultra-wideband initialisation</summary> public override void uwbinit() { lowdec = new org.xiph.speex.SbDecoder(); ((org.xiph.speex.SbDecoder)lowdec).wbinit(); lowdec.setPerceptualEnhancement(enhanced); // Initialize SubModes base.uwbinit(); // Initialize variables init(320, 80, 8, 1280, .5f); }
/// <summary>Wideband initialisation</summary> public override void wbinit() { lowdec = new org.xiph.speex.NbDecoder(); ((org.xiph.speex.NbDecoder)lowdec).nbinit(); lowdec.setPerceptualEnhancement(enhanced); // Initialize SubModes base.wbinit(); // Initialize variables init(160, 40, 8, 640, .7f); }
/// <summary>Initialises the Ogg Speex to PCM InputStream.</summary> /// <remarks> /// Initialises the Ogg Speex to PCM InputStream. /// Read the Ogg Speex header and extract the speex decoder parameters to /// initialise the decoder. Then read the Comment header. /// Ogg Header description: /// <pre> /// 0 - 3: capture_pattern /// 4: stream_structure_version /// 5: header_type_flag (2=bos: beginning of sream) /// 6 - 13: absolute granule position /// 14 - 17: stream serial number /// 18 - 21: page sequence no /// 22 - 25: page checksum /// 26: page_segments /// 27 -...: segment_table /// </pre> /// Speex Header description /// <pre> /// 0 - 7: speex_string /// 8 - 27: speex_version /// 28 - 31: speex_version_id /// 32 - 35: header_size /// 36 - 39: rate /// 40 - 43: mode (0=narrowband, 1=wb, 2=uwb) /// 44 - 47: mode_bitstream_version /// 48 - 51: nb_channels /// 52 - 55: bitrate /// 56 - 59: frame_size /// 60 - 63: vbr /// 64 - 67: frames_per_packet /// 68 - 71: extra_headers /// 72 - 75: reserved1 /// 76 - 79: reserved2 /// </pre> /// </remarks> /// <param name="blocking"> /// whether the method should block until initialisation is /// successfully completed or not. /// </param> /// <exception>IOException</exception> /// <exception cref="System.IO.IOException"></exception> protected virtual void initialise(bool blocking) { while (!initialised) { int readsize = prebuf.Length - precount - 1; int avail = @in.available(); if (!blocking && avail <= 0) { return; } readsize = (avail > 0 ? System.Math.Min(avail, readsize) : readsize); int n = @in.read(prebuf, precount, readsize); if (n < 0) { throw new java.io.StreamCorruptedException("Incomplete Ogg Headers"); } if (n == 0) { } // This should never happen. //assert false : "Read 0 bytes from stream - possible infinate loop"; precount += n; if (decoder == null && precount >= 108) { // we can process the speex header if (!(cspeex.StringUtil.getStringForBytes(prebuf, 0, 4).Equals("OggS"))) { throw new java.io.StreamCorruptedException("The given stream does not appear to be Ogg." ); } streamSerialNumber = readInt(prebuf, 14); if (!(cspeex.StringUtil.getStringForBytes(prebuf, 28, 8).Equals("Speex "))) { throw new java.io.StreamCorruptedException("The given stream does not appear to be Ogg Speex." ); } sampleRate = readInt(prebuf, 28 + 36); channelCount = readInt(prebuf, 28 + 48); framesPerPacket = readInt(prebuf, 28 + 64); int mode = readInt(prebuf, 28 + 40); switch (mode) { case 0: { decoder = new org.xiph.speex.NbDecoder(); ((org.xiph.speex.NbDecoder)decoder).nbinit(); break; } case 1: { decoder = new org.xiph.speex.SbDecoder(); ((org.xiph.speex.SbDecoder)decoder).wbinit(); break; } case 2: { decoder = new org.xiph.speex.SbDecoder(); ((org.xiph.speex.SbDecoder)decoder).uwbinit(); break; } default: { break; } } decoder.setPerceptualEnhancement(true); frameSize = decoder.getFrameSize(); decodedData = new float[frameSize * channelCount]; outputData = new byte[2 * frameSize * channelCount * framesPerPacket]; bits.init(); } if (decoder != null && precount >= 108 + 27) { // we can process the comment (skip them) packetsPerOggPage = unchecked((int)(0xff)) & prebuf[108 + 26]; if (precount >= 108 + 27 + packetsPerOggPage) { int size = 0; for (int i = 0; i < packetsPerOggPage; i++) { size += unchecked((int)(0xff)) & prebuf[108 + 27 + i]; } if (precount >= 108 + 27 + packetsPerOggPage + size) { // we have read the complete comment page prepos = 108 + 27 + packetsPerOggPage + size; packetsPerOggPage = 0; packetCount = 255; initialised = true; } } } } }