Search in sources :

Example 1 with BufferData

use of com.libra.sinvoice.Buffer.BufferData in project SinVoice by JesseGu.

the class PcmPlayer method start.

public void start() {
    LogHelper.d(TAG, "start");
    if (STATE_STOP == mState && null != mAudio) {
        mPlayedLen = 0;
        if (null != mCallback) {
            mState = STATE_START;
            LogHelper.d(TAG, "start");
            if (null != mListener) {
                mListener.onPlayStart();
            }
            while (STATE_START == mState) {
                LogHelper.d(TAG, "start getbuffer");
                BufferData data = mCallback.getPlayBuffer();
                if (null != data) {
                    if (null != data.mData) {
                        int len = mAudio.write(data.mData, 0, data.getFilledSize());
                        if (0 == mPlayedLen) {
                            mAudio.play();
                        }
                        mPlayedLen += len;
                        mCallback.freePlayData(data);
                    } else {
                        // it is the end of input, so need stop
                        LogHelper.d(TAG, "it is the end of input, so need stop");
                        break;
                    }
                } else {
                    LogHelper.e(TAG, "get null data");
                    break;
                }
            }
            if (null != mAudio) {
                mAudio.pause();
                mAudio.flush();
                mAudio.stop();
            }
            mState = STATE_STOP;
            if (null != mListener) {
                mListener.onPlayStop();
            }
            LogHelper.d(TAG, "end");
        }
    }
}
Also used : BufferData(com.libra.sinvoice.Buffer.BufferData)

Example 2 with BufferData

use of com.libra.sinvoice.Buffer.BufferData in project SinVoice by JesseGu.

the class SinGenerator method gen.

public void gen(int genRate, int duration) {
    if (STATE_START == mState) {
        mGenRate = genRate;
        mDuration = duration;
        if (null != mListener) {
            mListener.onStartGen();
        }
        int n = mBits / 2;
        int totalCount = (mDuration * mSampleRate) / 1000;
        double per = (mGenRate / (double) mSampleRate) * 2 * Math.PI;
        double d = 0;
        LogHelper.d(TAG, "genRate:" + genRate);
        if (null != mCallback) {
            mFilledSize = 0;
            BufferData buffer = mCallback.getGenBuffer();
            if (null != buffer) {
                for (int i = 0; i < totalCount; ++i) {
                    if (STATE_START == mState) {
                        int out = (int) (Math.sin(d) * n) + 128;
                        if (mFilledSize >= mBufferSize - 1) {
                            // free buffer
                            buffer.setFilledSize(mFilledSize);
                            mCallback.freeGenBuffer(buffer);
                            mFilledSize = 0;
                            buffer = mCallback.getGenBuffer();
                            if (null == buffer) {
                                LogHelper.e(TAG, "get null buffer");
                                break;
                            }
                        }
                        buffer.mData[mFilledSize++] = (byte) (out & 0xff);
                        if (BITS_16 == mBits) {
                            buffer.mData[mFilledSize++] = (byte) ((out >> 8) & 0xff);
                        }
                        d += per;
                    } else {
                        LogHelper.d(TAG, "sin gen force stop");
                        break;
                    }
                }
            } else {
                LogHelper.e(TAG, "get null buffer");
            }
            if (null != buffer) {
                buffer.setFilledSize(mFilledSize);
                mCallback.freeGenBuffer(buffer);
            }
            mFilledSize = 0;
            if (null != mListener) {
                mListener.onStopGen();
            }
        }
    }
}
Also used : BufferData(com.libra.sinvoice.Buffer.BufferData)

Example 3 with BufferData

use of com.libra.sinvoice.Buffer.BufferData in project SinVoice by JesseGu.

the class SinVoiceRecognition method stopRecognition.

