Musical scale: Difference between revisions

Content deleted Content added
Aamrun (talk | contribs)
Added Processing implementation
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 16: Line 16:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>DEFINE PTR="CARD"
<syntaxhighlight lang="action!">DEFINE PTR="CARD"


PROC Wait(BYTE frames)
PROC Wait(BYTE frames)
Line 61: Line 61:
AUDCTL=$00 ;restore default configuration
AUDCTL=$00 ;restore default configuration
Wait(20)
Wait(20)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Musical_scale.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Musical_scale.png Screenshot from Atari 8-bit computer]
Line 74: Line 74:
=={{header|AmigaBASIC}}==
=={{header|AmigaBASIC}}==


<lang amigabasic>FOR i=1 to 8
<syntaxhighlight lang="amigabasic">FOR i=1 to 8
READ f
READ f
SOUND f,10
SOUND f,10
NEXT
NEXT


DATA 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25</lang>
DATA 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
{{works with|AutoHotkey 1.1}}
<lang AutoHotkey>for key, val in [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]
<syntaxhighlight lang="autohotkey">for key, val in [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]
SoundBeep, % val, 500</lang>
SoundBeep, % val, 500</syntaxhighlight>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang BASIC256>sound {261.63, 500, 293.66, 500, 329.63, 500, 349.23, 500, 392, 500, 440, 500, 493.88, 500, 523.25, 500}</lang>
<syntaxhighlight lang="basic256">sound {261.63, 500, 293.66, 500, 329.63, 500, 349.23, 500, 392, 500, 440, 500, 493.88, 500, 523.25, 500}</syntaxhighlight>


=={{header|Befunge}}==
=={{header|Befunge}}==
Line 94: Line 94:
The tune to be played is specified on the first line of the code. The notes are specified as MIDI note numbers in reverse order in a Befunge string (for example, ''Middle C'' is note number 60, which is the character "<"). This is followed by the count of notes in the string - 8 in this example.
The tune to be played is specified on the first line of the code. The notes are specified as MIDI note numbers in reverse order in a Befunge string (for example, ''Middle C'' is note number 60, which is the character "<"). This is followed by the count of notes in the string - 8 in this example.


<lang befunge>> "HGECA@><"8: v
<syntaxhighlight lang="befunge">> "HGECA@><"8: v
v0*73"MThd"0006010101"MTrk"000<
v0*73"MThd"0006010101"MTrk"000<
>>#1-#,:#\_$8*74++,0,28*"'"039v
>>#1-#,:#\_$8*74++,0,28*"'"039v
v,:,\,*2,1"Hd":\<,,,,,,*3"U"*9<
v,:,\,*2,1"Hd":\<,,,,,,*3"U"*9<
>"@1",2*,\,,1-:#^_"/3d",5*,,, @</lang>
>"@1",2*,\,,1-:#^_"/3d",5*,,, @</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Line 111: Line 111:
It is therefore essential to call nosound() at the end which ends the speaker output, otherwise the Turbo C (DOS) session will have to be ended.
It is therefore essential to call nosound() at the end which ends the speaker output, otherwise the Turbo C (DOS) session will have to be ended.


<syntaxhighlight lang="c">
<lang c>
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
Line 138: Line 138:
nosound();
nosound();
return 0;
return 0;
}</lang>
}</syntaxhighlight>


===Windows C===
===Windows C===
Line 146: Line 146:
Beep() can only play integer frequencies and thus the tones are noticeably lower than the ones played by sound() above.
Beep() can only play integer frequencies and thus the tones are noticeably lower than the ones played by sound() above.


<syntaxhighlight lang="c">
<lang c>
#include<windows.h>
#include<windows.h>
#include<stdio.h>
#include<stdio.h>
Line 170: Line 170:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
Uses Windows MIDI device
Uses Windows MIDI device
<lang cpp>
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>
#include <windows.h>
#include <windows.h>
Line 248: Line 248:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
{{libheader|Overtone}}
{{libheader|Overtone}}
<lang clojure>(use 'overtone.live)
<syntaxhighlight lang="clojure">(use 'overtone.live)


; Define your desired instrument
; Define your desired instrument
Line 265: Line 265:
(Thread/sleep ms))
(Thread/sleep ms))


(doseq [note (scale :c4 :major)] (play note 500))</lang>
(doseq [note (scale :c4 :major)] (play note 500))</syntaxhighlight>


