aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/jsyn/util/TestVoiceAllocator.java
blob: 061e2ae10c9e904af93991a0453d32a32c755168 (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 com.jsyn.instruments.SubtractiveSynthVoice;
import com.jsyn.unitgen.UnitVoice;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

    @BeforeEach
    private void beforeEach() {
        voices = new UnitVoice[max];
        for (int i = 0; i < max; i++) {
            voices[i] = new SubtractiveSynthVoice();
        }

        allocator = new VoiceAllocator(voices);
    }

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

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

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

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

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

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

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

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

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

    @Test
    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(allocator.isOn(tag3), "voice 3 should start on");
        allocator.off(tag3);
        assertFalse(allocator.isOn(tag3), "voice 3 should now be off");

        allocator.off(tag2);

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

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