PSPTune 0.1

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

Moderators: Shine, Insert_witty_name

Post Reply
Dark Killer
Posts: 32
Joined: Tue Jan 25, 2005 3:10 am

PSPTune 0.1

Post by Dark Killer »

PSPTune is a simple guitar tuner coded in LUA:

http://rapidshare.de/files/4196896/PSPT ... K.rar.html

anyone have any idea why the wav files are playing back in poor quality? would it be better if I converted them to XM modules?
emumaniac
Posts: 79
Joined: Sun May 08, 2005 12:22 am

Post by emumaniac »

excellent , it should help my daughter a bit ;)

mirrored the file here --> http://psp-news.dcemu.co.uk/psptune.shtml
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: PSPTune 0.1

Post by Shine »

Dark Killer wrote: anyone have any idea why the wav files are playing back in poor quality? would it be better if I converted them to XM modules?
You are playing multiple sounds at once, because everytime your controls function is called, another play command is started, until the button is released. There are 4 sound channels (I think), so you hear an overlay of 4 times your sound, started with some pause between. You should start a new sound only when the button state changes. Then it is possible to play chords as well with your program. Try this code:

Code: Select all

oldPad = Controls.read()
function controls()
	pad = Controls.read()
	if pad ~= oldPad then
		oldPad = pad
		if pad:cross() then
			lowe:play()
		end
		if pad:circle() then
			astring:play()
		end
		if pad:triangle() then
			dstring:play()
		end
		if pad:square() then
			gstring:play()
		end
		if pad:l() then
			bstring:play()
		end
		if pad:r() then
			highe:play()
		end
		if pad:select() then
			tabview()
		end
	end
end
Some more improvements: the native sampling rate of the PSP is 22050 Hz, so it doesn't make sense to save it in higher sample rates, because it will be scaled down with quality loss on the PSP. And you should set your recordings to maximum gain, the peak level of your wav files are below -10 dB (with 0 dB = maximum digital amplitude).
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

I don't know anything about guitar tuning, but what about a clear sine, like from a tuning fork? The code below creates the notes for a guitar (change the baseFrequency, if necessary) and caches it as wave files on memory stick (this takes some 2 minutes on PSP for the first start, on the PC emulator on my PC 2 seconds, for every next start it reads it from memory stick).

Looks like Lua is a bit slow for synthesizing sounds, any ideas for a nice Lua interface or some good C library to include for a software synthesizer?

Code: Select all

-- write a number as a 32 bit integer to a file
function write32(file, value)
	value = math.floor(value)
	file:write(string.char(math.mod(value, 256), math.mod(value / 256, 256), math.mod(value / 256 / 256, 256), math.mod(value / 256 / 256 / 256, 256)))
end

-- write a number as a 16 bit integer to a file
function write16(file, value)
	value = math.floor(value)
	if value < 0 then value = 65536 + value end
	file&#58;write&#40;string.char&#40;math.mod&#40;value, 256&#41;, math.mod&#40;value / 256, 256&#41;&#41;&#41;
end

-- write a samples array to as a wave file
function writeWave&#40;filename, sampleRate, samples&#41;
	file = io.open&#40;filename, "w"&#41;
	file&#58;write&#40;"RIFF"&#41;  -- chunk id
	len = table.getn&#40;samples&#41;
	write32&#40;file, 2 * len + 36&#41;  -- file size
	file&#58;write&#40;"WAVE"&#41;  -- riff type
	file&#58;write&#40;"fmt "&#41;  -- format chunk id
	write32&#40;file, 16&#41;  -- chunk size
	write16&#40;file, 1&#41;  -- PCM/umcompressed
	write16&#40;file, 1&#41;  -- number of channels
	write32&#40;file, sampleRate&#41;  -- sample rate
	write32&#40;file, 2 * sampleRate&#41;  -- bytes per second
	write16&#40;file, 2&#41;  -- number of bytes per sample for all channels
	write16&#40;file, 16&#41;  -- number of bits per sample for one channel
	file&#58;write&#40;"data"&#41;
	write32&#40;file, 2 * len&#41;
	for i=1,len do write16&#40;file, samples&#91;i&#93; * 32767&#41; end
	file&#58;close&#40;&#41;
end

-- create note table with frequency entry
function createNotes&#40;octaveCount, base&#41;
	factor = math.pow&#40;2, 1/12&#41;
	notes = &#123;&#125;
	noteNames = &#123; "c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "b" &#125;
	for octave = 1, octaveCount do
		i = 0
		for _, noteName in noteNames do
			fullNoteName = noteName .. octave
			notes&#91;fullNoteName&#93; = &#123;&#125;
			notes&#91;fullNoteName&#93;.freq = base * math.pow&#40;factor, i&#41;
			i = i + 1
		end
		base = base * 2
	end
	return notes
end
baseFrequency = 440
notes = createNotes&#40;3, baseFrequency&#41;

-- create all guitar notes and cache on memory stick
sampleRate = 22050
pi = math.atan&#40;1&#41; * 4
guitarNotes = &#123; "e1", "a1", "d2", "g2", "b2", "e3" &#125;
for _, guitarNote in guitarNotes do
	waveFilename = guitarNote .. ".wav"
	file = io.open&#40;waveFilename&#41;
	if file then
		file&#58;close&#40;&#41;
	else
		samples = &#123;&#125;
		step = 2 * pi * notes&#91;guitarNote&#93;.freq / sampleRate
		seconds = 3
		for i = 0, sampleRate * seconds - 1 do
			samples&#91;i+1&#93; = math.sin&#40;step * i&#41;
		end
		writeWave&#40;waveFilename, sampleRate, samples&#41;
	end
	notes&#91;guitarNote&#93;.sound = Sound.load&#40;waveFilename&#41;
end

-- load guitar notes
lowe = notes.e1.sound
astring = notes.a1.sound
dstring = notes.d2.sound
gstring = notes.g2.sound
bstring = notes.b2.sound
highe = notes.e3.sound
Post Reply