=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==
Line 272: Line 272:


'''Commodore 64'''
'''Commodore 64'''
<lang commodorebasic>10 rem musical scale
<syntaxhighlight lang="commodorebasic">10 rem musical scale
15 rem rosetta code
15 rem rosetta code
20 print chr$(147)
20 print chr$(147)
Line 289: Line 289:
90 for d=1 to 25:next
90 for d=1 to 25:next
95 next i
95 next i
500 data 261.63,293.66,329.63,349.23,392,440,493.88,523.25</lang>
500 data 261.63,293.66,329.63,349.23,392,440,493.88,523.25</syntaxhighlight>


'''Commodore 128'''
'''Commodore 128'''
<lang commodorebasic>10 print chr$(147)
<syntaxhighlight lang="commodorebasic">10 print chr$(147)
20 play "o4 cdefgab o5 c"</lang>
20 play "o4 cdefgab o5 c"</syntaxhighlight>


Line 299: Line 299:
=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| Winapi.Windows}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Musical_scale;
program Musical_scale;


Line 315: Line 315:
Beep(Round(note), 500);
Beep(Round(note), 500);
readln;
readln;
end.</lang>
end.</syntaxhighlight>


=={{header|EasyLang}}==
=={{header|EasyLang}}==


<lang>n[] = [ 262 294 330 349 392 440 494 523 ]
<syntaxhighlight lang="text">n[] = [ 262 294 330 349 392 440 494 523 ]
for i range len n[]
for i range len n[]
sound [ n[i] 0.5 ]
sound [ n[i] 0.5 ]
sleep 0.6
sleep 0.6
.</lang>
.</syntaxhighlight>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
Line 329: Line 329:


