Search in sources :

Example 46 with AudioRecord

use of android.media.AudioRecord in project cythara by gstraube.

the class AudioDispatcherFactory method fromDefaultMicrophone.

/**
 * Create a new AudioDispatcher connected to the default microphone.
 *
 * @param sampleRate
 *            The requested sample rate.
 * @param audioBufferSize
 *            The size of the audio buffer (in samples).
 *
 * @param bufferOverlap
 *            The size of the overlap (in samples).
 * @return A new AudioDispatcher
 */
public static AudioDispatcher fromDefaultMicrophone(final int sampleRate, final int audioBufferSize, final int bufferOverlap) {
    int minAudioBufferSize = AudioRecord.getMinBufferSize(sampleRate, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT);
    int minAudioBufferSizeInSamples = minAudioBufferSize / 2;
    if (minAudioBufferSizeInSamples <= audioBufferSize) {
        AudioRecord audioInputStream = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, android.media.AudioFormat.CHANNEL_IN_MONO, android.media.AudioFormat.ENCODING_PCM_16BIT, audioBufferSize * 2);
        TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(sampleRate, 16, 1, true, false);
        TarsosDSPAudioInputStream audioStream = new AndroidAudioInputStream(audioInputStream, format);
        // start recording ! Opens the stream.
        audioInputStream.startRecording();
        return new AudioDispatcher(audioStream, audioBufferSize, bufferOverlap);
    } else {
        throw new IllegalArgumentException("Buffer size too small should be at least " + (minAudioBufferSize * 2));
    }
}
Also used : AudioRecord(android.media.AudioRecord) TarsosDSPAudioInputStream(be.tarsos.dsp.io.TarsosDSPAudioInputStream) TarsosDSPAudioFormat(be.tarsos.dsp.io.TarsosDSPAudioFormat) AudioDispatcher(be.tarsos.dsp.AudioDispatcher)

Example 47 with AudioRecord

use of android.media.AudioRecord in project CameraView by CJT2325.

the class CheckPermission method getRecordState.

/**
 * 用于检测是否具有录音权限
 *
 * @return
 */
public static int getRecordState() {
    int minBuffer = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, (minBuffer * 100));
    short[] point = new short[minBuffer];
    int readSize = 0;
    try {
        // 检测是否可以进入初始化状态
        audioRecord.startRecording();
    } catch (Exception e) {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
        return STATE_NO_PERMISSION;
    }
    if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
        // 检测是否在录音中
        if (audioRecord != null) {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            Log.d("CheckAudioPermission", "录音机被占用");
        }
        return STATE_RECORDING;
    } else {
        // 检测是否可以获取录音结果
        readSize = audioRecord.read(point, 0, point.length);
        if (readSize <= 0) {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;
            }
            Log.d("CheckAudioPermission", "录音的结果为空");
            return STATE_NO_PERMISSION;
        } else {
            if (audioRecord != null) {
                audioRecord.stop();
                audioRecord.release();
                audioRecord = null;
            }
            return STATE_SUCCESS;
        }
    }
}
Also used : AudioRecord(android.media.AudioRecord)

Example 48 with AudioRecord

use of android.media.AudioRecord in project speechutils by Kaljurand.

the class SpeechAudioRecord method create.

@RequiresPermission(RECORD_AUDIO)
public static AudioRecord create(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, boolean noise, boolean gain, boolean echo) throws IllegalArgumentException, SecurityException {
    // TODO: setPrivacySensitive(true) if API 30+
    AudioRecord audioRecord = new AudioRecord.Builder().setAudioSource(audioSource).setAudioFormat(new AudioFormat.Builder().setEncoding(audioFormat).setSampleRate(sampleRateInHz).setChannelMask(channelConfig).build()).setBufferSizeInBytes(bufferSizeInBytes).build();
    int audioSessionId = audioRecord.getAudioSessionId();
    if (noise) {
        if (NoiseSuppressor.create(audioSessionId) == null) {
            Log.i("NoiseSuppressor: failed");
        } else {
            Log.i("NoiseSuppressor: ON");
        }
    } else {
        Log.i("NoiseSuppressor: OFF");
    }
    if (gain) {
        if (AutomaticGainControl.create(audioSessionId) == null) {
            Log.i("AutomaticGainControl: failed");
        } else {
            Log.i("AutomaticGainControl: ON");
        }
    } else {
        Log.i("AutomaticGainControl: OFF");
    }
    if (echo) {
        if (AcousticEchoCanceler.create(audioSessionId) == null) {
            Log.i("AcousticEchoCanceler: failed");
        } else {
            Log.i("AcousticEchoCanceler: ON");
        }
    } else {
        Log.i("AcousticEchoCanceler: OFF");
    }
    return audioRecord;
}
Also used : AudioRecord(android.media.AudioRecord) AudioFormat(android.media.AudioFormat) RequiresPermission(androidx.annotation.RequiresPermission)

