First develop commit. Adding all existing files.
This commit is contained in:
42
src/audio/PlayThatTune.java
Normal file
42
src/audio/PlayThatTune.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package audio;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import stdlib.StdAudio;
|
||||
import stdlib.StdIn;
|
||||
|
||||
/**
|
||||
* This is a data-driven program that plays pure tones from
|
||||
* the notes on the chromatic scale, specified on standard input
|
||||
* by their distance from concert A.
|
||||
* % java PlayThatTune < elise.txt
|
||||
* @author Daniel Weschke
|
||||
*/
|
||||
public class PlayThatTune {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if(args.length == 0) return;
|
||||
|
||||
// repeat as long as there are more integers to read in
|
||||
while(!StdIn.isEmpty()) {
|
||||
|
||||
// read in the pitch, where 0 = Concert A (A4)
|
||||
int pitch = StdIn.readInt();
|
||||
|
||||
// read in duration in seconds
|
||||
double duration = StdIn.readDouble();
|
||||
|
||||
// build sine wave with desired frequency
|
||||
double hz = 440 * Math.pow(2, pitch / 12.0);
|
||||
int N = (int) (StdAudio.SAMPLE_RATE * duration);
|
||||
double[] a = new double[N+1];
|
||||
int i;
|
||||
for(i=0; i<=N; i++) {
|
||||
a[i] = Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
|
||||
}
|
||||
|
||||
// play it using standard audio
|
||||
StdAudio.play(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user