Difference between revisions of "SoundData:setSample"

(add Example: Play a sine wave)
(Improved example that chooses better values and explains choices, and uses frequency. thanks to zorg for help.)
Line 28: Line 28:
  
 
<source lang="lua">
 
<source lang="lua">
local samplerate = 44100
+
local tau          =  math.pi * 2
local bits = 8
+
local samplerate   = 44100 -- Hz
local channels = 8
+
local bits         =     16 -- 8 bits would yield very low quality sound
local buffercount = 30
+
local channels     =     2 -- löve only supports mono or stereo
local qsource = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
+
local buffercount =     2 -- probably better to use fewer buffers
local buffer = love.sound.newSoundData(8, samplerate, bits, channels)
+
local qsource     = love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
local sampleChunkSize = 64
+
local samplepoints =    64 -- 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 time = 0
 
local pitch = 200
 
  
local function synth(t)
+
local function synth()
     local tau = math.pi * 2
+
     phase = phase + (tau * frequency / samplerate)
     return math.sin(tau * t * pitch / samplerate)
+
     return math.sin(phase) * amplitude
 
end
 
end
  
 
function love.update(dt)
 
function love.update(dt)
    -- keep trying to fill up the queue with buffers so it can play
 
    -- continuously.
 
 
     while qsource:getFreeBufferCount() > 0 do
 
     while qsource:getFreeBufferCount() > 0 do
         for i = 0, sampleChunkSize-1 do
+
         for i = 0, samplepoints - 1 do
            local n = synth(i + time)
+
             buffer:setSample(i, synth())
             buffer:setSample(i, n)
 
 
         end
 
         end
        time = time + sampleChunkSize
 
 
         qsource:queue(buffer)
 
         qsource:queue(buffer)
 +
        qsource:play()
 
     end
 
     end
    qsource:play()
 
 
end
 
end
  
 
function love.keypressed(key)
 
function love.keypressed(key)
     if key == "k" then
+
     if key == 'k' then
         pitch = pitch + 200
+
         frequency = frequency * 2^(1/2)
     elseif key == "j" then
+
     elseif key == 'j' then
         pitch = pitch - 200
+
         frequency = frequency * 2^(-1/2)
 
     end
 
     end
 
end
 
end
 
</source>
 
</source>
 +
 +
  
  

Revision as of 22:11, 24 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 tau          =  math.pi * 2
local samplerate   =  44100 -- Hz
local bits         =     16 -- 8 bits would yield very low quality sound
local channels     =      2 -- löve only supports mono or stereo
local buffercount  =      2 -- probably better to use fewer buffers
local qsource      =  love.audio.newQueueableSource(samplerate, bits, channels, buffercount)
local samplepoints =     64 -- 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/2)
    elseif key == 'j' then
        frequency = frequency * 2^(-1/2)
    end
end



See Also


Other Languages