Does not work with the Windows version of Emacs.
Does not work with the Windows version of Emacs.
<lang lisp>(defun play-scale (freq-list dur)
<syntaxhighlight lang="lisp">(defun play-scale (freq-list dur)
"Play a list of frequencies."
"Play a list of frequencies."
(setq header (unibyte-string ; AU header:
(setq header (unibyte-string ; AU header:
Line 350: Line 350:
(play-sound `(sound :data ,s)))
(play-sound `(sound :data ,s)))


(play-scale '(261.63 293.66 329.63 349.23 392.00 440.00 493.88 523.25) .5)</lang>
(play-scale '(261.63 293.66 329.63 349.23 392.00 440.00 493.88 523.25) .5)</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
As a low level stack language Forth programming methodology prefers short simple definitions that extend the language in the direction that allows you to solve the problem. The creation of application specific language is common in Forth.
As a low level stack language Forth programming methodology prefers short simple definitions that extend the language in the direction that allows you to solve the problem. The creation of application specific language is common in Forth.
This demonstration code uses the PC speaker to generate musical tones. A simple device driver is created for hardware control via PORT I/O. A set of primitive operations are created to control the on:off times of the sounds. Then a small MUSIC language is created to create notes of different types. Finally 2 scales are created using "crotcheted notes" as they are called. We chose 1/8 notes. For fun we added the ability to change the expression of the notes using Italian musical terms.
This demonstration code uses the PC speaker to generate musical tones. A simple device driver is created for hardware control via PORT I/O. A set of primitive operations are created to control the on:off times of the sounds. Then a small MUSIC language is created to create notes of different types. Finally 2 scales are created using "crotcheted notes" as they are called. We chose 1/8 notes. For fun we added the ability to change the expression of the notes using Italian musical terms.
<lang>HEX
<syntaxhighlight lang="text">HEX
\ PC speaker hardware control (requires giveio or DOSBOX for windows operation)
\ PC speaker hardware control (requires giveio or DOSBOX for windows operation)
042 constant fctrl 061 constant sctrl
042 constant fctrl 061 constant sctrl
Line 444: Line 444:
: Cmajor 1/8 C3 D3 E3 F3 G3 A3 B3 C4 ;
: Cmajor 1/8 C3 D3 E3 F3 G3 A3 B3 C4 ;
: Chromatic 1/8 C3 C#3 D3 D#3 E3 F3 F#3 G3 G#3 A3 A#3 B3 C4 ;
: Chromatic 1/8 C3 C#3 D3 D#3 E3 F3 F#3 G3 G#3 A3 A#3 B3 C4 ;
</syntaxhighlight>
</lang>
Interactive test at the Console
Interactive test at the Console
<pre>music ok
<pre>music ok
Line 454: Line 454:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>REM FreeBASIC no tiene la capacidad de emitir sonido de forma nativa.
<syntaxhighlight lang="freebasic">REM FreeBASIC no tiene la capacidad de emitir sonido de forma nativa.
REM La función Sound no es mía, incluyo los créditos correspondientes.
REM La función Sound no es mía, incluyo los créditos correspondientes.
' Sound Function v0.3 For DOS/Linux/Win by yetifoot
' Sound Function v0.3 For DOS/Linux/Win by yetifoot
Line 526: Line 526:
Sound(494, 250) 'B4
Sound(494, 250) 'B4
Sound(523, 250) 'C5
Sound(523, 250) 'C5
Sleep</lang>
Sleep</syntaxhighlight>
=={{header|FreePascal}}==
=={{header|FreePascal}}==
<lang pascal>{$mode objfpc}
<syntaxhighlight lang="pascal">{$mode objfpc}
uses windows,math;{ windows only }
uses windows,math;{ windows only }
var
var
Line 536: Line 536:
for i:= 0 to 11 do
for i:= 0 to 11 do
beep(Round(440.0*interval**i),500);
beep(Round(440.0*interval**i),500);
end.</lang>
end.</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Line 542: Line 542:
<br>
<br>
As Go doesn't have any audio support in its standard library, we instead build a .wav file which can then be played using a utility such as SoX.
As Go doesn't have any audio support in its standard library, we instead build a .wav file which can then be played using a utility such as SoX.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 608: Line 608:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==


<lang J>require'media/wav'
<syntaxhighlight lang="j">require'media/wav'
0.25 wavnote 0 2 4 5 7 9 11 12</lang>
0.25 wavnote 0 2 4 5 7 9 11 12</syntaxhighlight>


This assumes a version such as J6 which supports media/wav.
This assumes a version such as J6 which supports media/wav.
Line 623: Line 623:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
Using the Web Audio API
Using the Web Audio API
<lang javascript><!doctype html>
<syntaxhighlight lang="javascript"><!doctype html>
<html>
<html>
<head>
<head>
Line 660: Line 660:
</script>
</script>
</body>
</body>
</html></lang>
</html></syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using PortAudio
<syntaxhighlight lang="julia">using PortAudio


function paudio()
function paudio()
Line 688: Line 688:
sleep(0.4)
sleep(0.4)
end
end
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
Line 694: Line 694:


When building win32.klib from windows.h, one needs to make sure NOT to filter out utilapiset.h because this is where the Beep function now resides, not in winbase.h as stated in the MSDN documentation.
When building win32.klib from windows.h, one needs to make sure NOT to filter out utilapiset.h because this is where the Beep function now resides, not in winbase.h as stated in the MSDN documentation.
<lang scala>// Kotlin Native v0.3
<syntaxhighlight lang="scala">// Kotlin Native v0.3


import kotlinx.cinterop.*
import kotlinx.cinterop.*
Line 703: Line 703:
val dur = 500
val dur = 500
repeat(5) { for (freq in freqs) Beep(freq, dur) }
repeat(5) { for (freq in freqs) Beep(freq, dur) }
}</lang>
}</syntaxhighlight>


=={{header|Lilypond}}==
=={{header|Lilypond}}==
The lilypond tool produces musical score sheets and midi files - if asked for - but does not output notes to the sound device directly.
The lilypond tool produces musical score sheets and midi files - if asked for - but does not output notes to the sound device directly.
<lang lilypond>% Start at middle C
<syntaxhighlight lang="lilypond">% Start at middle C
\relative c' {
\relative c' {
c d e f
c d e f
g a b c
g a b c
}</lang>
}</syntaxhighlight>


=={{header|Locomotive Basic}}==
=={{header|Locomotive Basic}}==


<lang locobasic>10 mode 1
<syntaxhighlight lang="locobasic">10 mode 1
20 print "Note","Freq. (Hz)","Period"
20 print "Note","Freq. (Hz)","Period"
30 ' program loop:
30 ' program loop:
Line 730: Line 730:
140 sound 1,p,100
140 sound 1,p,100
150 return
150 return
160 data 1,3,5,6,8,10,12,13,-1</lang>
160 data 1,3,5,6,8,10,12,13,-1</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
Line 736: Line 736:
===Lua Portable===
===Lua Portable===
The most portable native solution that could actually ''play'' the scale (''with some external help from a media player'') would be to write a MIDI file..
The most portable native solution that could actually ''play'' the scale (''with some external help from a media player'') would be to write a MIDI file..
<lang lua>c = string.char
<syntaxhighlight lang="lua">c = string.char
midi = "MThd" .. c(0,0,0,6,0,0,0,1,0,96) -- header
midi = "MThd" .. c(0,0,0,6,0,0,0,1,0,96) -- header
midi = midi .. "MTrk" .. c(0,0,0,8*8+4) -- track
midi = midi .. "MTrk" .. c(0,0,0,8*8+4) -- track
Line 749: Line 749:


-- (optional: hex dump to screen)
-- (optional: hex dump to screen)
midi:gsub(".", function(c) io.write(string.format("%02X ", string.byte(c))) end)</lang>
midi:gsub(".", function(c) io.write(string.format("%02X ", string.byte(c))) end)</syntaxhighlight>
{{out}}
{{out}}
<pre>4D 54 68 64 00 00 00 06 00 00 00 01 00 60 4D 54 72 6B 00 00 00 44 00 90 3C 40 60 80 3C 00 00 90 3E 40 60 80 3E 00 00 90 40 40 60 80 40 00 00 90 41 40 60 80 41 00 00 90 43 40 60 80 43 00 00 90 45 40 60 80 45 00 00 90 47 40 60 80 47 00 00 90 48 40 60 80 48 00 00 FF 2F 00</pre>
<pre>4D 54 68 64 00 00 00 06 00 00 00 01 00 60 4D 54 72 6B 00 00 00 44 00 90 3C 40 60 80 3C 00 00 90 3E 40 60 80 3E 00 00 90 40 40 60 80 40 00 00 90 41 40 60 80 41 00 00 90 43 40 60 80 43 00 00 90 45 40 60 80 45 00 00 90 47 40 60 80 47 00 00 90 48 40 60 80 48 00 00 FF 2F 00</pre>
Line 755: Line 755:
===Lua ASCII===
===Lua ASCII===
The task allows for score output, which could also be done natively..
The task allows for score output, which could also be done natively..
<lang lua>staff = {
<syntaxhighlight lang="lua">staff = {
lines = { "", "", "", "", "", "", "", "", "", "", "" },
lines = { "", "", "", "", "", "", "", "", "", "", "" },
nnotes = 0,
nnotes = 0,
Line 781: Line 781:
end
end
staff:measure()
staff:measure()
staff:dump()</lang>
staff:dump()</syntaxhighlight>
{{out}}
{{out}}
<pre>|----------------|----------------|
<pre>|----------------|----------------|
Line 797: Line 797:
===Lua Windows===
===Lua Windows===
Non-portable, O/S-specific, requires <code>alien</code> library..
Non-portable, O/S-specific, requires <code>alien</code> library..
<lang lua>beep = require"alien".kernel32.Beep
<syntaxhighlight lang="lua">beep = require"alien".kernel32.Beep
beep:types{ret='long', abi='stdcall', 'long', 'long'}
beep:types{ret='long', abi='stdcall', 'long', 'long'}
for _,step in ipairs{0,2,4,5,7,9,11,12} do
for _,step in ipairs{0,2,4,5,7,9,11,12} do
beep(math.floor(261.63 * 2^(step/12) + 0.5), 1000)
beep(math.floor(261.63 * 2^(step/12) + 0.5), 1000)
end</lang>
end</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
Line 809: Line 809:
TUNE use kernel [https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx Beep] which is synchronous and not leaving M2000 threads to process, until ends.
TUNE use kernel [https://msdn.microsoft.com/en-us/library/windows/desktop/ms679277(v=vs.85).aspx Beep] which is synchronous and not leaving M2000 threads to process, until ends.


<lang>
<syntaxhighlight lang="text">
Module checkit {
Module checkit {
\\ using internal speaker
\\ using internal speaker
Line 824: Line 824:
}
}
checkit
checkit
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang mathematica>EmitSound@Sound[SoundNote /@ {0, 2, 4, 5, 7, 9, 11, 12}]</lang>
<syntaxhighlight lang="mathematica">EmitSound@Sound[SoundNote /@ {0, 2, 4, 5, 7, 9, 11, 12}]</syntaxhighlight>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang nanoquery>import tonegen
<syntaxhighlight lang="nanoquery">import tonegen


note_freqs = {261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25}
note_freqs = {261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25}
Line 837: Line 837:
for freq in note_freqs
for freq in note_freqs
tg.beep(freq)
tg.beep(freq)
end</lang>
end</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import endians, math
<syntaxhighlight lang="nim">import endians, math


const
const
Line 892: Line 892:
file.write chr(y.byte) # Signed int to byte then to char as it’s easier this way.
file.write chr(y.byte) # Signed int to byte then to char as it’s easier this way.


file.close()</lang>
file.close()</syntaxhighlight>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang ooRexx>/* REXX ---------------------------------------------------------------
<syntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 24.02.2013 Walter Pachl derived from original REXX version
* 24.02.2013 Walter Pachl derived from original REXX version
* Changes: sound(f,sec) --> beep(trunc(f),millisec)
* Changes: sound(f,sec) --> beep(trunc(f),millisec)
Line 928: Line 928:
If f.note\==0 Then
If f.note\==0 Then
Call beep trunc(f.note),dur /* sound the "note". */
Call beep trunc(f.note),dur /* sound the "note". */
Return</lang>
Return</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use MIDI::Simple;
<syntaxhighlight lang="perl">use MIDI::Simple;


# setup, 1 quarter note is 0.5 seconds (500,000 microseconds)
# setup, 1 quarter note is 0.5 seconds (500,000 microseconds)
Line 939: Line 939:
n 60; n 62; n 64; n 65; n 67; n 69; n 71; n 72;
n 60; n 62; n 64; n 65; n 67; n 69; n 71; n 72;


write_score 'scale.mid';</lang>
write_score 'scale.mid';</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 945: Line 945:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Musical_scale.htm here].
You can run this online [http://phix.x10.mx/p2js/Musical_scale.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Musical_scale.exw
-- demo\rosetta\Musical_scale.exw
Line 972: Line 972:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PowerShell}}==
=={{header|PowerShell}}==
List of frequencies directly taken from the Python example.
List of frequencies directly taken from the Python example.
<lang powershell>$frequencies = 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25
<syntaxhighlight lang="powershell">$frequencies = 261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25
foreach($tone in $frequencies){
foreach($tone in $frequencies){
[Console]::beep($tone, 500)
[Console]::beep($tone, 500)
}</lang>
}</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
Requires the Processing Sound library.
Requires the Processing Sound library.


<lang java>
<syntaxhighlight lang="java">
//Aamrun, 2nd July 2022
//Aamrun, 2nd July 2022


Line 1,003: Line 1,003:
delay(500);
delay(500);
}
}
</syntaxhighlight>
</lang>


=={{header|Pure Data}}==
=={{header|Pure Data}}==
Line 1,070: Line 1,070:
=={{header|Python}}==
=={{header|Python}}==
(Windows)
(Windows)
<lang python>>>> import winsound
<syntaxhighlight lang="python">>>> import winsound
>>> for note in [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]:
>>> for note in [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25]:
winsound.Beep(int(note+.5), 500)
winsound.Beep(int(note+.5), 500)
>>> </lang>
>>> </syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
install.packages("audio")
install.packages("audio")
library(audio)
library(audio)
Line 1,084: Line 1,084:
Sys.sleep(.7)
Sys.sleep(.7)
}
}
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
With a quick and dirty WinMM interface.
With a quick and dirty WinMM interface.
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require ffi/unsafe ffi/unsafe/define)
(require ffi/unsafe ffi/unsafe/define)
Line 1,100: Line 1,100:
(for ([i '(60 62 64 65 67 69 71 72)]) (midi #x90 i 127) (sleep 0.5))
(for ([i '(60 62 64 65 67 69 71 72)]) (midi #x90 i 127) (sleep 0.5))
(sleep 2)
(sleep 2)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>for 0,2,4,5,7,9,11,12 {
<syntaxhighlight lang="raku" line>for 0,2,4,5,7,9,11,12 {
shell "play -n -c1 synth 0.2 sin %{$_ - 9}"
shell "play -n -c1 synth 0.2 sin %{$_ - 9}"
}</lang>
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Line 1,112: Line 1,112:
{{works with|Personal REXX}}
{{works with|Personal REXX}}
{{works with|Regina}}
{{works with|Regina}}
<lang rexx>/*REXX program sounds eight notes of the C major natural diatonic music scale.*/
<syntaxhighlight lang="rexx">/*REXX program sounds eight notes of the C major natural diatonic music scale.*/
parse arg ! /*obtain optional arguments from the CL*/
parse arg ! /*obtain optional arguments from the CL*/
/* [↓] invoke boilerplate REXX code. */
/* [↓] invoke boilerplate REXX code. */
Line 1,149: Line 1,149:
!rex: parse upper version !ver !vernum !verdate .; !brexx= 'BY'==!vernum; !kexx= "KEXX"==!ver; !pcrexx= 'REXX/PERSONAL'==!ver | "REXX/PC"==!ver; !r4= 'REXX-R4'==!ver; !regina= "REXX-REGINA"==left(!ver, 11); !roo= 'REXX-ROO'==!ver; call !env; return
!rex: parse upper version !ver !vernum !verdate .; !brexx= 'BY'==!vernum; !kexx= "KEXX"==!ver; !pcrexx= 'REXX/PERSONAL'==!ver | "REXX/PC"==!ver; !r4= 'REXX-R4'==!ver; !regina= "REXX-REGINA"==left(!ver, 11); !roo= 'REXX-ROO'==!ver; call !env; return
!sys: !cms= !sys=='CMS'; !os2= !sys=="OS2"; !tso= !sys=='TSO' | !sys=="MVS"; !vse= !sys=='VSE'; !dos= pos("DOS", !sys)\==0 | pos('WIN', !sys)\==0 | !sys=="CMD"; !crx= left(!sys, 6)=='DOSCRX'; call !rex; return
!sys: !cms= !sys=='CMS'; !os2= !sys=="OS2"; !tso= !sys=='TSO' | !sys=="MVS"; !vse= !sys=='VSE'; !dos= pos("DOS", !sys)\==0 | pos('WIN', !sys)\==0 | !sys=="CMD"; !crx= left(!sys, 6)=='DOSCRX'; call !rex; return
!var: call !fid; if !kexx then return space( dosenv( arg(1) ) ); return space( value( arg(1), , !env) )</lang>
!var: call !fid; if !kexx then return space( dosenv( arg(1) ) ); return space( value( arg(1), , !env) )</syntaxhighlight>


Programming note: &nbsp;
Programming note: &nbsp;
Line 1,171: Line 1,171:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Musical scale
# Project : Musical scale


Line 1,180: Line 1,180:
beep(freqs[f][1],300)
beep(freqs[f][1],300)
next
next
</syntaxhighlight>
</lang>
Output video:
Output video:


Line 1,188: Line 1,188:
===Windows===
===Windows===
{{libheader|net.java.dev.sna.SNA}}
{{libheader|net.java.dev.sna.SNA}}
<lang Scala>import net.java.dev.sna.SNA
<syntaxhighlight lang="scala">import net.java.dev.sna.SNA


object PlayMusicScale extends App with SNA {
object PlayMusicScale extends App with SNA {
Line 1,199: Line 1,199:
foreach(f => Beep((261.63 * math.pow(2, f / 12.0)).toInt, if (f == 12) 1000 else 500))
foreach(f => Beep((261.63 * math.pow(2, f / 12.0)).toInt, if (f == 12) 1000 else 500))
println("That's all")
println("That's all")
}</lang>
}</syntaxhighlight>


=={{header|Sparkling}}==
=={{header|Sparkling}}==
The following Sparkling program generates a WAVE audio file named "notes.wav"
The following Sparkling program generates a WAVE audio file named "notes.wav"
that can be played in order to achieve the required effect:
that can be played in order to achieve the required effect:
<lang Sparkling>var sampleRate = 44100.0;
<syntaxhighlight lang="sparkling">var sampleRate = 44100.0;
var duration = 8.0;
var duration = 8.0;
var dataLength = round(sampleRate * duration);
var dataLength = round(sampleRate * duration);
Line 1,253: Line 1,253:
}
}


fclose(f);</lang>
fclose(f);</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Snack}}
{{libheader|Snack}}
<lang tcl>package require sound
<syntaxhighlight lang="tcl">package require sound


# Encapsulate the tone generation
# Encapsulate the tone generation
Line 1,279: Line 1,279:
foreach i {0 2 4 5 7 9 11 12 11 9 7 5 4 2 0} {
foreach i {0 2 4 5 7 9 11 12 11 9 7 5 4 2 0} {
play [expr {$tonicFrequency*2**($i/12.0)}] [expr {$i%12?250:500}]
play [expr {$tonicFrequency*2**($i/12.0)}] [expr {$i%12?250:500}]
}</lang>
}</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
{{trans|Python}}
{{trans|Python}}
<lang ursa>decl double<> notes
<syntaxhighlight lang="ursa">decl double<> notes
append 261.63 293.66 329.63 349.23 392.00 440.00 493.88 523.25 notes
append 261.63 293.66 329.63 349.23 392.00 440.00 493.88 523.25 notes


for (decl int i) (< i (size notes)) (inc i)
for (decl int i) (< i (size notes)) (inc i)
ursa.util.sound.beep notes<i> 0.5
ursa.util.sound.beep notes<i> 0.5
end for</lang>
end for</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit


Declare Function Beep Lib "kernel32" (ByVal Freq As Long, ByVal Dur As Long) As Long
Declare Function Beep Lib "kernel32" (ByVal Freq As Long, ByVal Dur As Long) As Long
Line 1,301: Line 1,301:
Beep Fqs(i), 500
Beep Fqs(i), 500
Next
Next
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|Vlang}}==
=={{header|Vlang}}==
Line 1,307: Line 1,307:
<br>
<br>
As Vlang doesn't have any audio support in its standard library, we instead build a .wav file which can then be played using a utility such as SoX.
As Vlang doesn't have any audio support in its standard library, we instead build a .wav file which can then be played using a utility such as SoX.
<lang vlang>import strings
<syntaxhighlight lang="vlang">import strings
import os
import os
import encoding.binary
import encoding.binary
Line 1,367: Line 1,367:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
Line 1,373: Line 1,373:
{{libheader|Wren-sound}}
{{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.
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.
<lang ecmascript>import "/sound" for Wav
<syntaxhighlight lang="ecmascript">import "/sound" for Wav


var sampleRate = 44100
var sampleRate = 44100
Line 1,387: Line 1,387:
}
}
}
}
Wav.create("musical_scale.wav", data, sampleRate)</lang>
Wav.create("musical_scale.wav", data, sampleRate)</syntaxhighlight>
<br>
<br>
It's also possible to play .wav files which (preferably) have a sample rate of 44.1 kHz using DOME:
It's also possible to play .wav files which (preferably) have a sample rate of 44.1 kHz using DOME:
{{libheader|DOME}}
{{libheader|DOME}}
<lang ecmascript>import "audio" for AudioEngine
<syntaxhighlight lang="ecmascript">import "audio" for AudioEngine


