First Löve attempt with sound & pitch not working, pls help!

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
alex_
Prole
Posts: 19
Joined: Sat Jan 16, 2010 11:39 pm

First Löve attempt with sound & pitch not working, pls help!

Post by alex_ »

Hi, I'm new here! I just started using Löve, to make a simple program to play with the pitch of a sound.

The code is as follows:

:::::CODE STARTS:::::

function load()
sound = love.audio.newSound("catmeow.wav")
src1 = love.audio.newSource(sound)
src1:setVolume(0.9) -- is this necessary to set the volume?
x = love.mouse.getX()
end

function update(dt)
src1:setPitch(x)
love.audio.play(src1)
end

:::::CODE ENDS:::::

As you can see, I want the position of x of the mouse to control the pitch of the .wav sample, so the pitch can be controlled in real time.

but when the program loads, the window pops up as expected, but there is no sound.

I wrote simple scripts without any functions to load up the .wav and play it, and they worked. I also wrote one with the pitch set to a predetermined number (example: 3) and it worked.

But if I want to control the pitch in real time, how can it be done?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by Robin »

alex_ wrote:Hi, I'm new here! I just started using Löve, to make a simple program to play with the pitch of a sound.
Hi there.

0.6.0 uses love.load() instead of load() etc.

You do x = love.mouse.getX() in load(), which means x will be set to the mouse position at the time of load().

Code: Select all

function love.load()
	sound = love.audio.newSound("catmeow.wav")
	src1 = love.audio.newSource(sound)
	love.audio.play(src1)
end
 
function love.update(dt)
	src1:setPitch(love.mouse.getX())
end
I think this should work, try it. ;)
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by TechnoCat »

Corrections to robin's code.

Controlled with the mouse.
Up/Down is volume (1.0 is a normal volume)
Left/Right is pitch (goes from 0 (actually an invalid pitch) to 1.0 (normal pitch))

Code: Select all

function love.load()
	sound = love.audio.newSound("catmeow.wav") --Create a sound object for catmeow
	src1 = love.audio.newSource(sound) --Create a source object for sound
	src1:setLooping(true) --Keeps looping src1s playback
	love.audio.play(src1) --Starts playing src1
end

function love.update(dt)
	src1:setVolume(1-love.mouse.getY()/love.graphics.getHeight()) --ratio of the screen
	src1:setPitch(love.mouse.getX()/love.graphics.getWidth()) --ratio of the screen
end
testbed.love
0.6.0
(50.2 KiB) Downloaded 177 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by Robin »

Right, of course. Have I mentioned I know nothing of the sound system?
Help us help you: attach a .love.
alex_
Prole
Posts: 19
Joined: Sat Jan 16, 2010 11:39 pm

Re: First Löve attempt with sound & pitch not working, pls help!

Post by alex_ »

Let me start off by saying both of you have been incredibly helpful!

I tried the above code and it worked except for one problem, the sound does not actually loop. The sample plays through once and then stops. Or at least, it is no longer heard.

I will try and figure it out for myself but if someone notices something wrong please post it!

Thanks a lot.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by bartbes »

It is possible this is a bug, what kind of audio file are you using? (type)
alex_
Prole
Posts: 19
Joined: Sat Jan 16, 2010 11:39 pm

Re: First Löve attempt with sound & pitch not working, pls help!

Post by alex_ »

The type of file is .wav

Nothing special
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by bartbes »

Well, yes, having worked personally on mp3 I can say that mp3 supports looping and I just heard that wavs (and other files handled by libmodplug, like xm) fail to loop.

EDIT: TechnoCat reports that wav looping *should* work, but with a delay.
alex_
Prole
Posts: 19
Joined: Sat Jan 16, 2010 11:39 pm

Re: First Löve attempt with sound & pitch not working, pls help!

Post by alex_ »

Oh, ok, sweet. I'll just convert the samples to mp3! Thanks a lot.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: First Löve attempt with sound & pitch not working, pls help!

Post by bmelts »

.xm files loop for me, as long as I explicitly tell them to...
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests