Difference between revisions of "Tutorial:Audio"

(Add i18n tag)
(See Also)
 
(16 intermediate revisions by 11 users not shown)
Line 2: Line 2:
 
<source lang="lua">
 
<source lang="lua">
 
sound = love.audio.newSource("pling.wav", "static") -- the "static" tells LÖVE to load the file into memory, good for short sound effects
 
sound = love.audio.newSource("pling.wav", "static") -- the "static" tells LÖVE to load the file into memory, good for short sound effects
music = love.audio.newSource("techno.ogg") -- if "static" is omitted, LÖVE will stream the file from disk, good for longer music tracks
+
music = love.audio.newSource("techno.ogg", "stream") -- the "stream" tells LÖVE to stream the file from disk, good for longer music tracks
love.audio.play(sound)
+
sound:play()
love.audio.play(music)
+
music:play()
 
</source>
 
</source>
  
Line 12: Line 12:
  
 
* MP3
 
* MP3
* OGG
+
* Ogg Vorbis
* WAV
+
* WAVE
* and just about every tracker format you can think of - XM, MIDI, MOD, and over twenty others.
+
* and just about every tracker format you can think of - XM, MOD, [http://sourceforge.net/p/modplug-xmms/git/ci/master/tree/libmodplug/README#l39 and over twenty others].
 +
 
 +
Ogg Vorbis and 16-bit WAVE are the recommended formats. Others may have minor quirks. For example, the MP3 decoder may pad a few samples depending on what encoder was used. These issues come from the libraries LÖVE uses to decode the audio files and can't be fixed in LÖVE directly.
  
 
== Static vs. Streaming ==
 
== Static vs. Streaming ==
Line 28: Line 30:
 
<source lang="lua">
 
<source lang="lua">
 
src1 = love.audio.newSource("bang.wav", "static")
 
src1 = love.audio.newSource("bang.wav", "static")
src2 = love.audio.newSource("bgm.mp3")
+
src2 = love.audio.newSource("bgm.mp3", "stream")
  
 
src1:setVolume(0.9) -- 90% of ordinary volume
 
src1:setVolume(0.9) -- 90% of ordinary volume
Line 34: Line 36:
 
src2:setVolume(0.7)
 
src2:setVolume(0.7)
  
love.audio.play(src1)
+
src1:play()
love.audio.play(src2)
+
src2:play()
 +
</source>
 +
 
 +
== Example ==
 +
 
 +
<source lang="lua">
 +
function love.load()
 +
source = love.audio.newSource("Ye-Olde-Pub_Looping.mp3", "stream")
 +
end
 +
 
 +
function love.update(dt)
 +
if not source:isPlaying( ) then
 +
love.audio.play( source )
 +
end
 +
end
 
</source>
 
</source>
  
 
Further details can be found in the [[Source|Source documentation]].
 
Further details can be found in the [[Source|Source documentation]].
 +
 +
== See Also ==
 +
* [[parent::love.audio]]
 +
* [[TEsound]] - A sound manager which makes it easier to use sound effects and music
 +
* [[Minimalist Sound Manager]] - A [[Snippets|snippet]] that makes usage of love.audio easier and automatically manages sources
 +
 +
== Other Languages ==
 +
{{i18n|Tutorial:Audio}}
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
Line 44: Line 68:
 
{{#set:LOVE Version=0.6.1}}
 
{{#set:LOVE Version=0.6.1}}
 
{{#set:Description=A tutorial for learning to use [[love.audio]].}}
 
{{#set:Description=A tutorial for learning to use [[love.audio]].}}
 
+
{{#set:Tutorial=Yes}}
 
 
== Autres langues ==
 
{{i18n|Tutorial:Audio}}
 

Latest revision as of 10:06, 20 October 2022

In LÖVE, audio is the domain of the love.audio module, which uses OpenAL for playback. love.audio has only one type of audio object - a Source. You can load audio and play it like so:

sound = love.audio.newSource("pling.wav", "static") -- the "static" tells LÖVE to load the file into memory, good for short sound effects
music = love.audio.newSource("techno.ogg", "stream") -- the "stream" tells LÖVE to stream the file from disk, good for longer music tracks
sound:play()
music:play()

Formats

LÖVE supports a lot of audio formats, thanks to the love.sound module, which handles all the decoding. Supported formats include:

Ogg Vorbis and 16-bit WAVE are the recommended formats. Others may have minor quirks. For example, the MP3 decoder may pad a few samples depending on what encoder was used. These issues come from the libraries LÖVE uses to decode the audio files and can't be fixed in LÖVE directly.

Static vs. Streaming

Keep in mind that, if you pass love.audio.newSource "static" as a second argument, the sound file will be expanded into memory, so if you load a 5MB compressed .ogg file that way, it would consume ~50MB RAM when fully decompressed. Consider not using "static" in such cases.

If you omit the "static", the audio will be streamed from the file as it's played, something that saves a lot of memory when you're dealing with massive files.

Audio control

To pause, stop, change volume, looping, pitch, etc., simply call the relevant method of a Source.

src1 = love.audio.newSource("bang.wav", "static")
src2 = love.audio.newSource("bgm.mp3", "stream")

src1:setVolume(0.9) -- 90% of ordinary volume
src1:setPitch(0.5) -- one octave lower
src2:setVolume(0.7)

src1:play()
src2:play()

Example

function love.load()
	source = love.audio.newSource("Ye-Olde-Pub_Looping.mp3", "stream")
end

function love.update(dt)
	if not source:isPlaying( ) then
		love.audio.play( source )
	end
end

Further details can be found in the Source documentation.

See Also

Other Languages