class Main {
class Main {
Line 1,406: Line 1,406:
}
}


var Game = Main.new()</lang>
var Game = Main.new()</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>\Square waves on the beeper speaker:
<syntaxhighlight lang="xpl0">\Square waves on the beeper speaker:
code Sound=39;
code Sound=39;
real Period; int I;
real Period; int I;
Line 1,429: Line 1,429:
Note:= Note + (if I&3 then 2 else 1);
Note:= Note + (if I&3 then 2 else 1);
];
];
]</lang>
]</syntaxhighlight>


=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Phix}}
{{trans|Phix}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Musical_scale
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Musical_scale
// by Galileo, 03/2022
// by Galileo, 03/2022


Line 1,486: Line 1,486:
else // Linux
else // Linux
system("aplay notesyab.wav")
system("aplay notesyab.wav")
endif</lang>
endif</syntaxhighlight>


=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 REM Musical scale
<syntaxhighlight lang="zxbasic">10 REM Musical scale
20 LET n=0: REM Start at middle C
20 LET n=0: REM Start at middle C
30 LET d=0.2: REM Make each note 0.2 seconds in duration
30 LET d=0.2: REM Make each note 0.2 seconds in duration
Line 1,498: Line 1,498:
80 NEXT l
80 NEXT l
90 STOP
90 STOP
9000 DATA 2,2,1,2,2,2,1,2:REM WWHWWWH</lang>
9000 DATA 2,2,1,2,2,2,1,2:REM WWHWWWH</syntaxhighlight>