Difference between revisions of "SoundData:setSample"
m |
|||
(14 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | + | Sets the value of the sample-point at the specified position. For stereo SoundData objects, the data from the left and right channels are interleaved in that order. | |
+ | == Function == | ||
+ | === Synopsis === | ||
+ | <source lang="lua"> | ||
+ | SoundData:setSample( i, sample ) | ||
+ | </source> | ||
+ | === Arguments === | ||
+ | {{param|number|i|An integer value specifying the position of the sample (starting at 0).}} | ||
+ | {{param|number|sample|The normalized samplepoint (range -1.0 to 1.0).}} | ||
+ | === Returns === | ||
+ | Nothing. | ||
== Function == | == Function == | ||
+ | {{newin|[[11.0]]|110|type=variant}} | ||
+ | Sets the value of a sample using an explicit sample index instead of interleaving them in the sample position parameter. | ||
=== Synopsis === | === Synopsis === | ||
<source lang="lua"> | <source lang="lua"> | ||
− | SoundData:setSample( i, sample ) | + | SoundData:setSample( i, channel, sample ) |
</source> | </source> | ||
=== Arguments === | === Arguments === | ||
− | {{param|number|i| | + | {{param|number|i|An integer value specifying the position of the sample (starting at 0).}} |
− | {{param|number|sample| | + | {{param|number|channel|The index of the channel to set within the given sample (starting at 1). For stereo, 1 is left channel and 2 is right channel.}} |
+ | {{param|number|sample|The normalized samplepoint (range -1.0 to 1.0).}} | ||
=== Returns === | === Returns === | ||
Nothing. | Nothing. | ||
+ | |||
+ | == Example: Generate notes with a sine wave == | ||
+ | |||
+ | <source lang="lua"> | ||
+ | local tau = math.pi * 2 | ||
+ | local samplerate = 44100 -- Hz | ||
+ | local bits = 16 -- 8 bits results in very low quality sound | ||
+ | local channels = 2 -- löve only supports mono or stereo | ||
+ | local buffercount = 2 -- don't need too many buffers | ||
+ | local qsource = love.audio.newQueueableSource(samplerate, bits, channels, buffercount) | ||
+ | local samplepoints = samplerate/10 -- how many numbers does one buffer hold | ||
+ | local buffer = love.sound.newSoundData(samplepoints, samplerate, bits, channels) | ||
+ | local phase = 0.0 | ||
+ | local frequency = 220.00 -- Hz | ||
+ | local amplitude = 0.5 -- 50% | ||
+ | |||
+ | |||
+ | local function synth() | ||
+ | phase = phase + (tau * frequency / samplerate) | ||
+ | return math.sin(phase) * amplitude | ||
+ | end | ||
+ | |||
+ | function love.update(dt) | ||
+ | while qsource:getFreeBufferCount() > 0 do | ||
+ | for i = 0, samplepoints - 1 do | ||
+ | buffer:setSample(i, synth()) | ||
+ | end | ||
+ | qsource:queue(buffer) | ||
+ | qsource:play() | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function love.keypressed(key) | ||
+ | if key == 'k' then | ||
+ | frequency = frequency * 2^(1/12) | ||
+ | elseif key == 'j' then | ||
+ | frequency = frequency * 2^(-1/12) | ||
+ | end | ||
+ | end | ||
+ | </source> | ||
+ | |||
== See Also == | == See Also == | ||
* [[parent::SoundData]] | * [[parent::SoundData]] | ||
[[Category:Functions]] | [[Category:Functions]] | ||
− | {{#set:Description=}} | + | {{#set:Description=Sets the sample at the specified position.}} |
+ | {{#set:Since=000}} | ||
+ | == Other Languages == | ||
+ | {{i18n|SoundData:setSample}} |
Latest revision as of 13:47, 19 March 2022
Sets the value of the sample-point at the specified position. For stereo SoundData objects, the data from the left and right channels are interleaved in that order.
Contents
Function
Synopsis
SoundData:setSample( i, sample )
Arguments
number i
- An integer value specifying the position of the sample (starting at 0).
number sample
- The normalized samplepoint (range -1.0 to 1.0).
Returns
Nothing.
Function
Available since LÖVE 11.0 |
This variant is not supported in earlier versions. |
Sets the value of a sample using an explicit sample index instead of interleaving them in the sample position parameter.
Synopsis
SoundData:setSample( i, channel, sample )
Arguments
number i
- An integer value specifying the position of the sample (starting at 0).
number channel
- The index of the channel to set within the given sample (starting at 1). For stereo, 1 is left channel and 2 is right channel.
number sample
- The normalized samplepoint (range -1.0 to 1.0).
Returns
Nothing.
Example: Generate notes with a sine wave
local tau = math.pi * 2
local samplerate = 44100 -- Hz
local bits = 16 -- 8 bits results in very low quality sound
local channels = 2 -- löve only supports mono or stereo
local buffercount = 2 -- don't need too many buffers
local qsource = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
local samplepoints = samplerate/10 -- how many numbers does one buffer hold
local buffer = love.sound.newSoundData(samplepoints, samplerate, bits, channels)
local phase = 0.0
local frequency = 220.00 -- Hz
local amplitude = 0.5 -- 50%
local function synth()
phase = phase + (tau * frequency / samplerate)
return math.sin(phase) * amplitude
end
function love.update(dt)
while qsource:getFreeBufferCount() > 0 do
for i = 0, samplepoints - 1 do
buffer:setSample(i, synth())
end
qsource:queue(buffer)
qsource:play()
end
end
function love.keypressed(key)
if key == 'k' then
frequency = frequency * 2^(1/12)
elseif key == 'j' then
frequency = frequency * 2^(-1/12)
end
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info