1

Play music when excavating

This commit is contained in:
2025-10-06 23:50:29 +02:00
parent b5cb13732e
commit b71267bf42
5 changed files with 165 additions and 56 deletions

View File

@ -2,6 +2,8 @@
"Lua.diagnostics.globals": [
"printError",
"turtle",
"shell"
"shell",
"peripheral",
"parallel"
]
}

BIN
audio/bangarang.dfpwm Normal file

Binary file not shown.

View File

@ -0,0 +1,96 @@
local dfpwm = require("cc.audio.dfpwm")
local Direction = require("lib.direction")
---@alias AudioControllerConfig {buffer_length_seconds: number}
---@class AudioController
---@field config AudioControllerConfig
---@field play boolean
local AudioController = {}
AudioController.__index = AudioController
---@return AudioController
function AudioController:Create()
local t = {}
setmetatable(t, AudioController)
-----------------------------------------------------------------------------------------------
-- Fields
-----------------------------------------------------------------------------------------------
t.config = {
buffer_length_seconds = 1
}
t.play = false
return t
end
-----------------------------------------------------------------------------------------------
-- Audio Methods
-----------------------------------------------------------------------------------------------
function AudioController:PlayAudio(filename)
self.play = true
local decoder = dfpwm.make_decoder()
while self.play do
for chunk in io.lines(("audio/%s.dfpwm"):format(filename), self.config.buffer_length_seconds * 1024) do
if not self.play then
break
end
local buffer = decoder(chunk)
while not self:GetSpeaker().playAudio(buffer) do
---@diagnostic disable-next-line: undefined-field
os.pullEvent("speaker_audio_empty")
end
end
end
end
function AudioController:PlayAudioFactory(filename)
local function play()
self:PlayAudio(filename)
end
return play
end
-----------------------------------------------------------------------------------------------
-- Management Methods
-----------------------------------------------------------------------------------------------
---@return table | nil
function AudioController:GetSpeaker()
return peripheral.find("speaker")
end
function AudioController:StopPlaying()
self.play = false
self:GetSpeaker().stop()
end
-----------------------------------------------------------------------------------------------
-- Main Method
-----------------------------------------------------------------------------------------------
function AudioController:Run()
-- self:Configure()
self:PlayAudio("bangarang")
end
return AudioController

View File

@ -1,10 +1,12 @@
local Direction = require("lib.direction")
local TurtleController = require("controller.turtle_controller")
local AudioController = require("controller.audio_controller")
---@alias ExcavationControllerConfig {slice_length: number, slice_height: number, center_slice_width: number, slice_width: number, slice_padding: number, slices_left: number, slices_right: number}
---@class ExcavationController
---@field controller TurtleController
---@field audio AudioController
---@field config ExcavationControllerConfig
local ExcavationController = {}
ExcavationController.__index = ExcavationController
@ -22,6 +24,7 @@ function ExcavationController:Create()
t.controller = TurtleController:Create()
t.audio = AudioController:Create()
t.config = {
slice_length = 0,
slice_height = 0,
@ -176,6 +179,7 @@ function ExcavationController:Excavate()
self:Excavate_WxHxL(true)
self.controller:MoveBack() -- (0, 0, 1)
if self.config.slices_right > 0 then
-- Move to right slices starting location
local padded_width_right = self.config.slices_right * (self.config.slice_width + self.config.slice_padding)
self.controller:StorePosition()
@ -206,7 +210,9 @@ function ExcavationController:Excavate()
self.controller:TurnToDirection(Direction.NORTH)
end
self.controller:MoveBack() -- (0, 0, 1)
end
if self.config.slices_left > 0 then
-- Move to left slices starting location
local center_width_left = self.config.center_slice_width - center_width_right
self.controller:StorePosition()
@ -245,11 +251,13 @@ function ExcavationController:Excavate()
self.controller:TurnToDirection(Direction.NORTH)
end
self.controller:MoveBack() -- (0, 0, 1)
end
-- Finish up
self.controller:MoveToPosition(0, 0, 0, self.controller.config.storage_direction)
self.controller:DropInventory()
self.controller:TurnToDirection(Direction.NORTH)
self.audio:StopPlaying()
end
@ -317,7 +325,7 @@ function ExcavationController:Run()
turtle.refuel()
self.controller:RefuelIfEmpty()
self:Excavate()
parallel.waitForAll(function() self:Excavate() end, self.audio:PlayAudioFactory("bangarang"))
end

View File

@ -1,15 +1,18 @@
local TestingController = require("controller.testing_controller")
local ExcavationController = require("controller.excavation_controller")
local AudioController = require("controller.audio_controller")
local controllers = {
TestingController:Create(),
ExcavationController:Create(),
AudioController:Create(),
}
print("Multiple controllers are available:")
print("1: Testing Mode")
print("2: Volume Excavation")
print("3: Play Bangarang")
local choice = 0
while choice < 1 or choice > #controllers do