private void stopRecognition() {
    mRecognition.stop();
    // put end buffer
    BufferData data = new BufferData(0);
    mBuffer.putFull(data);
    if (null != mRecognitionThread) {
        try {
            mRecognitionThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            mRecognitionThread = null;
        }
    }
    mBuffer.reset();
}
Also used : BufferData(com.libra.sinvoice.Buffer.BufferData)

Example 4 with BufferData

use of com.libra.sinvoice.Buffer.BufferData in project SinVoice by JesseGu.

the class Record method start.

public void start() {
    if (STATE_STOP == mState) {
        switch(mChannel) {
            case CHANNEL_1:
                mChannelConfig = AudioFormat.CHANNEL_IN_MONO;
                break;
            case CHANNEL_2:
                mChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
                break;
        }
        switch(mBits) {
            case BITS_8:
                mAudioEncoding = AudioFormat.ENCODING_PCM_8BIT;
                break;
            case BITS_16:
                mAudioEncoding = AudioFormat.ENCODING_PCM_16BIT;
                break;
        }
        int minBufferSize = AudioRecord.getMinBufferSize(mFrequence, mChannelConfig, mAudioEncoding);
        LogHelper.d(TAG, "minBufferSize:" + minBufferSize);
        if (mBufferSize >= minBufferSize) {
            AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC, mFrequence, mChannelConfig, mAudioEncoding, mBufferSize);
            if (null != record) {
                try {
                    mState = STATE_START;
                    record.startRecording();
                    LogHelper.d(TAG, "record start");
                    if (null != mCallback) {
                        if (null != mListener) {
                            mListener.onStartRecord();
                        }
                        while (STATE_START == mState) {
                            BufferData data = mCallback.getRecordBuffer();
                            if (null != data) {
                                if (null != data.mData) {
                                    int bufferReadResult = record.read(data.mData, 0, mBufferSize);
                                    data.setFilledSize(bufferReadResult);
                                    mCallback.freeRecordBuffer(data);
                                } else {
                                    // end of input
                                    LogHelper.d(TAG, "get end input data, so stop");
                                    break;
                                }
                            } else {
                                LogHelper.e(TAG, "get null data");
                                break;
                            }
                        }
                        if (null != mListener) {
                            mListener.onStopRecord();
                        }
                    }
                    record.stop();
                    record.release();
                    LogHelper.d(TAG, "record stop");
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    LogHelper.e(TAG, "start record error");
                }
                mState = STATE_STOP;
            }
        } else {
            LogHelper.e(TAG, "bufferSize is too small");
        }
    }
}
Also used : AudioRecord(android.media.AudioRecord) BufferData(com.libra.sinvoice.Buffer.BufferData)

Example 5 with BufferData

use of com.libra.sinvoice.Buffer.BufferData in project SinVoice by JesseGu.

the class VoiceRecognition method start.

public void start() {
    if (STATE_STOP == mState) {
        if (null != mCallback) {
            mState = STATE_START;
            mSamplingPointCount = 0;
            mIsStartCounting = false;
            mStep = STEP1;
            mIsBeginning = false;
            mStartingDet = false;
            mStartingDetCount = 0;
            mPreRegCircle = -1;
            if (null != mListener) {
                mListener.onStartRecognition();
            }
            while (STATE_START == mState) {
                BufferData data = mCallback.getRecognitionBuffer();
                if (null != data) {
                    if (null != data.mData) {
                        process(data);
                        mCallback.freeRecognitionBuffer(data);
                    } else {
                        LogHelper.d(TAG, "end input buffer, so stop");
                        break;
                    }
                } else {
                    LogHelper.e(TAG, "get null recognition buffer");
                    break;
                }
            }
            mState = STATE_STOP;
            if (null != mListener) {
                mListener.onStopRecognition();
            }
        }
    }
}
Also used : BufferData(com.libra.sinvoice.Buffer.BufferData)

Aggregations

BufferData (com.libra.sinvoice.Buffer.BufferData)5 AudioRecord (android.media.AudioRecord)1