This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Windows10User's profile picture

[SOLVED] Cannot serialize file's contents

Started by Windows10User, 25 May 2018 - 04:46 PM
Windows10User #1
Posted 25 May 2018 - 06:46 PM
As the development of my OS went further (also plz tell me what the hell do coroutines even do in them), I decided to rewrite the bootloader and make a sound & logging API. Now, the part of the bootloader I wrote so far and the logging API work better than great, when I go ahead to play the boot sound, it throws this:



I seriously don't know what's wrong here. I'm sure my sound is formatted correctly, I even did a print on type(fs handle here) and it said table, so it indeed should have read the file and formatted it into a table.

Enough of my walls of text and time to see the code for yourself! :D/>

Bootloader:

os.loadAPI("DoorOS/apis/logging")
os.loadAPI("DoorOS/apis/wavesound")

term.clear()
term.setCursorPos(1, 1)
version = 0.1

cosversion = os.version()

if cosversion ~= "CraftOS 1.8" then
  logging.warn("Not running on COS 1.8, continue?")
  logging.warn("To continue, press Enter.")
  logging.warn("To exit, press any other key.")
  logging.warn("(NOTE: May be very unstable!)")
  local event, k, held = os.pullEvent("key")
  if k ~= keys.enter then
	error()
  end
end


logging.info("Running DoorOS v0.1a on "..cosversion)

local fcfg = fs.open("Boot/cfg.lua", "r")
local cfg = fcfg.readAll()
fcfg.close()

logging.info("Performing main files check...")

local files = {
  cfg.loader,
  "DoorOS/desktop.lua",
  "DoorOS/apis/graphics",
  "DoorOS/apis/compact",
  "DoorOS/apis/pastebin",
  "DoorOS/apis/updcheck",
  "DoorOS/apis/wavesound",
}

local missing = nil

for i, f in pairs(files) do
  if not fs.exists(f) then
	missing = {}
	table.insert(missing, f)
  end
end

if missing then
  logging.error("Boot-time fail.")
  logging.error("Missing file(s):")
  for i, f in pairs(missing) do
	logging.error(f)
  end
  logging.error("DoorOS cannot boot.")
  error()
end

logging.info("Files OK.")
logging.info("Scanning peripherals...")

peripherals = {}

for i, s in pairs(rs.getSides()) do
  if peripheral.getType(s) then
	if peripheral.getType(s) == "modem" then
	  if peripheral.wrap(s).isWireless() then
		peripherals["wifi_modem"] = s
	  else
		peripherals["eth_modem"] = s
	  end
	else
	  peripherals[peripheral.getType(s)] = s
	end
  end
end

logging.info("Found peripherals:")
for i, p in pairs(peripherals) do
  logging.info(i.." on side "..p)
end

if peripheral.find("speaker") then
  local sf = fs.open("DoorOS/sounds/boot.wave", "r")
  local s = textutils.unserialize(sf.readAll())
  sf.close()
  print(type(s))
  local ok, err = wavesound.play(s)
  if not ok then
	logging.warn("Could not play bootsound")
	logging.warn("Error: "..err)
  end
end

Wavesound API:

function play(snd)
  if type(snd) ~= "table" then
	return false, "snd is not a table"
  end

  local spkr = peripheral.find("speaker")
  if not spkr then
	return false, "no speaker found"
  end

  for i, s in pairs(snd) do
	spkr.playNote(s.note, tonumber(s.vol), tonumber(s.pitch))
  end

  return true
end

Sound file:

{
  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }

  {
	note = "snare",
	vol = "10",
	pitch = "1",
  }
}

Any help is appreciated!
Edited on 25 May 2018 - 04:57 PM
SquidDev #2
Posted 25 May 2018 - 06:55 PM
You sound file isn't valid Lua, meaning textutils.unserialise returns nil. You need to add commas between each of the notes.
Windows10User #3
Posted 25 May 2018 - 06:57 PM
You sound file isn't valid Lua, meaning textutils.unserialise returns nil. You need to add commas between each of the notes.

Oh yeah, what a dumbo I am. Thanks!