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
|
/*
* 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.
*/
/**
* Test recording to disk in non-real-time.
* Play several frequencies of a sine wave.
* Save data in a WAV file format.
*
* @author (C) 2010 Phil Burk
*/
package com.jsyn.examples;
import java.io.File;
import java.io.IOException;
import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.unitgen.ExponentialRamp;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.SawtoothOscillatorBL;
import com.jsyn.unitgen.SineOscillator;
import com.jsyn.unitgen.UnitOscillator;
import com.jsyn.util.WaveRecorder;
public class RecordSineSweep {
final static double SONG_DURATION = 4.0;
private Synthesizer synth;
private UnitOscillator leftOsc;
private UnitOscillator rightOsc;
private ExponentialRamp sweeper;
private LineOut lineOut;
private WaveRecorder recorder;
private final static boolean useRecorder = true;
private void test() throws IOException {
// Create a context for the synthesizer.
synth = JSyn.createSynthesizer();
synth.setRealTime(false);
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());
}
// Add some tone generators.
synth.add(leftOsc = new SineOscillator());
synth.add(rightOsc = new SawtoothOscillatorBL());
// Add a controller that will sweep up.
synth.add(sweeper = new ExponentialRamp());
// Add an output unit.
synth.add(lineOut = new LineOut());
sweeper.current.set(50.0);
sweeper.input.set(1400.0);
sweeper.time.set(SONG_DURATION);
sweeper.output.connect(leftOsc.frequency);
sweeper.output.connect(rightOsc.frequency);
// Connect the oscillator to the left and right audio output.
leftOsc.output.connect(0, lineOut.input, 0);
rightOsc.output.connect(0, lineOut.input, 1);
// Start synthesizer using default stereo output at 44100 Hz.
synth.start();
if (useRecorder) {
leftOsc.output.connect(0, recorder.getInput(), 0);
rightOsc.output.connect(0, recorder.getInput(), 1);
// 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 timeNow = synth.getCurrentTime();
// Sleep while the sound is being generated in the background thread.
try {
synth.sleepUntil(timeNow + SONG_DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Test stopping and restarting a recorder. This will cause a pop.
if (recorder != null) {
System.out.println("Stop and restart recorder.");
recorder.stop();
}
sweeper.input.set(100.0);
timeNow = synth.getCurrentTime();
if (recorder != null) {
recorder.start();
}
// Sleep while the sound is being generated in the background thread.
try {
synth.sleepUntil(timeNow + SONG_DURATION);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (recorder != null) {
recorder.stop();
recorder.close();
}
// Stop everything.
synth.stop();
}
public static void main(String[] args) {
try {
new RecordSineSweep().test();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|