Music plays choppy

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
hpnuts
Posts: 1
Joined: Tue Jun 20, 2006 12:29 am

Music plays choppy

Post by hpnuts »

Hi,

I'm writing a game in Lua and trying to add music playback.
I've tried to play a .xm or .s3m music file but they are playing pausing playing pausing....
Actually the .xm is giving better result but still not smooth.

Is there something to add in my code to have a smooth music ?

I was thinking that may be a wait time was needed to allow lua to fill is buffer... So I've tried to add some screen:waitVblankStart() all over my code without any good result...

Any help ?

Thanks in advance, Pascal
PSP 2.01
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

The biggest problem with LUAPlayer is that it uses a single thread for almost everything. When you blit/print too many images during each frame the entire system gets choppy and slows down. In order to decrease the time between. Just remember kids Simpler is better! I do believe you should totally rewrite your code.
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Yeah same problem here. But how do I cut down on the choppyness if I need alot of units on the screen. I tried to optimize my code and cut out 16 for loops (but without any extra blitting), but still not enough.

I also tried to blit one half of the amount of units one loop and then the second time it blits the other half, but that doesn't really help so it seems. Hmm maybe I should give that another try, because Im running out of ideas here.

Do you have any tips maybe?
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Eliminate blitting, and use less code. Less is more!The more functions you use the slower it becomes. The less functions you use.. can also slow it down. Find the right combination. Also use the blit buffer! Only reblit changes in the picture. Never do more than that. Complex code makes it harder for it to work. Steer clear of "if 1 > variable then else end" code it slows stuff down.

Dont reblit every frame.
Limit code so its reactive. And based on timers. It helps

Use Coroutines.
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Eliminate blitting, and use less code. Less is more!The more functions you use the slower it becomes. The less functions you use.. can also slow it down. Find the right combination. Also use the blit buffer! Only reblit changes in the picture. Never do more than that. Complex code makes it harder for it to work. Steer clear of "if 1 > variable then else end" code it slows stuff down.

Dont reblit every frame.
Limit code so its reactive. And based on timers. It helps

Use Coroutines.
The thing is I eliminated as much as possible. I only blit stuff when its actually visible and moves/changes and I use in fact only a few functions, but only for stuff thats done ones, so not for loops that call upon it every loop.

What exactly is the "blit buffer"? Do you mean the second screen? As in you have to flip the two screens, and I shouldn't do that every loop?

And you're also saying I shouldn't use to many if statements in general, or ones with numbers in them, or other specific ones? Are some things faster than other ones? I mean is for faster/slower or equally fast as while for instance? And are boolean if statemnets faster then numbers?

Also what exactly do you mean by limiting the code so its reactive? It should react to changes? But isn't that (almost) always?

Do you maybe have a good tut on coroutines? Otherwise I'll just read the manual.

Thx for the advice!
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

I dont really have a tutorial. But I do have an example useing a Corutine SDK I made.

Code: Select all

Process_LIB = {
	["ThreadList"] = { },
	Exit = nil
}

function Process_LIB:init ()
	while true do
		if (self.Exit) then
			break
		end
		for key,value in pairs(Process_LIB["ThreadList"]) do

			-- GetStatus()
			-- 1 - Go
			-- 2 - Pause
			-- 3 - SelfTerm
			-- 4 - Term (Cleanup)
			if (value:GetStatus() == 1) then
				-- Do Threadlist
				--local Status
				if (coroutine.status(value.ThreadID) == "dead") then
					-- Thread Ended cleanup
					--value = nil
				else
					errorfree, errorvalue = coroutine.resume(value.ThreadID, value)
					if (self.Exit) then
						break
					end
				end
			end
			if (value:GetStatus() == 2) then
				-- Pause
				-- Do nothing 
			end
			if (value:GetStatus() == 3) then	
				-- Self Term
			end

			if (value:GetStatus() == 4) then
				-- Terminate Code
			end
			if (value:GetStatus() == 5) then
				-- Unknown Command
			end
		end
	end
end


function Process_LIB:exit()
	self.Exit = true
end

function Process_LIB:cleanup()
	ProcessThread_LIB = nil
	Process_LIB = nil
end










-- Table here
ProcessThread_LIB = { }

function ProcessThread_LIB:new(name)
	if (not name) then
		return nil
	end
	NewThread = { }
	local exists = nil
	for key,value in pairs(Process_LIB["ThreadList"]) do
		if (value:GetName() == name) then
			exists = 1
		end
	end
	if (not exists) then
		setmetatable(NewThread, self)
		table.insert(Process_LIB["ThreadList"], NewThread)
		self.__index = self
		self.status = 1
		self["Name"] = name
		return NewThread
	end
	return nil
end

function ProcessThread_LIB:Startup()
	self.ThreadID = coroutine.create(function(self) self:Thread(self) end)
end

function ProcessThread_LIB:Thread()
	self.wait()
end








function ProcessThread_LIB:GetStatus()
	return self["status"]
end


function ProcessThread_LIB:wait()
	return coroutine.yield()
end

function ProcessThread_LIB:GetName()
	return self["Name"]
end
An example of the code

Code: Select all

if (Process_LIB) then
	_Wifi = ProcessThread_LIB:new("_Wifi")
	if (_Wifi) then
		function _Wifi:Thread(self)
			while true do

				self = self.wait() -- Waits every loop (without this itll lock up)
				

			end
			

		end
		_Wifi:Startup()

	end
end
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Alright I'll try and see if I can understand it. Thx for the example!
Post Reply