Musical scale: Difference between revisions
Content deleted Content added
→{{header|Phix}}: replaced with gui version and online link |
|||
Line 942: | Line 942: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/pGUI}} |
|||
=== version 1 === |
|||
{{libheader|Phix/online}} |
|||
{{trans|Scala}} |
|||
You can run this online [http://phix.x10.mx/p2js/Musical_scale.htm here]. |
|||
{{trans|Raku}} |
|||
<lang Phix> |
<!--<lang Phix>(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
|||
-- demo\rosetta\Musical_scale.exw |
|||
procedure beep(integer fi) |
|||
--</span> |
|||
if platform()=WINDOWS then |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
integer frequency = floor(261.63 * power(2, fi/12)), |
|||
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
duration = iff(fi == 12 ? 1000 : 500) |
|||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #7060A8;">beep</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
if xBeep=0 then |
|||
atom kernel32 = open_dll("kernel32.dll") |
|||
xBeep = define_c_proc(kernel32, "Beep", {C_INT,C_INT}) |
|||
end if |
|||
c_proc(xBeep,{frequency,duration}) |
|||
elsif platform()=LINUX then |
|||
string play = sprintf("play -n -c1 synth 0.2 sin %%%d",fi-9) |
|||
system(play) |
|||
end if |
|||
end procedure |
|||
printf(1,"Please don't shoot the piano player, he's doing the best that he can!\n") |
|||
constant f = {0, 2, 4, 5, 7, 9, 11, 12} |
|||
for i=1 to length(f) do |
|||
beep(f[i]) |
|||
end for |
|||
printf(1,"That's all\n")</lang> |
|||
=== version 2 === |
|||
{{trans|Sparkling}} |
|||
{{trans|Go}} |
|||
<lang Phix>constant sample_rate = 44100, |
|||
duration = 8, |
|||
dataLength = sample_rate * duration, |
|||
hdrSize = 44, |
|||
fileLen = dataLength + hdrSize - 8, |
|||
freqs = { 261.6, 293.6, 329.6, 349.2, 392.0, 440.0, 493.9, 523.3 }, |
|||
wavhdr = "RIFF"& |
|||
int_to_bytes(fileLen,4)& |
|||
"WAVE"& |
|||
"fmt "& |
|||
int_to_bytes(16,4)& -- length of format data (= 16) |
|||
int_to_bytes(1,2)& -- type of format (= 1 (PCM)) |
|||
int_to_bytes(1,2)& -- number of channels (= 1) |
|||
int_to_bytes(sample_rate,4)& -- sample rate |
|||
int_to_bytes(sample_rate,4)& -- sample rate * bps(8) * channels(1) / 8 (= sample rate) |
|||
int_to_bytes(1,2)& -- bps(8) * channels(1) / 8 (= 1) |
|||
int_to_bytes(8,2)& -- bits per sample (bps) (= 8) |
|||
"data"& |
|||
int_to_bytes(dataLength,4) -- size of data section |
|||
if length(wavhdr)!=hdrSize then ?9/0 end if -- sanity check |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">freq</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">261.63</span><span style="color: #0000FF;">,</span><span style="color: #000000;">293.66</span><span style="color: #0000FF;">,</span><span style="color: #000000;">329.63</span><span style="color: #0000FF;">,</span><span style="color: #000000;">349.23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">392</span><span style="color: #0000FF;">,</span><span style="color: #000000;">440</span><span style="color: #0000FF;">,</span><span style="color: #000000;">493.88</span><span style="color: #0000FF;">,</span><span style="color: #000000;">523.25</span><span style="color: #0000FF;">},</span> |
|||
integer fn = open("notes.wav", "wb") |
|||
<span style="color: #000000;">durations</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">freq</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">1000</span> |
|||
puts(fn, wavhdr) |
|||
for j=1 to duration do |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">button_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*playbtn*/</span><span style="color: #0000FF;">)</span> |
|||
atom omega = 2 * PI * freqs[j] |
|||
<span style="color: #7060A8;">beep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">freq</span><span style="color: #0000FF;">,</span><span style="color: #000000;">durations</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span> |
|||
for i=0 to dataLength/duration-1 do |
|||
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> |
|||
atom y = 32 * sin(omega * i / sample_rate) |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
integer byte = and_bits(y,#FF) |
|||
puts(fn,byte) |
|||
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span> |
|||
end for |
|||
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">label</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Please don't shoot the piano player, he's doing the best that he can!"</span><span style="color: #0000FF;">),</span> |
|||
end for |
|||
<span style="color: #000000;">playbtn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Play"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"button_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"PADDING=30x0"</span><span style="color: #0000FF;">),</span> |
|||
close(fn) |
|||
<span style="color: #000000;">hbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupFill</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">playbtn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">IupFill</span><span style="color: #0000FF;">()},</span><span style="color: #008000;">"MARGIN=0x20"</span><span style="color: #0000FF;">),</span> |
|||
<span style="color: #000000;">vbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">label</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hbox</span><span style="color: #0000FF;">},</span> <span style="color: #008000;">"MARGIN=10x5, GAP=5"</span><span style="color: #0000FF;">),</span> |
|||
if platform()=WINDOWS then |
|||
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Musical Scale"`</span><span style="color: #0000FF;">)</span> |
|||
system("notes.wav") |
|||
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span> |
|||
elsif platform()=LINUX then |
|||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> |
|||
system("aplay notes.wav") |
|||
<span style="color: #7060A8;">IupMainLoop</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> |
|||
⚫ | |||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |