Android Open Source - speech_trainer Controller Factory






From Project

Back to project page speech_trainer.

License

The source code is released under:

GNU General Public License

If you think the Android project speech_trainer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * This file is part of Speech Trainer.//  w  w w.j  a v a 2s.  co  m
 * Copyright (C) 2011 Jan Wrobel <wrr@mixedbit.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package mixedbit.speechtrainer.controller;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import mixedbit.speechtrainer.SpeechTrainerConfig;
import mixedbit.speechtrainer.controller.RecordPlayTaskManager.RecordPlayTaskPriority;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder.AudioSource;

/**
 * Creates training controllers. It is OK to create multiple controllers with
 * this class, but the user must ensure that multiple controllers are never
 * started simultaneously.
 */
public class ControllerFactory {
    public class InitializationException extends Exception {
        private static final long serialVersionUID = -4092114494178018622L;

        public InitializationException(String detailMessage) {
            super(detailMessage);
        }
    }

    // Objects that are reused between successive controllers to avoid
    // reallocations of large buffers and recreation of an executor thread.
    private final AudioBufferAllocator audioBufferAllocator;
    private final ExecutorService executor;
    private AudioRecord audioRecord;
    private AudioTrack audioTrack;
    private int audioTrackBufferSizeInBytes;

    public ControllerFactory() {
        audioBufferAllocator = new AudioBufferAllocator(
                SpeechTrainerConfig.NUMBER_OF_AUDIO_BUFFERS,
                SpeechTrainerConfig.SINGLE_AUDIO_BUFFER_SIZE_IN_SHORTS);
        executor = Executors.newSingleThreadExecutor();
    }

    /**
     * @param audioEventListener
     *            Listener that will be informed about events handled by the
     *            created controller.
     */
    public InteractiveTrainingController createInteractiveTrainingController(
            AudioEventListener audioEventListener) throws InitializationException {
        if (audioRecord == null) {
            createAudioRecord();
        }
        if (audioTrack == null) {
            createAudioTrack();
        }
        final Recorder recorder = new RecorderImpl(audioRecord, audioEventListener);
        final Player player = new PlayerImpl(audioTrack, audioTrackBufferSizeInBytes,
                audioEventListener);
        final RecordPlayTaskManager recordPlayTaskManager = new RecordPlayTaskManager(recorder,
                player, executor, RecordPlayTaskPriority.HIGH);
        return new InteractiveTrainingController(recordPlayTaskManager, audioBufferAllocator);
    }

    /**
     * @param audioEventListener
     *            Listener that will be informed about events handled by the
     *            created controller.
     */
    public AutomaticTrainingController createAutomaticTrainingController(
            AudioEventListener audioEventListener) throws InitializationException {
        if (audioRecord == null) {
            createAudioRecord();
        }
        if (audioTrack == null) {
            createAudioTrack();
        }
        final Player player = new PlayerImpl(audioTrack, audioTrackBufferSizeInBytes,
                audioEventListener);
        final Recorder recorder = new RecorderImpl(audioRecord, audioEventListener);
        final RecordPlayTaskManager recordPlayTaskManager = new RecordPlayTaskManager(recorder,
                player, executor, RecordPlayTaskPriority.HIGH);
        return new AutomaticTrainingController(recordPlayTaskManager, new SilenceFilter(
                new SilenceLevelDetector()), audioBufferAllocator);
    }

