Musical scale: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
(3 intermediate revisions by 2 users not shown)
Line 895:
 
=={{header|Java}}==
* Java can play sounds without external libraries.
 
<syntaxhighlight lang="java">
 
import java.util.List;
import javax.sound.sampled.AudioFormat;
Line 904 ⟶ 903:
import javax.sound.sampled.SourceDataLine;
 
public final class MusicalScale {
/**
* Java can play sounds without external libraries.
*/
public 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 ⟶ 917:
}
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();
}
}
Line 1,439 ⟶ 1,438:
winsound.Beep(int(note+.5), 500)
>>> </syntaxhighlight>
 
'''Library:''' SDL2 (pip install PySDL2)
 
<syntaxhighlight lang="python">
import sys
import ctypes
import math
import time
import struct
import sdl2
import sdl2.sdlmixer as mixer
 
 
def create_sound(frequency, duration):
"""Create a buffer with sound at given frequency and duration"""
num_samples = int(48000 * duration)
wave = struct.pack(
f"<{num_samples}i",
*[
int(2**30 * math.sin(2 * math.pi * frequency / 48000 * t))
for t in range(num_samples)
]
)
length = num_samples * 4
sound_buffer = (ctypes.c_ubyte * length).from_buffer_copy(wave)
sound = mixer.Mix_QuickLoad_RAW(
ctypes.cast(sound_buffer, ctypes.POINTER(ctypes.c_ubyte)), length
)
return sound
 
 
def main():
"""Play sine wave"""
mixer.Mix_Init(0)
 
mixer.Mix_OpenAudioDevice(48000, sdl2.AUDIO_S32, 1, 2048, None, 0)
 
note = 261.63
semitone = math.pow(2, 1 / 12)
duration = 0.5 # seconds
 
for step in [0, 2, 2, 1, 2, 2, 2, 1]:
note *= semitone**step
sound = create_sound(note, duration)
mixer.Mix_PlayChannel(0, sound, 1)
time.sleep(duration)
 
return 0
 
 
if __name__ == "__main__":
sys.exit(main())
 
</syntaxhighlight>
 
=={{header|R}}==
Line 1,747 ⟶ 1,800:
{{libheader|Wren-sound}}
As Wren-cli doesn't have any built-in audio support, we instead build a .wav file which can then be played using a utility such as rhythmbox or SoX.
<syntaxhighlight lang="ecmascriptwren">import "./sound" for Wav
 
var sampleRate = 44100
Line 1,765 ⟶ 1,818:
It's also possible to play .wav files which (preferably) have a sample rate of 44.1 kHz using DOME:
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "audio" for AudioEngine
 
class Main {
9,488

edits