aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/examples/PlaySegmentedEnvelope.java
blob: e7cc8f7b7efa63c773b75c69648367710e715de0 (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
/*
 * 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.io.File;
import java.io.IOException;

import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.data.SegmentedEnvelope;
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;
import com.jsyn.util.WaveRecorder;

/**
 * Modulate the amplitude of an oscillator using a segmented envelope.
 * 
 * @author Phil Burk (C) 2010 Mobileer Inc
 */
public class PlaySegmentedEnvelope {
    private Synthesizer synth;
    private UnitOscillator osc;
    private LineOut lineOut;
    private SegmentedEnvelope envelope;
    private VariableRateDataReader envelopePlayer;
    private WaveRecorder recorder;
    private final static boolean useRecorder = true;

    private void test() throws IOException {
        // 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());

        if (useRecorder) {
            File waveFile = new File("temp_recording.wav");
            // Default is stereo, 16 bits.
            recorder = new WaveRecorder(synth, waveFile);
            System.out.println("Writing to WAV file " + waveFile.getAbsolutePath());
        }

        // Create an envelope consisting of (duration,value) pairs.
        double[] pairs = {
                0.1, 1.0, 0.2, 0.3, 0.6, 0.0
        };
        envelope = new SegmentedEnvelope(pairs);

        // 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();

        if (useRecorder) {
            osc.output.connect(0, recorder.getInput(), 0);
            envelopePlayer.output.connect(0, recorder.getInput(), 1);
            // When we start the recorder it will pull data from the oscillator
            // and sweeper.
            recorder.start();
        }

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

        try {
            // ---------------------------------------------
            // Queue the entire envelope to play once.
            envelopePlayer.dataQueue.queue(envelope);
            synth.sleepFor(2.0);

            // ---------------------------------------------
            // Queue the attack, then sustain for a while, then queue the
            // release.
            osc.frequency.set(750.0);
            envelopePlayer.dataQueue.queue(envelope, 0, 2); // attack
            synth.sleepFor(2.0);
            envelopePlayer.dataQueue.queue(envelope, 2, 1); // release
            synth.sleepFor(2.0);

            // ---------------------------------------------
            // Queue the attack, then sustain for a while, then queue the
            // release. But this time use the sustain loop points.
            osc.frequency.set(950.0);
            // For noteOn, we want to play frames 0 and 1 then stop before 2.
            envelope.setSustainBegin(2);
            envelope.setSustainEnd(2);
            envelopePlayer.dataQueue.queueOn(envelope); // attack
            synth.sleepFor(2.0);
            envelopePlayer.dataQueue.queueOff(envelope); // release
            synth.sleepFor(2.0);

            // ---------------------------------------------
            // Queue the entire envelope to play 4 times (3 loops back).
            osc.frequency.set(350.0);
            envelopePlayer.dataQueue.queueLoop(envelope, 0, envelope.getNumFrames(), 3);
            synth.sleepFor(5.0);

            // ---------------------------------------------
            // Queue the entire envelope as a repeating loop.
            // It will loop until something else is queued.
            osc.frequency.set(450.0);
            envelopePlayer.dataQueue.queueLoop(envelope, 0, envelope.getNumFrames());
            envelopePlayer.rate.set(3.0);
            synth.sleepFor(5.0);
            // Queue last frame to stop the looping.
            envelopePlayer.dataQueue.queue(envelope, envelope.getNumFrames() - 1, 1);
            synth.sleepFor(1.0);

            if (recorder != null) {
                recorder.stop();
                recorder.close();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Stop everything.
        synth.stop();
    }

    public static void main(String[] args) {
        try {
            new PlaySegmentedEnvelope().test();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}