Android Open Source - speech_trainer Audio Event Collector Test






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.//from w  w  w.  j  a  v a2s  . c o 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.model;

import java.util.Iterator;

import junit.framework.TestCase;
import mixedbit.speechtrainer.controller.AudioEventListener;

import org.easymock.EasyMock;


public class AudioEventCollectorTest extends TestCase {

    private static final double DELTA = 0.001;
    AudioEventListener mockAudioEventListener;
    AudioEventCollector audioEventCollector;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mockAudioEventListener = EasyMock.createStrictMock(AudioEventListener.class);
        audioEventCollector = new AudioEventCollector(mockAudioEventListener);
    }

    private void replayAll() {
        EasyMock.replay(mockAudioEventListener);
    }

    private void verifyAll() {
        EasyMock.verify(mockAudioEventListener);
    }

    public void testMinAndMaxRecordedSoundLevel() {
        audioEventCollector.recordingStarted();
        audioEventCollector.audioBufferRecorded(0, 156.0);
        assertEquals(156.0, audioEventCollector.getMaxSoundLevel(), DELTA);
        assertEquals(156.0, audioEventCollector.getMinSoundLevel(), DELTA);

        audioEventCollector.audioBufferRecorded(1, 172.0);
        assertEquals(172.0, audioEventCollector.getMaxSoundLevel(), DELTA);
        assertEquals(156.0, audioEventCollector.getMinSoundLevel(), DELTA);

        audioEventCollector.audioBufferRecorded(2, 122.0);
        assertEquals(172.0, audioEventCollector.getMaxSoundLevel(), DELTA);
        assertEquals(122.0, audioEventCollector.getMinSoundLevel(), DELTA);
    }

    public void testAudioEventsPropagatedToNextListener() {
        final int bufferId = 1;
        final int soundLevel = 112;
        mockAudioEventListener.recordingStarted();
        mockAudioEventListener.audioBufferRecorded(bufferId, soundLevel);
        mockAudioEventListener.audioBufferRecordingFailed();
        mockAudioEventListener.recordingStopped();
        mockAudioEventListener.playingStarted();
        mockAudioEventListener.audioBufferPlayed(bufferId, soundLevel);
        mockAudioEventListener.playingStopped();
        replayAll();

        audioEventCollector.recordingStarted();
        audioEventCollector.audioBufferRecorded(bufferId, soundLevel);
        audioEventCollector.audioBufferRecordingFailed();
        audioEventCollector.recordingStopped();
        audioEventCollector.playingStarted();
        audioEventCollector.audioBufferPlayed(bufferId, soundLevel);
        audioEventCollector.playingStopped();
        verifyAll();
    }

    public void testAudioEventIterationPerformedOverAllEventsInLIFOOrder() {
        final int eventsCount = 100;
        audioEventCollector.recordingStarted();
        for (int i = 0; i < eventsCount; i++) {
            audioEventCollector.audioBufferRecorded(i, 100 + i);
        }
        ;
        final Iterator<AudioBufferInfo> it =
            audioEventCollector.getIteratorOverAudioEventsToPlot(10);
        int iteratedEventsCount = 0;
        while (it.hasNext()) {
            final AudioBufferInfo audioBufferInfo = it.next();
            iteratedEventsCount += 1;
            assertEquals(eventsCount - iteratedEventsCount, audioBufferInfo.getAudioBufferId());
            assertEquals(100 + eventsCount - iteratedEventsCount,
                    audioBufferInfo.getSoundLevel(), DELTA);
            // No buffers were played.
            assertFalse(audioBufferInfo.isPlayed());
        }
        assertEquals(eventsCount, iteratedEventsCount);
    }

    public void testResetHistory() {
        audioEventCollector.recordingStarted();
        audioEventCollector.audioBufferRecorded(0, 156.0);
        audioEventCollector.audioBufferRecorded(1, 12.0);
        assertEquals(12.0, audioEventCollector.getMinSoundLevel(), DELTA);
        assertEquals(156.0, audioEventCollector.getMaxSoundLevel(), DELTA);
        assertTrue(audioEventCollector.getIteratorOverAudioEventsToPlot(1).hasNext());

        audioEventCollector.resetHistory();
        // Make sure information about two recorded buffers is removed.
        assertEquals(Double.MAX_VALUE, audioEventCollector.getMinSoundLevel(), DELTA);
        assertEquals(0.0, audioEventCollector.getMaxSoundLevel(), DELTA);
        assertFalse(audioEventCollector.getIteratorOverAudioEventsToPlot(1).hasNext());
    }

    public void testHistorySizeRespectedOldEventsRemoved() {
        final int eventsCount = 2 * AudioEventCollector.HISTORY_SIZE;
        audioEventCollector.recordingStarted();
        for (int i = 0; i < eventsCount; i++) {
            audioEventCollector.audioBufferRecorded(i, 100 + i);
        }
        final Iterator<AudioBufferInfo> it =
            audioEventCollector.getIteratorOverAudioEventsToPlot(10);

        int iteratedEventsCount = 0;
        while (it.hasNext()) {
            final AudioBufferInfo audioBufferInfo = it.next();
            iteratedEventsCount += 1;
            assertEquals(eventsCount - iteratedEventsCount, audioBufferInfo.getAudioBufferId());
            assertEquals(100 + eventsCount - iteratedEventsCount,
                    audioBufferInfo.getSoundLevel(), DELTA);
            // No buffers were played.
            assertFalse(audioBufferInfo.isPlayed());
        }
        assertEquals(AudioEventCollector.HISTORY_SIZE, iteratedEventsCount);
    }

    public void testIteratorRemainsValidUntilTheNextCallToGetIterator() {
        final int eventsCount = AudioEventCollector.HISTORY_SIZE;
        final double soundLevel = 51.5;
        audioEventCollector.recordingStarted();
        for (int i = 0; i < eventsCount; i++) {
            audioEventCollector.audioBufferRecorded(i, soundLevel);
        }
        Iterator<AudioBufferInfo> it = audioEventCollector
        .getIteratorOverAudioEventsToPlot(10);

        // New events cause HISTORY_SIZE to be exceeded, but the iterator over
        // audio events should remain valid until the next call to
        // getIteratorOverAudioEventsToDisplay().
        for (int i = 0; i < eventsCount; i++) {
            audioEventCollector.audioBufferRecorded(i + eventsCount, soundLevel);
        }

        int iteratedEventsCount = 0;
        while (it.hasNext()) {
            final AudioBufferInfo audioBufferInfo = it.next();
            iteratedEventsCount += 1;
            // Make sure iterator goes through old events.
            assertEquals(eventsCount - iteratedEventsCount, audioBufferInfo.getAudioBufferId());
        }
        assertEquals(AudioEventCollector.HISTORY_SIZE, eventsCount);

        // Get the iterator again, this time old events should be removed.
        it = audioEventCollector.getIteratorOverAudioEventsToPlot(10);
        iteratedEventsCount = 0;
        while (it.hasNext()) {
            final AudioBufferInfo audioBufferInfo = it.next();
            iteratedEventsCount += 1;
            assertEquals(2 * eventsCount - iteratedEventsCount, audioBufferInfo.getAudioBufferId());
        }
        assertEquals(AudioEventCollector.HISTORY_SIZE, eventsCount);
    }

    public void testRecentlyPlayedBuffersMarked() {
        final double soundLevel = 12.0;
        audioEventCollector.recordingStarted();
        audioEventCollector.audioBufferRecorded(0, soundLevel);
        audioEventCollector.audioBufferRecorded(1, soundLevel);
        audioEventCollector.audioBufferRecorded(2, soundLevel);
        audioEventCollector.audioBufferRecorded(3, soundLevel);
        audioEventCollector.recordingStopped();
        audioEventCollector.playingStarted();
        audioEventCollector.audioBufferPlayed(1, soundLevel);
        audioEventCollector.audioBufferPlayed(2, soundLevel);
        audioEventCollector.playingStopped();

        Iterator<AudioBufferInfo> it = audioEventCollector
        .getIteratorOverAudioEventsToPlot(10);
        AudioBufferInfo audioBufferInfo = it.next();
        assertEquals(3, audioBufferInfo.getAudioBufferId());
        assertFalse(audioBufferInfo.isPlayed());

        audioBufferInfo = it.next();
        assertEquals(2, audioBufferInfo.getAudioBufferId());
        assertTrue(audioBufferInfo.isPlayed());

        audioBufferInfo = it.next();
        assertEquals(1, audioBufferInfo.getAudioBufferId());
        assertTrue(audioBufferInfo.isPlayed());

        audioBufferInfo = it.next();
        assertEquals(0, audioBufferInfo.getAudioBufferId());
        assertFalse(audioBufferInfo.isPlayed());

        assertFalse(it.hasNext());

        audioEventCollector.playingStarted();
        it = audioEventCollector.getIteratorOverAudioEventsToPlot(10);
        assertFalse(it.next().isPlayed());
        assertFalse(it.next().isPlayed());
        assertFalse(it.next().isPlayed());
        assertFalse(it.next().isPlayed());
        assertFalse(it.hasNext());
    }

    public void testIteratorCenteredOnLastPlayedBufferWhilePlaying() {
        final double soundLevel = 12.0;
        final int plotWidth = 3;
        audioEventCollector.recordingStarted();
        audioEventCollector.audioBufferRecorded(0, soundLevel);
        audioEventCollector.audioBufferRecorded(1, soundLevel);
        audioEventCollector.audioBufferRecorded(2, soundLevel);
        audioEventCollector.audioBufferRecorded(3, soundLevel);
        audioEventCollector.audioBufferRecorded(4, soundLevel);
        audioEventCollector.recordingStopped();
        audioEventCollector.playingStarted();
        audioEventCollector.audioBufferPlayed(1, soundLevel);

        Iterator<AudioBufferInfo> it =
            audioEventCollector.getIteratorOverAudioEventsToPlot(plotWidth);
        AudioBufferInfo audioBufferInfo = it.next();
        // Last played buffer with id 1 should be at the center of the plot.
        // The plot has a width of 3, this means the iterator should point at
        // the buffer with id 2.
        assertEquals(2, audioBufferInfo.getAudioBufferId());
        assertFalse(audioBufferInfo.isPlayed());
        assertEquals(1, it.next().getAudioBufferId());

        audioEventCollector.audioBufferPlayed(2, soundLevel);
        // The last played buffer has id 2, so the iterator should point at 3.
        it = audioEventCollector.getIteratorOverAudioEventsToPlot(3);
        audioBufferInfo = it.next();
        assertEquals(3, audioBufferInfo.getAudioBufferId());
        assertFalse(audioBufferInfo.isPlayed());

        audioEventCollector.playingStopped();
        audioEventCollector.recordingStarted();

        // Recording started, the iterator should point at the last recorded
        // buffer (the one with id 4).
        it = audioEventCollector.getIteratorOverAudioEventsToPlot(3);
        audioBufferInfo = it.next();
        assertEquals(4, audioBufferInfo.getAudioBufferId());
        assertFalse(audioBufferInfo.isPlayed());
    }
}




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