Musical scale: Difference between revisions

Content deleted Content added
Aerobar (talk | contribs)
add RPL
PSNOW123 (talk | contribs)
Line 907:
* Java can play sounds without external libraries.
*/
public final class MusicalScale {
 
public static void main(String[] argsaArgs) throws LineUnavailableException {
List<Double> frequencies = List.of( 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25 );
final int duration = 500;
Line 921:
}
private static void musicalTone(double aFrequency, int aDuration, int aVolume) throws LineUnavailableException {
byte[] buffer = new byte[1];
AudioFormat audioFormat = getAudioFormat();
try ( SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat); ) {
sourceDataLine.open(audioFormat);
sourceDataLine.start();
for ( int i = 0; i < aDuration * 8; i++ ) {
double angle = i / ( SAMPLE_RATE / aFrequency ) * 2 * Math.PI;
buffer[0] = (byte) ( Math.sin(angle) * 127 * aVolume );
sourceDataLine.write(buffer, BYTE_OFFSET, buffer.length);
}
sourceDataLine.drain();
sourceDataLine.stop();
sourceDataLine.close();
} catch (LineUnavailableException exception) {
exception.printStackTrace();
}
}