Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.buildconfig: bump kernel version base/hooks: add logging when hooks get called stdlib/package: add /usr/lib to the package path base/thread: don't give userspace wrapped coroutines base/tty: i have no idea base/util: messed with pipes a bit extra/sound: am currently rewriting it to be Better:tm: extra/sound/*: see above extra/sound_old*: old sound api
- Loading branch information
1 parent
1be07ad
commit 726a19b
Showing
15 changed files
with
232 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
KMODS=extra/net/base,extra/getgpu,extra/sound | ||
KRELEASE=1.30 | ||
KRELEASE=1.41 | ||
KCUSTOMNAME=default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,77 @@ | ||
-- sound subsystem for Cynosure -- | ||
-- sound api v2: emulate the sound card for everything -- | ||
|
||
k.log(k.loglevels.info, "extra/sound") | ||
|
||
do | ||
k.log(k.loglevels.debug, "registering sound-related component detection") | ||
local api = {} | ||
local tiers = { | ||
internal = 0, | ||
beep = 1, | ||
noise = 2, | ||
sound = 3, | ||
[0] = "internal", | ||
"beep", | ||
"noise", | ||
"sound" | ||
} | ||
|
||
local api = { | ||
MAX_CHANNELS = 1, | ||
CARD_TYPE = "computer.beep", -- also beep, noise, sound | ||
voice = { | ||
SQUARE = "square", | ||
SINE = "sine", | ||
TRIANGLE = "triangle", | ||
SAWTOOTH = "sawtooth", | ||
NOISE = "noise", | ||
} | ||
local available = { | ||
internal = 1, | ||
beep = 0, | ||
noise = 0, | ||
sound = 0, | ||
} | ||
|
||
local component_cache = {} | ||
|
||
-- all handlers must contain: | ||
-- a table of supported voices | ||
-- the maximum number of channels | ||
-- a play(tab) function to play simultaneous notes | ||
-- through { frequency, ms[, volume][, voice] } pairs contained in | ||
-- `tab` | ||
-- a mode(chan, voice) function to set the voice | ||
-- for each channel | ||
local card_handlers = { | ||
["computer.beep"] = { | ||
voices = { [api.voice.SINE] = true }, | ||
channels = 1, | ||
play = function(tab) | ||
local freq, dur = table.unpack(select(2, next(tab))) | ||
dur = dur / 1000 -- ms -> s | ||
computer.beep(freq, dur) | ||
end | ||
local proxies = { | ||
internal = { | ||
[computer.address()] = { | ||
beep = function(tab) | ||
return computer.beep(tab[1][1], tab[1][2]) | ||
end | ||
} | ||
}, | ||
--#include "extra/sound/beep.lua" | ||
--#include "extra/sound/noise.lua" | ||
--#include "extra/sound/sound.lua" | ||
beep = {}, | ||
noise = {}, | ||
sound = {} | ||
} | ||
|
||
local current = "internal" | ||
local caddr = computer.address() | ||
|
||
function api.play(notes) | ||
checkArg(1, notes, "table") | ||
return card_handlers[api.CARD_TYPE].play(notes) | ||
end | ||
|
||
local chandler = function(s, add, typ) | ||
s = s == "component_added" | ||
if s then | ||
if typ == "beep" or typ == "noise" or typ == "sound" then | ||
local card = component_cache[typ] or add | ||
if type(card) == "string" then | ||
component_cache[typ] = component.proxy(card) | ||
end | ||
end | ||
elseif component_cache[typ] and component_cache[typ].address == add then | ||
component_cache[typ] = component.list(typ, true)() | ||
if component_cache[typ] then | ||
component_cache[typ] = component.proxy(component_cache[typ]) | ||
local function component_changed(sig, addr, ctype) | ||
if sig == "component_added" then | ||
if tiers[ctype] and tiers[ctype] > tiers[current] then | ||
current = ctype | ||
available[ctype] = math.max(1, available[ctype] + 1) | ||
proxies[ctype][addr] = component.proxy(addr) | ||
end | ||
end | ||
if component_cache.sound or component_cache.noise or | ||
component_cache.beep then | ||
api.MAX_CHANNELS = 8 | ||
else | ||
api.MAX_CHANNELS = 1 | ||
end | ||
if component_cache.sound then | ||
api.CARD_TYPE = "sound" | ||
elseif component_cache.noise then | ||
api.CARD_TYPE = "noise" | ||
elseif component_cache.beep then | ||
api.CARD_TYPE = "beep" | ||
else | ||
api.CARD_TYPE = "computer.beep" | ||
if tiers[ctype] then | ||
available[ctype] = math.min(0, available[ctype] - 1) | ||
proxies[ctype][addr] = nil | ||
if caddr == addr then | ||
for i=#tiers, 0, -1 do | ||
if available[tiers[i]] > 0 then | ||
current = tiers[i] | ||
caddr = next(proxies[current]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
local id = k.event.register("component_added", chandler) | ||
k.event.register("component_removed", chandler) | ||
k.event.register("component_added", component_changed) | ||
k.event.register("component_removed", component_changed) | ||
|
||
local handlers = { | ||
internal = {play = select(2, next(proxies.internal)).beep}, | ||
--#include "extra/sound/beep.lua" | ||
--#include "extra/sound/noise.lua" | ||
--#include "extra/sound/sound.lua" | ||
} | ||
|
||
k.hooks.add("sandbox", function() | ||
k.userspace.package.loaded.sound = package.protect(api) | ||
end) | ||
function api.play(notes) | ||
return handlers[current].play(notes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +0,0 @@ | ||
beep = { | ||
voices = { [api.voice.SINE] = true }, | ||
channels = 8, | ||
play = function(tab) | ||
local bcard = component_cache.beep | ||
if bcard then | ||
local o = tab | ||
tab = {} | ||
for i=1, #o, 1 do | ||
tab[o[i][1]] = o[i][2] / 1000 | ||
end | ||
bcard.play(tab) | ||
else | ||
return nil, "no beep card installed" | ||
end | ||
end, | ||
mode = function() end | ||
}, | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +0,0 @@ | ||
noise = { | ||
voices = { [api.voice.SQUARE] = true, [api.voice.SINE] = true, | ||
[api.voice.TRIANGLE] = true, [api.voice.SAWTOOTH] = true }, | ||
channels = 8, | ||
play = function(tab) | ||
local card = component_cache.noise | ||
if card then | ||
for i=1, #tab, 1 do | ||
if tab[i][4] then card.setMode(i, card.modes[tab[i][4]]) end | ||
tab[i] = { tab[i][1], tab[i][2] / 100} | ||
end | ||
card.play(tab) | ||
end | ||
end, | ||
}, | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +0,0 @@ | ||
sound = { | ||
voices = { [api.voice.SQUARE] = true, [api.voice.SINE] = true, | ||
[api.voice.TRIANGLE] = true, [api.voice.SAWTOOTH] = true, | ||
[api.voice.NOISE] = true }, | ||
channels = 8, | ||
play = function(tab) | ||
local card = component_cache.sound | ||
if card then | ||
local dur = 0 | ||
for i in pairs(tab) do | ||
local freq, _dur, vol, voi = table.unpack(tab[i]) | ||
dur = math.max(dur, _dur) | ||
card.open(i) | ||
card.setFrequency(i, freq) | ||
card.setADSR(i, 0, _dur, 0.25, _dur // 2) | ||
if vol then card.setVolume(i, vol / 100) end | ||
if voi then card.setWave(i, card.modes[voi]) end | ||
end | ||
card.delay(dur or 0) | ||
for i=1, 10, 1 do card.process() end | ||
for i=1, #tab, 1 do card.close(i) end | ||
end | ||
end, | ||
}, | ||
Oops, something went wrong.