    private void createAudioRecord() throws InitializationException {
        // The AudioRecord configurations parameters used here, are guaranteed
        // to be supported on all devices.

        // AudioFormat.CHANNEL_IN_MONO should be used in place of deprecated
        // AudioFormat.CHANNEL_CONFIGURATION_MONO, but it is not available for
        // API level 3.

        // Unlike AudioTrack buffer, AudioRecord buffer could be larger than
        // minimum without causing any problems. But minimum works well.
        final int audioRecordBufferSizeInBytes = AudioRecord.getMinBufferSize(
                SpeechTrainerConfig.SAMPLE_RATE_HZ, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        if (audioRecordBufferSizeInBytes <= 0) {
            throw new InitializationException("Failed to initialize recording.");
        }

        // CHANNEL_IN_MONO is guaranteed to work on all devices.
        // ENCODING_PCM_16BIT is guaranteed to work on all devices.
        audioRecord = new AudioRecord(AudioSource.MIC, SpeechTrainerConfig.SAMPLE_RATE_HZ,
                AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
                audioRecordBufferSizeInBytes);
        if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) {
            audioRecord = null;
            throw new InitializationException("Failed to initialize recording.");
        }
    }

    private void createAudioTrack() throws InitializationException {
        // The AudioTrack configurations parameters used here, are guaranteed to
        // be supported on all devices.

        // AudioFormat.CHANNEL_OUT_MONO should be used in place of deprecated
        // AudioFormat.CHANNEL_CONFIGURATION_MONO, but it is not available for
        // API level 3.

        // Output buffer for playing should be as short as possible, so
        // AudioBufferPlayed events are not invoked long before audio buffer is
        // actually played. Also, when AudioTrack is stopped, it is filled with
        // silence of length audioTrackBufferSizeInBytes. If the silence is too
        // long, it causes a delay before the next recorded data starts playing.
        audioTrackBufferSizeInBytes = AudioTrack.getMinBufferSize(
                SpeechTrainerConfig.SAMPLE_RATE_HZ,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);
        if (audioTrackBufferSizeInBytes <= 0) {
            throw new InitializationException("Failed to initialize playback.");
        }

        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                SpeechTrainerConfig.SAMPLE_RATE_HZ,
                AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
                audioTrackBufferSizeInBytes,
                AudioTrack.MODE_STREAM);
        if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
            audioTrack = null;
            throw new InitializationException("Failed to initialize playback.");
        }
    }


}




Java Source Code List

mixedbit.speechtrainer.AssertionsTest.java
mixedbit.speechtrainer.Assertions.java
mixedbit.speechtrainer.SpeechTrainerConfig.java
mixedbit.speechtrainer.TrainingApplication.java
mixedbit.speechtrainer.controller.AudioBufferAllocatorTest.java
mixedbit.speechtrainer.controller.AudioBufferAllocator.java
mixedbit.speechtrainer.controller.AudioEventListener.java
mixedbit.speechtrainer.controller.AutomaticTrainingControllerTest.java
mixedbit.speechtrainer.controller.AutomaticTrainingController.java
mixedbit.speechtrainer.controller.ControllerFactory.java
mixedbit.speechtrainer.controller.InteractiveTrainingControllerTest.java
mixedbit.speechtrainer.controller.InteractiveTrainingController.java
mixedbit.speechtrainer.controller.Player.java
mixedbit.speechtrainer.controller.RecordPlayTaskManagerTest.java
mixedbit.speechtrainer.controller.RecordPlayTaskManager.java
mixedbit.speechtrainer.controller.Recorder.java
mixedbit.speechtrainer.controller.SilenceFilterTest.java
mixedbit.speechtrainer.controller.SilenceFilter.java
mixedbit.speechtrainer.controller.SilenceLevelDetectorTest.java
mixedbit.speechtrainer.controller.SilenceLevelDetector.java
mixedbit.speechtrainer.controller.TrainingControllerTest.java
mixedbit.speechtrainer.controller.TrainingController.java
mixedbit.speechtrainer.model.AudioBufferInfo.java
mixedbit.speechtrainer.model.AudioEventCollectorTest.java
mixedbit.speechtrainer.model.AudioEventCollector.java
mixedbit.speechtrainer.model.AudioEventHistory.java
mixedbit.speechtrainer.view.AudioEventView.java
mixedbit.speechtrainer.view.FileViewerActivity.java
mixedbit.speechtrainer.view.TrainingActivity.java
mixedbit.speechtrainer.view.TrainingPreferenceActivity.java