aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/examples/SeeOscillators.java
blob: b8088c4f3901d59d979c31744eb6fe01459cfc4c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright 2010 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.examples;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.scope.AudioScope;
import com.jsyn.scope.AudioScopeProbe;
import com.jsyn.swing.DoubleBoundedRangeSlider;
import com.jsyn.swing.JAppletFrame;
import com.jsyn.swing.PortControllerFactory;
import com.jsyn.unitgen.ImpulseOscillator;
import com.jsyn.unitgen.ImpulseOscillatorBL;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.LinearRamp;
import com.jsyn.unitgen.MorphingOscillatorBL;
import com.jsyn.unitgen.Multiply;
import com.jsyn.unitgen.PulseOscillator;
import com.jsyn.unitgen.PulseOscillatorBL;
import com.jsyn.unitgen.RedNoise;
import com.jsyn.unitgen.SawtoothOscillator;
import com.jsyn.unitgen.SawtoothOscillatorBL;
import com.jsyn.unitgen.SawtoothOscillatorDPW;
import com.jsyn.unitgen.SineOscillator;
import com.jsyn.unitgen.SquareOscillator;
import com.jsyn.unitgen.SquareOscillatorBL;
import com.jsyn.unitgen.TriangleOscillator;
import com.jsyn.unitgen.UnitOscillator;

/**
 * Display each oscillator's waveform using the AudioScope. This is a re-implementation of the
 * TJ_SeeOsc Applet from the old API.
 *
 * @author Phil Burk (C) 2010 Mobileer Inc
 */
public class SeeOscillators extends JApplet {
    private static final long serialVersionUID = -8315903842197137926L;
    private Synthesizer synth;
    private ArrayList<UnitOscillator> oscillators = new ArrayList<UnitOscillator>();
    private LineOut lineOut;
    private AudioScope scope;
    private JPanel oscPanel;
    private Multiply oscGain;
    private ButtonGroup buttonGroup;
    private LinearRamp freqRamp;
    private LinearRamp widthRamp;
    private LinearRamp shapeRamp;
    private DoubleBoundedRangeSlider widthSlider;
    private DoubleBoundedRangeSlider shapeSlider;

    /* Can be run as either an application or as an applet. */
    public static void main(String args[]) {
        SeeOscillators applet = new SeeOscillators();
        JAppletFrame frame = new JAppletFrame("ShowWaves", applet);
        frame.setSize(640, 500);
        frame.setVisible(true);
        frame.test();
        frame.validate();
    }

    private void setupGUI() {
        setLayout(new BorderLayout());

        add(BorderLayout.NORTH, new JLabel("Show Oscillators in an AudioScope"));

        scope = new AudioScope(synth);
        AudioScopeProbe probe = scope.addProbe(oscGain.output);
        probe.setAutoScaleEnabled(false);
        probe.setVerticalScale(1.1);
        scope.setTriggerMode(AudioScope.TriggerMode.NORMAL);
        // scope.getModel().getTriggerModel().getLevelModel().setDoubleValue( 0.0001 );
        // Turn off the gain and trigger control GUI.
        scope.getView().setControlsVisible(false);
        scope.start();
        add(BorderLayout.CENTER, scope.getView());

        JPanel southPanel = new JPanel();
        southPanel.setLayout(new GridLayout(0, 1));
        add(BorderLayout.SOUTH, southPanel);

        oscPanel = new JPanel();
        oscPanel.setLayout(new GridLayout(2, 5));
        southPanel.add(oscPanel);

        southPanel.add(PortControllerFactory.createExponentialPortSlider(freqRamp.input));
        southPanel.add(PortControllerFactory.createExponentialPortSlider(oscGain.inputB));
        southPanel.add(widthSlider = PortControllerFactory.createPortSlider(widthRamp.input));
        widthSlider.setEnabled(false);
        southPanel.add(shapeSlider = PortControllerFactory.createPortSlider(shapeRamp.input));
        shapeSlider.setEnabled(false);

        oscPanel.validate();
        validate();
    }

    @Override
    public void start() {
        synth = JSyn.createSynthesizer();

        // Use a multiplier for gain control and so we can hook up to the scope
        // from a single unit.
        synth.add(oscGain = new Multiply());
        oscGain.inputB.setup(0.02, 0.5, 1.0);
        oscGain.inputB.setName("Amplitude");

        synth.add(freqRamp = new LinearRamp());
        freqRamp.input.setup(50.0, 300.0, 20000.0);
        freqRamp.input.setName("Frequency");
        freqRamp.time.set(0.1);

        synth.add(widthRamp = new LinearRamp());
        widthRamp.input.setup(-1.0, 0.0, 1.0);
        widthRamp.input.setName("Width");
        widthRamp.time.set(0.1);

        synth.add(shapeRamp = new LinearRamp());
        shapeRamp.input.setup(-1.0, 0.0, 1.0);
        shapeRamp.input.setName("Shape");
        shapeRamp.time.set(0.1);

        // Add an output so we can hear the oscillators.
        synth.add(lineOut = new LineOut());

        oscGain.output.connect(0, lineOut.input, 0);
        oscGain.output.connect(0, lineOut.input, 1);

        setupGUI();

        buttonGroup = new ButtonGroup();

        addOscillator(new SineOscillator(), "Sine");
        addOscillator(new TriangleOscillator(), "Triangle");
        addOscillator(new SawtoothOscillator(), "Sawtooth");
        addOscillator(new SawtoothOscillatorBL(), "SawBL");
        addOscillator(new SawtoothOscillatorDPW(), "SawDPW");
        addOscillator(new RedNoise(), "RedNoise");

        addOscillator(new SquareOscillator(), "Square");
        addOscillator(new SquareOscillatorBL(), "SquareBL");
        addOscillator(new PulseOscillator(), "Pulse");
        addOscillator(new PulseOscillatorBL(), "PulseBL");
        addOscillator(new MorphingOscillatorBL(), "MorphBL");
        addOscillator(new ImpulseOscillator(), "Impulse");
        addOscillator(new ImpulseOscillatorBL(), "ImpulseBL");

        // Start synthesizer using default stereo output at 44100 Hz.
        synth.start();
        // Start lineOut so it can pull data from other units.
        lineOut.start();

        // We only need to start the LineOut. It will pull data from the
        // oscillator.
        lineOut.start();

    }

    private void addOscillator(final UnitOscillator osc, String label) {
        oscillators.add(osc);
        synth.add(osc);
        freqRamp.output.connect(osc.frequency);
        if (osc instanceof PulseOscillatorBL) {
            widthRamp.output.connect(((PulseOscillatorBL)osc).width);
        }
        if (osc instanceof PulseOscillator) {
            widthRamp.output.connect(((PulseOscillator)osc).width);
        }
        if (osc instanceof MorphingOscillatorBL) {
            shapeRamp.output.connect(((MorphingOscillatorBL)osc).shape);
        }
        osc.amplitude.set(1.0);
        JRadioButton checkBox = new JRadioButton(label);
        buttonGroup.add(checkBox);
        checkBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // Disconnect other oscillators.
                oscGain.inputA.disconnectAll(0);
                // Connect this one.
                osc.output.connect(oscGain.inputA);
                widthSlider.setEnabled(osc instanceof PulseOscillator
                        || osc instanceof PulseOscillatorBL);
                shapeSlider.setEnabled(osc instanceof MorphingOscillatorBL);
            }
        });
        oscPanel.add(checkBox);
    }

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

}