Difference between revisions of "love.sound.newSoundData"

 
m (Beep function: - fix example.)
 
(19 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
Creates new [[SoundData]] from a filepath, [[File]], or [[Decoder]]. It's also possible to create SoundData with a custom sample rate, channel and bit depth.
  
 
+
The sound data will be decoded to the memory in a raw format. It is recommended to create only short sounds like effects, as a 3 minute song uses 30 MB of memory this way.
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
sounddata = love.sound.newSoundData( decoder )
+
soundData = love.sound.newSoundData( filename )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|Decoder|decoder|Decode data from this Decoder until EOF.}}
+
{{param|string|filename|The file name of the file to load.}}
 
=== Returns ===
 
=== Returns ===
{{param|SoundData|sounddata|A SoundData object.}}
+
{{param|SoundData|soundData|A new SoundData object.}}
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
sd = love.sound.newSoundData( filename )
+
soundData = love.sound.newSoundData( file )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|filename|The filename of the file to load.}}
+
{{param|File|file|A File pointing to an audio file.}}
 
=== Returns ===
 
=== Returns ===
{{param|SoundData|sd|A new SoundData object.}}
+
{{param|SoundData|soundData|A new SoundData object.}}
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
sounddata = love.sound.newSoundData( samples, rate, bits, channels )
+
soundData = love.sound.newSoundData( decoder )
 +
</source>
 +
=== Arguments ===
 +
{{param|Decoder|decoder|Decode data from this Decoder until EOF.}}
 +
=== Returns ===
 +
{{param|SoundData|soundData|A new SoundData object.}}
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
soundData = love.sound.newSoundData( samples, rate, bits, channels )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
 
{{param|number|samples|Total number of samples.}}
 
{{param|number|samples|Total number of samples.}}
{{param|number|rate|Number of samples per second}}
+
{{param|number|rate (44100)|Number of samples per second}}
{{param|number|bits|Bits per sample (8 or 16).}}
+
{{param|number|bits (16)|Bits per sample (8 or 16).}}
{{param|number|channels|Either 1 for mono, or 2 for stereo}}
+
{{param|number|channels (2)|Either 1 for mono or 2 for stereo.}}
 
=== Returns ===
 
=== Returns ===
{{param|SoundData|sounddata|A new SoundData object, or zero in case of errors.}}
+
{{param|SoundData|soundData|A new SoundData object.}}
 
== Examples ==
 
== Examples ==
 
=== Loading SoundData from files ===
 
=== Loading SoundData from files ===
Line 39: Line 49:
 
mp3 = love.sound.newSoundData("doom.mp3")
 
mp3 = love.sound.newSoundData("doom.mp3")
 
</source>
 
</source>
 +
 +
=== Beep function ===
 +
<source lang="lua">
 +
local rate      = 44100 -- samples per second
 +
local length    = 1/32  -- 0.03125 seconds
 +
local tone      = 440.0 -- Hz
 +
local p        = math.floor(rate/tone) -- 100 (wave length in samples)
 +
local soundData = love.sound.newSoundData(math.floor(length*rate), rate, 16, 1)
 +
for i=0, soundData:getSampleCount() - 1 do
 +
-- soundData:setSample(i, math.sin(2*math.pi*i/p)) -- sine wave.
 +
soundData:setSample(i, i%p<p/2 and 1 or -1)    -- square wave; the first half of the wave is 1, the second half is -1.
 +
end
 +
local source = love.audio.newSource(soundData)
 +
local function beep() source:play() end
 +
</source>
 +
Call it as:
 +
<source lang="lua">
 +
beep()
 +
</source>
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.sound]]
 
* [[parent::love.sound]]
 +
* [[Constructs::SoundData]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=}}
+
{{#set:Description=Creates a new SoundData. }}
 +
{{#set:Since=000}}
 +
== Other Languages ==
 +
{{i18n|love.sound.newSoundData}}

Latest revision as of 18:24, 14 February 2022

Creates new SoundData from a filepath, File, or Decoder. It's also possible to create SoundData with a custom sample rate, channel and bit depth.

The sound data will be decoded to the memory in a raw format. It is recommended to create only short sounds like effects, as a 3 minute song uses 30 MB of memory this way.

Function

Synopsis

soundData = love.sound.newSoundData( filename )

Arguments

string filename
The file name of the file to load.

Returns

SoundData soundData
A new SoundData object.

Function

Synopsis

soundData = love.sound.newSoundData( file )

Arguments

File file
A File pointing to an audio file.

Returns

SoundData soundData
A new SoundData object.

Function

Synopsis

soundData = love.sound.newSoundData( decoder )

Arguments

Decoder decoder
Decode data from this Decoder until EOF.

Returns

SoundData soundData
A new SoundData object.

Function

Synopsis

soundData = love.sound.newSoundData( samples, rate, bits, channels )

Arguments

number samples
Total number of samples.
number rate (44100)
Number of samples per second
number bits (16)
Bits per sample (8 or 16).
number channels (2)
Either 1 for mono or 2 for stereo.

Returns

SoundData soundData
A new SoundData object.

Examples

Loading SoundData from files

wav = love.sound.newSoundData("doom.wav")
-- Beware: if doom.mp3 is a huge file, it will take 
-- ages to decode.
mp3 = love.sound.newSoundData("doom.mp3")

Beep function

local rate      = 44100 -- samples per second
local length    = 1/32  -- 0.03125 seconds
local tone      = 440.0 -- Hz
local p         = math.floor(rate/tone) -- 100 (wave length in samples)
local soundData = love.sound.newSoundData(math.floor(length*rate), rate, 16, 1)
for i=0, soundData:getSampleCount() - 1 do
--	soundData:setSample(i, math.sin(2*math.pi*i/p)) -- sine wave.
	soundData:setSample(i, i%p<p/2 and 1 or -1)     -- square wave; the first half of the wave is 1, the second half is -1.
end
local source = love.audio.newSource(soundData)
local function beep() source:play() end

Call it as:

beep()

See Also


Other Languages