aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/examples/EditEnvelope1.java
blob: 763037bfa6420565529983f4bd22a1fd76ba9fd9 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright 1997 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.
 */
/** 
 * Test Envelope using Java Audio Synthesizer
 * Trigger attack or release portion.
 *
 * @author (C) 1997 Phil Burk
 */

package com.jsyn.examples;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.data.SegmentedEnvelope;
import com.jsyn.swing.EnvelopeEditorPanel;
import com.jsyn.swing.EnvelopePoints;
import com.jsyn.swing.JAppletFrame;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.SawtoothOscillatorBL;
import com.jsyn.unitgen.UnitOscillator;
import com.jsyn.unitgen.VariableRateDataReader;
import com.jsyn.unitgen.VariableRateMonoReader;

public class EditEnvelope1 extends JApplet {
    private Synthesizer synth;
    private UnitOscillator osc;
    private LineOut lineOut;
    private SegmentedEnvelope envelope;
    private VariableRateDataReader envelopePlayer;

    final int MAX_FRAMES = 16;
    JButton hitme;
    JButton attackButton;
    JButton releaseButton;
    private EnvelopeEditorPanel envEditor;
    private EnvelopePoints points;

    /* Can be run as either an application or as an applet. */
    public static void main(String args[]) {
        EditEnvelope1 applet = new EditEnvelope1();
        JAppletFrame frame = new JAppletFrame("Test SynthEnvelope", applet);
        frame.setSize(440, 200);
        frame.setVisible(true);
        frame.test();
    }

    /*
     * Setup synthesis.
     */
    @Override
    public void start() {
        setLayout(new BorderLayout());

        // Create a context for the synthesizer.
        synth = JSyn.createSynthesizer();
        // Add a tone generator.
        synth.add(osc = new SawtoothOscillatorBL());
        // Add an envelope player.
        synth.add(envelopePlayer = new VariableRateMonoReader());

        envelope = new SegmentedEnvelope(MAX_FRAMES);

        // Add an output mixer.
        synth.add(lineOut = new LineOut());
        envelopePlayer.output.connect(osc.amplitude);
        // Connect the oscillator to the output.
        osc.output.connect(0, lineOut.input, 0);
        osc.output.connect(0, lineOut.input, 1);

        // Start synthesizer using default stereo output at 44100 Hz.
        synth.start();

        JPanel bottomPanel = new JPanel();
        bottomPanel.add(hitme = new JButton("On"));
        hitme.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                points.updateEnvelopeIfDirty(envelope);
                envelopePlayer.dataQueue.queueOn(envelope);
            }
        });

        bottomPanel.add(attackButton = new JButton("Off"));
        attackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                points.updateEnvelopeIfDirty(envelope);
                envelopePlayer.dataQueue.queueOff(envelope);
            }
        });

        bottomPanel.add(releaseButton = new JButton("Queue"));
        releaseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                points.updateEnvelopeIfDirty(envelope);
                envelopePlayer.dataQueue.queue(envelope);
            }
        });

        add(bottomPanel, BorderLayout.SOUTH);
        lineOut.start();

        // Create vector of points for editor.
        points = new EnvelopePoints();
        points.setName(osc.amplitude.getName());

        // Setup initial envelope shape.
        points.add(0.5, 1.0);
        points.add(0.5, 0.2);
        points.add(0.5, 0.8);
        points.add(0.5, 0.0);
        points.updateEnvelope(envelope);

        // Add an envelope editor to the center of the panel.
        add("Center", envEditor = new EnvelopeEditorPanel(points, MAX_FRAMES));

        /* Synchronize Java display. */
        getParent().validate();
        getToolkit().sync();
    }

    @Override
    public void stop() {
        synth.stop();
    }
}