Difference between revisions of "SoundData:setSample"

m (0.11.0 -> 11.0)
(add Example: Play a sine wave)
Line 24: Line 24:
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Example: Play a sine wave ==
 +
 +
<source lang="lua">
 +
local samplerate = 44100
 +
local bits = 8
 +
local channels = 8
 +
local buffercount = 30
 +
local qsource = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
 +
local buffer = love.sound.newSoundData(8, samplerate, bits, channels)
 +
local sampleChunkSize = 64
 +
 +
local time = 0
 +
local pitch = 200
 +
 +
local function synth(t)
 +
    local tau = math.pi * 2
 +
    return math.sin(tau * t * pitch / samplerate)
 +
end
 +
 +
function love.update(dt)
 +
    -- keep trying to fill up the queue with buffers so it can play
 +
    -- continuously.
 +
    while qsource:getFreeBufferCount() > 0 do
 +
        for i = 0, sampleChunkSize-1 do
 +
            local n = synth(i + time)
 +
            buffer:setSample(i, n)
 +
        end
 +
        time = time + sampleChunkSize
 +
        qsource:queue(buffer)
 +
    end
 +
    qsource:play()
 +
end
 +
 +
function love.keypressed(key)
 +
    if key == "k" then
 +
        pitch = pitch + 200
 +
    elseif key == "j" then
 +
        pitch = pitch - 200
 +
    end
 +
end
 +
</source>
 +
  
 
== See Also ==
 
== See Also ==

Revision as of 22:08, 23 November 2021

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

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.
number sample
The normalized samplepoint (range -1.0 to 1.0).

Returns

Nothing.

Example: Play a sine wave

local samplerate = 44100
local bits = 8
local channels = 8
local buffercount = 30
local qsource = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
local buffer = love.sound.newSoundData(8, samplerate, bits, channels)
local sampleChunkSize = 64

local time = 0
local pitch = 200

local function synth(t)
    local tau = math.pi * 2
    return math.sin(tau * t * pitch / samplerate)
end

function love.update(dt)
    -- keep trying to fill up the queue with buffers so it can play
    -- continuously.
    while qsource:getFreeBufferCount() > 0 do
        for i = 0, sampleChunkSize-1 do
            local n = synth(i + time)
            buffer:setSample(i, n)
        end
        time = time + sampleChunkSize
        qsource:queue(buffer)
    end
    qsource:play()
end

function love.keypressed(key)
    if key == "k" then
        pitch = pitch + 200
    elseif key == "j" then
        pitch = pitch - 200
    end
end


See Also


Other Languages