aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/util/TestVoiceAllocator.java
blob: fd5ba0b6b3cedae23387ce324fe8f23c4b4077a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2009 Phil Burk, Mobileer Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsyn.util;

import junit.framework.TestCase;

import com.jsyn.instruments.SubtractiveSynthVoice;
import com.jsyn.unitgen.UnitVoice;

public class TestVoiceAllocator extends TestCase {
    VoiceAllocator allocator;
    int max = 4;
    private UnitVoice[] voices;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        voices = new UnitVoice[max];
        for (int i = 0; i < max; i++) {
            voices[i] = new SubtractiveSynthVoice();
        }

        allocator = new VoiceAllocator(voices);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testAllocation() {
        assertEquals("get max", max, allocator.getVoiceCount());

        int tag1 = 61;
        int tag2 = 62;
        int tag3 = 63;
        int tag4 = 64;
        int tag5 = 65;
        int tag6 = 66;
        UnitVoice voice1 = allocator.allocate(tag1);
        assertTrue("voice should be non-null", (voice1 != null));

        UnitVoice voice2 = allocator.allocate(tag2);
        assertTrue("voice should be non-null", (voice2 != null));
        assertTrue("new voice ", (voice2 != voice1));

        UnitVoice voice = allocator.allocate(tag1);
        assertTrue("should be voice1 again ", (voice == voice1));

        voice = allocator.allocate(tag2);
        assertTrue("should be voice2 again ", (voice == voice2));

        UnitVoice voice3 = allocator.allocate(tag3);
        @SuppressWarnings("unused")
        UnitVoice voice4 = allocator.allocate(tag4);

        UnitVoice voice5 = allocator.allocate(tag5);
        assertTrue("ran out so get voice1 as oldest", (voice5 == voice1));

        voice = allocator.allocate(tag2);
        assertTrue("should be voice2 again ", (voice == voice2));

        // Now voice 3 should be the oldest cuz voice 2 was touched.
        UnitVoice voice6 = allocator.allocate(tag6);
        assertTrue("ran out so get voice3 as oldest", (voice6 == voice3));
    }

    public void testOff() {
        int tag1 = 61;
        int tag2 = 62;
        int tag3 = 63;
        int tag4 = 64;
        int tag5 = 65;
        int tag6 = 66;
        UnitVoice voice1 = allocator.allocate(tag1);
        UnitVoice voice2 = allocator.allocate(tag2);
        UnitVoice voice3 = allocator.allocate(tag3);
        UnitVoice voice4 = allocator.allocate(tag4);

        assertTrue("voice 3 should start on", allocator.isOn(tag3));
        allocator.off(tag3);
        assertEquals("voice 3 should now be off", false, allocator.isOn(tag3));

        allocator.off(tag2);

        UnitVoice voice5 = allocator.allocate(tag5);
        assertTrue("should get voice3 cuz off first", (voice5 == voice3));
        UnitVoice voice6 = allocator.allocate(tag6);
        assertTrue("should get voice2 cuz off second", (voice6 == voice2));
        voice3 = allocator.allocate(tag3);
        assertTrue("should get voice1 cuz on first", (voice3 == voice1));

        voice1 = allocator.allocate(tag1);
        assertTrue("should get voice4 cuz next up", (voice1 == voice4));
    }
}