aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/research/RecordVariousRamps.java
blob: c90ea9a4ad2e1c5a44dd78524e33716b9fdd6dd9 (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
/*
 * Copyright 2014 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.
 */
/** 
 * Generate steps, linear ramps and smooth ramps.
 *
 * @author (C) 2014 Phil Burk
 */

package com.jsyn.research;

import java.io.File;
import java.io.IOException;

import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.unitgen.ContinuousRamp;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.LinearRamp;
import com.jsyn.unitgen.Multiply;
import com.jsyn.unitgen.PassThrough;
import com.jsyn.unitgen.PowerOfTwo;
import com.jsyn.unitgen.SawtoothOscillatorBL;
import com.jsyn.unitgen.UnitFilter;
import com.jsyn.unitgen.UnitOscillator;
import com.jsyn.util.WaveRecorder;

public class RecordVariousRamps {
    private Synthesizer synth;
    private UnitOscillator osc;
    private Multiply multiplier;
    private UnitFilter ramp;
    private LinearRamp linearRamp;
    private ContinuousRamp continuousRamp;
    private LineOut lineOut;
    private WaveRecorder recorder;
    private PowerOfTwo powerOfTwo;
    private static final int MODE_STEP = 0;
    private static final int MODE_LINEAR = 1;
    private static final int MODE_SMOOTH = 2;
    private static final String[] modeNames = {
            "step", "linear", "smooth"
    };

    private RampEvent[] rampData = {
            new RampEvent(1.0, 1.5, 2.0), new RampEvent(-0.9, 0.5, 1.0),
            new RampEvent(0.9, 0.5, 0.8), new RampEvent(-0.3, 0.5, 0.8),
            new RampEvent(0.9, 0.5, 0.3), new RampEvent(-0.5, 0.5, 0.3),
            new RampEvent(0.8, 2.0, 1.0),
    };

    private static class RampEvent {
        double target;
        double eventDuration;
        double rampDuration;

        RampEvent(double target, double eventDuration, double rampDuration) {
            this.target = target;
            this.eventDuration = eventDuration;
            this.rampDuration = rampDuration;
        }
    }

    private void test(int mode) throws IOException {
        // Create a context for the synthesizer.
        synth = JSyn.createSynthesizer();
        synth.setRealTime(false);

        File waveFile = new File("ramp_pitch_" + modeNames[mode] + ".wav");
        // Mono 16 bits.
        recorder = new WaveRecorder(synth, waveFile, 1, 16);
        System.out.println("Writing to 16-bit WAV file " + waveFile.getAbsolutePath());

        // Add some tone generators.
        synth.add(osc = new SawtoothOscillatorBL());

        // Add a controller that will sweep up.
        synth.add(multiplier = new Multiply());
        synth.add(powerOfTwo = new PowerOfTwo());
        // Add an output unit.
        synth.add(lineOut = new LineOut());
        multiplier.inputB.set(660.0);

        switch (mode) {
            case MODE_STEP:
                synth.add(ramp = new PassThrough());
                break;
            case MODE_LINEAR:
                synth.add(ramp = linearRamp = new LinearRamp());
                linearRamp.current.set(-1.0);
                linearRamp.time.set(10.0);
                break;
            case MODE_SMOOTH:
                synth.add(ramp = continuousRamp = new ContinuousRamp());
                continuousRamp.current.set(-1.0);
                continuousRamp.time.set(10.0);
                break;
        }

        ramp.getInput().set(-1.0);
        ramp.getOutput().connect(powerOfTwo.input);

        powerOfTwo.output.connect(multiplier.inputA);
        multiplier.output.connect(osc.frequency);

        // Connect the oscillator to the left and right audio 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();

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

        // We also need to start the LineOut if we want to hear it now.
        lineOut.start();

        // Get synthesizer time in seconds.
        double nextEventTime = synth.getCurrentTime() + 1.0;
        try {
            synth.sleepUntil(nextEventTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (RampEvent rampEvent : rampData) {

            switch (mode) {
                case MODE_STEP:
                    break;
                case MODE_LINEAR:
                    linearRamp.time.set(rampEvent.rampDuration);
                    break;
                case MODE_SMOOTH:
                    continuousRamp.time.set(rampEvent.rampDuration);
                    break;
            }
            ramp.getInput().set(rampEvent.target);

            nextEventTime += rampEvent.eventDuration;
            System.out.println("target = " + rampEvent.target + ", rampDur = "
                    + rampEvent.rampDuration + ", eventDur = " + rampEvent.eventDuration);
            try {
                synth.sleepUntil(nextEventTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

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

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

}