Musical scale: Difference between revisions

→‎{{header|Lua}}: additional Lua solutions
(→‎{{header|Lua}}: added Lua solution)
(→‎{{header|Lua}}: additional Lua solutions)
Line 640:
 
=={{header|Lua}}==
Lua has no native sound support. The most portable solution is to write a MIDI file..
===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..
<lang lua>c = string.char
midi = "MThd" .. c(0,0,0,6,0,0,0,1,0,96) -- header
Line 657 ⟶ 659:
{{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>
 
===Lua ASCII===
The task allows for score output, which could also be done natively..
<lang lua>staff = {
lines = { "", "", "", "", "", "", "", "", "", "", "" },
nnotes = 0,
measure = function(self)
for i, line in ipairs(self.lines) do
self.lines[i] = line .. (i<#self.lines-1 and "|" or " ")
end
end,
play = function(self, note)
if self.nnotes%4==0 then self:measure() end
local n = #self.lines-note
for i, line in ipairs(self.lines) do
local linechar = (i%2==0) and " " or "-"
local fillchar = (i<#self.lines) and linechar or " "
self.lines[i] = line .. (i==n and linechar.."@"..linechar..fillchar or (i==n-1 or i==n-2) and string.rep(fillchar,2).."|"..fillchar or string.rep(fillchar,4))
end
self.nnotes = self.nnotes + 1
end,
dump = function(self)
for i, line in ipairs(self.lines) do print(line) end
end
}
for note = 0,7 do
staff:play(note)
end
staff:measure()
staff:dump()</lang>
{{out}}
<pre>|----------------|----------------|
| | | |
|----------------|----------|---|-|
| | | | @ |
|----------------|--|---|--@------|
| | | | @ |
|----------|---|-|-@--------------|
| | | @ | |
|--|---|--@------|----------------|
| @
-@-</pre>
 
===Lua Windows===
Non-portable, O/S-specific, requires <code>alien</code> library..
<lang lua>beep = require"alien".kernel32.Beep
beep:types{ret='long', abi='stdcall', 'long', 'long'}
for _,step in ipairs{0,2,4,5,7,9,11,12} do
beep(math.floor(261.63 * (2^(1/12))^step + 0.5), 1000)
end</lang>
 
=={{header|M2000 Interpreter}}==
Anonymous user