Example 49 with AudioRecord

use of android.media.AudioRecord in project storymaker by StoryMaker.

the class AudioRecorderView method startRecording.

public void startRecording() {
    // mTempFile = new File(mContext.getExternalFilesDir(null),AUDIO_RECORDER_TEMP_FILE);
    mTempFile = new File(StorageHelper.getActualStorageDirectory(mContext), AUDIO_RECORDER_TEMP_FILE);
    if (mTempFile.exists())
        mTempFile.delete();
    recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, mAudioSampleRate, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
    /*
		livePlayer =  new AudioTrack( AudioManager.STREAM_MUSIC, mAudioSampleRate, 
				RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, 
				bufferSize, AudioTrack.MODE_STREAM);
		*/
    recorder.startRecording();
    isRecording = true;
    recordingThread = new Thread(new Runnable() {

        @Override
        public void run() {
            writeAudioDataToFile();
        }
    }, "AudioRecorder Thread");
    recordingThread.start();
}
Also used : AudioRecord(android.media.AudioRecord) File(java.io.File)

Example 50 with AudioRecord

use of android.media.AudioRecord in project storymaker by StoryMaker.

the class ExtAudioRecorder method reset.

/**
 * Resets the recorder to the INITIALIZING state, as if it was just created.
 * In case the class was in RECORDING state, the recording is stopped.
 * In case of exceptions the class is set to the ERROR state.
 */
public void reset() {
    try {
        if (state != State.ERROR) {
            release();
            // Reset file path
            filePath = null;
            // Reset amplitude
            cAmplitude = 0;
            if (rUncompressed) {
                audioRecorder = new AudioRecord(aSource, sRate, nChannels + 1, aFormat, bufferSize);
            } else {
                mediaRecorder = new MediaRecorder();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            }
            state = State.INITIALIZING;
        }
    } catch (Exception e) {
        Timber.e(e.getMessage());
        state = State.ERROR;
    }
}
Also used : AudioRecord(android.media.AudioRecord) MediaRecorder(android.media.MediaRecorder) IOException(java.io.IOException)

Aggregations

AudioRecord (android.media.AudioRecord)64 Test (org.junit.Test)22 AudioRecordSource (org.robolectric.shadows.ShadowAudioRecord.AudioRecordSource)13 AssetFileDescriptor (android.content.res.AssetFileDescriptor)12 AudioEffect (android.media.audiofx.AudioEffect)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 Config (org.robolectric.annotation.Config)12 AudioFormat (android.media.AudioFormat)6 SystemApi (android.annotation.SystemApi)5 ByteBuffer (java.nio.ByteBuffer)4 SuppressLint (android.annotation.SuppressLint)1 MediaFormat (android.media.MediaFormat)1 MediaRecorder (android.media.MediaRecorder)1 Handler (android.os.Handler)1 RequiresPermission (androidx.annotation.RequiresPermission)1 AudioDispatcher (be.tarsos.dsp.AudioDispatcher)1 TarsosDSPAudioFormat (be.tarsos.dsp.io.TarsosDSPAudioFormat)1 TarsosDSPAudioInputStream (be.tarsos.dsp.io.TarsosDSPAudioInputStream)1 BufferData (com.libra.sinvoice.Buffer.BufferData)1 ActorCreator (im.actor.runtime.actors.ActorCreator)1