Musical scale: Difference between revisions

m
(add lambdatalk task)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 3 users not shown)
Line 893:
 
0.25 is the duration of each note (in seconds).
 
=={{header|Java}}==
Java can play sounds without external libraries.
<syntaxhighlight lang="java">
import java.util.List;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
 
public final class MusicalScale {
 
public static void main(String[] aArgs) {
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;
final int volume = 1;
for ( int i = 0; i < 3; i++ ) {
for ( double frequency : frequencies ) {
musicalTone(frequency, duration, volume);
}
}
}
private static void musicalTone(double aFrequency, int aDuration, int aVolume) {
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();
}
}
private static AudioFormat getAudioFormat() {
final int sampleSizeInBits = 8;
final int numberChannels = 1;
final boolean signedData = true;
final boolean isBigEndian = false;
return new AudioFormat(SAMPLE_RATE, sampleSizeInBits, numberChannels, signedData, isBigEndian);
}
private static float SAMPLE_RATE = 8_000.0F;
private static final int BYTE_OFFSET = 0;
 
}
</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,378 ⟶ 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,488 ⟶ 1,602:
 
[https://www.dropbox.com/s/jf34s6apalw0k7c/CalmoSoftMusicalScale.avi?dl=0 Musical scale]
 
=={{header|RPL}}==
≪ 2 SWAP 12 / ^ 440 * ≫ '<span style="color:blue>FREQ</span>' STO
≪ { -9 -7 -5 -4 -2 0 2 3 }
1 OVER SIZE '''FOR''' j
DUP j GET <span style="color:blue>FREQ</span> .1 BEEP '''NEXT'''
DROP
≫ '<span style="color:blue>GAMME</span>' STO
 
=={{header|Scala}}==
Line 1,677 ⟶ 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,695 ⟶ 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,476

edits