⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaencoder.java

📁 FMJ(freedom media for java)是java视频开发的新选择
💻 JAVA
字号:
/* * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client. * * Distributable under LGPL license. * See terms of license at gnu.org. */package net.java.sip.communicator.impl.media.codec.audio.speex;import javax.media.*;import javax.media.format.*;import org.xiph.speex.*;import net.java.sip.communicator.impl.media.codec.*;/** * The Speex Encoder * * @author Damian Minkov */public class JavaEncoder    extends com.ibm.media.codec.audio.AudioCodec{    private Format lastFormat = null;    private int numberOfChannels = 0;    private static int FRAME_SIZE = 320;    private SpeexEncoder encoder = null;    public JavaEncoder()    {        supportedInputFormats = new AudioFormat[]            {            new AudioFormat(                AudioFormat.LINEAR,                8000,                16,                1,                AudioFormat.LITTLE_ENDIAN, //isBigEndian(),                AudioFormat.SIGNED //isSigned());            )};        defaultOutputFormats = new AudioFormat[]            {new AudioFormat(Constants.SPEEX_RTP)};        PLUGIN_NAME = "pcm to speex converter";    }    protected Format[] getMatchingOutputFormats(Format in)    {        AudioFormat af = (AudioFormat) in;        supportedOutputFormats = new AudioFormat[]            {new AudioFormat(                Constants.SPEEX_RTP,                af.getSampleRate(),                8,                af.getChannels(),                af.getEndian(),                af.getSigned()            )};        return supportedOutputFormats;    }    public void open() throws ResourceUnavailableException    {    }    public void close()    {    }    private void initConverter(AudioFormat inFormat)    {        lastFormat = inFormat;        numberOfChannels = inFormat.getChannels();        encoder = new SpeexEncoder();        encoder.init(0, 4, (int)inFormat.getSampleRate(), 1);    }    public int process(Buffer inputBuffer, Buffer outputBuffer)    {        if (!checkInputBuffer(inputBuffer))        {            return BUFFER_PROCESSED_FAILED;        }        if (isEOM(inputBuffer))        {            propagateEOM(outputBuffer);            return BUFFER_PROCESSED_OK;        }        Format newFormat = inputBuffer.getFormat();        if (lastFormat != newFormat)        {            initConverter( (AudioFormat) newFormat);        }        int inpLength = inputBuffer.getLength();        byte[] inpData = (byte[]) inputBuffer.getData();        int inOffset = inputBuffer.getOffset();        if (inpLength == 0)        {            return OUTPUT_BUFFER_NOT_FILLED;        }        if ( (inpLength - inOffset) >= FRAME_SIZE)        {            encoder.processData(inpData, inOffset, FRAME_SIZE);            byte[] buff = new byte[encoder.getProcessedDataByteSize()];            encoder.getProcessedData(buff, 0);            byte[] outData = validateByteArraySize(outputBuffer, buff.length);            System.arraycopy(buff, 0, outData, outputBuffer.getOffset(),                             buff.length);            updateOutput(outputBuffer, outputFormat, outData.length, 0);            if ( (inpLength - inOffset) > FRAME_SIZE)            {                inputBuffer.setOffset(inOffset + FRAME_SIZE);                return BUFFER_PROCESSED_OK | INPUT_BUFFER_NOT_CONSUMED;            }            else            {                return BUFFER_PROCESSED_OK;            }        }        else        {            return OUTPUT_BUFFER_NOT_FILLED;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -